// JavaScript Document
global_counter_img=0;
global_img_loaded=0;
global_section=0;
imagePath='';
list_images='';
global_timeout='';




function starter_2(index) {

	document.getElementById('jas-container').style.filter = ''; // disattiva clear_type per la didascalia
	if ((index>0)&&(index<=list_images.length)) { // e' stato fatto click su un'immagine: va a quell'immagine
		JaS.imageIndex = index-1;	
		
		//  i setTimeout servono perché altrimenti l'evento JaS.init() legato a window.load:
		//  addEvent(window, "load", function(){JaS.init();}, false);  
		//  in IE parte DOPO JaS.setImage() e JaS.startSlideshow()
		
		global_timeout=setTimeout('JaS.setImage()',50);  
	} else { 																			//parte con slideshow
		global_timeout=setTimeout('JaS.startSlideshow()',50);
	}
}


function starter() {
	global_timeout=setTimeout('preload_images()',1000);
}


function preload_images(){
	replace_thumb();
	
	link_thumb_on();
	
	ready_to_go();

/* vecchio preload*/
/*global_forced_start= true;
global_counter_img=0;
	document.getElementById('loader_check').src=imagePath+list_images[0][0];
	if (document.all) {
		for (x=0; x<list_images.length; x++) {

				var nuova_immagine_DOM = document.createElement('img');
				var new_id='loader_check_'+x;
				nuova_immagine_DOM.id=new_id;
				document.getElementById('loader_check_wrapper').appendChild(nuova_immagine_DOM);
				nuova_immagine_DOM.setAttribute("width", '20');
				nuova_immagine_DOM.setAttribute("height", '20');
				nuova_immagine_DOM.setAttribute("src", imagePath+list_images[x][0]);
				if (nuova_immagine_DOM.readyState=="complete") 	next_just_loaded_IE();

				document.getElementById(new_id).onreadystatechange=next_loading_IE;

		}	
	
	} else {
		document.getElementById('loader_check').onload=next_loading;
	}*/
	
}

function replace_thumb() {
	var lo=getElementsBySelector('img.preload_thumb');
	var thumb=getElementsBySelector('img.thumb');
	var j=0;

	while (j<(lo.length))  {
		thumb[j].src=lo[j].src;
		j++;
	}
	
}

function link_thumb_on(){
	var a=getElementsBySelector('a.link_thumb');
	var j=0;
	while (j<(a.length))  {
		// non serve + perché si usa la posizione dell'immagine nella quadreria invece del nomefile come riferimento
		//var arr_filename=a[j].firstChild.src.split("/");
		//var filename=arr_filename[arr_filename.length-1];	
		//var	index=parseInt(filename,10); //toglie zeri iniziali e ".jpg"
		//a[j].href=document.location.href.replace(/_cover/,'')+'?index='+index;
		a[j].href=document.location.href.replace(/_cover/,'')+'?index='+(j+1);
		j++;
	}

}	

function next_loading_IE() {
global_forced_start= false;

		if (this.readyState=='complete') {
			//alert("next_loading: "+this.id);
			next_just_loaded_IE();
		}
}

function next_just_loaded_IE() {
		global_img_loaded++;
			if (global_img_loaded<list_images.length) {
				document.getElementById('loading_counter').innerHTML='('+global_img_loaded+'/'+list_images.length+')';
			} else {
				document.getElementById('loader_check').onreadystatechange= null;
				
				window.status='Caricamento immagini terminato';
				ready_to_go();
			}
	
}

/*function current_is_loaded(){
if (document.all) { // IE ha bisogno di questo controllo, per FF invece ritorniamo sempre TRUE
		if (id.readyState=='complete') {
			return true;
		} else {
			return false;
		}
}
return true;
}*/

function next_loading() {
global_forced_start= false;

		if (true) {

			if (global_counter_img<list_images.length-1) {
				global_counter_img=global_counter_img+1;
				document.getElementById('loader_check').src=imagePath+list_images[global_counter_img][0];
				document.getElementById('loading_counter').innerHTML='('+global_counter_img+'/'+list_images.length+')';
			} else {
				document.getElementById('loader_check').onreadystatechange= null;
				
				window.status='Caricamento immagini terminato';
				ready_to_go();
			}
		}
}



function ready_to_go() {
	document.getElementById('loading').style.display='none';
	document.getElementById('go_to_slideshow').style.display='block';
	if (!global_section){ //se non siamo in nessuna sezione...(=se non siamo in una quadreria)
/*		document.getElementById('a_go_to_slideshow').href='javascript:go_to_slideshow()';
		document.getElementById('cover_go_to_slideshow').href='javascript:go_to_slideshow()';*/  //non verve +
		
		document.getElementById('velo').style.opacity='1'; 
		document.getElementById('velo').style.filter='alpha(opacity=100)'; 
		document.getElementById('loading_velo').style.display='none'; 
	} else {
		go_to_slideshow();
	}
}

function go_to_slideshow() {
	document.getElementById('quit').style.display="block";
	document.getElementById('cover').style.display='none';
	document.getElementById('go_to_slideshow').style.display='none';
	
	document.getElementById('player').style.display='block';
	document.getElementById('slideshow').style.display='block';
	
	document.getElementById('jas-container').style.filter = ''; // disattiva clear_type per la didascalia

	document.getElementById('wrapper_opere').style.display='block';

	JaS.startSlideshow();
}

function go_to_cover() {

	JaS.stopSlideshow();
	JaS.imageIndex = 0;	
	JaS.setImage();
	
	document.getElementById('loader_check_wrapper').innerHTML='<img id="loader_check" src="/html/img/pix_bianco.gif" height="1" width="1">';
	
	document.getElementById('quit').style.display="none";
	
	document.getElementById('cover').style.display='block';
	
	if (!global_section){
		document.getElementById('go_to_slideshow').style.display='block';
	} else {
		//alert('stiamo tornando da una quadreria');
		
		location.reload();  //funziona
			/* dovrebbe essere inutile*/
		document.getElementById('ciclo_'+global_section).className='velo';
		document.getElementById('loading_status').innerHTML='';
		document.getElementById('loading_counter').innerHTML='';
		document.getElementById('loading').style.display='block';
	}
	
	document.getElementById('player').style.display='none';
	document.getElementById('slideshow').style.display='none';

	document.getElementById('wrapper_opere').style.display='none';
	
	
	/* dovrebbe essere inutile*/
	if (global_section!=0) {
		global_section=0; // azzera la sezione selezionata;
		a=getElementsByClass('a_quadreria_no_mano');
		var j=0;
		while (j<=(a.length-1))  {
			a[j].className="a_quadreria";
			j++;
		}
	}
}

function pop_up_hires() {
 	JaS.stopSlideshow();
	hi_res_source=(document.getElementById('jas-image').src).replace('/lo/','/hi/');
	window.open(hi_res_source);
	
/* vecchio script*/
/*	hi_res_source=(document.getElementById('jas-image').src).replace('/lo/','/hi/');
	
	
	document.getElementById('hires_caption').innerHTML=document.getElementById('jas-image-text').innerHTML;
	document.getElementById('img_hires').src='';
	document.getElementById('pop_up').style.display='block';
	
	document.getElementById('img_hires').src=hi_res_source;
	document.getElementById('img_hires').src='www';
	document.getElementById('img_hires').src=hi_res_source;
	
	//document.body.onunload=function (){back_button_fixer()};

	//window.onunload=function (){back_button_fixer()};	 //precedente
	window.attachEvent('onunload', back_button_fixer);*/

}

function close_me() {
	document.getElementById('page').style.display='block';
	document.getElementById('pop_up').style.display='none';
	document.getElementById('hires_caption').innerHTML='';
	document.getElementById('img_hires').src="";
	//document.body.onunload=function (){};
	//window.onunload=function (){};
	
	window.detachEvent('onunload', back_button_fixer);
}

function go_to_section(section_num) {
	
	// cambia la classe per togliere il cursore a manina;
	a=getElementsByClass('a_quadreria');
	var j=0;
	while (j<=(a.length-1))  {
		a[j].className="a_quadreria_no_mano";
		j++;
	}
	

	if (global_section!=0) {
	
	} else {
		global_section=section_num; //anche per dire che siamo in una quadreria (global_section<>0)
		if (global_section) {
			document.getElementById('ciclo_'+global_section).className='no_velo';
			document.getElementById('loading_status').innerHTML='ricezione immagini ';
		}

		eval("JaS.images=list_images=list_images_"+section_num);
		eval("JaS.imagePath=imagePath=imagePath_"+section_num);		
		JaS.init();
		preload_images();
	}
		
}

function back_button_fixer() {
	window.detachEvent('onunload', back_button_fixer);
	if (document.all) {
		history.go(1);	// funziona con IE
	} else {
		document.location.replace(document.location.href); // funziona con FF
	}
}

function is_safari() {
	return (document.childNodes && !document.all && !navigator.taintEnabled && !navigator.accentColorName);
}

