// para aplicar o javascript é necessário um container 
// que suporte onmouseover e onmouseout
// depois esse container apanha com a iframe

//initial banner id's
var banners=[];
//set duration for each image
var banner_duration = 20; 
//initial active banner
var active_banner=0;
//timer usado
var timer_banner=0;

function pickRandom(range) {
	if (Math.random){
		return Math.round(Math.random() * (range-1));
	} else {
		var now = new Date();
		return (now.getTime() / 1000) % range;
	}
}

function switchBanner() {
	if ( banners.length == 0 ) //não existem banners definidos
		return;
	if ( timer_banner ){
		window.clearTimeout(timer_banner);
		timer_banner=0;
	}
	myban = document.getElementById("banner_frame");
	if ( myban == null ){
		//active_banner=[um calculo qualquer]
		// random banner
		active_banner=pickRandom(banners.length);
		//adiciona a iframe dentro do container se a iframe ainda não existe
		myparent=document.getElementById("banner_container");
		myparent.innerHTML='<iframe id="banner_frame" src="get_banner.php?id='+banners[active_banner]+'" frameborder="0" class="bannerstyle" scrolling="no"></iframe>';
		myparent.onmouseover=banner_mouse_in;
		myparent.onmouseout=banner_mouse_out;
	if ( document.getElementById("banner_frame") == null )
			return; //algo correu mesmo mal
	}else{
		// random banner
		new_banner=pickRandom(banners.length);
		if ( new_banner == active_banner ){
			new_banner=pickRandom(banners.length);
		}
		//increment banner ( not random )
		//new_banner=active_banner+1;
		if ( new_banner >= banners.length ) new_banner=0;
		myban.src='get_banner.php?id='+banners[new_banner];
		active_banner=new_banner;
	}
	//so activa o timer se ouver mais do que um banner possivel para carregar
	if ( banners.length > 1 ){
		timer_banner=window.setTimeout("switchBanner()",banner_duration*1000);
	}
}
//funções para quando se passa com o rato pela zona do banner
//assim se se colocar o rato em cima ele não continua a mostrar os banners
//em vez disso espera que o rato saia para continuar a rodar
function banner_mouse_in(){
	if ( timer_banner ){
		window.clearTimeout(timer_banner);
		timer_banner=0;
	}
}
function banner_mouse_out(){
	if ( banners.length > 1 ){
		timer_banner=window.setTimeout("switchBanner()",banner_duration*1000);
	}
}
