function $(idMO){
	return document.getElementById(idMO);
}

// tab correlati + commentati
function switchTabMO(id,label,cont,css){
	var i = 0;
	while(i < 2){
		if(id==i){
			$(label+id).className = css+'Ac MO-nlnk';
			$(cont+id).style.display = 'block';
		}
		else{
			$(label+i).className = css+'Ina';
			$(cont+i).style.display = 'none';
		}
		i++
	}
}


// oroscopo
function oroscopo(block,hide){
	$(block).style.display = 'block';
	$(hide).style.display = 'none';
}

function sckOrs(segno){
	var location = 'http://oroscopo.donna.tiscali.it/oroscopo_del_giorno/'+segno+'/';		
	SetCookie('oroscopo_hp',segno,'365','/','tiscali.it');
	window.location.href = location;	
}


// nascondo box meteo 
function clickDetect(e){
	if(!e) e=window.event;
	var src=(e.target)?e.target:e.srcElement;
	while(src){
		if(src.id && src.id=='Meteo-search-div-t') return;
		src=src.parentNode;
	}
	$('Meteo-search-div-t').style.display='none';
}
window.onload = target;

// layer meteo
function getMeteo(){
	$('localita').setAttribute( "autocomplete","off" );
	$('MO-pMto').style.display='block';
}

// box meteo personalizzato
document.domain='tiscali.it';
var formName = '';
var idMeteoBoxResults = '';
var pid = '';
var lastQuery = '';
var numElement = 0;
var currentNode = 0;
var lastNode;
var country;

function callback_result(previsioni){
	var localitaT  = (previsioni.nome_localita.length >= 15)?previsioni.nome_localita.substring(0,15)+'...':previsioni.nome_localita;
	var dataTomorrow   = previsioni.data;
	var regione   = previsioni.regione;
	var regVideoT = previsioni.regioneTitle;
	var regVideo  = previsioni.regioneVideo;
	var tmax      = previsioni.max;
	var tmin      = previsioni.min;
	var tmaxTomorrow      = previsioni.maxTomorrow;
	var tminTomorrow      = previsioni.minTomorrow;	
	var id        = previsioni.id;
	var icona     = previsioni.segni;	
	var iconaTomorrow     = previsioni.segniTomorrow;		
	var desc      = previsioni.descrizione;		
	var descTomorrow      = previsioni.descrizioneTomorrow;			
	var mresultTop = '<div class="MeteoLocalita"><a class="f14" title="'+previsioni.nome_localita+'" href="http://meteo.tiscali.it/previsioni/'+regione+'/'+id+'/"><strong>'+localitaT+'</strong></a>&nbsp;&nbsp;<a href="#" onclick="getMeteo();return false">modifica citt&aacute;</a></div>';
	mresultTop+='<div class="MeteoToday"><a href="http://meteo.tiscali.it/previsioni/'+regione+'/'+id+'/"><img src="http://www.tiscali.it/v004/img/meteo/'+icona+'.png" width="40" height="40" class="fL" alt="" title="" />Oggi</a><br><span class="MeteoFreddo">'+tmin+'&deg;</span>&nbsp;&nbsp;<span class="MeteoCaldo">'+tmax+'&deg;</span></div>';
	mresultTop+='<div class="MeteoTomorrow"><a href="http://meteo.tiscali.it/previsioni/'+regione+'/'+id+'/?data='+dataTomorrow+'"><img src="http://www.tiscali.it/v004/img/meteo/'+iconaTomorrow+'.png" width="40" height="40" class="fL" alt="" title="" />Domani</a><br><span class="MeteoFreddo">'+tminTomorrow+'&deg;</span>&nbsp;&nbsp;<span class="MeteoCaldo">'+tmaxTomorrow+'&deg;</span></div>';	
	mresultTop+='<div class="MeteoLink"><a href="http://meteo.tiscali.it/previsioni/mari/">mari e venti</a> | <a href="http://meteo.tiscali.it/previsioni/">il meteo nei prossimi giorni</a></div>';	
	//mresultTop.= '<a href="http://meteo.tiscali.it/">Meteo</a>&nbsp;<a href="http://meteo.tiscali.it/previsioni/'+regione+'/'+id+'/"><img src="http://www.tiscali.it/v004/img/meteo/'+icona+'.png" width="30" height="30" class="vAm" alt="" title="" /></a>&nbsp;&nbsp;&nbsp;<span class="min">'+tmin+'&deg;</span>  <span class="Red">'+tmax+'&deg;</span></a>&nbsp;<a href="#" onclick="getMeteo();return false">modifica città</a>';
	$('meteoTop').innerHTML=mresultTop;	
}

function add_get_meteo_call(localita){
	lastQuery=localita; 
		var url = 'http://meteo.tiscali.it/makejson.php?id='+localita;
	var headLocation = document.getElementsByTagName('head').item(0);
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = url;
	headLocation.appendChild(script);		
	pid = '';
}

function add_get_meteo(localita,delay){	
	if (localita!=lastQuery || naz!=country){
		if (pid != '') {
			clearTimeout(pid);
		}
		pid = window.setTimeout("add_get_meteo_call('"+localita+"')", delay);
	}		
}

function callback_element(jSonObj,localita){
	var meteo_element = $(idMeteoBoxResults);
	var n = 0;
	var html='';
	if ($(formName).naz[0].checked==true){
		var naz = 'italia';
		var url = 'http://meteo.tiscali.it/previsioni/scheda.php'
	}else{
		var naz = 'estero';
		var url = 'http://meteo.tiscali.it/estero/scheda.php'
	}
	for (i in jSonObj.citta){
		n++;
		var nome_localita 	=jSonObj.citta[i].nome_localita;
		var id_localita		=jSonObj.citta[i].id;
		var regione_localita=jSonObj.citta[i].regione;
		var id = i;
		id++;
		if (nome_localita == 'Altri')
			html+='<a href="http://meteo.tiscali.it/ricerca.php?naz='+naz+'&localita='+regione_localita+'&set" onmouseover="cursor(this)" class="lnkMto"><u>'+nome_localita+'</u></a>';
		else
			html+='<a href="'+url+'?id='+id_localita+'&regione='+regione_localita+'&set" onmouseover="cursor(this)" class="lnkMto">'+nome_localita+'</a>';	
	}
	var result = "<div class=\"tips\">"+n+" localit&agrave; di "+jSonObj.tot_risultati+" trovate</div>";
	meteo_element.innerHTML=result+html;
	meteo_element.style.display="block";
	numElement=meteo_element.childNodes.length-1;
}

function add_script_meteo_call(localita,min_length_city_name){
	lastQuery=localita; 
	var naz=($(formName).naz[0].checked==true)?'it':'es';
	var url = 'http://meteo.tiscali.it/city_finder.php?city='+localita+'&minlocalitalng='+min_length_city_name+'&naz='+naz;
	var headLocation = document.getElementsByTagName('head').item(0);
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = url;
	headLocation.appendChild(script);		
	pid = '';
}

function add_script_meteo(localita,min_length_city_name,delay,e,divCont,frmName){	
	idMeteoBoxResults = divCont;
	formName = frmName;
	var naz=($(formName).naz[0].checked==true)?'it':'es';
	if (localita.length >= min_length_city_name){
		if (keyboardControl(e)){			
			if (localita!=lastQuery || naz!=country){
				if (pid != '') {
					clearTimeout(pid);
				}
				pid = window.setTimeout("add_script_meteo_call('"+localita+"',"+min_length_city_name+")", delay);
				naz=country;
				currentNode=0;
			}		
		}
	}else{
		$(idMeteoBoxResults).style.display="none";
		lastQuery='';
	}
}

function defLoc(formNm){
	if($(formNm).localita.value=='Cerca Localita\''){
		$(formNm).localita.value='';
	}
}

function keyboardControl(e){
	e = getObjKey(e);
	if (e.keyCode==13 || !e){	// tasto invio
		if ($(formName).meteo_link.value!='')
			setTimeout(document.location.href=$(formName).meteo_link.value,500);
		else{
			$(formName).action='http://meteo.tiscali.it/ricerca.php';
			setTimeout($(formName).submit(),500);
		}
	}	
	if (e.keyCode>40 || e.keyCode<35){
		$(formName).meteo_link.value='';
		return true
	}else{
		if (e.keyCode==40){ // freccia giu'
			upDown('d');
		}else if(e.keyCode==38){
			upDown('u');
		}
		return false;
	}
	if (e.which==1 || e.button==0) return true;
}

function getObjKey(e){
	if (!e){
		if (window.event)
			return window.event;
		else
			return false;
	}else{
		return e;
	}
}

function upDown(x){
	var meteo_element = $(idMeteoBoxResults);
	if (x=='d'){
		if (++currentNode>numElement) currentNode = 1;
		cursor(meteo_element.childNodes[currentNode]);
	}else if(x=='u'){
		if (--currentNode==0) currentNode = numElement;
		cursor(meteo_element.childNodes[currentNode]);
	}
}

function cursor(element){
	var list = $(idMeteoBoxResults); 
	for (var i=0; i < list.childNodes.length; i++){
		var node = list.childNodes[i];
		if (node==element){
			node.style.backgroundColor='white';
			node.style.color='#0750D9';
			currentNode=i;
		}else if(node.style.backgroundColor == 'white'){
			node.style.backgroundColor='';
			node.style.color='#000';
			lastNode=i;
		}
	}
	$(formName).localita.value=(element.innerHTML.match(/Altri/))?'tutta la lista':element.innerHTML;
	$(formName).meteo_link.value=element;	
}

// src
function src(t,w){
	var q=$('frm_src').key.value;
	setTimeout(document.location.href="http://search.tiscali.it/?engine=hp&tiscalitype="+t+"&collection="+w+"&q="+q,500);
}



//cookie
function getCookieVal(offset){var endstr=document.cookie.indexOf(";",offset);if(endstr==-1)endstr=document.cookie.length;return unescape(document.cookie.substring(offset, endstr));}
function GetCookie(name){var arg=name+"=";var alen=arg.length;var clen=document.cookie.length;var i=0;while(i<clen){var j=i+alen;if(document.cookie.substring(i,j)==arg)return getCookieVal(j);i=document.cookie.indexOf(" ",i)+1;if(i==0)break;}return null;}
function SetCookie(name,value,expires,path,domain,secure){
	var today = new Date();
	var expires_date = new Date();
	if (isFinite(expires)){
		expires_date.setTime(today.getTime() + 3600000*24*expires);
	}else{
		expires_date=expires;
		}
        document.cookie=name+"="+escape(value)+((expires)?"; expires="+expires_date.toGMTString():"")+((path)?"; path="+path:"")+((domain)?";domain="+domain:"")+((secure)?"; secure":"");
}
function DeleteCookie(name,path,domain){if(GetCookie(name)){document.cookie=name+"="+((path)?"; path="+path:"")+((domain)?"; domain="+domain:"")+"; expires=Tue, 01-Jan-2002 00:00:01 GMT";}}
