var picture_back;
var picture_front;

var actual_pic = 0;
var actual_folder = 0;
pic_names = new Array();
pic_captions = new Array();

fullsize_pics = new Array();
fullsize_pics_size = new Array();
fullsize_pics_dimensions_x = new Array();
fullsize_pics_dimensions_y = new Array();

preview_pics_dimension_x = new Array();
preview_pics_dimension_y = new Array();

folder_names = new Array();
folder_sounds = new Array();

folder_nr = 0;
pic_nr = new Array();

main_clr=new Array();
main_clr[0] = 120;
main_clr[1] =130;
main_clr[2] =150;

selected_gallery_color = "rgb(255,0,0)";

function setGalleryColors(){
 inset_clr = new Array();
 inset_clr[0] = main_clr[0]+30;         inset_clr[1] = main_clr[1]+30;         inset_clr[2] = main_clr[2]+30;
 if(inset_clr[0]>255) inset_clr[0]=255; if(inset_clr[1]>255) inset_clr[1]=255; if(inset_clr[2]>255) inset_clr[2]=255;
 
 /*
 sel_clr = new Array();
 sel_clr[0] = 255-inset_clr[0];   sel_clr[1] = 255-inset_clr[1];   sel_clr[2] = 255-inset_clr[2];
 selected_gallery_color = "rgb("+sel_clr[0]+","+sel_clr[1]+","+sel_clr[2]+")";*/
 
 light_clr = new Array();
 light_clr[0] = main_clr[0]+60;        light_clr[1] = main_clr[1]+60;        light_clr[2] = main_clr[2]+60;
 if(light_clr[0]>255) light_clr[0]=255; if(light_clr[1]>255) light_clr[1]=255; if(light_clr[2]>255) light_clr[2]=255;
 
 shadow_clr = new Array();
 shadow_clr[0] = main_clr[0]-100;     shadow_clr[1] = main_clr[1]-100;     shadow_clr[2] = main_clr[2]-100;
 if(shadow_clr[0]<0) shadow_clr[0]=0; if(shadow_clr[1]<0) shadow_clr[1]=0; if(shadow_clr[2]<0) shadow_clr[2]=0;
  
 ABCD = document.getElementById("main_picture_div");
 ABCD.style.borderWidth=1; ABCD.style.borderStyle="solid";
 ABCD.style.backgroundColor="rgb("+main_clr[0]+","+main_clr[1]+","+main_clr[2]+")";
 ABCD.style.borderTopColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 ABCD.style.borderLeftColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 ABCD.style.borderBottomColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 ABCD.style.borderRightColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
  
 elem = document.getElementById("front_picture_div");
 elem.style.borderWidth=1; elem.style.borderStyle="solid";
 //elem.style.backgroundColor="rgb("+main_clr[0]+","+main_clr[1]+","+main_clr[2]+")";
 elem.style.borderBottomColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderRightColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderTopColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 elem.style.borderLeftColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
  
 elem = document.getElementById("titles_div");
 elem.style.borderWidth=1; elem.style.borderStyle="solid";
 elem.style.backgroundColor="rgb("+inset_clr[0]+","+inset_clr[1]+","+inset_clr[2]+")";
 elem.style.borderBottomColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderRightColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderTopColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 elem.style.borderLeftColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 
 elem = document.getElementById("gallery_links_main_div");
 elem.style.borderWidth=1; elem.style.borderStyle="solid";
 elem.style.backgroundColor="rgb("+main_clr[0]+","+main_clr[1]+","+main_clr[2]+")";
 elem.style.borderTopColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderLeftColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderBottomColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 elem.style.borderRightColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 
 elem = document.getElementById("gallery_links_inner_div");
 elem.style.borderWidth=1; elem.style.borderStyle="solid";
 elem.style.backgroundColor="rgb("+inset_clr[0]+","+inset_clr[1]+","+inset_clr[2]+")";
 elem.style.borderBottomColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderRightColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderTopColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 elem.style.borderLeftColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 
 elem = document.getElementById("caption_main_div");
 elem.style.borderWidth=1; elem.style.borderStyle="solid";
 elem.style.backgroundColor="rgb("+main_clr[0]+","+main_clr[1]+","+main_clr[2]+")";
 elem.style.borderTopColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderLeftColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderBottomColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 elem.style.borderRightColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 
 elem = document.getElementById("caption_inner_div");
 elem.style.borderWidth=1; elem.style.borderStyle="solid";
 elem.style.backgroundColor="rgb("+inset_clr[0]+","+inset_clr[1]+","+inset_clr[2]+")";
 elem.style.borderBottomColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderRightColor="rgb("+light_clr[0]+","+light_clr[1]+","+light_clr[2]+")";
 elem.style.borderTopColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
 elem.style.borderLeftColor="rgb("+shadow_clr[0]+","+shadow_clr[1]+","+shadow_clr[2]+")";
}// --- end setGalleryColors() -------------------------------------------------------------


lang_str = new Array()
function initializeGallery(){
  picture_front = document.getElementById("picture");
  picture_back = document.getElementById("picture_back");

  if(typeof language == 'undefined') language="eng";
  if(language=="eng"){
	     lang_str[0] = "Fullsize Picture info";
		 lang_str[1] ="File name: ";
		 lang_str[2] = "Size: ";
		 lang_str[3] = "Resolution: ";
		 lang_str[4] ="No Fullsize Picture or Video available";
		 lang_str[5] = "Video info";
	   }



if(language=="pol"){
  lang_str[0] = "Obraz w pelnych wymiarach";
  lang_str[1] = "Nazwa pliku: ";
    lang_str[2] = "Rozmiar: ";
    lang_str[3] = "Rozdzielczosc: ";
    lang_str[4] = "Brak dostepu do obrazu lub filmu w pelnych wymiarach";
    lang_str[5] = "Video info";
    }


  if(language=="ita"){
	         lang_str[0] = "Info immagine fullsize";
		 lang_str[1] ="Nome del file: ";
		 lang_str[2] = "Dimensione: ";
		 lang_str[3] = "Risoluzione: ";
		 lang_str[4] ="Nessun Video o Immagine fullsize disponibile";
		 lang_str[5] = "Info Video";
	   }
 if(language=="dut"){
    lang_str[0] = "Volledig Foto Info";
    lang_str[1] = "Bestandsnaam: ";
    lang_str[2] = "Grootte: ";
    lang_str[3] = "Resolutie: ";
    lang_str[4] = "Geen Foto of Video in volledig formaat aanwezig";
    lang_str[5] = "Video info";
	  }
if(language=="est"){
    lang_str[0] = "T?issuuruses pildi info";
    lang_str[1] = "Faili nimetus: ";
    lang_str[2] = "suurus: ";
    lang_str[3] = "Resolutsioon: ";
    lang_str[4] = "T?issuuruses pilt v?i video pole saadaval";
    lang_str[5] = "Video info";
    }
 
 setGalleryColors();
 //initializeSounds();
 
//set this flag to true if you want that the images set showing when the page is opened is the last one      
//set it to false if you want that the images set showing when the page is opened is the first one   
 if(true){                              
   bottom_gallery=folder_names.length-1;
   placeGalleryLinks(bottom_gallery); 
   showGalleryNr(folder_nr-1);	
   document.getElementById("down_btn_a").style.visibility="hidden";
   if(folder_names.length<=6) document.getElementById("up_btn_a").style.visibility="hidden";
   } else{
   bottom_gallery=5;
   placeGalleryLinks(bottom_gallery); 
   showGalleryNr(0);
	 document.getElementById("up_btn_a").style.visibility="hidden";
	 if(folder_names.length<=6) document.getElementById("down_btn_a").style.visibility="hidden";
   }
 
 
 document.getElementById("prev_btn_a").style.visibility="hidden";
}

bottom_gallery = 0;
function placeGalleryLinks(bottom_lnk){
 
   for( i=0; i<folder_nr; i++){ 
     elem = document.getElementById("gallery"+i);
     elem.style.visibility = "visible";
     elem.style.top = i*50 - (bottom_lnk+1-6)*50 ;
     elem.childNodes[0].nodeValue = folder_names[i].substring(3); 
     }
}




function playSound(sound_nr) {
 for (i=0;i<folder_nr;i++){
 var sound = eval("document.sound" + i);
 
 // make an effort to stop and rewind any playback
 // already in progress so that the sound starts over.
 // Otherwise this call has no effect when the sound
 // is already in progress. You can remove these lines
 // if you don't like this behavior.
 try {
  sound.Stop();
  sound.Rewind();
 } catch (e) {
  // A player that doesn't support
  // Stop and Rewind
 }
 }
 
var sound = eval("document.sound" + sound_nr);
if (folder_sounds[sound_nr]!="nosound"){
 try {
  // For RealPlayer-enabled browsers.
  // Some versions of RealPlayer do not
  // offer a Play() function and will
  // fail to play sound if we try to
  // call Play(). 
  sound.DoPlay();
 } catch (e) {
  // If DoPlay doesn't work, call Play.
  // This works for all other audio 
  // plug-ins.
	alert(sound.src);
  sound.Play();
	
 }
 }
}



function initializeSounds(){
   for(i=0;i<folder_nr;i++)	{ var sound = eval("document.sound"+i);
	 sound.src = folder_names[i]+"/"+folder_sounds[i];
	
	 } 
	
   }


next_pressed = new Image();
next_pressed.src = "/img/button/next_prs.png";
prev_pressed = new Image();
prev_pressed.src = "/img/button/prev_prs.png";
next_button = new Image();
next_button.src = "/img/button/next_btn.png";
prev_button = new Image();
prev_button.src = "/img/button/prev_btn.png";	 
	 
function mouseoverNext(){   document.getElementById("next_btn_a").src = next_pressed.src;   }	  
function mouseoverPrev(){   document.getElementById("prev_btn_a").src = prev_pressed.src;   }	 	 
function mouseoutNext() {   document.getElementById("next_btn_a").src = next_button.src;    }	 	 	 
function mouseoutPrev() {   document.getElementById("prev_btn_a").src = prev_button.src;    }	 	 	 

up_pressed = new Image();
up_pressed.src = "./structure.images/up_prs.png";
down_pressed = new Image();
down_pressed.src = "./structure.images/down_prs.png";
up_button = new Image();
up_button.src = "./structure.images/up_btn.png";
down_button = new Image();
down_button.src = "./structure.images/down_btn.png";	 
	 
function mouseoverUp(){   document.getElementById("up_btn_img").src = up_pressed.src;   }	  
function mouseoverDown(){   document.getElementById("down_btn_img").src = down_pressed.src;   }	 	 
function mouseoutUp() {   document.getElementById("up_btn_img").src = up_button.src;    }	 	 	 
function mouseoutDown() {   document.getElementById("down_btn_img").src = down_button.src;    }	 	 	 



function slideDownLnk(){
  if(bottom_gallery<folder_names.length-1){
   bottom_gallery++;
	 placeGalleryLinks(bottom_gallery);
	 }
	 document.getElementById("up_btn_a").style.visibility="visible";
	 if(bottom_gallery==folder_names.length-1) document.getElementById("down_btn_a").style.visibility="hidden";
	
   }
	 
function slideUpLnk(){ 
	 if(bottom_gallery>5){
   bottom_gallery--;
	 placeGalleryLinks(bottom_gallery);
	 }
	  document.getElementById("down_btn_a").style.visibility="visible";
	 if(bottom_gallery==5) document.getElementById("up_btn_a").style.visibility="hidden";
   }	 
	 	 
function showFullsize(){ 
 if (fullsize_pics[actual_folder][actual_pic]==1) window.open("./fullsize_pic.php?cartella="+folder_names[actual_folder]+"&nome="+pic_names[actual_folder][actual_pic]+"&lang="+language,"fullsize");
 if (fullsize_pics[actual_folder][actual_pic]==2) {
  video_name = pic_names[actual_folder][actual_pic].split(".")[0];
  window.open(  folder_names[actual_folder]+"/full/"+video_name+".avi"	);  
  }
 	
}	 

/* la variabile last_movement indica se l'utente ha richiesto l'immagine successiva, 
   quella precedente oppure si ha richiesto un altro gruppo di immagini.
   ? necessaria perch? quendo si ? passati all'immagine precedente o successiva si possono utilizzare
   le immagini precaricate per il fading.
   0 = nuovo gruppo, 1 = immagine successiva, 2 = immagine precedente                 */
var last_movement = 0;


function nextPic(){
   last_movement = 1;
   actual_pic++;	
   showPicNr(actual_pic);	
   }

function prevPic(){
    last_movement = 2;
    actual_pic--;
    showPicNr(actual_pic);	
	}



function showGalleryNr(nr){
     last_movement = 0;	
	 actual_folder = nr;
	 document.getElementById("gallery_title").childNodes[0].nodeValue = folder_names[actual_folder].substring(3);
	 for (j=0;j<200;j++) document.getElementById("gallery"+j).style.color = "black";
	 document.getElementById("gallery"+nr).style.color= selected_gallery_color;
	 
	 showPicNr(0);
	// playSound(actual_folder);  //non ancora utilizzato
   }
	 
prev_img = new Image();
next_img = new Image();
prev_img.src = "";
next_img.src = "";

 
function showPicNr(nr){
     actual_pic=nr;
	  
	 document.getElementById("prev_btn_a").style.visibility="visible";
	 document.getElementById("next_btn_a").style.visibility="visible";	 
	 
	 document.getElementById("gallery_title").childNodes[0].nodeValue = "hggghh";//folder_names[actual_folder].substring(3)+ " ("+(actual_pic+1)+"/"+pic_names[actual_folder].length+")";
	 document.getElementById("picture_title").childNodes[0].nodeValue = pic_names[actual_folder][actual_pic].split(".")[0];
	
	
	
	
	if(true){  //ridimensionamento e riposizionamento dell'immagine 	       
		    coeff =   preview_pics_dimension_y[actual_folder][actual_pic]/ preview_pics_dimension_x[actual_folder][actual_pic];	
			if(coeff>0.75){	
			   picture_front.style.height = 300;	
			   picture_front.style.width = 300/coeff;	
			   picture_front.style.top = 0;
			  }else{
			   picture_front.style.width = 400;
			   picture_front.style.height = coeff*400;
			   picture_front.style.top = (300 - coeff*400)/2  ;
              }// fine else
		}// fine if(true)
		else{			
			   picture_front.style.top = (300-preview_pics_dimension_y[actual_folder][actual_pic])/2;        
			  }// fine else
		
		
			
	actual_opacity=0;
	picture_front.style.MozOpacity=0;
	picture_front.style.filter = "alpha(opacity = 0)";	  
	
	 if(last_movement == 0){ picture_front.src = folder_names[actual_folder]+"/redu/"+pic_names[actual_folder][actual_pic]; 	 }	 
     if(last_movement == 1){ picture_front.src = next_img.src;   }		 
	 if(last_movement == 2){ picture_front.src = prev_img.src; 	 }
	
	
	
	start_fading_time = new Date();	
	setTimeout('fadePicture()',50);


    // --- preload delle immagini successiva e precedente -------------------------------------------
	if(actual_pic==0){ document.getElementById("prev_btn_a").style.visibility="hidden";   }
    else prev_img.src = folder_names[actual_folder]+"/redu/"+pic_names[actual_folder][actual_pic-1];

	if(actual_pic >= pic_nr[actual_folder]-1 ){   document.getElementById("next_btn_a").style.visibility="hidden";   }
    else next_img.src = folder_names[actual_folder]+"/redu/"+pic_names[actual_folder][actual_pic+1];


	
	 if (fullsize_pics[actual_folder][actual_pic]==0){
	        elem = document.getElementById("picture_link");
	        elem.style.cursor = "default";
			document.getElementById("video_alert").style.visibility = "hidden";					
			document.getElementById("caption_row0").style.visibility = "visible";
			document.getElementById("caption_row0").childNodes[0].nodeValue = lang_str[4];
			document.getElementById("caption_row1").style.visibility = "hidden";
			document.getElementById("caption_row2").style.visibility = "hidden";
			document.getElementById("caption_row3").style.visibility = "hidden";
		    }
	
	  if (fullsize_pics[actual_folder][actual_pic]==1) {
	        elem = document.getElementById("picture_link");
	        elem.style.cursor = "pointer";
	  	    document.getElementById("video_alert").style.visibility = "hidden";		
			document.getElementById("caption_row0").style.visibility = "visible";
			document.getElementById("caption_row0").childNodes[0].nodeValue = lang_str[0];
			document.getElementById("caption_row1").style.visibility = "visible";
			document.getElementById("caption_row1").childNodes[0].nodeValue = lang_str[1] +pic_names[actual_folder][actual_pic];
			document.getElementById("caption_row2").style.visibility = "visible";
			document.getElementById("caption_row2").childNodes[0].nodeValue =  lang_str[2] + Math.floor(fullsize_pics_size[actual_folder][actual_pic]/1024)+" KB";
			document.getElementById("caption_row3").style.visibility = "visible";
			document.getElementById("caption_row3").childNodes[0].nodeValue =  lang_str[3] + fullsize_pics_dimensions_x[actual_folder][actual_pic] + "x" + fullsize_pics_dimensions_y[actual_folder][actual_pic]+" px";
			}	
			
	 if (fullsize_pics[actual_folder][actual_pic]==2){	
	        elem = document.getElementById("picture_link");
	        elem.style.cursor = "pointer";
			document.getElementById("video_alert").style.visibility = "visible";		
	        document.getElementById("caption_row0").style.visibility = "visible";
			document.getElementById("caption_row0").childNodes[0].nodeValue = lang_str[5];
			document.getElementById("caption_row1").style.visibility = "visible";
			document.getElementById("caption_row1").childNodes[0].nodeValue =  lang_str[1] +video_name+".avi";
			document.getElementById("caption_row2").style.visibility = "visible";
			document.getElementById("caption_row2").childNodes[0].nodeValue =  lang_str[2] + Math.floor(fullsize_pics_size[actual_folder][actual_pic]/1024)+" KB";
			document.getElementById("caption_row3").style.visibility = "hidden";
			} 
			
	if(pic_captions[actual_folder][actual_pic]){
	        document.getElementById("caption_row4").style.visibility = "visible";
			document.getElementById("caption_row4").childNodes[0].nodeValue =   pic_captions[actual_folder][actual_pic];
			}
	else{ document.getElementById("caption_row4").style.visibility = "hidden";}		
	
   }
	

var start_fading_time;


function fadePicture(){

  actual_opacity = actual_opacity+10;	
	picture_front.style.MozOpacity = actual_opacity/100;
	picture_front.style.filter = "alpha(opacity = "+actual_opacity+")";
	temp_time = new Date();
	delay = temp_time.getTime() - start_fading_time.getTime();
	if((actual_opacity <90)&&(delay<1000)) setTimeout('fadePicture()',50);
	else {
	   picture_front.style.filter = "alpha(opacity = 100)";
	     picture_front.style.MozOpacity = 1;

		 picture_back.src = picture_front.src;
	     picture_back.style.top    = picture_front.style.top;
		 picture_back.style.left   = picture_front.style.left;
		 picture_back.style.height = picture_front.style.height ;
		 picture_back.style.width  = picture_front.style.width;	
		 }//fine else
		 
	 }// fine function fadePicture()

	
	 
	




