
var objContentTab;
var strURL;
var strLoadStatusText="<font id='load'>&nbsp;Carregando...&nbsp;</font>"
var enabletabpersistence=1 //enable tab persistence via session only cookies, so selected tab is remembered (1=yes, 0=no)?
var defaultcontentarray=new Object()


function fSetaTab(tabcontentid, tabnumber){ //interface for selecting a tab (plus expand corresponding content)

	var thetab=document.getElementById(tabcontentid).getElementsByTagName("a")[tabnumber];

	
	if (thetab.getAttribute("rel")!=null || thetab.getAttribute("divid")!=null){
	
		objContentTab = thetab.getAttribute("rel");
		strURL = thetab.getAttribute("href");
		strDivID = thetab.getAttribute("divid");
		
		if(strDivID=="" || strDivID==null ){
		    new Ajax.Updater(	objContentTab, 
							    strURL,
								    {
									    method: 'get',
									    onComplete:fVerificaErro
								    });
    	}
    	fSelecionaTab(tabcontentid, tabnumber);
	}
}

function fSelecionaTab(tabcontentid, tabnumber){
	var thetab = document.getElementById(tabcontentid).getElementsByTagName("a")[tabnumber]
	
	var ullist=thetab.parentNode.parentNode.getElementsByTagName("li")
	for (var i=0; i<ullist.length; i++)
		ullist[i].className=""  //deselect all tabs
	
	thetab.parentNode.className="selected";  //highlight currently clicked on tab
            
            
    var aux="";
 
    strDivID = thetab.getAttribute("divid");
    
    for(var i=0; i< document.getElementById(tabcontentid).getElementsByTagName("a").length; i++){
        aux = document.getElementById(tabcontentid).getElementsByTagName("a")[i];
        if(strDivID!="" && strDivID!=null){
            $(aux.getAttribute("divid")).style.display = "none";
            $(aux.getAttribute("divid")).style.position = "absolute";  
        }
    }
    
    if(strDivID!="" && strDivID!= null){
        $(strDivID).style.display = "inline";
        $(strDivID).style.position = "fixed";
        
    }
}

function fVerificaErro(response){
	
	document.getElementById(objContentTab + "Load").innerHTML="";
	
	if((response.responseText.indexOf('The page cannot be found')!= -1) ||
	(response.responseText.indexOf('HTTP 404')!= -1)){
		$(objContentTab).innerHTML = "<span class=erroClass>erro: P&aacute;gina não encontrada<br>"+ strURL +"</span>";
	}
	else{
		
		try{
			setupZoom();
		}catch(e){}
	}
}

function fMostraErro(response){
//alert('nao deu')
}


function savedefaultcontent(contentid){// save default ajax tab content
	if (typeof defaultcontentarray[contentid]=="undefined") //if default content hasn't already been saved
		defaultcontentarray[contentid]=document.getElementById(contentid).innerHTML
}

function fInicializaTabs(){
    
	for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
	    
		var ulobj=document.getElementById(arguments[i])
		var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
		
		var persisttabindex=(enabletabpersistence==1)? parseInt(getCookie(arguments[i])) : "" //get index of persisted tab (if applicable)
		
		var isvalidpersist=(persisttabindex<ulist.length)? true : false //check if persisted tab index falls within range of defined tabs
		
		for (var x=0; x<ulist.length; x++){ //loop through each LI element
			var ulistlink=ulist[x].getElementsByTagName("a")[0]
			ulistlink.index=x
			if (ulistlink.getAttribute("rel") || ulistlink.getAttribute("divid")){
				ulistlink.onclick=function(){
				
					objContentTab = this.getAttribute("rel");
					strURL= this.getAttribute("href");
					strDivID = this.getAttribute("divid");

					if(strDivID=="" || strDivID==null ){
					    document.getElementById(this.getAttribute("rel")+ "Load").innerHTML=strLoadStatusText;
					    new Ajax.Updater(objContentTab, 
									    strURL,
									    {
										     method: 'get',
										     onComplete:fVerificaErro
									    });
    				}
    				
					fSelecionaTab(this.parentNode.parentNode.id, this.index);
					saveselectedtabindex(this.parentNode.parentNode.id, this.index);
				
					return false;
				}
				
				if ((enabletabpersistence==1 && persisttabindex<ulist.length && x==persisttabindex) || (enabletabpersistence==0 && ulist[x].className=="selected")){
				
					objContentTab = ulistlink.getAttribute("rel");
					strURL = ulistlink.getAttribute("href");
					strDivID = ulistlink.getAttribute("divid");
					
					if(strDivID=="" || strDivID==null ){
		                new Ajax.Updater(objContentTab, 
									    strURL,
									    {
										     method: 'get',
										     onComplete:fVerificaErro
									    });
    				}
    	            fSelecionaTab(ulistlink.parentNode.parentNode.id, ulistlink.index);
				}
			}
		}
	}
}

////////////Persistence related functions//////////////////////////

function saveselectedtabindex(ulid, index){ //remember currently selected tab (based on order relative to other tabs)
	if (enabletabpersistence==1) //if persistence feature turned on
		setCookie(ulid, index)
}

function getCookie(Name){ 
	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) //if cookie found
		return document.cookie.match(re)[0].split("=")[1] //return its value
	return ""
}

function setCookie(name, value){
	document.cookie = name+"="+value //cookie value is domain wide (path=/)
}


var scrollx
var scrolly
function scrollToCoordinates() { 
  window.scrollTo(scrollx, scrolly); 
} 

function saveScrollCoordinates() { 
  scrollx = (document.all)?document.body.scrollLeft:window.pageXOffset; 
  scrolly = (document.all)?document.body.scrollTop:window.pageYOffset; 
}

function fRecarregaTab(strURL, objContentTab){
    strURL = strURL.replace(/&amp;/g, "&");
	new Ajax.Updater(
		objContentTab, 
		strURL, 
		{
		method: 'get',
		onComplete:fVerificaErro
		}
	);
}