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_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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 showover(imagename,imageover,type){
	if(type=="previousimage" && currentimage>0){
		MM_swapImage(imagename,'',imageover,1);
	}
	if(type=="nextimage" && currentimage<totimg-1){
		MM_swapImage(imagename,'',imageover,1);
	}
	if(type=="previouspage" && currentpage>1){
		MM_swapImage(imagename,'',imageover,1);
	}
	if(type=="nextpage" && currentpage<totpage){
		MM_swapImage(imagename,'',imageover,1);
	}
}

function js_in_array(the_needle, the_haystack){
    var the_hay = the_haystack.toString();
    if(the_hay == ''){
        return false;
    }
    var the_pattern = new RegExp(the_needle, 'g');
    var matched = the_pattern.test(the_haystack);
    return matched;
}

function nextpage(){
    if(currentpage==0){
	document.getElementById('totfoto').innerHTML=totimg;
        document.getElementById('bigimg').src="public/gallery/"+pagina1[0]+"g.jpg";
        document.getElementById('descrizione').innerHTML=descrizione[0];
    }
    if(currentpage<totpage){
        currentpage++;
        document.getElementById('img1').src="public/gallery/"+eval("pagina"+currentpage+"[0]")+"pn.jpg";
        document.getElementById('img2').src="public/gallery/"+eval("pagina"+currentpage+"[1]")+"pn.jpg";
        document.getElementById('img3').src="public/gallery/"+eval("pagina"+currentpage+"[2]")+"pn.jpg";
        document.getElementById('img4').src="public/gallery/"+eval("pagina"+currentpage+"[3]")+"pn.jpg";
    }
    resto=totimg%4;
    if(resto>0 && currentpage==totpage){
	for(s=resto+1;s<=4;s++){
		document.getElementById('img'+s).style.visibility="hidden";
	}
    }
    indietro="";
    avanti="";
    if(currentpage>1){indietro="<a href=\"javascript:previouspage();\" class=\"link_bianco\">&lt;&lt;</a>";}
    if(currentpage<totpage){avanti="<a href=\"javascript:nextpage();\" class=\"link_bianco\">&gt;&gt;</a>";}
    document.getElementById('segnalibro').innerHTML="Pag. "+currentpage+" di "+totpage;
    document.getElementById('ltpage').innerHTML=indietro;
    document.getElementById('gtpage').innerHTML=avanti;
   
}
function previouspage(){
	 for(s=1;s<=4;s++){
	document.getElementById('img'+s).style.visibility="visible";
	}
    if(currentpage>1){
        currentpage--;
        document.getElementById('img1').src="public/gallery/"+eval("pagina"+currentpage+"[0]")+"pn.jpg";
        document.getElementById('img2').src="public/gallery/"+eval("pagina"+currentpage+"[1]")+"pn.jpg";
        document.getElementById('img3').src="public/gallery/"+eval("pagina"+currentpage+"[2]")+"pn.jpg";
        document.getElementById('img4').src="public/gallery/"+eval("pagina"+currentpage+"[3]")+"pn.jpg";
    }
    resto=totimg%4;
    if(resto>0 && currentpage==totpage){
	for(s=resto+1;s<=4;s++){
		document.getElementById('img'+s).style.visibility="hidden";
	}
    }
    indietro="";
    avanti="";
    if(currentpage>1){indietro="<a href=\"javascript:previouspage();\" class=\"link_bianco\">&lt;&lt;</a>";}
    if(currentpage<totpage){avanti="<a href=\"javascript:nextpage();\" class=\"link_bianco\">&gt;&gt;</a>";}
    document.getElementById('segnalibro').innerHTML="Pag. "+currentpage+" di "+totpage;
    document.getElementById('ltpage').innerHTML=indietro;
    document.getElementById('gtpage').innerHTML=avanti;
}

function showbig(numimg){
    document.getElementById('bigimg').src="public/gallery/"+eval("pagina"+currentpage+"["+(numimg-1)+"]")+"g.jpg";
    document.getElementById('img1').src="public/gallery/"+eval("pagina"+currentpage+"[0]")+"pn.jpg";
    document.getElementById('img2').src="public/gallery/"+eval("pagina"+currentpage+"[1]")+"pn.jpg";
    document.getElementById('img3').src="public/gallery/"+eval("pagina"+currentpage+"[2]")+"pn.jpg";
    document.getElementById('img4').src="public/gallery/"+eval("pagina"+currentpage+"[3]")+"pn.jpg";
    document.getElementById('img'+numimg).src="public/gallery/"+eval("pagina"+currentpage+"["+(numimg-1)+"]")+"p.jpg";
    idcurrentimage=eval("pagina"+currentpage+"["+(numimg-1)+"]");
    for(i=0;i<totimg;i++){
        if(allimmagini[i]==idcurrentimage){currentimage=i;}
    }
    document.getElementById('descrizione').innerHTML=descrizione[currentimage];
   
}

function nextimg(){
    if(currentimage<totimg-1){
        document.getElementById('bigimg').src="public/gallery/"+eval("allimmagini["+(currentimage+1)+"]")+"g.jpg";
        currentimage++;
        idcurrentimage=eval("allimmagini["+(currentimage)+"]");
        if(!js_in_array(idcurrentimage,eval("pagina"+currentpage))){
            nextpage();
        }
        for(k=0;k<4;k++){
            if(eval("pagina"+currentpage+"["+k+"]")==idcurrentimage){
            document.getElementById('img'+eval(k+1)).src="public/gallery/"+eval("pagina"+currentpage+"["+(k)+"]")+"p.jpg";
            }else{
            document.getElementById('img'+(k+1)).src="public/gallery/"+eval("pagina"+currentpage+"["+(k)+"]")+"pn.jpg";
            }
        }
        document.getElementById('descrizione').innerHTML=descrizione[currentimage];
    }
}

function previousimg(){
    if(currentimage>0){
        document.getElementById('bigimg').src="public/gallery/"+eval("allimmagini["+(currentimage-1)+"]")+"g.jpg";
        currentimage--;
        idcurrentimage=eval("allimmagini["+(currentimage)+"]");
        if(!js_in_array(idcurrentimage,eval("pagina"+currentpage))){
            previouspage();
        }
        for(k=0;k<4;k++){
            
            if(eval("pagina"+currentpage+"["+k+"]")==idcurrentimage){
            document.getElementById('img'+(k+1)).src="public/gallery/"+eval("pagina"+currentpage+"["+(k)+"]")+"p.jpg";
            }else{
            document.getElementById('img'+(k+1)).src="public/gallery/"+eval("pagina"+currentpage+"["+(k)+"]")+"pn.jpg";
            }
        }
        document.getElementById('descrizione').innerHTML=descrizione[currentimage];
    }
}

function imgover(numimg){
    document.getElementById('img'+numimg).src="public/gallery/"+eval("pagina"+currentpage+"["+(numimg-1)+"]")+"p.jpg";
}

function imgout(numimg){
    if(idcurrentimage!=eval("pagina"+currentpage+"["+(numimg-1)+"]")){
    document.getElementById('img'+numimg).src="public/gallery/"+eval("pagina"+currentpage+"["+(numimg-1)+"]")+"pn.jpg";
    }
}

function popup(){
    window.open('popup.php?img='+idcurrentimage,'Zoom','width=640, height=700, toolbar=no, location=no,status=no,menubar=no, scrollbars=yes');
}

function bigimageover(){
	document.getElementById('tableimg').style.backgroundColor="#FF0000";
	document.getElementById('bigimg').style.border="1px solid #FF0000";
}

function bigimageout(){
	document.getElementById('tableimg').style.backgroundColor="#FFFFFF";
	document.getElementById('bigimg').style.border="1px solid #FFFFFF";
}
