var translationCookie = 'nonprofit_language_preference'; // This is the name of the cookie that is used.

function setLanguage(theLanguage){
	SetCookie(translationCookie,theLanguage); // TL
	var theURL=encodeURIComponent(document.location.href);
	top.location.href="http://translate.google.com/translate?u="+theURL+"&sl=en&tl="+theLanguage+"&hl=en&ie=UTF-8";
}

window.onload=function(){
	if(this.location.href.indexOf("tl=")==-1&&top.location!=this.location){
		top.location.href=this.location.href;
	}
};

if(this.location.href.indexOf("tl=")==-1&&top.location==this.location){
	if (getQuerystring("translate") == "off") {
		SetCookie(translationCookie,"");
		DeleteCookie(translationCookie);
	}
}

function showTranslate(){
	document.getElementById("shim").style.height=document.getElementById("translate_popup").offsetHeight;
	document.getElementById("shim").style.width=document.getElementById("translate_popup").offsetWidth;
	document.getElementById("translate_popup").style.top = 356;
	document.getElementById("translate_popup").style.visibility = "visible";
	document.getElementById("shim").style.top = document.getElementById("translate_popup").style.top;
	document.getElementById("shim").style.visibility = "visible";
}

function hideTranslate(){
	document.getElementById("translate_popup").style.visibility = "hidden";
	document.getElementById("shim").style.visibility = "hidden";
}


function resetLang() {
	var tmpURL = unescape(getQuerystring("u"));
	tmpURL = tmpURL.replace(/%3Ftranslate%3Doff/gi, "");
	tmpURL = tmpURL.replace(/\?translate=off/gi, "");
	top.location.href = tmpURL + "?translate=off";
}

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
} 

function setNav(){
	if ((window.location.href).indexOf("translate_c?hl=en") != -1) { //show simple nav
		document.getElementById("altnav").style.display = "block";
		document.getElementById("translate_button").innerHTML = '<a href="javascript:resetLang()">Back to English</a>';
	}
}

function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
		window.onload = func; 
	} else { 
		window.onload = function() { 
			if (oldonload) { 
				oldonload(); 
			} 
			func(); 
		} 
	} 
} 
addLoadEvent(setNav);

