/*	Serpent Productions
    Scriptoids
*/

$(document).ready(function() {

    if (typeof(disableflash) != 'undefined' && disableflash == true) {
        // do nowt
    } else {
        flashinate(); 
    }
});

// Analytinator
function trackPage(vValue) {
		pageTracker._trackPageview(values);
}

function trackClick(vValue, vValue2, vValue3) {
	if (vValue3 == "undefined"){
		pageTracker._trackEvent(vValue, vValue2);
	} else {
		pageTracker._trackEvent(vValue, vValue2, vValue3);
	}
}
	
function getCookie(key, defaultValue) {
	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;
}

function showHtmlSite() {
    var href = window.location.href;
    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("/#/", "/");
    }
}

    // embedSWF
    var params     = {};
    var flashvars  = {};
    var attributes = {};
    params.menu               = "false";
    params.quality            = "high";
    params.bgcolor            = "#000000";
    params.allowfullscreen    = "true";
    params.base				  = ".";
    params.allowscriptaccess  = "true";
    attributes.id			  = "container";
    
function doSwfEmbed() {

    
	flashvars.language = getCookie("language", "en");	
    //swfobject.embedSWF("/swf/loader.swf", "background", "100%", "100%", "9.0.0", false, flashvars, params, attributes);
    swfobject.embedSWF("/swf/stage.swf?v=2", "stage", "100%", "100%", "9.0.0", "/swf/expressInstall.swf", {}, {menu: 'false'}, {id: 'stage'});
}    

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 += ("#/");
        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 flashinate() {

	//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();
	    }
	}
}

