/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

function checkIt(string){
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*
ho definito l'oggetto xmlHttp in cima al file
*/
if (browser=="Internet Explorer"){
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			xmlHttp = false;
		}
	}
}else{
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
}

/*
 * The image dissolve effects code uses work presented by article entitled
 * "Cross-browser BlendTrans Filter JavaScript" on 
 * http://www.brainerror.net/scripts_js_blendtrans.php
 */
//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function changeOpac_reverse(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity * 100);
    object.MozOpacity = (opacity * 100);
    object.KhtmlOpacity = (opacity * 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

/*
 * FUNCTION BLEND_IMAGE
 * Elenco parametri
 * divid:				id del div che contiene la prima immagine di sfondo
 * imageid:				id del div che contiene le altre immagini di sfondo
 * elenco_indici:		array con gli id di tutti i banner --> servono per fare la query di aggiornamento delle impression
 * elenco_img:			array con i path delle immagini
 * elenco_titoli:		array con i titoli
 * elenco_testi:		array con i testi
 * elenco_link:			array con i link al quale puntano i banner
 * elenco_durata:		array con la durata dei banner
 * elenco_bottoni:		array con i path delle immagini che si usano per i bottoni
 * tipo_rotazione:		C - casuale senza bottone
 						S - in sequenza con bottone - i banner ruotano solo cliccando sul bottone-immagine
						B - in sequenza senza bottone
 * path:				contiene il percorso sul server dove vengono salvate le immagini dei bottoni; è definita in un punto solo in un file esterno
 * prima:				definisce se è la prima volta che devo far vedere i banner --> la prima volta non ci deve essere rotazione
 						0 (zero) - NON è il primo banner
 						1 (uno)  - è il primo banner
 * visualizzo_tagA:		se mancano sia titolo che testo da tutti i banner inserisco il tag A ed in questo modo abilito il link sul banner
 						true  - inserisco il tag
 						false - NON inserisco il tag
 * larghezza_banner:	larghezza dedicata al banner; serve per i calcoli sul posizionamento del bottone e dei testi
 * altezza_banner:		altezza dedicata al banner; serve per i calcoli sul posizionamento del bottone e dei testi
 */
function blend_image(divid, imageid, elenco_indici, elenco_id, elenco_img, elenco_titoli, elenco_testi, elenco_link, elenco_durata, elenco_bottoni, tipo_rotazione, path, prima, visualizzo_tagA, larghezza_banner, altezza_banner){

	//nella rotazione CASUALE l'input J_OLD viene utilizzato per memorizzare l'indice dell'elemento dell'array degli indici
	//nella rotazione SEQUENZIALE l'input J_OLD viene utilizzato per memorizzare l'indice dell'elemento dell'array sugli id

	var elenco_titoli = elenco_titoli.replace(/\\'/g, "'");
	var elenco_testi = elenco_testi.replace(/\\'/g, "'");
	
	/*
	 * creo gli array
	 */
	var singoli_indici = elenco_indici.split("[]");
	var singoli_id = elenco_id.split("[]");
	var singole_img = elenco_img.split("[]");
	var singoli_titoli = elenco_titoli.split("[]");
	var singoli_testi = elenco_testi.split("[]");
	var singoli_link = elenco_link.split("[]");
	var singola_durata = elenco_durata.split("[]");
	var singoli_bottoni = elenco_bottoni.split("[]");
	
	/*
	 * conto il numero di immagini
	 */
	var numero_img = (singole_img.length);
	var numero_indici = (singoli_indici.length);

	/*
	 * per controllare che non venga presa la stessa immagine 2 volte setto il valore di j in un campo, 
	 * lo recupero e lo confronto con quello appena calcolato
	 */
	var j_old = parseInt(document.getElementById('j_old').value);

	/*
	 * il tipo di rotazione delle immagini può essere:
	 * - casuale		le immagini appaiono casualmente	valore: C
	 *					in realtà l'ordine degli ID è creato ad hoc in base ai pesi dei singoli banner
	 * - sequenziale	le immagini seguono l'ordine		valore: S
	 */
	if ( tipo_rotazione == 'C' ){
		numero_indici = parseInt(numero_indici) - 1
		if ( j_old == numero_indici || j_old > numero_indici ){
			j = 0;
		}else{
			j = j_old + 1;
		}
		var indice = singoli_indici[j];
		/*
		//questo codice serve per far apparire le immagini in maniera del tutto casuale
		var j = parseInt(Math.floor(Math.random() * numero_img));
		while ( j == j_old ){
			var j = parseInt(Math.floor(Math.random() * numero_img));
		}
		*/
	}else{
		numero_img = parseInt(numero_img) - 1;
		if ( j_old == numero_img || j_old > numero_img ){
			j = 0
		}else{
			j = j_old + 1;
		}
		indice = j;
	}
	
	//per il primo banner devo settare gli indici a zero per far partire i banner dal primo e non dal secondo
	if ( prima == 1 ){
		indice = 0;
		j = 0;
	}
	
	//setto il valore j nel campo j_old
	document.getElementById('j_old').value = j;
	
	//recupero i valori dai rispettivi array
	var id_banner = singoli_id[indice];
	var image_file = singole_img[indice];
	var titolo = singoli_titoli[indice];
	var testo = singoli_testi[indice];
	var strLink = singoli_link[indice];
	var durata = singola_durata[indice];
	var bottoni = singoli_bottoni[indice];

	if ( prima == 0 ){
		var speed = Math.round(1000 / 100);
		var timer = 0;
	
		//set the current image as background
		document.getElementById(divid).style.backgroundImage = "" + document.getElementById(imageid).style.backgroundImage + "";
	
		//make image transparent
		changeOpac(0, imageid);
	
		//make new image
		document.getElementById(imageid).style.backgroundImage = "url(" + image_file + ")";
	
		//fade in image
		for(i = 0; i <= 100; i++) {
			setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
			timer++;
		}
	}else{
		document.getElementById(divid).style.backgroundImage = "url("+image_file+")";
		document.getElementById(imageid).style.backgroundImage = "url("+image_file+")";
	}

	/*
	 * assegno i nuovi valori
	 */
	if ( titolo != 'undefined' ){
		document.getElementById('titolo_div').innerHTML = titolo;
	}else{
		document.getElementById('titolo_div').innerHTML = '';
	}
	
	if ( testo != 'undefined' ){
		document.getElementById('testo_div').innerHTML = testo;
	}else{
		document.getElementById('testo_div').innerHTML = '';
	}

	if ( visualizzo_tagA == 'true' ){
		if ( strLink != 'undefined' && strLink.length > 0 ){
			document.getElementById('link_image').href = '/asp/link.asp?'+strLink;
			document.getElementById('link_image').target = "_blank";
		}else{
			//strLink = "/default.asp?page=biblioman.asp&section=s_informazioni";
			document.getElementById('link_image').href = '#';
			document.getElementById('link_image').target = "_top";
		}
	}
	
	//BOTTONI - TEMPORIZZAZIONE
	if ( tipo_rotazione == "S" ){
		bottoni = bottoni.toString();
		var bottone = bottoni.split("][");
		if ( bottone[0].length > 0 ){

			document.getElementById('button_next_banner').innerHTML = '<img border="0" name="next_banner" id="next_banner"/>';

			document.getElementById('next_banner').src = path + bottone[0];
			posiziona_immagine(larghezza_banner,altezza_banner);

			document.getElementById('next_banner').onmouseout = function(){ cambia_img(path,bottone[0],larghezza_banner,altezza_banner); }

			if ( bottone[1].length > 0 ){
				document.getElementById('next_banner').onmouseover = function(){ cambia_img(path,bottone[1],larghezza_banner,altezza_banner); }
			}

		}else{

			var altezza_totale = altezza_banner - 23;
			var larghezza_totale = larghezza_banner - 80;

			document.getElementById('button_next_banner').innerHTML = '<input type="button" value="pagina seguente" name="next_banner" id="next_banner"/>';
			document.getElementById('next_banner').style.width = "79px";
			document.getElementById('next_banner').style.fontFamily = "Trebuchet MS, Arial, Helvetica, sans-serif";
			document.getElementById('next_banner').style.fontSize = "9px";
			document.getElementById('next_banner').style.textAlign = "center";
			document.getElementById('next_banner').style.top = altezza_totale+"px";
			document.getElementById('next_banner').style.left = larghezza_totale+"px";
			
			//posiziono titolo e testo
			document.getElementById('titolo_div').style.top = "-20px";
			document.getElementById('testo_div').style.top = "-20px";
		}
		
		var elenco_titoli = elenco_titoli.replace(/'/g, "\\'");
		var elenco_testi = elenco_testi.replace(/'/g, "\\'");
		document.getElementById('next_banner').onclick = function(){ blend_image( divid, imageid, elenco_indici, elenco_id, elenco_img, elenco_titoli, elenco_testi, elenco_link, elenco_durata, elenco_bottoni, tipo_rotazione, path, 0, visualizzo_tagA, larghezza_banner, altezza_banner); }
		
	}else{
		durata = parseFloat(durata) * 1000;
		
		var elenco_titoli = elenco_titoli.replace(/'/g, "\\'");
		var elenco_testi = elenco_testi.replace(/'/g, "\\'");
		setTimeout("blend_image('"+divid+"', '"+imageid+"', '"+elenco_indici+"', '"+elenco_id+"', '"+elenco_img+"', '"+elenco_titoli+"', '"+elenco_testi+"', '"+elenco_link+"', '"+elenco_durata+"', '"+elenco_bottoni+"', '"+tipo_rotazione+"', '"+path+"', 0, '"+visualizzo_tagA+"', "+larghezza_banner+", "+altezza_banner+");",durata);
	}

	/*
	 * devo aumentare di uno anche le impression
	 */
	if ( id_banner == null || id_banner == '' || id_banner == 0 ){ return; }

	var url = '/getData_rot.asp?opt=increaseImpression&id_banner='+id_banner;
	
	xmlHttp.open("GET", url, true);
	
	xmlHttp.onreadystatechange = updateImpression;
	
	xmlHttp.send(null);
}

function updateImpression(){
	if (xmlHttp.readyState == 4){
		var response = xmlHttp.responseText;
	}
}

function cambia_img(path,bottone,larghezza_banner,altezza_banner){
	document.getElementById('next_banner').src = path + bottone;
	posiziona_immagine(larghezza_banner,altezza_banner);
}

function posiziona_immagine(larghezza_banner,altezza_banner){
	//var larghezza = parseInt(document.getElementById('next_banner').width);
	//var altezza = parseInt(document.getElementById('next_banner').height);
	var larghezza = 30;
	var altezza = 20;
	
	var altezza_totale = altezza_banner - altezza - 3;
	var larghezza_totale = larghezza_banner - larghezza - 3;
	
	//posiziono il bottone
	document.getElementById('next_banner').style.top = altezza_totale+"px";
	document.getElementById('next_banner').style.left = larghezza_totale+"px";
	
	//posiziono titolo e testo
	document.getElementById('titolo_div').style.top = "-"+altezza+"px";
	document.getElementById('testo_div').style.top = "-"+altezza+"px";
}