//Redirect da skylife.it a sky.it
document.write('<script type="text/javascript" src="/js/redirectSkylifeToSky.js"></script>');

/*******************************************/
/************** COMMONS ********************/
/*******************************************/
 
function popup(url, width, height, otherSettings) { 
	var pw = width ? width : 450;
	var ph = height ? height : 300;
	var leftPos = (screen.width-pw)/2;
   	var topPos = (screen.height-ph)/2;
	var os = otherSettings ? ',' + otherSettings : '';
	var popup = window.open(url, '', 'width='+pw+',height='+ph+',top='+topPos+', left='+leftPos + os);
	popup.moveTo(leftPos, topPos);
	if (popup.focus) popup.focus();
}

function popupOW(url,name, width, height, otherSettings) { 
	var pw = width ? width : 450;
	var ph = height ? height : 300;
	var leftPos = (screen.width-pw)/2;
   	var topPos = (screen.height-ph)/2;
	var os = otherSettings ? ',' + otherSettings : '';
	var popup = window.open(url, name, 'width='+pw+',height='+ph+',top='+topPos+', left='+leftPos + os);
	popup.moveTo(leftPos, topPos);
	if (popup.focus) popup.focus();
}

function retrieveUrlCallback(req){
	if (req.readyState == 4){
		if (req.status == 200){
			document.getElementById('registrationForm').innerHTML = req.responseText;
		}else{
			alert("Si e' verificato il seguente problema:\n " + req.statusText);
		}
	}
}

function callLogout(url){
	var xhr = MochiKit.Async.getXMLHttpRequest();
	xhr.open("GET", url, true);
	var deferred = MochiKit.Async.sendXMLHttpRequest(xhr);
	deferred.addBoth(retrieveUrlCallback);
}


//per pagine non Sky Life, non applicative, non video Tg24
function setPageTitle(){
  try{
    if(document.location.href.indexOf('html/skylife')<=0 && document.location.href.indexOf('.do')<=0 && document.location.href.indexOf('/jforum')<=0 && document.location.href.indexOf('/singolo_')<=0){
      document.title = 'SKY Life';
    }
  }catch(e){
  }
}



function iframeControl(src,elementToHide,elementToShow){
	document.getElementById(elementToShow).src=src;
	MochiKit.DOM.hideElement(elementToHide);
	MochiKit.DOM.showElement(elementToShow);
}


function loginPost() {
	var f = document.getElementById("loginform");
	var username = f.username.value;
	var password = f.password.value;
	var formData = 'username=' + username + '&password=' + password;
	var xhr = MochiKit.Async.getXMLHttpRequest();
	//xhr.open("POST", SkyWeb.Constants.baseHttpsAddress+'/login.do', true);
	xhr.open("POST", '/login.do', true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	var deferred = MochiKit.Async.sendXMLHttpRequest(xhr, formData);
	deferred.addBoth(onLoginPostResult);
}

function onLoginPostResult(req) {
	if (req.status == 200) {
		document.getElementById('registrationForm').innerHTML =
		req.responseText;
	} else {
		alert("Si e' verificato un errore:\n"+req.statusText);
	}
}




/*******************************************/
/************** COMMONS ********************/
/*******************************************/


//apre una finestra Pop-Up
function openPopup(urlPopup,popWidth,popHeight){
	popup(urlPopup,popWidth,popHeight,'scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no');
}

function openPopup2(urlPopup,popWidth,popHeight){
	popup(urlPopup,popWidth,popHeight,'scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no');
}


//Pop-Up canali video variante
var cliccato="none";
var cliccatoTmp="none";
var box = 'novita';

function mostraBoxVideo(id){
	//document.getElementById(id+'_mediaPlayer').stop();
	document.getElementById(box + "_box").style.display = 'none';
	document.getElementById(id + "_box").style.display = 'block';
	//alert(document.getElementById(id+'_mediaPlayer').mrl());
	
	box = id;
	cliccatoTmp=cliccato;	
	changeimg2(id,'on');		
	cliccato = id;
	changeimg2(cliccatoTmp,'off');
}

function changeimg2(id,come){
	if(cliccato!=id && id!="none")
		eval("document." + id).src="/img/PopUp/"+ id + "_"+ come +".gif";
}

function Navigate() {
	
	indpag = document.Form2.cmbCanale.selectedIndex;
	pag = document.Form2.cmbCanale.options[indpag].value;
	if (pag == "") return;
    window.location = pag;
}

function loginRedirect() {
	var f = document.getElementById("loginform");
	var username = f.username.value;
	var password = f.password.value;
	retrieveUrl('/login.do?username=' + username + '&password=' + password+'','registrationForm')
}

function showHideId(id,action)
{
	document.getElementById(id).style.display = action;
	document.location.href ='#textCommento';

}
        
function callExternalAjax(url){
	var xhr = MochiKit.Async.getXMLHttpRequest();
	xhr.open("GET", url, true);
	var deferred = MochiKit.Async.sendXMLHttpRequest(xhr);
	deferred.addBoth(retrieveUrlCallback);
}

//refresh user session every xx minute
function refreshUser(){
	//2 minutes
	var time = 2 * 60 * 1000;
	callExternalAjax('/refreshUser.do');
	setTimeout("refreshUser()",time);
}

//spezza una stringa ogni n caratteri
function spezzaTesto(testo,num) {

	var numeroMax = num;
	var fraseIni = testo;
	var word_array = fraseIni.split(" ");
	var fraseTemp ="";
	var cont = 0;
	for (i=0; i<word_array.length; i++) {
				
		if (word_array[i].length > numeroMax) {

			cont = Math.floor(word_array[i].length / numeroMax);
			for (j=0; j<cont; j++) {
				fraseTemp = fraseTemp + word_array[i].substring(j*numeroMax,j*numeroMax+numeroMax) + " ";
			}
			if (word_array[i].length % numeroMax != 0) fraseTemp = fraseTemp + word_array[i].substring(cont*numeroMax,word_array[i].length) + " ";
		}
		else fraseTemp = fraseTemp + word_array[i];

		if (i < word_array.length-1) fraseTemp = fraseTemp + " ";
	}
	fraseResult = fraseTemp;
	return fraseResult;
}

//visualizza player FULLSCREEN
function fullScreen(image, videoTitle, description, videoData, videoHigh, linkUrl1, linkUrl2, videoBumperH1, videoBumperH2){
	var page = "/pages/tg24/homePageSezione/fullscreen.jsp";
	var stile = "top=0, left=0,status=no, menubar=no, toolbar=no, scrollbar=yes";
	
	page=page+"?image="+image;
	page=page+"&titolo="+videoTitle;
	page=page+"&dataVideo="+videoData;
	page=page+"&videoHigh="+videoHigh;
	page=page+"&linkUrl1="+linkUrl1;
	page=page+"&linkUrl2="+linkUrl2;
	page=page+"&videoBumperH1="+videoBumperH1;
	page=page+"&videoBumperH2="+videoBumperH2;
	newwindow=window.open(page,"FullScreen",stile);
	
}

//max maxLen caratteri in textarea
function checkSizeAgainstMax(textArea, str, maxLen) {
	if (str.length >= maxLen) {
        textArea.value = textArea.value.substring(0, maxLen);
    }
}

//Funzione che esegue il submit di un form premendo Enter
function checkEnter(e,formName){ 
	//e is event object passed from function invocation
	//formName is name form
	var characterCode; 
	//literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		
		//Nel caso di login nell'header
		if (formName == 'loginform')
		{
			document.getElementById('parent').value = window.parent.location.href; 
			if ((document.getElementById('username').value == 'Username' && document.getElementById('password').value == 'Password') || (document.getElementById('username').value == '' && document.getElementById('password').value == '')) return false; 
		}

		document.forms[formName].submit(); //submit the form
		return false;
	}
	else{
		return true;
	}
}

function opSGA(myLink){
	window.open(myLink,'SGA','width=805,height=695,left=0,top=0,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no')
}

function opSGA_2(myLink){
	window.open(myLink,'SGA','width=858,height=695,left=0,top=0,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no')
}


function opSGA_3(myLink){

// PopUp Oscar 2008
	window.open(myLink,'SGA','width=750,height=630,left=0,top=0,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no')
}


function goCloseF(){
	alert('Invio eseguito correttamente');
	myWin.close();
}

function sendPgSVC(){
	myWin=window.open("/html/skylife/skyvivo/vuoto.htm","formIns","width=5,height=5,left=5000,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no");
	document.getElementById("sMail");
	document.getElementById("sMail").target="formIns" ;
	//document.getElementById("testoSezione").innerHTML=document.getElementById("testoSezione").innerHTML
	setTimeout('goCloseF()',500)
}

//Generatore numero random per DART
v_randomnumber=Math.random()*10000000000000000;

//Funzione per il motore di ricerca nella Header di SKY Life
function goGSA()
{
var searchQ=document.getElementById("q").value;
if (searchQ!="Ricerca in SKY" && searchQ!="")
{document.location.href="http://www.sky.it/itstatic/googlesearch/GSA.html?q="+searchQ;}
}

var elMenusSel;
function switchmenus(base,el,nrt,imc,imoff,bdiv, od,cs){
	/*
	base=prefisso immagini (es. myim + n°)
	el = nr elemento da switchare
	nrt = numero totale degli elementi presenti (gli elementi devono partire da 1)
	imc = immagine da cambiare (on oppure off se switch)
	imoff = immagine off
	base=prefisso div (es. mydiv + n°)
	od = Y/N attiva l'attivazione della funzione di apertura/chiusura div
	cs = C= Cambio Immagine S= Solo switch (tipo mouseover) O=Indica onmouseover
	*/
	
	// Se Cambio Immagine "spengo" tutto e chiudo div
	if (cs == "C") {
		for (i=1;i<=nrt;i++){
			myR=""+i
			if (i<10){
				myR="0"+myR
			}
			document.getElementById(base+i).src=imoff.replace("##",myR);
			if (od=="Y"){
				document.getElementById(bdiv+i).style.display="none";
			}
		}
		elMenusSel=el;
	}
	
	// "Accendo" elemento richiesto
	if (cs == "O") {
		if (elMenusSel!=el){
				document.getElementById(base+el).src=imc;
		}
	}else{
		document.getElementById(base+el).src=imc;
	}
	
	if (od=="Y"){
		document.getElementById(bdiv+el).style.display="block";
	}
	
}
