var cookieKey = "grey_country_key";
var requestUrl = document.location.href;

function toggleMenu()
{
	$("#country_sites").toggle("slow");
}

function setCountryCookie(country)
{	
	var cookieKey0 = cookieKey + "_0";
	var cookieKey1 = cookieKey + "_1";
	
	var existingCookie0 = getCookie(cookieKey0);
	var existingCookie1 = getCookie(cookieKey1);
	
	if (existingCookie0 != null)
	{
		setCookie(cookieKey1, country, 30);
		delCookie(cookieKey0);
	}
	else
	{
		setCookie(cookieKey0, country, 30);
		delCookie(cookieKey1);
	}
}

function getCountryCookie()
{
	var cookieKey0 = cookieKey + "_0";
	var cookieKey1 = cookieKey + "_1";
	
	var existingCookie0 = getCookie(cookieKey0);
	var existingCookie1 = getCookie(cookieKey1);

	var existingCookie;
	if (existingCookie0 != null)
		existingCookie = existingCookie0;
	else if (existingCookie1 != null)
		existingCookie = existingCookie1;
	else
		existingCookie = null;

	return existingCookie;
}

function redirectBaseOnCookie()
{
	var existingCookie = getCountryCookie();
	var currentUrl = document.location.href;
	
	if (existingCookie != null && (currentUrl.indexOf(existingCookie) == -1))
	{
		var urlBase = window.location.protocol + "//" + window.location.host + "/";
		var redirectUrl = urlBase + existingCookie;
		
		window.location.replace(redirectUrl);
	}
}

function gotoSeeAllMenu()
{
	// hide "main_menu_content" and display "all_country_sites"
	$("#main_menu_content").hide("slow");
	$("#all_country_sites").show("slow");
}

function hideMainLocationMenu()
{
	$("#main_location_menu").hide("slow");
}

function selectAndRedirectBaseOnSelection()
{
	var selectedValue = document.getElementById("selected_location").value;
	var rememberValue = document.getElementById("remember_location").checked;
	
	if (selectedValue == null || selectedValue == "-")
		return;
	
	if (rememberValue == true)
	{
		// remember location - set cookie and redirect to selected site
		setCountryCookie(selectedValue);
		
		var newUrl = "/" + selectedValue;
		var urlBase = window.location.protocol + "//" + window.location.host + "/";
		var redirectUrl = urlBase + newUrl;
		
		window.location.replace(redirectUrl);
	}
	else
	{
		// don't remember location - redirect to selected site
		var newUrl = "/" + selectedValue;
		var urlBase = window.location.protocol + "//" + window.location.host + "/";
		var redirectUrl = urlBase + newUrl;
		
		window.location.replace(redirectUrl);
	}
}

//-------------------------------------------//

function getCookie(NameOfCookie)
{ 
    var nameEQ = NameOfCookie + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function setCookie(NameOfCookie, value, expiredays)
{ 
    if (expiredays) {
        var date = new Date();
        date.setTime(date.getTime()+(expiredays*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = NameOfCookie+"="+value + expires+"; path=/";
}



function delCookie (NameOfCookie)
{ 
	setCookie(NameOfCookie, "", -1);
}
