var load_next = true;
var x_target = 0;
var animation_interval = null;
var thumbview = true;
var n=0;

function hidediv(t) {
	//document.getElementById(t).style.visibility = 'hidden';
	if (document.getElementById) { // DOM3 = IE5, NS6
	document.getElementById(t).style.visibility = 'hidden';
	}else {
		if (document.layers) { // Netscape 4
			document.hideShow.visibility = 'hidden';
		}else { // IE 4
			document.all.hideShow.style.visibility = 'hidden';
		}
	}
	
}

function showdiv(x) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(x).style.visibility = 'visible';
	}else {
		if (document.layers) { // Netscape 4
			document.hideShow.visibility = 'visible';
		}else { // IE 4
			document.all.hideShow.style.visibility = 'visible';
		}
	}
} 

function click_image()
{
	if (!$(this).hasClass("invisible")) return false;
	
	window.clearInterval(animation_interval);
	
	$("#scroll-images div div a:not(.invisible)")
		.click(click_image)
		.hover(rollover_image, rollout_image)
		.mousemove(rollover_image)
		.toggleClass("invisible");
	
	$(this)
		.click(
			function()
			{
				return false;
			}
		)
		.unbind("mouseenter")
		.unbind("mouseleave")
		.unbind("mousemove")
		.toggleClass("invisible")
		.removeClass("opacity50");
	
	var id = $(this).attr("id").split("-");
	//console.log("var id=",id);
	if (location.hash != id[1]) location.hash = id[1];
	//console.log("width of this", this);
	//Math.round($(this).width()/2)
	var x = parseInt($(this).parent().css("left").replace("px","")) + Math.round($(this).width()/2);
	x_target = -x;
	
	
	//console.log("id=", id, "width=",$(this).width(),"left",$(this).parent().css("left"), "x=",x,"x_target=", x_target);

	animation_interval = window.setInterval("animate()",25);
	
	return false;
}

function animate()
{
// JQuery is too slow
	var e = document.getElementById("scroll-images");
	var x = parseInt(e.style.left.replace("px",""));
	
	var x_diff = x_target - x;
	
	e.style.left = (x + (x_diff/4)) + "px";

	if (Math.round(x_diff) == 0) {
		window.clearInterval(animation_interval);
	}
	//console.log("left",$(this).parent().css("left"), "x=",x,"x_target=", x_target);

}

function load_next_image()
{

	if (load_next)
	{
		//console.log("line 60, length", $("#scroll-images div div:has(a.loaded):last").next().length);
		if ($("#scroll-images div div:has(a.loaded):last").next().length > 0)
		{
					//console.log("line 64");

			$("#scroll-images div div:has(a.loaded):last")
				.next()
				.children("a")
				.each(
					function(i)
					{		//console.log("i=", i);

						var image_url = $(this).attr("href");
						$(this)
							.children("img")
							.attr("src", image_url)
							.load(load_next_image);
						$(this)
							.addClass("loaded");
					}
				);
				
			if ($("#scroll-images div div:has(a.loaded):first").prevAll().length > 0) load_next = false;
					//console.log("line 84");

		}
		else
		{
			load_next = false;
			load_next_image();
		}
	}
	else
	{
		//console.log("line 95");

		$("#scroll-images div div:has(a.loaded):first")

			.prev()
			.children("a")
			.each(
				function(i)
				{
					//console.log("line104");
					var image_url = $(this).attr("href");
					$(this)
						.children("img")
						.attr("src", image_url)
						.load(load_next_image);
					$(this)
						.addClass("loaded");
									//console.log(" image loaded", this);

				}
			);
		
		if ($("#scroll-images div div:has(a.loaded):last").nextAll().length > 0) load_next = true;
	}
//console.log("load_next",load_next);

}

function rollover_image()
{
	$(this).addClass("opacity50");
	//console.log("mouseover", this);
}

function rollout_image()
{
	$(this).removeClass("opacity50");
		//console.log("rollout", this);

}


function thumbviewer() {
	
	thumbview=true;
		$("#scroll-images").css('display','none');

	$("#thumbnails").fadeIn(500);
	$("#text").fadeIn(500);
	
	hidediv("pagename");
	showdiv("sfoglia");
	//$("#thumbnails a").click(
							 
							 
		//				toggleview
									 //  );
	//$("#thumbnails a").click(slideview);
	}
	
function slideview()
	{

	showdiv("pagename");
	hidediv("sfoglia");
	//console.log("calling slideview()");
	//	n = (location.hash) ? parseInt(location.hash.replace("#","")) : 1;
	thumbview=false;
		$("#thumbnails").css('display','none');
		$("#text").css('display','none');
		$("#scroll-images").fadeIn(500);	
	
			
			 	$("#scroll-images div div a")
				.click(click_image)
				.hover(rollover_image, rollout_image)
				.mousemove(rollover_image);
				
					
				$("#js-"+n).each(
				function(i)
						{
							//console.log("n is ", n);
		
						var image_url = $(this).attr("href");
						//console.log("first this ", this);
		
						$(this)							.children("img") .attr("src",image_url).load(load_next_image);
		
						var img = new Image();
						$(this)
							.addClass("loaded")
							.trigger("click");
				
						}
				) //each	
				
		

	
		
	} //init()
	
function toggleview(){
	
	thumbview=!thumbview; 
	if (thumbview) {
		thumbviewer();
	}else{
		slideview();}
	}
	

$(document).ready(function(){
	if (thumbview) {thumbviewer();}else {slideview();}

	$("#pagename").click(toggleview);
	
	$("#thumbnails a").click(function(){
						n = $(this).attr("href").replace("#", "");
									  toggleview(n);});

	$("#pagename").hover(function(){ $(this).css('color','#636363');  $('body').css('cursor', 'pointer'); }, 
						 function(){  $(this).css('color','#b2b2b2');  $('body').css('cursor', 'default');}	);}
				  

);
