
//rotator
var interval = 5; // delay between rotating images (in seconds)
var random_display = 1; // 0 = no, 1 = yes
interval *= 1000;
var intervaloBanner;
var imagemBanner
var proximaImagem=1



function rotateImage(place) {
if(!document.getElementById(place)){
	if(!intervaloBanner)return false //hack para nao começar logo no inicio
clearTimeout(intervaloBanner)
return false
}



proximaImagem=(proximaImagem==imagemBanner)?1:(proximaImagem+1);
$.post("empresa/escolheImagem.php?n="+proximaImagem+'&r='+ Math.floor ( Math.random ( ) * 50 + 1 ),function(data){
		
			
$('#' + place).fadeOut("slow",
					 function(){
						 $(this).html(data)
						 $(this).fadeIn("slow");
						 })

var recur_call = "rotateImage('"+place+"')";
intervaloBanner=setTimeout(recur_call, interval);
});

}

function abrepagina(site){
		clearInterval(intervalID);
		clearTimeout(intervaloBanner)
		
		//rotateImage('bannerRotator')		
		//alert(site)
		$("#conteudo").html('<img src="js/ajax-loader_base.gif" style="margin-left:15px;">')
		$("#conteudo").load(site  +'?r='+ Math.floor ( Math.random ( ) * 50 + 1 ), function(){
										    recur_call = "rotateImage('bannerRotator')";
											intervaloBanner=setTimeout(recur_call, interval);
										   timer();
												   
												   });

		if(navigator.userAgent.indexOf("Firefox")!=-1) 
		{
			makeHistoryF(site);
		}
		else
		{
			makeHistoryIE(site);
		}
		//alert(site)
		$('[@class=subMenu]').css({ backgroundColor:"", fontWeight:"" });
		if(site)$('[@class=subMenu][@href*='+site+']').css({ fontWeight:"bold" });
}


function muda_banner(num,total)
{
	for(i = 1 ; i <= total ; i++){
		$('#head' + i).hide()
		
	}
	$('#head' + num).show()
	
}
function mostra_banner(num)
{
	if(expectedHash=='projectos/projectos.htm' )
	{
		$('[@id^=head]').hide()
		$('#head' + num).show(); 
	}
}
var intervalID;
$(document).ready(function() {


	
	initShowHideDivs();
	//Mudar Tamanho da Estrutura da Pagina caso browser = Firefox
	
	
	abrepagina('empresa/quem_somos.php')
	//$('#dhtmlgoodies_q1').click();
	if(navigator.userAgent.indexOf("Firefox")!=-1)
	{
		document.all('corpo').style.width=820;
		document.all('reguladorMenu').style.width=160;
		document.all('reguladorCorpo').style.width=788;
		setTimeout('pollHashF();',2000);
		
	}
	$.post("empresa/escolheImagem.php",function(data){
					imagemBanner=data
					
					})
	//historyPage('empresa/empresa.htm'); //Abrir pagina
	
})

//Internet Explorer Back Functions
function makeHistoryIE(newHash)
{
	if(newHash=="#")return false
	document.IFrame.location.href = window.location.href.replace("index.php","iframe.php?a=") + newHash;
  expectedHash = newHash;
  return true;
}
function handleHistoryIE()
{
	//Se pagina foi mudada //Back clicado
	hash = document.IFrame.location.href.replace(window.location.href.replace("index.php","iframe.php?a="),"");
	//alert(hash)
	
	if(hash=="#")return false
	
	if ( hash != expectedHash )
	{
		expectedHash = hash;
		var newoption = expectedHash.replace("#","");
		historyPage(newoption);
  }
  return true;
}
function pollHashIE() 
{
	handleHistoryIE();
	//intervalID = window.setInterval("handleHistoryIE()", 1000);
}

//FIREFOX Back Functions
var expectedHash = "";
//Mudança de pagina
function makeHistoryF(newHash)
{
  window.location.hash = newHash;
  expectedHash = window.location.hash;
  return true;
}
function handleHistoryF()
{
	//Se pagina foi mudada //Back clicado
  if ( window.location.hash != expectedHash )
  {
    expectedHash = window.location.hash;
    var newoption = expectedHash.replace("#","");
		historyPage(newoption);
  }
  return true;
}
function pollHashF() 
{
	handleHistoryF();
	window.setInterval("handleHistoryF()", 1000);
}
//LOGIN
function login() {
	//parent.top.location="http://imo.novopca.pt";
}

//SLIDE
var dhtmlgoodies_slideSpeed = 10;	// Higher value = faster
var dhtmlgoodies_timer = 10;	// Lower value = faster

var objectIdToSlideDown = false;
var dhtmlgoodies_activeId = false;
function showHideContent(e,inputId)
{
	
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	//alert(e + ' :' + inputId)
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('dhtmlgoodies_a' + numericId);
	
	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){

		if(dhtmlgoodies_activeId &&  dhtmlgoodies_activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(dhtmlgoodies_activeId,(dhtmlgoodies_slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,dhtmlgoodies_slideSpeed);
		}
	}else{
		slideContent(numericId,(dhtmlgoodies_slideSpeed*-1));
		dhtmlgoodies_activeId = false;
	}	
}

function slideContent(inputId,direction)
{
	//alert(inputId + ': ' + direction)
	var obj =document.getElementById('dhtmlgoodies_a' + inputId);
	var contentObj = document.getElementById('dhtmlgoodies_ac' + inputId);
	height = obj.clientHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',dhtmlgoodies_timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.display='block';
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,dhtmlgoodies_slideSpeed);				
			}
		}else{
			dhtmlgoodies_activeId = inputId;
		}
	}
}



function initShowHideDivs()
{
	var divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='dhtmlgoodies_question'){
			divs[no].onclick = showHideContent;
			divs[no].id = 'dhtmlgoodies_q'+divCounter;
			
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			
			answer.id = 'dhtmlgoodies_a'+divCounter;	
			
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='dhtmlgoodies_answer_content';
			contentDiv.id = 'dhtmlgoodies_ac' + divCounter;
			answer.style.display='none';
			answer.style.height='1px';
			divCounter++;
		}		
	}	
	
	
	
	
	
	

}




function timer()
{	
	
//clearTimeout(intervaloBanner); 

	if(navigator.userAgent.indexOf("Firefox")!=-1)
	{
		setTimeout('pollHashF();',2000);
	}
	else //Se browser = IE -> iniciar funcoes de back
	{
		intervalID=setTimeout('pollHashIE();',2000);
	}
}
//enviar_imag = new sack();
function historyPage(site){
	
	clearInterval(intervalID);
		if(site == "" || site == "#") site = 'empresa/empresa.htm';
		$("#conteudo").load(site, function(){timer();});
	
}


