// embedSWF
var params     = {};
var flashvars  = {};
var attributes = {};
params.menu               = "false";
params.quality            = "high";
params.bgcolor            = "#ffffff";
params.allowFullScreen    = "true";
params.base				  = "/";

attributes.allowFullScreen = "true";

attributes.id			  = "background";

/*
flashvars.file             = "playlist.xml";
flashvars.displaywidth     = "400";
flashvars.displayheight    = "300";

if (swfobject.hasFlashPlayerVersion("6.0.0")) {
  swfobject.createCSS("body", "margin:0; padding:0; text-align:center");
  swfobject.createCSS("#putHere", "margin: 5%");
}
*/

function language() {
	var match = window.location.href.match(/\/(\w{2})\//);
	if (match) {
		setCookie("language", match[1]);
	}
}
language();

var ran = String(Math.random());

function getCookie(key, defaultValue) {
	//Match end-of-line because the last cookie doens't terminate with ;
	var match = document.cookie.match(new RegExp(key + "=(.*?)(;|$)"));
	return match ? match[1] : defaultValue;
}		

function setCookie(key, value) {
    var date = new Date();
    date.setYear(date.getFullYear() + 1);
    var cookieData = "; expires=" + date.toGMTString() + "; path=/";
        
    document.cookie = key + "=" + value + cookieData;
}

function bFlashOn() {
	return getCookie("flash", "true") == "true" ? true : false;
/*    if (document.cookie.indexOf("flash") == -1) {
        setCookie("flash", "true");
    }
    return document.cookie.indexOf("flash=true") != -1;*/
}

function showHtmlSite() {
    var href = window.location.href;
    
    /*HACK: When clicking on home in the Flash version, the anchor URL changes from /#/ to
    /#/en/home which doesn't exist on the server and gives a 404 when trying to show the HTML version
    so here we chop out /en/home and send back to the root url*/
    var catchHome = href.match(/\/#\/((\w{2})\/home)/);
    if (catchHome) {
        window.location.href = href.replace(catchHome[1], catchHome[2]);
    }
    
    else {
        window.location.href = window.location.href.replace("/#/", "/");
    }
}

function doSwfEmbed() {
	flashvars.language = getCookie("language", "en");	
    swfobject.embedSWF("/swf/loader.swf", "background", "100%", "100%", "9.0.0", false, flashvars, params, attributes);
}    

function showFlashSite() {

    //Regex matches first slash after the end of the domain name. Should be domain agnostic
    //Do this so we know where to insert the # symbol for URLs like http://cabosaoroque.com/en/residential
    var path = window.location.href.match(/\w{1}\/\w{1}/);            
    
    //If we have a path then we are adding an anchor to the URL, which will result in a server request as the
    //URL is changing from .com/some/path to .com/#/some/path which is .com/ as far as the browser is concerned.
    if (path) {
        path = path.toString();
        window.location.href = window.location.href.replace(path, path.replace("/", "/#/"));
    }
    //If we are at the base site .com/ then we add the anchor for Flash's benefit, but we have to do the SWF
    //embed because adding the anchor doesn't constitute a page refresh (no request to the server)
    else {
        window.location.href += ("#/" + getCookie("language", "en") + "/");
        doSwfEmbed();
    }
    
}

function toggleFlash() {
    //Turning Flash off
    if (bFlashOn()) {
        setCookie("flash", "false");
        //This will result in a server roundtrip
        showHtmlSite();
    }
    //Turning Flash on
    else {
        setCookie("flash", "true");
        showFlashSite();
    }
}

function flash() {
	//If we have no # then we may want to turn Flash on
	if (window.location.href.indexOf("#") == -1) {
	    //This is true by default when there's no cookie
	    if (bFlashOn()) {
	        showFlashSite();
	    }
	}
	else {
	    if (bFlashOn()) {
	        doSwfEmbed();
	    }
	    else {
	        showHtmlSite();
	    }
	}
}
flash();