// VARIABILI GLOBALI
var splash_gallery_interval;
var splash_gallery_index = 0;
var home_slider_index = 0;
/* ********************************** */

$(document).ready(function() {
	// CENTERING MENU	
	$("header nav#menu ul li:last").css("margin-right","0");
	header_menu_span = 0;
	$("header nav#menu ul li").each(function() {header_menu_span += $(this).width()+10;});
	$("header nav#menu ul").css({"left" : (960-header_menu_span)/2 + "px"});
	$("header nav#menu").css("visibility","visible");
	/* ********************************** */
	
	// SPLASH
	$("header #splash").prepend("<div id=\"cover\"></div>");
	/* ********************************** */
	
	// SPLASH GALLERY
	$("#splash-spot").after("<ul class=\"clearfix\" id=\"splash-gallery-nav\"></ul>");
	$("#splash-gallery div").cycle({
		pager : $("#splash-gallery-nav"),
		activePagerClass : "current",
		pagerAnchorBuilder : function() {return "<li><a href=\"#\"></a></li>";},
		pagerClick : function() {$('#splash-gallery div').cycle('pause');}
	});
	/* ********************************** */
	
	// HOME SLIDER
	$("#home-slider ul").wrap("<div>");
	$("#home-slider nav #home-slider-prev").click(function(e) {
		e.preventDefault();	
		if(home_slider_index > 0) {
			home_slider_index--;
			$("#home-slider div ul").animate({
				"margin-left" : "+=220px"
			},500);
		}	
	});
	
	$("#home-slider nav #home-slider-next").click(function(e) {
		e.preventDefault();	
		if(home_slider_index <= $("#home-slider div ul").size()) {
			home_slider_index++;
			$("#home-slider div ul").animate({
				"margin-left" : "-=220px"
			},500);			
		}
	});	
	
	if($("#home-slider div ul li").size() <=4) $("#home-slider nav").remove();	
	/* ********************************** */
	
	var codice_prodotto = $("#codiceprodotto").val();
	var leasing = parseInt($("#leasing_list > li").size(),10) - 1;
	var indice = parseInt($("#leasing_list > li > a[rel="+codice_prodotto+"]").parent().index(),10);
	var link_next = indice==leasing ? 0 : indice + 1;
	var link_prev = indice==0 ? leasing : indice - 1;
	
	$("#contents-prev").attr("href",$("#leasing_list > li").eq(link_prev).find("a").attr("href"));
	$("#contents-next").attr("href",$("#leasing_list > li").eq(link_next).find("a").attr("href"));
	
}); 
