//serie di img da caricare
var imgseries = "";
var imgdir = dirstart + "img/";
var fotodir = dirstart + "foto/";
//inizializzazione dopo caricamento di pagina (basato su jquery)
$(document).ready(function(){
	//imgloader() // caricamento immagini
	//inizializza();//popup
	blankPage(); //blank page
	//inizializza Gallery home
	if (isrc != "") {
		galhome();
	}
});

//caricamento immagine
function imgloader() {
	var imgarr = imgseries.split(",");
	var imgLoadernb = new Array();
	for (var i=0; i<imgarr.length; i++) {
		imgLoadernb[i] = new Image();
		imgLoadernb[i].src = imgdir + imgarr[i];
	};
}
function inizializza() {
	a = document.getElementsByTagName('A');
	for(i=0;a[i];i++) 
		if(a[i].className.indexOf('imagepopup') != -1) {
			a[i].title += " [il collegamento apre una nuova finestra]";
			a[i].onclick = function () {immagine=window.open(this.href,'immagine','status,height=700,width=820,scrollbars=auto,resizable=no,menubar=no,status=no');immagine.focus();return false;};
			a[i].onkeypress = function (e) {
				k = (e) ? e.keyCode : window.event.keyCode;
				if(k==13) {
					immagine=window.open(this.href,'immagine','status,height=700,width=820,scrollbars=auto,resizable=no,menubar=no,status=no');immagine.focus(); return false;
			}
		}
	}
}
function blankPage(){
	for (var i=0; i<document.links.length; i++)
		if (document.links[i].className.indexOf("blank") != -1) {
			document.links[i].target="_blank";
		}
}

/* Home page gallery */

var timer_delay = 6000; // 1000 = 1 secondo
var timer = true; // attivazione del timer per scorrimento automatico
var currenti = 0;//indice di partenza
var fadeinspeed = "slow";// velocita' di fade in
var fadeoutspeed = "normal";// velocita' di fade out
var imgarrh;
var htxt;
var ptxt;
var timer_id = '';

function galhome() {
	//split degli array da variare in gallery
	imgarrh = isrc.split("$$");
	htxtarr = htxt.split("$$");
	ptxtarr = ptxt.split("$$");
	//preload delle immagini
	/*for (var i=0; i<imgarrh.length; i++) {
		imgLoaderh = new Image();
		imgLoaderh.src = fotodir + imgarrh[i];
	};*/
	if (timer == true) {
		timerstart();
	}
}
function next(sel){
	if(currenti < (imgarrh.length-1)){
		currenti ++;
	}
	else {
		currenti = 0;
	}
	if (sel != "") {
		timerstartstop();
	}
	select();
}
function prev(sel){
	if(currenti > 0){
		currenti --;
	}
	else {
		currenti = imgarrh.length -1;
	}
	if (sel != "") {
		timerstartstop();
	}
	select();
}
function getnum(sel){
	if (currenti != sel - 1) {
		currenti = sel-1;
		timerstartstop();
		select();	
	}
}
function timerstart(){
	if (timer) {
		timer_id = window.setTimeout("next()", timer_delay);

	}
}
function timerstartstop(){
	//timer = false;
	if (timer_id) { 
		window.clearTimeout(timer_id);
		timer_id = '';
		}
}
function pause(){
	if (timer_id) { 
		timer = false;
		$(".pause").addClass("play");
		$(".pause").removeClass("pause");
		timerstartstop();
	}
	else {
		timer = true;
		$(".play").addClass("pause");
		$(".play").removeClass("play");
		timerstart();
	}
}
function select(sel) {
	//cambia selezione dei controlli numerici
	$(".selgal").removeClass("selgal");
	$(".item:eq(" + currenti + ")").addClass("selgal");
	
	//immagini
	$(imgdiv).fadeOut(fadeoutspeed, function(){
		//$(imgdiv).hide(); 
		$(imgdiv).html('<img src="'+ fotodir + imgarrh[currenti] + '" alt="" width="'+ imgw +'" height="'+ imgh +'" />');
		$(imgdiv).fadeIn(fadeinspeed);
	});
	// titoli
	$(idtit).fadeOut(fadeoutspeed, function(){
		$(idtit).hide(); 
		$(idtit).html(htxtarr[currenti]);
		$(idtit).fadeIn(fadeinspeed);
	});
	// testi
	$(idcorpo).fadeOut(fadeoutspeed, function(){
		$(idcorpo).hide(); 
		$(idcorpo).html(ptxtarr[currenti]);
		$(idcorpo).fadeIn(fadeinspeed);
	});
	if (!sel) {
		timerstart();
	}
}




