var videoWidth = 567;
var videoHeight = 311;
var videoWidth2 = 481;
var videoHeight2 = 271;
var currenttab = 1;
var tabCount = 3;
var videourlarray = new Array();
var shareurlarray = new Array();
var loadPanVideo= true

var timeoutId = null;


/*function autoSwitchHeader()
{
	if (autoSwitch)
	{
		currenttab++;
		if (currenttab > tabCount) currenttab = 1;
		
		changeLanciHeaderTab(undefined,currenttab);		
		timeoutId = setTimeout('autoSwitchHeader()', switchInterval);
	}
}*/ 

function autoSwitchHeader()
{
	if (autoSwitch && tabCount!=1 )
	{
		currenttab++;
		if (currenttab > tabCount) currenttab = 1;

		var onclickFun = ($('#lanciheader_li_'+currenttab+' .box_Img a').attr('onclick')).toString();
		var onclickParam = onclickFun.substring(onclickFun.indexOf("changeLanciHeaderTab(")+21,onclickFun.indexOf(");")-1);
		var onclickParamItem = onclickParam.split(",");
		var paramlenght = onclickParamItem.length;

		if(paramlenght == 4){
			
			var urlVideo = onclickParamItem[3].substring(onclickParamItem[3].indexOf("http://"));
		//	alert("-->"+onclickParamItem[3] +'-----'+onclickParamItem[3].indexOf("http://")+"------------"+urlVideo);
			changeLanciHeaderTab(undefined,currenttab,true,urlVideo);	
		}
		else{
			changeLanciHeaderTab(undefined,currenttab);		
			timeoutId = setTimeout('autoSwitchHeader()', switchInterval);
		}
		
	}
	else if (autoSwitch && tabCount==1 && urlVideoPanFirst!=''){
	  stopAutoSwitch();
		openVideoPlayerPan(urlVideoPanFirst,undefined);
	}
}

function startAutoSwitch()
{
	//alert('start');
	autoSwitch = true;
	timeoutId = setTimeout('autoSwitchHeader()', switchInterval);
}

function stopAutoSwitch(){	
	//alert('stop');
	clearTimeout(timeoutId);
}

function openVideoPlayer(event)
{

	if (videourlarray != null && currenttab != null && videourlarray[currenttab] != null && videourlarray[currenttab] != "")
	{
		var playerDiv = document.getElementById("PolymediaShowFX_MediasetPremium_div");
		if (playerDiv != null)
		{
			playerDiv.innerHTML = PSFX_GetPlayerCode(videoWidth, videoHeight, videourlarray[currenttab], shareurlarray[currenttab]);
			
			var videoTrack = "http%3A//www.mediasetpremium.it/video";
			try{
				videoTrack = videoTrack + videourlarray[currenttab].substring(videourlarray[currenttab].lastIndexOf("/"));
			} catch(exc){
				videoTrack = videoUrl;
			}
			
			//playerDiv.innerHTML += '<div style="z-index: 30; right: 0; bottom: 0;"><img src="//secure-it.imrworldwide.com/cgi-bin/m?ci=gruppomediaset-it&cg=0&si=' + videoTrack + '" alt=""/></div>';
			playerDiv.innerHTML += '<div style="z-index: 30; right: 0; bottom: 0;"><img src="//secure-it.imrworldwide.com/cgi-bin/m?ci=gruppomediaset-it&cg=0&si=' + videoTrack + '%26refresh_ce-awe" alt=""/></div>';

			var arrayPageSize = getPageSize();

			var bgPlayerDiv = document.getElementById("box_Player_div_bg");
			if (bgPlayerDiv != null)
			{
				bgPlayerDiv.style.width = arrayPageSize[0] + "px";
				bgPlayerDiv.style.height = arrayPageSize[1] + "px";
				bgPlayerDiv.style.display = "block";
				bgPlayerDiv.onclick = closeVideoPlayer;
			}

			var boxPlayerDiv = document.getElementById("box_Player_div");
			if (boxPlayerDiv != null)
			{
				boxPlayerDiv.style.display = "block";
				autoSwitch = false;
			}
		}
	}
	
	if (event!=undefined){						
			cancelEvent(event);				
	}	
	return true;
}

function closeVideoPlayer(event)
{
	var playerDiv = document.getElementById("PolymediaShowFX_MediasetPremium_div");
	if (playerDiv != null)
	{
		playerDiv.innerHTML = "";
	}						

	var bgPlayerDiv = document.getElementById("box_Player_div_bg");
	if (bgPlayerDiv != null)
	{
		bgPlayerDiv.style.display = "none";
	}

	var boxPlayerDiv = document.getElementById("box_Player_div");
	if (boxPlayerDiv != null)
	{
		boxPlayerDiv.style.display = "none";
	}
	if (event!=undefined)
		cancelEvent(event);
	return true;
}

function checkStartVideo()
{
	if (document.location.href.indexOf("video=true") > 0)
	{
		if (currenttab == 1 && videourlarray != null && videourlarray[1] != null && videourlarray[1] != '')
		{
			openVideoPlayer(undefined);
		}
	}
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------


function openVideoPlayerPan(videourlarrayPan,event)
{
	urlVideoPanFirst = videourlarrayPan;
//	alert(videourlarrayPan+' - '+event)
  shareurlarray = '';
	if (videourlarrayPan != null  && videourlarrayPan != null && videourlarrayPan != "")
	{
		var playerDiv = $('#autopromoPanoramico');
		
		
		if (playerDiv != null)
		{
			stopAutoSwitch();
			$(".box_Featured .box_Main").css('display','none');
			var playerCode = PSFX2_GetPlayerCode(videoWidth2, videoHeight2, videourlarrayPan, shareurlarray);
			$('#autopromoPanoramico').html(playerCode);
		}
	}
	if (event!=undefined){						
			cancelEvent(event);				
	}
	return true;
}
