function translator(pattern) {
	indexof_p = pattern.indexOf('|');
	var psplit = pattern.split('|');
	var lg = psplit[1];

	var thisurl = '';
	if(lg == "en") thisurl = document.location.toString().replace(/\/(pt\/|es\/)/gi, "/");
	else if((lg != "pt") || (lg != "es")) thisurl = document.location.href.replace(/\/(pt\/|es\/)?[^\/]*$/gi, "/") + "language.php?to=" + lg + "&url=" + document.location.toString().replace(/\/(pt\/|es\/)/gi, "/");

	if(thisurl) window.location.href = thisurl;
}

//Zhe following is Writed By Jammy
function Id(key){return document.getElementById(key);}
function Tag(obj,key){if(key==null)return document.getElementsByTagName(obj);else return obj.getElementsByTagName(key);}
function Style(obj,key){return obj.currentStyle?obj.currentStyle[key]:document.defaultView.getComputedStyle(obj,null)[key];}
function stopBubble(){if(window.event)window.event.cancelBubble=true;else arguments[0].stopPropagation();}
function over(sender,maxwid,minpad)//over-flow
{
	var obj=Id(sender);
	var link=Tag(obj,"a");
	if(link.length==0)link=Tag(obj,"span");
	var wid=0;
	for(var i=0;i<link.length;i++)
	{
		link[i].style.paddingLeft=minpad+"px";
		link[i].style.paddingRight=minpad+"px";
		wid+=link[i].offsetWidth;
	}
	while(wid>maxwid){
		var size=Style(link[0],"fontSize");
		size=parseInt(size)-1;
		wid=0;
		for(var i=0;i<link.length;i++)
		{
			link[i].style.fontSize=size+"px";
			wid+=link[i].offsetWidth;
		}
	}
	var pad=(maxwid-wid+link.length*minpad*2)/(link.length*2);
	for(var i=0;i<link.length;i++)
	{
		link[i].style.paddingLeft=pad+"px";
		link[i].style.paddingRight=pad+"px";
	}
}
function tables()
{
	var tables=Tag("table");
	for(var i=0;i<tables.length;i++)
	{
		tables[i].border="0";
		tables[i].cellpadding="0";
		tables[i].cellspacing="0";
	}
}

//Start Run
init();

function init()
{
	try{document.execCommand("BackgroundImageCache",false,true);}catch(e){}
	over("mainmenu",578,10);//overflow
	over("secondmenu",778,10);//overflow
	//over("title1",238,10);over("title2",238,10);over("title3",238,10);//over(sender,max-width,min-padding);
	

	try {
		var lang=Id("lang");
		var tran=Id("trans");
		//lang.onmouseover=function(){this.style.cursor="pointer";tran.style.visibility="visible";}
		//lang.onclick=stopBubble;
		//document.onclick=function(){tran.style.visibility="hidden";}

		var trans=Tag(tran,"dd");
		for(var i=0;i<trans.length;i++)
		{
			trans[i].onmouseover=function(){this.style.cursor="pointer";}
			//trans[i].onclick=function(){translator("en|"+this.id);};
		}
	}
	catch (e){}


	var q=Id("q");var searchbut=Id("searchbut");
	q.onkeyup=function(e){var key=window.event?window.event.keyCode:e.which;if(key==13)searchbut.click();}
	searchbut.onclick=function()
	{
		var num=document.location.href.lastIndexOf('/');
		var base=document.location.href.substring(0,num);
		document.location.href=base+"/search.php?q="+q.value;
	}

	tables();

	setTimeout(function(){
		var getes = document.getElementById("es").getElementsByTagName("a")[0];
		eshref = getes.href.replace(/^[\w\W]*?url=http/gi, "http");
		eshref = eshref.replace(/%3a/g, ":");
		getes.href = eshref.replace(/%2f/g, "/");

		var getpt = document.getElementById("pt").getElementsByTagName("a")[0];
		pthref = getpt.href.replace(/^[\w\W]*?url=http/gi, "http");
		pthref = pthref.replace(/%3a/g, ":");
		getpt.href = pthref.replace(/%2f/g, "/");
	}, 0);
}

// modify the connect word after H3 tags
function setH3(){
	var geth3 = document.getElementsByTagName("h3");
	for(i = 0; i < geth3.length; i++) geth3[i].innerHTML = geth3[i].innerHTML + " ";
}
setH3();

function addLang(){
	var getdiv = document.getElementsByTagName("dl");
	for(g = 0; g < getdiv.length; g++){
		if(getdiv[g].id == "trans"){
			var getimg = getdiv[g].getElementsByTagName("img");
			for(i = 0; i < getimg.length; i++){
				
				getimg[i].onclick = function(){
					var path = this.src;
					path = path.split("fl_")[1].split(".gif")[0];
					if((path != "pt") && (path != "es")) translator("en|" + path);
				};
			}
		}
	}
}
addLang();
