var sliding_images = Array();
var scroll_height;//228;
var subscroll_height = 10;
var opacity_speed = 200;
var scroll_speed = 400;
var subscroll_speed = 600;
var big_image_height = 0;
var big_image_width = 0;
var rapporto;

var current_page = 1;
var box_top = 0;
var box_bottom = 0;
var img_elem;

function addImage_slide(image) {
	var elem;
	if (sliding_images.length > 0) {
		elem = $(".notizia_gallery span:first").clone(true).attr("style", "background: #e2e2e2 url(" + image.lowUrl + ") no-repeat");
		elem.insertAfter($(".notizia_gallery span:last"));
		elem.attr("id", "");
	} else {
		elem = $(".notizia_gallery span");
		elem.attr("style", "background: #e2e2e2 url(" + image.lowUrl + ") no-repeat");
	}

	elem.attr("class", "imgThumb" );
	elem.attr("id_gal", sliding_images.length);
	sliding_images.push(image);

}

function checkTop() {
	var top = $("#box").css("top").replace("px", "");

	//03/10/2008 - Commentato da Giorgio per correggere su ie l'attivazione delle frecce
	//if ( parseInt(top) >=0)  {

	if(current_page == 1) {
		$("#up").attr("style", "visibility:hidden");
	} else {
		$("#up").attr("style", "visibility:visible");
	}

	height = $("#box").height();
	res = parseInt(top) + parseInt(height) - scroll_height;
	
	//03/10/2008 -  Commentato da Giorgio per correggere su ie l'attivazione delle frecce
	// if (res < 5) {

	var tot_pag =  Math.ceil(sliding_images.length / 8);

	if (current_page == tot_pag) {
		$("#down").attr("style", "visibility:hidden");
	} else {
		$("#down").attr("style", "visibility:visible");
	}

	// 18/05/2009 - Aggiunto da Giorgio per gestire una sola pagina
	if (tot_pag <= 1) {
		$("#current").attr("style", "visibility:hidden");
		$(".pipe").hide();
	}

}

function checkPrevNext(curr_id) {
	if (curr_id >=  sliding_images.length-1)
		$("#next").attr("style", "visibility:hidden");
	else
		$("#next").attr("style", "visibility:visible");

	if (curr_id <=  0)
		$("#prev").attr("style", "visibility:hidden");
	else
		$("#prev").attr("style", "visibility:visible");
}

function paginate() {
	var length = sliding_images.length

	//03/10/08 - Commentato da Giorgio per risolvere il problema del calcolo delle pagine
	//if (length>1) length = length +1;
	//var num_pag = ((length +1) / 6);
	
	num_pag = Math.ceil(sliding_images.length/8);
	
	for (var i=2;i<=num_pag;i++) {
		elem = $(".sliding_pag:first").clone(true).attr("id", "").text("|");
		elem.insertAfter($(".sliding_pag:last"));
		elem = $(".sliding_pag:first").clone(true).attr("id", "").text(i);
		elem.insertAfter($(".sliding_pag:last"));
	}
}

$(document).ready(function(){
	if ($("#box").length > 0){
		img_elem = $(".img_gallery");
		paginate();
		scroll_height = parseInt($(".notizia_gallery").height());
		box_top = parseInt($("#box").offset().top);
		box_bottom =  $(".notizia_gallery").offset().top + $(".notizia_gallery").height();
		big_image_height = $("#gallery_big").height();
		big_image_width = $("#gallery_big").width();
		rapporto = big_image_height / big_image_width;
		checkTop();

		$("#up").click(function(){
			var top = $("#box").css("top").replace("px", "");
			if (top == 'auto') top = 0;
			if (parseInt(top) <0) {
				$(".sliding_pag:contains('"+ current_page +"')").attr("id", "");
				current_page = current_page -1;

				$(".sliding_pag:contains('" +current_page+ "')").each(function() { 
					if($(this).text() == current_page){
						$(this).attr("id", "current");
					}
				});

				$("#box").animate({top: "+=" + (scroll_height + subscroll_height)}, scroll_speed, function(){checkTop()});
			}
		});

		$("#down").click(function(){
			var top = $("#box").css("top").replace("px", "");
			if (top == 'auto') top = 0;

			var height = $("#box").height();
			var res = parseInt(top) + parseInt(height) - scroll_height;
			res = 1;
			if (res > 0) {
				$(".sliding_pag:contains('"+ current_page +"')").attr("id", "");
				current_page = current_page +1;

				$(".sliding_pag:contains('" +current_page+ "')").each(function() { 
					if($(this).text() == current_page){
						$(this).attr("id", "current");
					}
				});

				$("#box").animate({top: "-="+(scroll_height + subscroll_height)}, scroll_speed, function(){checkTop()});
			}
		});

		$(".imgThumb").click(function(){
			$(".notizia_gallery span#activeLink").attr("id", "");
			$(this).attr("id", "activeLink");
			checkPrevNext(parseInt($(".notizia_gallery span#activeLink").attr("id_gal")));
			switchImg($(this).attr("id_gal"));
		});

		img_elem.load(function (){
			var paddingleft = 0;
			var top = 0;

			if (rapporto > (img_elem.height()/img_elem.width())) {
				img_elem.width(big_image_width);
				img_elem.height("auto");
				top = (big_image_height - img_elem.height())/2;
			} else {
				img_elem.height(big_image_height);
				img_elem.width("auto");
				paddingleft = (big_image_width - img_elem.width())/2;
			}
			img_elem.animate({left: paddingleft, top :top}, 400);
			$(".img_caption").text(img.caption);
		});
	
		function switchImg(id) {
			img = sliding_images[id];
			$("#current_img").text(parseInt(id)+1);

			img_elem.animate({left: 0}, 1,function() {
				img_elem.animate({left: "-=" + 700}, 400, function() {
					img_elem.attr("src", img.url);
				});
			});

		}

		$("#prev").click(function(){
			id_precedente =  parseInt($(".notizia_gallery span#activeLink").attr("id_gal"))-1;
			checkPrevNext(id_precedente);

			if (id_precedente >= 0) {
				switchImg(id_precedente);
				$(".notizia_gallery span#activeLink").attr("id", "");
				thumb = $("span[id_gal='"+ id_precedente +"']");
				thumb.attr("id", "activeLink");
				var top = thumb.offset().top - box_top;
				if (top < 0)
					$("#up").click();
			}
		});

		$("#next").click(function(){
			id_successivo =  parseInt($(".notizia_gallery span#activeLink").attr("id_gal"))+1;
			checkPrevNext(id_successivo);
			if (id_successivo >= 0) {
				switchImg(id_successivo);
				$(".notizia_gallery span#activeLink").attr("id", "");
				thumb = $("span[id_gal='"+ id_successivo +"']");
				thumb.attr("id", "activeLink");
				var top = thumb.offset().top;
				if (top >= box_bottom)
					$("#down").click();
			}
		});

		$(".imgThumb:first").click();
	
	}
});

function ImmagineSlide () {
	var url;
	var lowUrl;
	var caption;
	var city;
	var date;
	var photographer;

	this.setUrl = function(p_url) {
		this.url = p_url;
	}

	this.setLowUrl = function(p_url) {
		this.lowUrl = p_url;
	}

	this.setCaption = function(p_caption) {
		this.caption = p_caption;
	}

	this.setCity = function(p_city) {
		this.city = p_city;
	}

	this.setDate = function(p_date) {
		this.date = p_date;
	}

	this.setPhotographer = function(p_photographer) {
		this.photographer = p_photographer;
	}

}
