var homeServer;
var exceptions = {
				'en-ca':'en-ww',
				'en-gb':'en-ww',
				'en-au':'en-ww',
				'fr-ca':'fr-fr',
				'en-eu':'FA-0'
				};
var exceptionsA = [
				   'en-ca',
				   'en-gb',
				   'en-au',
				   'fr-ca',
				   'en-eu',
				   ];

var langs = ['en-ww','en-eu','en-au','pt-br','en-ca','fr-ca','cs-cz','de-de','es-es','es-ww','fr-fr','it-it','pl-pl','pt-pt','ru-ru','tr-tr','en-gb','ja-jp','zh-cn','ar-ww','de-ch','fr-ch'];
$(document).ready(function(){
	
	//document.cookie = "avastCoMLocale=cs-cz";

	if(document.cookie)
	{
		var loc = "";
		for(var i = 0; i < langs.length; i++)
		{
			if(document.URL.indexOf(langs[i]) != -1) loc = langs[i];
		}
		if(loc == "") loc = getCookie("avastComLocale");
		
		if(InArray(exceptionsA,loc)) loc = exceptions[loc];
			
		for (var i = 0;i<document.fDownload.download.length;i++)
		{
			if (document.fDownload.download.options[i].value == loc)
			{
				document.fDownload.download.options[i].selected = true;	
			}
		}
	}				
	else document.fDownload.download.options[0].selected=true;
	SelectDownload(document.fDownload.download);
});

function InArray(array, value) 
{
	inArray = false;
	for (i = 0; i < array.length; i++)
	if (value == array[i]) inArray = true;
	return inArray;
}

function SelectDownload(dForm)
{
	
	$.ajax({
		type: "POST",
		url: RootPath+"scripts/getdownloadfile.php",
		cache: false,
		data: "locale="+dForm.value+"&product=FA",
		dataType: "text",
		beforeSend: function(){
			$("#dButton").css("visibility","hidden");
		},
		success: function(downloadFile){
			setDownloadAttributes(downloadFile);
			$("#dButton").css("visibility","visible");
		},
		error: function(){
		}
	});
}

function setDownloadAttributes(downloadFile)
{
	$("#dButton").attr("href", downloadFile)
		.unbind("click")
		.bind('click',function(){pageTracker._trackEvent('Download', 'Free Antivirus', downloadFile)});
}

function getCookie(name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}