var fullscreen=false;
var autoPlay=true;
var PLcomplete=false;
var aPlayList= new Array();

var ajaxpage=getXmlHttpRequestObject();
var ajaxplaylist=getXmlHttpRequestObject();
var vflag=null;//id do video que esta a ser visto
var vmax=0;//numero max de videos na playlist
var tvid='tv'+Math.round(Math.random()*255+1);
var cload=0;//Contador de complete load's
var speed=0;//variavel com a velocidade de acesso a net




function sizewindow(){
	var winmax= new Array;
	if(typeof( window.innerWidth ) == 'number' ) {
		//Não-IE
		winmax[0] = window.innerWidth;
		winmax[1] = window.innerHeight;
	}else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+
		winmax[0] = document.documentElement.clientWidth;
		winmax[1] = document.documentElement.clientHeight;
	}else if(document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4
		winmax[0] = document.body.clientWidth;
		winmax[1] = document.body.clientHeight;
	}else{
		winmax[0]=800;
		winmax[1]=600;
	}
return winmax;
}

function resize(){
	win=sizewindow();
	//document.getElementById("video_playlist").innerHTML = 'max X: '+win[0]+' max Y: '+win[1]+'<br>'+'X: '+(win[0]-160)+' Y: '+(win[1]-333)+'';
	if (win[0]<968) {
		document.getElementById("site_fundo").style.width = (win[0]-168)+"px";
		document.getElementById("site_content").style.width = (win[0]-168)+"px";
	}else{
		document.getElementById("site_fundo").style.width = "805px";
		document.getElementById("site_content").style.width = "805px";
	}
	var defheight=348;
	//document.getElementById("site_fundo").style.width = (win[0]-176)+"px";
	document.getElementById("site_fundo").style.height = ((win[1]-defheight)+8)+"px";
	//document.getElementById("site_content").style.width = (win[0]-176)+"px";
	document.getElementById("site_content").style.height = ((win[1]-defheight))+"px";
}

function getCookie(c_name)
{
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "":(";expires=" + exdate.toGMTString()));
}


function create_tv(id){
	src='player.swf';//PARAM NAME="Scale" VALUE="NoScale
	document.getElementById("video_player").innerHTML=('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' +
	'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" ' +
	'width="100%" height="100%" id="playertv" name="playertv" align="middle"><param name="allowScriptAccess" value="sameDomain" />' +
	'<param name="movie" value="'+src+'" /><param name="Scale" value="NoScale" /><param name="allowFullScreen" value="true" /><param name="quality" value="high" /><param name="flashvars" value="lcId='+id+'"/>' + 
	'<embed src="'+src+'" quality="high" width="100%" height="100%" name="playertv" id="playertv" flashvars="lcId='+id+'" ' +
	' align="middle" allowScriptAccess="sameDomain" Scale="NoScale" allowFullScreen="true" type="application/x-shockwave-flash"'+
	' pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
}
function callflasfunction(ft_flash,value){
	vars='lcId='+tvid+'&functionName='+ft_flash+'&t0=num&d0='+value+'';
	document.getElementById("flvfunction").innerHTML='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
	'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,65,0" width="1" height="1">' +
	'<param name="movie" value="JSFG.swf"/><param name="quality" value="high"/><param name="bgcolor" value="#ffffff"/><param name="flashvars"' +
	'value="'+vars+'"/><embed src="JSFG.swf" quality="high" bgcolor="#ffffff" width="1" height="1" type="application/x-shockwave-flash"' +
	' flashvars="'+vars+'" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
	return false;
}

function changefullscreen(){
	if (fullscreen==true) {
		document.getElementById("site").style.display = "";
		document.getElementById("video_player").className="";
		fullscreen=false;
		if (navigator.appName=="Netscape") {
			setTimeout('playvideo(vflag);',500);
		}else{
			document.getElementById("video_player").style.width = "";
			document.getElementById("video_player").style.height = "";
		}
	}else{
		document.getElementById("site").style.display = "none";
		document.getElementById("video_player").className='full';
		fullscreen=true;
		if (navigator.appName=="Netscape") {
			setTimeout('playvideo(vflag);',500);
		}else{
			win=sizewindow();
			document.getElementById("video_player").style.width = (win[0])+"px";
			document.getElementById("video_player").style.height = (win[1])+"px";
		}
	}
}

function playlist(a){
	aPlayList=a;
	vmax=aPlayList.length;
	autoPlay=false;
	PLcomplete=true;
	var html='';
	for(i=0;i<vmax;i++){
		var nomeclass = (i%2==0?'listvp':'listvi');
		html+= '<div id="lv'+i+'" class="'+nomeclass+'" onclick="playvideo('+i+');">';
		html+= '<img src="video/jpg/'+aPlayList[i][0]+'.jpg" width="100" height="75" align="right">'+aPlayList[i][1]+'</div>';
	}
	document.getElementById("video_playlist").innerHTML=html;
}

function playvideo(id){
	if (PLcomplete==true) {
		autoPlay=true;
		vflag=(id);
		callflasfunction("playnext", aPlayList[vflag][0]);
		selectthis(vflag)
	}else{
		setTimeout('playvideo('+id+');',500);
	}
}

function selectthis(divid){
	for (i=0;i<vmax;i++) {
		document.getElementById("lv"+i).className=(i%2==0?'listvp':'listvi');
	}
	if (divid>=0) {
		document.getElementById("lv"+divid).className="listvselect";
		if (divid>0) {
			document.getElementById("video_playlist").scrollTop =(79*(divid-1));
		}
	}
}

function getnextvideo(){
	if (autoPlay==true) {
		vflag++;
		if (vmax<=vflag) {
			vflag=0;
			autoPlay=false;
		}else{
			playvideo(vflag);
		}
	}else{
		//alert("no autoPlay");
	}
}
//*********************************TABS***************************************


//inicio ajax ***************************************************************
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("O seu browser não é compactivel com este site.\n Pode utilizar o Firefox 2.0 para visitar este site");
	}
}

function msg_log_page(ok){
	if (ok==true) {
		document.getElementById("site_logos").style.display = "block";
	}else{
		document.getElementById('site_html').innerHTML='<div id="layer_loadsite"><img src="images/loading.gif" alt="A ler conteúdo" width="16" height="16"> '+ 'A ler conteúdo</div>';
		document.getElementById("site_logos").style.display = "none";
	}
}


function loadpage(url) {
	ajaxpage.abort();
	ajaxpage=getXmlHttpRequestObject();
	ajaxpage.onreadystatechange = loadpagecomplete;
	ajaxpage.open("GET", url, true);
	msg_log_page(false);
	ajaxpage.send(null);
	return false;
}

function loadpagecomplete() {
	if (ajaxpage.readyState == 4) {
		if (ajaxpage.status == 200) {
			msg_log_page(true);
			document.getElementById('site_html').innerHTML=(ajaxpage.responseText);
		}
	}
}

function loadplaylist_find(txt){
	ajaxplaylist.abort();
	ajaxplaylist=getXmlHttpRequestObject();
	ajaxplaylist.onreadystatechange = loadplaylistcomplete;
	ajaxplaylist.open("GET", "playlist.php?txt="+txt, true);
	ajaxplaylist.send(null);
	//tabs_select(-1);
	return false;
}

function loadplaylist(id){
	ajaxplaylist.abort();
	ajaxplaylist=getXmlHttpRequestObject();
	ajaxplaylist.onreadystatechange = loadplaylistcomplete;
	ajaxplaylist.open("GET", "playlist.php?id="+id, true);
	ajaxplaylist.send(null);
	//tabs_select(id);
	return false;
}

function tabs_select(id){
	var lis = document.getElementById('tabs').getElementsByTagName('A');
	for(var i=0; i<lis.length; i++)
		lis[i].className="";
	if (id>=0) {
		document.getElementById('tab'+id).className="on";
	}
}

function loadplaylistcomplete() {
	if (ajaxplaylist.readyState == 4) {
		if (ajaxplaylist.status == 200) {
			eval(ajaxplaylist.responseText);
			//autoPlay=false;
		}
	}
}

//fim da pesquisa ajax



function ini_calc_speed(){
	var speed=getCookie("speed");
	if (speed<6000) {
		document.getElementById("load_msg").innerHTML="Largura de banda de internet";
		var rand= Math.round(Math.random()*10000+1);
		var d = new Date();
		var t = d.getTime();
		document.getElementById("img_load").innerHTML = ('<img src="load.php?id=' + rand + '" onload="calc_speed(' + t + ');" width="1" height="1">');
	}else{
		setTimeout('loadcomple();',5000);
		//loadcomple();
	}
}

function calc_speed(t){
		var size_img=155.714;
		var d2 = new Date();
		var t2 = d2.getTime()
		ttotal = (t2-t)/1000;//em segundos
		speed=Math.round(size_img/ttotal);
		document.getElementById("img_load").innerHTML='';
		window.status='Tem Uma velocidade de acesso ao site de '+speed + 'Kb';
		setCookie("speed",speed,1);
				//document.getElementById("site_logos").innerHTML=speed;

		loadcomple();
}

function loadcomple(){
	cload++;
	if (cload>1) {
		autoPlay=true;
		playvideo(0);
		document.getElementById("layer_load").style.display = "none";
		document.getElementById("video_player").style.display = "block";
	}else{

	}
	//document.getElementById("layer_site").style.display = "block";
}


function loadscript(name){
	var head = document.getElementsByTagName("head")[0];
	var script = document.createElement("script");
	script.src = "css/" + name + "";
	head.appendChild(script);
}

function ini_win(){
	win=sizewindow();
	if (win[0]<800 || win[1]<555) {
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
				if (top.screenX>0 || top.screenY>0) top.moveTo(0,0);
				if (top.outerWidth < screen.availWidth)
					top.outerWidth=screen.availWidth;
				if (top.outerHeight < screen.availHeight) 
					top.outerHeight=screen.availHeight;
			} else {
				top.moveTo(-4,-4);
				//top.resizeTo(screen.availWidth+8,screen.availHeight+8);
			}
		}
	}
	loadplaylist(0);
	win=sizewindow();
	if (win[0]<760 || win[1]<500) {
		loadscript("create_min.js");
		alert("Poderá ter problemas a visualizar este site, uma vez que este esta optimizado para a resolução 1024 X 768");
	}else if(win[1]<560){//carregar sem o submenu
		submenu=false;
		loadscript("create_site.php");
	}else{//carregar com o submenu
		submenu=true;
		loadscript("create_site.php");
	}
	//ini_calc_speed();
}

function centermsg(){
	win=sizewindow();
	document.getElementById("lyr_center").style.left = ((win[0]-230)/2)+"px";
	document.getElementById("lyr_center").style.top= ((win[1]-120)/2)+"px";
}

//document.onload=ini_calc_speed();
/*document.write('<div id="layer_load"><div style=" _position: absolute; _top: 50%; _left: 50%; display: table-cell; vertical-align: middle;">' +
'<div style=" _position: relative; _top: -50%; _left: -50%;"><center><div style="margin-bottom:10px;"><img src="images/logo.gif" width="215"'+
' height="60" align="absmiddle"></div><img src="images/loading.gif" alt="A carregar dados" width="16" height="16"> A Carregar Site <br>'+
'<div id="load_msg">HTML</div><div id="img_load"></div><noscript>Este site só functiona com Browser com javascript activo</noscript></center>'+
'</div></div></div>');
*/

document.write('<div id="layer_load"><div id="lyr_center">'+
'<center><img src="images/logo_b.gif" width="215" height="60" style="margin-bottom:15px;"><br>'+
'<img src="images/loading.gif" alt="A carregar dados" width="16" height="16"> A Carregar Site <br>'+
'<div id="load_msg">HTML</div><div id="img_load"></div></center></div></div><script>centermsg();</script>');

