var urlBB = 'indexbb.html?pmk=hpswu';
var urlNB = 'index.html?pmk=hpswu';
var defaultUrl = urlNB;
var connectionSpeed = "Connessione:NarrowBand";

/* animazione NEWS */
var n_tot = 3;
var delay = 3000;
var n_cor = 1;
var n_pre = n_tot;

function startnews(){

 	findLayer('art_'+n_pre).style.visibility = "hidden";
 	findLayer('art_'+n_cor).style.visibility = "visible";

	n_pre = n_cor;

	if (n_cor == n_tot) { n_cor = 1; } else { n_cor++; }
	setTimeout("startnews()", delay);
}
/*fine funzioni per animazione NEWS*/

function goSearch(dom, qs){
	if ( qs=='Cerca con Virgilio:' ){
		qs='';
	}
	document.location.href=('http://search.virgilio.it/search/cgi/search.cgi?dom=' + dom + '&f=hp&switch=0&offset=0&hits=10&qs=' + escape(qs));
	return false;
}

function checkScreen(){
	var w = screen.width;
	var h = screen.height;
	var ret = (w>=1024 && h>=768) ? true : false;
	return ret;
}


function genericPop(myUrl,myWinName,myWidth,myHeight,myPars) {
 var winl = (screen.width - myWidth) / 2;
 var wint = (screen.height - myHeight) / 2;
 parameters = myPars + '';
 if (parameters == '') parameters = ',toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no';
 gwin=window.open(myUrl,myWinName,'width='+myWidth+',height='+myHeight+',left='+winl+',top='+wint+parameters);
 gwin.focus();
}


function openRAWindow(anchorObject){
	openRAWindowFromFlash(anchorObject);
	return false;
}

function openRAWindowFromFlash(o){
	var features = 'scrollbars=no,resizable=no,status=yes';
	if (screen.width==800 && screen.height==600){
		features = 'scrollbars=yes,resizable=no,status=yes';
	}
	if(o.getAttribute){
		window.open(o.getAttribute('href'),'',features);
	}else{
		window.open(o,'',features);
	}
}

function checkFlashVersion() {
  var reqVer=7;
  var plugin = (navigator.mimeTypes &&
  navigator.mimeTypes["application/x-shockwave-flash"]) ?
  navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
  var isVerOk=false;
  if (plugin) {
      var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	  var flaVer;
      for (var i = 0; i < words.length; i++){
          if (isNaN(parseInt(words[i]))){
              continue;
		  }
          flaVer = words[i];
		  break;
      }
      isVerOk = (flaVer >= reqVer);
  }
  else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)){
      try {
		var instance=new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+reqVer);
		isVerOk = true;
	  }
      catch (e){isVerOk=false}
  }
  return isVerOk;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function findLayer(layerID){
	if(document.getElementById){
		return document.getElementById(layerID);
	}else if(document.all){
		return document.all[layerID];
	}else if(document.layers){
		return document.layers[layerID];
	}
}

function loadBoxMail(){
	findLayer('mail').style.display = 'block';
}

function goToNB(){
	var scade = new Date();
	scade.setTime(scade.getTime() + 30*24*60*60*1000);
	SetCookie("UserChoice", "NB", scade, "/", null, null);

	document.location.href=urlNB;
}

function goToBB(){
	var scade = new Date();
	scade.setTime(scade.getTime() + 30*24*60*60*1000);
	SetCookie("UserChoice", "BB", scade, "/", null, null);

	document.location.href=urlBB;
}

function checkBB(){
	/* da chiamare solamente nella BB */
	var normalized  = GetCookie('Normalized');
	if(  !(normalized && normalized=='once')  ){
		if( !(checkFlashVersion() && checkScreen()) ){
			var mai = new Date();
			mai.setTime(mai.getTime() + 2000*24*60*60*1000);
			SetCookie ('Normalized', 'once', mai, "/", null, null);
			setUserChoiceCookie( 'NarrowBand' );
			document.location.href = urlNB;
		}
	}
}

function goToTab(tab){
	var path = document.location.pathname;
	if (path.indexOf('index')){
		path=path.substring(1, path.lastIndexOf('index')-1);
	}
	if(path=='home'){
		path='aliceoggi';
	}
	var userChoice = GetCookie('UserChoice');
	if (userChoice=='NB'){
		document.location.href='/'+tab;
	}else{
		var url = tab+'/indexbb.html';
		if (tab!=''){
			url = '/' + url;
		}
		document.location.href=url + '?pmkf=' + path;
	}
}

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) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function drawCSImageTag( fileLocation, fileSize, imgTagProperties, showImage, elementId) {
  var x = document.getElementById(elementId);
	var start = (new Date()).getTime();
	var loc = fileLocation + '?t=' + escape(start);
  var visible = "hidden";
  var showImage = 'checked' ? visible = "visible" : visible = "hidden";
	var imtest = new Image();
	imtest.src = loc;
	try{
		if(  !(navigator.userAgent.indexOf('Mac')>-1 && navigator.appVersion.indexOf('MSIE')>-1)   ){
			imtest.onload = function compute(){
				computeConnectionSpeed( start, fileSize );
			}
		}else{
			eval('imtest.onload = new function compute(){computeConnectionSpeed( start, fileSize );}');
		}
	}catch(e){
		// per baco IE5: esegue la funzione sull'onload ma lancia comunque un eccezione
	}

	return;
}

function connectionType(speed) {

	SLOW_MODEM = 15;
  //FAST_MODEM = 57;
	FAST_MODEM = 70;
	ISDN_MODEM = 120;
	ADSL_MODEM = 600;

	if (speed) {
		if (speed < FAST_MODEM) {
			return "NarrowBand";
		} else {
			return "BroadBand";
		}
	} else {
		return "NarrowBand";
	}
}

function computeConnectionSpeed( start, fileSize ) {
	var end = (new Date()).getTime();
	speed = (Math.floor((((fileSize * 8) / ((end - start) / 1000)) / 1024) * 10) / 10);
	connectionSpeed = 'Connessione:' + connectionType( speed );
	var mai = new Date();
	mai.setTime(mai.getTime() + 2000*24*60*60*1000);
    var cookie = connectionSpeed + '&start=' + start + '&end=' + end + '&ms=' + (end-start) + '&kbps=' + parseInt(speed) + '&bytes=' + fileSize;
    var img = new Image();
    img.src="/home/images/connspeed-out.jpg?" + cookie + "&rand=" + Math.floor(1000000*Math.random());

	SetCookie ("ConnParam", cookie, mai, "/", null, null);

	setUserChoiceCookie( connectionType(speed) );

}

function startDraw() {
	var x = GetCookie("ConnParam");
	var u = GetCookie("UserChoice");
	if (!x) {
		draw();
	} else {
		if (x && !u){
			setUserChoiceCookie( x );
		}

		connectionSpeed = x;
		var img = new Image();
		img.src = "/home/images/connspeed-in.gif?" + x + "&rand=" + Math.floor(1000000*Math.random());
	}
}

function draw() {
	drawCSImageTag("/home/images/connspeed.bmp", // Image filename
                  5194, // Image size
                  "height=54 width=123", "checked", "connspeed");
}

function setUserChoiceCookie(connSpeed){
	var scade = new Date();
	scade.setTime(scade.getTime() + 30*24*60*60*1000);
	if(connSpeed.indexOf('BroadBand') !=-1 && checkFlashVersion() && checkScreen() ){
		SetCookie("UserChoice", "BB", scade, "/", null, null);
	}else{
		SetCookie("UserChoice", "NB", scade, "/", null, null);
	}
}

function writeFlashObject(movie, qs, width, height){
	document.write(
	   '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n'+
	   ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"\n'+
	   ' WIDTH="' + width + '" HEIGHT="' + height + '">\n'+
	   ' <PARAM NAME=movie VALUE="' + movie + '">\n'+
	   ' <PARAM NAME=FlashVars VALUE="'+qs+'">\n'+
	   ' <PARAM NAME=quality VALUE=high>\n'+
	   ' <PARAM NAME=menu VALUE=false>\n'+
	   ' <PARAM name="wmode" VALUE="transparent">\n'+
	   ' <EMBED src="' + movie +'"\n'+
	   '  FlashVars="'+qs+'"\n'+
	   '  wmode="transparent" ' +
	   '  quality=high menu="false" WIDTH="' + width + '" HEIGHT="' + height +
	   '  TYPE="application/x-shockwave-flash"\n'+
	   '  PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>\n'+
	   '</OBJECT>');
}