var pathArray = window.location.pathname.split("/");
//document.write(pathArray +"<BR>");

//Homepage identifiers
var englishHomepage = "http://www.davidsuzuki.org/";
var englishHomepageScore = "http://davidsuzuki.org/#";
var englishHomepageWWW = "http://davidsuzuki.org/";

var englishHomepageIndex = "http://davidsuzuki.org/index.php"; 
var englishHomepageIndexWWW = "http://www.davidsuzuki.org/index.php";

//Test variables

var englishHomepageIndexb = "http://davidsuzuki.org/indexr.php";
var englishHomepageIndexbWWW = "http://www.davidsuzuki.org/indexr.php";

var frenchHomepage = "http://www.davidsuzuki.org/fr/";
var frenchHomepageScore = "http://davidsuzuki.org/fr/#";
var frenchHomepageWWW = "http://davidsuzuki.org/fr/"; 

var frenchHomepageIndex = "http://davidsuzuki.org/fr/index.php"; 
var frenchHomepageIndexWWW = "http://www.davidsuzuki.org/fr/index.php"; 


var homePath = 1; // which place on path you want to detect homepages (1 is default: http://www.davidsuzuki.org/___)
var subPath = 2; // which place on path you want to detect subpages (2 is default: http://www.davidsuzuki.org/folder/___)

var domain = "davidsuzuki.org"; //domain for cookie
var indexDefault = "index.php"; //indexDefault for detector

function detectLocation(){
	
	if (((window.location) == englishHomepage) || ((window.location) == englishHomepageWWW) || ((window.location) == englishHomepageIndex) || ((window.location) == englishHomepageIndexWWW) || ((window.location) == englishHomepageScore) || ((window.location) == englishHomepageIndexb) || ((window.location) == englishHomepageIndexbWWW))
		return "en_home";
	
	if (((window.location) == frenchHomepage) || ((window.location) == frenchHomepageWWW) || ((window.location) == frenchHomepageIndex) || ((window.location) == frenchHomepageIndexWWW) || ((window.location) == frenchHomepageScore)) 
		return "fr_home";
	
	if(pathArray[homePath] == "fr" && pathArray[subPath] != indexDefault && pathArray[subPath] != "")
		return "fr_sub";
	
	if(pathArray[homePath] != "fr" && pathArray[homePath] != indexDefault && pathArray[homePath] != "")
		return "en_sub";
	
	else return null;
}


function createCookie(homepage){
	//create first cookie
	var cookie_string = "homepage" + "=" + ( homepage );
	var expires = new Date ( "2012", "12", "31");
	cookie_string += "; path=/; domain="+ domain +"; expires=" + expires.toGMTString();
	//document.write(cookie_string + "<BR>");
	document.cookie = cookie_string;
}

function checkCookie(){
	var cookieHomepage = document.cookie.match ( '(^|;) ?' + 'homepage' + '=([^;]*)(;|$)' );
	if ( cookieHomepage )
		return ( unescape ( cookieHomepage[2] ) );
	else
		return -1;
}

function goToFrench(){
	//set French cookie, then redirect depending where they are	
	createCookie(frenchHomepage);
}

function goToEnglish(){
	//set English cookie, then redirect depending where they are	
	createCookie(englishHomepage);
}

function redirectHomepage(){
	//If user is not on their cookie's homepage, go to cookie's homepage
//	if (checkCookie() != window.location){
//	window.location.href = checkCookie();	
//	}
//	
	//detect other all 5 variables of each homepage
	if (checkCookie() != window.location){	
		if(checkCookie() == englishHomepage){
			if(((window.location) == englishHomepage) || ((window.location) == englishHomepageScore) || ((window.location) == englishHomepageWWW) || ((window.location) == englishHomepageIndex) || ((window.location) == englishHomepageIndexWWW) || ((window.location) == englishHomepageIndexb) || ((window.location) == englishHomepageIndexbWWW)){
				//do nothing
			} else {
				//we don't think it's useful to redirect from fr to en homepage
				//window.location.href = checkCookie();	
			}
		}
		if(checkCookie() == frenchHomepage){
			if(((window.location) == frenchHomepage) || ((window.location) == frenchHomepageScore) || ((window.location) == frenchHomepageWWW) || ((window.location) == frenchHomepageIndex) || ((window.location) == frenchHomepageIndexWWW)){
				//do nothing
			} else {
				window.location.href = checkCookie();	
			}
		}
	}
}

function userSetEnglish(){
	//languageWindow button function: set English homepage as cookie and check
	createCookie(englishHomepage);
	document.getElementById("windowWrapperbg").style.display = "none";
	document.getElementById("windowWrapper").style.display = "none";
	document.body.style.overflow = "auto";
	redirectHomepage();
}

function userSetFrench(){
	//languageWindow button function: set French homepage as cookie and check
	createCookie(frenchHomepage);
	document.getElementById("windowWrapper").style.display = "none";
	document.getElementById("windowWrapperbg").style.display = "none";
	document.body.style.overflow = "auto";
	redirectHomepage();
}


//Run
//document.write("Browser Location: "+ window.location + "<BR>");
//document.write("Cookies exist? "+ checkCookie() +"<BR>");
//document.write("Location: "+ detectLocation() + "<BR>");

if((detectLocation() == "en_home") || (detectLocation() == "fr_home") || (window.location == "http://davidsuzuki.org/index-test.php") || (window.location == "http://www.davidsuzuki.org/index-test.php") || (window.location == "http://davidsuzuki.org/fr/index-test.php") || (window.location == "http://www.davidsuzuki.org/fr/index-test.php")){
	if(checkCookie() == -1){}
	else { redirectHomepage(); }
} else {	
}

//manually set cookie
function showLanguageLink() {
	if ((detectLocation() == "en_home") || (detectLocation() == "en_sub")){ //if cookie is english, show francais link
		document.write('<a href="'+ frenchHomepage +'" onClick="goToFrench();">Fran&#231;ais</a>');
	}
	
	if ((detectLocation() == "fr_home") || (detectLocation() == "fr_sub")){ //if cookie is french, show english link
		document.write('<a href="'+ englishHomepage +'" onClick="goToEnglish();">English</a>');	
	}
}
