// <![CDATA[
var t_top = "top";
var t_right = "right";
var t_bottom = "bottom";
var t_left = "left";
var t_direzione;
var t_velocità;
var t_divScorrevole;
var t_timerDivScorrevole;
var t_dimensione;
var t_divInterno;
var t_timeOutDivScorrevole;
var t_sbloccaDiv = true;
var t_sbloccaTempo = true;
var t_pausaMillisecondi = 0;
var t_myRequest = null;
var t_arraySponsor = new Array();

/**
 * Richiede l'array degli sponsor al server
 * 
 */
function getArraySponsor() {
	t_myRequest = createXmlHttpRequest(handlerGetArraySponsor);
	t_myRequest.open("POST", "./script/sponsorAjax.php", true);
	t_myRequest.setRequestHeader("content-type",
			"application/x-www-form-urlencoded");
	t_myRequest.send("listaSponsor=true");
}

function handlerGetArraySponsor() {
	if (t_myRequest.readyState == 4 && t_myRequest.status == 200) {
		var rispostaScript = t_myRequest.responseText;
		if (rispostaScript == "Errore") {
		} else {
			rispostaScript = eval('(' + rispostaScript + ')');
			t_arraySponsor = rispostaScript;
		}
	}
}

function impostaDivScorrevoleTemporizzato(nomeDiv, dim, dir, vel, pausaSecondi) {
	t_dimensione = dim;
	t_direzione = dir
	t_velocità = vel;
	t_pausaMillisecondi = pausaSecondi * 1000;

	t_divScorrevole = document.getElementById(nomeDiv);
	t_timerDivScorrevole = setInterval("t_sposta()", t_velocità);

	t_divInterno = document.createElement("div");
	var numNodi = t_divScorrevole.childNodes.length;
	var i = 0;
	while (i < numNodi) {
		try {
			var unNodo = t_divScorrevole.childNodes[0];
			if (unNodo.tagName != null) {
				t_divInterno.appendChild(unNodo);
			}
			t_divScorrevole.removeChild(unNodo);
		} catch (e) {
		}
		i++;
	}
	t_divScorrevole.appendChild(t_divInterno);

	switch (t_direzione) {
	case t_top:
		if ((navigator.appName == "Opera" || navigator.appName == "Netscape" || navigator.userAgent
				.indexOf("MSIE 8.0") > 0)
				&& navigator.userAgent.indexOf("Navigator") == -1) {
			t_divInterno.style.padding = "0px 0px " + t_dimensione + "px 0px";
		}
		t_divInterno.style.margin = t_dimensione + "px 0px " + t_dimensione
				+ "px 0px";
		break;
	case t_bottom:
		if (navigator.appName == "Opera"
				|| navigator.appVersion.indexOf("MSIE 8.0") > 0) {
			t_divInterno.style.padding = "0px 0px " + t_dimensione + "px 0px";
		}
		t_divInterno.style.margin = t_dimensione + "px 0px " + t_dimensione
				+ "px 0px";
		break;
	case t_left:
	case t_right:
		if (navigator.appName == "Microsoft Internet Explorer") {
			t_divInterno.style.padding = "0px " + t_dimensione + "px 0px "
					+ t_dimensione + "px";
		} else {
			t_divInterno.style.padding = "0px " + 2 * t_dimensione + "px 0px "
					+ t_dimensione + "px";
		}
		break;
	}
	t_aggiungiEventi();

	switch (t_direzione) {
	case t_top:
		t_divScorrevole.scrollTop = 0;
		break;
	case t_right:
		t_divScorrevole.scrollRight = t_divScorrevole.scrollWidth;
		break;
	case t_bottom:
		t_divScorrevole.scrollTop = t_divScorrevole.scrollHeight;
		break;
	case t_left:
		t_divScorrevole.scrollLeft = 0;
		break;
	}
}

function t_aggiungiEventi() {
	var numNodi = t_divInterno.childNodes.length;
	var i = 0;
	while (i < numNodi) {
		try {
			var unNodo = t_divInterno.childNodes[i];
			unNodo.onmouseover = function() {
				clearInterval(t_timerDivScorrevole);
				t_sbloccaDiv = false;
			};
			unNodo.onmouseout = function() {
				t_sbloccaDiv = true;
				if (t_sbloccaTempo) {
					t_timerDivScorrevole = setInterval('t_sposta()', t_velocità);
				}
			};
		} catch (e) {
		}
		i++;
	}
}

function t_sposta() {
	var vecchioScrollTop = t_divScorrevole.scrollTop;
	var vecchioScrollLeft = t_divScorrevole.scrollLeft;
	switch (t_direzione) {
	case t_top:
		t_divScorrevole.scrollTop = t_divScorrevole.scrollTop + 1;
		if (vecchioScrollTop == t_divScorrevole.scrollTop) {
			t_divScorrevole.scrollTop = 0;
		}
		break;
	case t_right:
		t_divScorrevole.scrollLeft = t_divScorrevole.scrollLeft - 3;
		if (vecchioScrollLeft == t_divScorrevole.scrollLeft) {
			t_divScorrevole.scrollLeft = t_divScorrevole.scrollWidth;
		}
		if (t_divScorrevole.scrollLeft == t_dimensione) {
			t_sbloccaTempo = false;
			clearInterval(t_timerDivScorrevole);
			t_timeOutDivScorrevole = setTimeout("impostaTimeOut()",
					t_pausaMillisecondi);
		}
		break;
	case t_bottom:
		t_divScorrevole.scrollTop = t_divScorrevole.scrollTop -2;
		if (vecchioScrollTop == t_divScorrevole.scrollTop) {
			t_divScorrevole.scrollTop = t_divScorrevole.scrollHeight;
			clearInterval(t_timerDivScorrevole);
			t_sbloccaDiv = false;
			cambiaSponsor();
		}
		if (t_divScorrevole.scrollTop == t_dimensione) {
			t_sbloccaTempo = false;
			clearInterval(t_timerDivScorrevole);
			t_timeOutDivScorrevole = setTimeout("impostaTimeOut()",
					t_pausaMillisecondi);
		}
		break;
	case t_left:
		t_divScorrevole.scrollLeft = t_divScorrevole.scrollLeft + 9;
		if (vecchioScrollLeft == t_divScorrevole.scrollLeft) {
			t_divScorrevole.scrollLeft = 0;
			clearInterval(t_timerDivScorrevole);
			t_sbloccaDiv = false;
		}
		if (t_divScorrevole.scrollLeft == t_dimensione) {
			t_sbloccaTempo = false;
			clearInterval(t_timerDivScorrevole);
			t_timeOutDivScorrevole = setTimeout("impostaTimeOut()",
					t_pausaMillisecondi);
		}
		break;
	}
}

function impostaTimeOut() {
	t_sbloccaTempo = true;
	if (t_sbloccaDiv) {
		t_timeOutDivScorrevole = t_timerDivScorrevole = setInterval(
				't_sposta()', t_velocità);
	}
}

function cambiaSponsor() {
	var imgVecchia = t_divInterno.getElementsByTagName("img")[0];
	var altezza = imgVecchia.height;
	var larghezza = imgVecchia.width;
	while (t_divInterno.firstChild) {
		t_divInterno.removeChild(t_divInterno.firstChild);
	}
	var indiceDaVisualizzare = t_arraySponsor.indiceVisualizzato + 1;
	var trovato = false;
	// se il successivo è nullo riparto dal primo
	if (t_arraySponsor.sponsor[indiceDaVisualizzare] == null) {
		indiceDaVisualizzare = 0;
	}
	t_arraySponsor.indiceVisualizzato = indiceDaVisualizzare;
	var link = t_arraySponsor.sponsor[indiceDaVisualizzare].link;
	var percorsoImg = t_arraySponsor.sponsor[indiceDaVisualizzare].img;
	var alt = t_arraySponsor.sponsor[indiceDaVisualizzare].alt;
	var img = document.createElement("img");
	img.width = larghezza;
	img.height = altezza;
	if (link != null) {
		var a = document.createElement("a");
		a.href = link;
		a.alt = alt;
		a.title = alt;
		a.appendChild(img);
		t_divInterno.appendChild(a);
	} else {
		t_divInterno.appendChild(img);
	}
	img.onload = function() {
		t_sbloccaDiv = true;
		if (t_sbloccaTempo) {
			t_timerDivScorrevole = setInterval('t_sposta()', t_velocità);
		}
	};
	img.src = percorsoImg;
	t_aggiungiEventi();
}
// ]]>

