var AjaxDettaglio;
var timerDettaglio;
var larghezzaDettaglio = 0;
var altezzaDettaglio = 0;
var larghezzaMassimaDettaglio = 500;
var altezzaMassimaDettaglio = 350;
var sinistraDettaglio = 0;
var altoDettaglio = 0;
var _codiceTipo;
var _codiceSoggetto;
var _codiceData;
var _codiceContrada;

function DettaglioCavallo(codiceCavallo, codiceData, codiceContrada)
{
	AjaxDettaglio = OggettoAjax();
	
	if(AjaxDettaglio == null)
	{
		var finLargh = 350;
		var finAltez = 400;
		var posX = screen.availWidth / 2 - finLargh / 2;
		var posY = screen.availHeight / 2 - finAltez / 2;
		
		var MettiData = "";
		if(codiceData != undefined)
		{
			MettiData = "&DATA=" + codiceData
			if(codiceContrada != undefined)
				MettiData += "&CONTR=" + codiceContrada
		}
		
		window.open("/Dettaglio/Cavallo.aspx?COD=" + codiceCavallo + MettiData, "Dettaglio", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=1,status=0,resizable=1,title=0");
	}
	else
	{
		DettaglioAjax('C', codiceCavallo, codiceData, codiceContrada);
	}
}


function DettaglioFantino(codiceFantino, codiceData, codiceContrada)
{
	AjaxDettaglio = OggettoAjax();
	
	if(AjaxDettaglio == null)
	{
		var finLargh = 300;
		var finAltez = 360;
		var posX = screen.availWidth / 2 - finLargh / 2;
		var posY = screen.availHeight / 2 - finAltez / 2;
		
		var MettiData = "";
		if(codiceData != undefined)
		{
			MettiData = "&DATA=" + codiceData
			if(codiceContrada != undefined)
				MettiData += "&CONTR=" + codiceContrada
		}	
		window.open("/Dettaglio/Fantino.aspx?COD=" + codiceFantino + MettiData, "Dettaglio", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=1,status=0,resizable=1,title=0");
	}
	else
	{
		DettaglioAjax('F', codiceFantino, codiceData, codiceContrada);
	}
}

function OggettoAjax()
{
	var oggetto = null;

	// informazioni sul nome del browser
	var browserUtente = navigator.userAgent.toUpperCase();

	// browser standard con supporto nativo
	// non importa il tipo di browser
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
		oggetto = new XMLHttpRequest();

	// browser Internet Explorer
	// è necessario filtrare la versione 4
	else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0)
	{
	// la versione 6 di IE ha un nome differente
	// per il tipo di oggetto ActiveX
	if(browserUtente.indexOf("MSIE 5") < 0)
		oggetto = new ActiveXObject("Msxml2.XMLHTTP");
	// le versioni 5 e 5.5 invece sfruttano lo stesso nome
	else
		oggetto = new ActiveXObject("Microsoft.XMLHTTP");
	}

 	return oggetto;
}
var Ajax;
function DettaglioContrada(codiceContrada, codiceData)
{
	AjaxDettaglio = OggettoAjax();

	if(AjaxDettaglio == null)
	{
		var finLargh = 500;
		var finAltez = 330;
		var posX = screen.availWidth / 2 - finLargh / 2;
		var posY = screen.availHeight / 2 - finAltez / 2;
		
		window.open("/Dettaglio/Contrada.aspx?COD=" + codiceContrada + "&DATA=" + codiceData, "Dettaglio", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=1,status=0,resizable=1,title=0");
	}
	else
	{
		DettaglioAjax("X", -1, codiceData, codiceContrada);	
	}
}

function stateChanged() 
{ 
	if (Ajax.readyState==4 || Ajax.readyState=="complete")
	{ 
		if(Ajax.status==200)
			document.getElementById("divDettaglio").innerHTML = Ajax.responseText; 
	} 
}
function ChiudiDettaglio()
{
	var oggetto = document.getElementById("divDettaglio");
	if(oggetto)
		oggetto.style.visibility = 'hidden';
}

function vediVittorie(codice)
{
	var finLargh = 600;
	var finAltez = 250;
	var posX = screen.availWidth / 2 - finLargh / 2;
	var posY = screen.availHeight / 2 - finAltez / 2;
	
	window.open("/Dettaglio/Vittorie.aspx?TIPO=V&COD=" + codice, "DettaglioVittorie", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=1,status=0,resizable=1,title=0");
}

function vediPaliiCorsi(codice)
{
	var finLargh = 600;
	var finAltez = 250;
	var posX = screen.availWidth / 2 - finLargh / 2;
	var posY = screen.availHeight / 2 - finAltez / 2;
	
	window.open("/Dettaglio/Vittorie.aspx?TIPO=C&COD=" + codice, "DettaglioVittorie", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=1,status=0,resizable=1,title=0");
}

function vediGraficoPaliiCorsi(codice)
{
	window.open("/Report/pgPartecipazioni.aspx?COD=" + codice, "GraficoPalii", "top=20,left=50,screenX=50,screenY=20,width=600,height=300,scrollbars=1,status=0,resizable=1,title=0");
}

function VediStemma(codice, immagine)
{
	var finLargh = 160;
	var finAltez = 200;
	var posX = screen.availWidth / 2 - finLargh / 2;
	var posY = screen.availHeight / 2 - finAltez / 2;
	
	window.open("/Dettaglio/Stemma.aspx?TIPO=" + codice + "&IMM=" + immagine, "DettaglioStemma", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=0,status=0,resizable=1,title=0");
}

function VediCronaca(codice)
{
	var finLargh = 500;
	var finAltez = 300;
	var posX = screen.availWidth / 2 - finLargh / 2;
	var posY = screen.availHeight / 2 - finAltez / 2;
	
	window.open("/Dettaglio/Cronaca.aspx?Codice=" + codice, "Cronaca", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=1,status=0,resizable=1,title=0");
}

function VediCopertina(codice)
{
	var finLargh = 500;
	var finAltez = 500;
	var posX = screen.availWidth / 2 - finLargh / 2;
	var posY = screen.availHeight / 2 - finAltez / 2;
	
	//window.open("/Dettaglio/Copertina.aspx?Codice=" + codice, "Copertina", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=1,status=0,resizable=1,title=0");
	window.open("/Palio/" + codice + "/NumeroUnico.ashx", "Copertina", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=1,status=0,resizable=1,title=0");
}

function VediMossa(codice)
{
	var finLargh = 500;
	var finAltez = 300;
	var posX = screen.availWidth / 2 - finLargh / 2;
	var posY = screen.availHeight / 2 - finAltez / 2;
	
	// window.open("/Dettaglio/Mossa.aspx?Codice=" + codice, "Mossa", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=1,status=0,resizable=1,title=0");
	window.open("/Palio/" + codice + "/FotoMossa.ashx", "Mossa", "top=" + posY + ",left=" + posX + ",screenX=" + posX + ",screenY=" + posY + ",width=" + finLargh + ",height=" + finAltez + ",scrollbars=1,status=0,resizable=1,title=0");
}


function DettaglioAjax(tipo, codice, codiceData, codiceContrada)
{
	_codiceTipo = tipo;
	_codiceSoggetto = codice;
	_codiceData = ((isNaN(codiceData)) ? "" : codiceData);
	_codiceContrada = ((codiceContrada == undefined) ? "" : codiceContrada);

	if (AjaxDettaglio != null)
	{
		var verifica = document.getElementById("MenuDettaglio2");
		if(verifica != null)
		{
			// Un pannello è già aperto.
			ChiudiDettaglio();
		}
		
		// if(verifica == null) 
		//{
			var oggetto = document.createElement("DIV");
			oggetto.id = "MenuDettaglio2";
			oggetto.style.width = larghezzaMassimaDettaglio + "px";	//"10px";
			oggetto.style.height = altezzaMassimaDettaglio + "px";	//"10px";
			oggetto.style.position = "absolute";
			oggetto.style.top = (PosizioneTop() - altezzaMassimaDettaglio / 2).toString() + "px";
			oggetto.style.left = (PosizioneLeft() - larghezzaMassimaDettaglio /2).toString() + "px";
			oggetto.style.border = "double 5px #a0522d";
			oggetto.style.overflowY = "hidden";
			oggetto.style.backgroundColor = "wheat";
			oggetto.style.display = "block";
			oggetto.style.zIndex = "5";
			oggetto.innerHTML = "<font color=black><br><img src=\"/Immagini/loading.gif\" border=0 title=\"Caricamento\"> Caricamento in corso...</font>";
//			oggetto.style.backgroundImage = "url(/Immagini/sfondoDettaglio.gif)";
//			oggetto.style.backgroundPosition = "right bottom";
//			oggetto.style.backgroundRepeat = "no-repeat";
			
			document.forms[0].appendChild(oggetto);	
		//}
			
		LanciaAjax(-1);
//		larghezzaDettaglio = 10;
//		altezzaDettaglio = 10;
//		sinistraDettaglio = PosizioneLeft();	// document.body.clientWidth / 2;
//		altoDettaglio = PosizioneTop();
//		ApriDettaglio(true);
//		timerDettaglio = setInterval("ApriDettaglio(true)", 30);
	} 
	else
	{
		// Ajax non supportato
	}
}

var PosizioneYMouse;
function PosizioneTop()
{
	var frameWidth  = -1;
	var frameHeight = -1;
	if (self.innerWidth)
	{
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}
	
	//return ((document.body.clientHeight + document.body.scrollTop) / 2 - 100);
	//return (document.body.scrollTop + 100);
return frameHeight / 2 + document.body.scrollTop + document.documentElement.scrollTop;
}
function PosizioneLeft()
{
	return (document.body.clientWidth / 2);
}

function LanciaAjax(tab)
{
	if(tab == -1)
	{
		//var oggetto = document.getElementById("MenuDettaglio2");
		// altoDettaglio = PosizioneTop();
		//oggetto.style.top = altoDettaglio.toString() + "px";	//(frameHeight / 2 - 100).toString() + "px";
		tab = 0;
	}
	// oggetto.style.left = PosizioneLeft().toString() + "px";
			
	var url="/Dettaglio/Dettaglio_Ajax.aspx";
	var postData = "tipo=" + _codiceTipo + "&cod=" + _codiceSoggetto
		+ "&codPalio=" + _codiceData
		+ "&contr=" + _codiceContrada
		+ "&tab=" + tab
		+ "&sid=" + Math.random();

	AjaxDettaglio.onreadystatechange = RispostaDettaglio;
	AjaxDettaglio.open("GET", url + "?" + postData, true);
	//AjaxDettaglio.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	//AjaxDettaglio.setRequestHeader("Content-length", postData.length);
	
	var iVersione = -1;
	var iBrowser = navigator.userAgent.toUpperCase().indexOf("MSIE");
	if(iBrowser != -1)
	{
	    iVersione = parseInt(navigator.userAgent.substr(iBrowser + 5, 1));
	}

	if(!(iBrowser != -1 && iVersione < 7))
	{
	    AjaxDettaglio.setRequestHeader("Connection", "close");
	}
	
	AjaxDettaglio.send(postData);
}

function ApriDettaglio(apri)
{
	var oggetto = document.getElementById("MenuDettaglio2");
	var fineLargh;
	var fineAlt;
		
	if(oggetto != null)
	{
		if(apri)
		{
			fineLargh = false;
			fineAlt = false;
			if(larghezzaDettaglio < larghezzaMassimaDettaglio / 2)
			{
				larghezzaDettaglio += 10;
				sinistraDettaglio -= 10;
				if(sinistraDettaglio < 0)
					sinistraDettaglio = 0;
					
				oggetto.style.width = (larghezzaDettaglio * 2).toString() + "px";
				oggetto.style.left = sinistraDettaglio.toString() + "px";
			}
			else
			{
				fineLargh = true;
			}
			
			if(altezzaDettaglio < altezzaMassimaDettaglio / 2)
			{
				altezzaDettaglio += 10;
				altoDettaglio -= 10;
				if(altoDettaglio < 0)
					altoDettaglio = 0;
					
				oggetto.style.height = (altezzaDettaglio * 2).toString() + "px";
				oggetto.style.top = altoDettaglio.toString() + "px";
			}
			else
			{
				fineAlt = true;
			}
			
			if(fineAlt && fineLargh)
			{
				clearInterval(timerDettaglio);
			}
		}
		else
		{
			oggetto.style.overflowX
				= oggetto.style.overflowY
				= "scroll";
			fineLargh = false;
			fineAlt = false;
			
			if(larghezzaDettaglio > 0)
			{
				larghezzaDettaglio -= (10);
				if(larghezzaDettaglio < 0)
					larghezzaDettaglio = 0;
					
				sinistraDettaglio += 10;
				oggetto.style.width = (larghezzaDettaglio * 2).toString() + "px";
				oggetto.style.left = sinistraDettaglio.toString() + "px";
			}
			else
				fineLargh = true;
				
			if(altezzaDettaglio > 0)
			{
				altezzaDettaglio -= (10);
				if(altezzaDettaglio < 0)
					altezzaDettaglio = 0;
					
				altoDettaglio += 10;
				oggetto.style.height = (altezzaDettaglio * 2).toString() + "px";
				oggetto.style.top = altoDettaglio.toString() + "px";
			}
			else
				fineAlt = true;
			
			if(fineLargh && fineAlt)
			{
				clearInterval(timerDettaglio);
				
				if(oggetto != null)
				{
					oggetto.style.display = "none";
					document.forms[0].removeChild(oggetto);	
				}
			}
		}
	}
	else
	{
		// oggetto == null
		if(timerDettaglio != null)
			clearInterval(timerDettaglio);
	}
}


function ChiudiDettaglioTempo()
{
	timerDettaglio = setInterval("ApriDettaglio(false)", 30);
}

function RispostaDettaglio()
{
	var oggetto = document.getElementById("MenuDettaglio2");
	if(oggetto != null)
	{
		if (AjaxDettaglio.readyState==4 || AjaxDettaglio.readyState=="complete")
		{ 
			if (AjaxDettaglio.status == 200)
			{
				oggetto.innerHTML = AjaxDettaglio.responseText; 
			}
			else
			{
				oggetto.innerHTML = "Si &egrave; verificato un errore.<br><br><a href='javascript:ChiudiDettaglio();' class=Normale><img src='/Immagini/chiudi.gif' border=0 align=absmiddle> <b>Chiudi pannello</b></a>"; 
			}
		}
		else
		{
			oggetto.innerHTML = "<p class=TitoloArea><br><img src=\"/Immagini/loading.gif\" border=0 title=\"Caricamento\"><br>Caricamento in corso...<br>&nbsp;</p>"; 
		}
	}
}

function CambiaVoceDettaglio(codiceTab)
{
	window.focus();
//	var oggetto = document.getElementById("MenuDettaglio");
//	var elementi = oggetto.getElementsByTagName("li");

// 	for(i=0; i<elementi.length; i++)
// 	{
// 		elementi[i].className = "";
// 	}

//	if(codice == 3 && elementi.length == 3)
//		codice--;
// 	elementi[codice].className="ON";

	AjaxDettaglio = null;
	AjaxDettaglio = OggettoAjax();
 	LanciaAjax(codiceTab);
}

function ChiudiDettaglio()
{
	var oggetto = document.getElementById("MenuDettaglio2");
	
	if(oggetto != null)
	{
		oggetto.style.display = "none";
		document.forms[0].removeChild(oggetto);
		
		// ChiudiDettaglioTempo();
	}
}

function ZummaFoto(codice, immagine)
{
	window.open("/Dettaglio/Stemma.aspx?TIPO=" + codice + "&IMM=" + immagine, "Zoom", "top=20,left=50,screenX=20,screenY=50,width=160,height=200,scrollbars=1,status=0,resizable=1,title=0");
}


// imposta il cookie sNome = sValore
// per la durata di iGiorni
function setCookie(sNome, sValore) {
  var dtOggi = new Date();
  var dtExpires = new Date();
  var iGiorni = 3650;
  
  dtExpires.setTime(dtOggi.getTime() + 24 * iGiorni * 3600000);
  document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString();
}

// restituisce il valore del cookie sNome
function getCookie(sNome) {
  // genera un array di coppie "Nome = Valore"
  // NOTA: i cookies sono separati da ';'
  var asCookies = document.cookie.split("; ");
  // ciclo su tutti i cookies
  for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
  {
    // leggo singolo cookie "Nome = Valore"
    var asCookie = asCookies[iCnt].split("=");
    if (sNome == asCookie[0]) { 
      return (unescape(asCookie[1]));
    }
  }

  // SE non esiste il cookie richiesto
  return("");
}

// rimuove un cookie
function delCookie(sNome) {
  setCookie(sNome, "");
}

function trimma(stringa)			//Toglie gli spazi presenti prima e dopo in "stringa"
{
	if (stringa != "")
	{
		while( ( i = stringa.indexOf(" ") ) == 0 )
			stringa = stringa.substring(1,stringa.length);
	}
	if (stringa != "")
	{
		while( ( i = stringa.lastIndexOf(" ") ) == (stringa.length-1))
			stringa = stringa.substring(0,stringa.length-1);
	}
	return stringa;
}

function setZoom(img, dir, width, height, margin, zIndex, delay) {
  setTimeout(function() {
    if (img.dir==dir) {
      img.style.width=width;
      img.style.height=height;
      img.style.margin=margin;
      img.style.zIndex=zIndex;
      img.parentNode.parentNode.style.zIndex=zIndex;
    }
  }, delay);
}

function allarga(img, width, height) {
  img.dir='rtl';
  now=parseInt(img.style.zIndex);
  for (i=now+1; i<=10; i++) {
    w=(width*(10+i))/20+'px';
    h=(height*(10+i))/20+'px';
    m=(-i)+'px 0 0 '+(-width*i/40)+'px';
    setZoom(img, 'rtl', w, h, m, i, 20*(i-now));
  }
}

function restringi(img, width, height) {
  img.dir='ltr';
  now=parseInt(img.style.zIndex);
  for (i=now-1; i>=0; i--) {
    w=(width*(10+i))/20+'px';
    h=(height*(10+i))/20+'px';
    m=(-i)+'px 0 0 '+(-width*i/40)+'px';
    setZoom(img, 'ltr', w, h, m, i, 20*(now-i));
  }
}

