$(document).ready(function () {
	
	$("a[href=#]").click(function(e){e.preventDefault();});
	
    $('.product, #productThumbnails').localScroll({ 
        duration: 600 
    });

	if($.browser.msie && (parseInt($.browser.version, 10) < 8)){

		$('.extraNav li:first-child').addClass("first-child");
		$('.extraNav li:last-child').addClass("last-child");
		$('.subNav li:last-child').addClass("last-child");
		
		$('.media a').css('opacity', '0.6');
		$('.media a').hover(function(){
			$(this).css('opacity', '1'); 
		}, function () {
			$(this).css('opacity', '0.6'); 			
		});
		
		$('#search button').hover(function () {
				$(this).addClass("btnHover");		
			}, function () {
				$(this).removeClass("btnHover");			
		});

		$('#loginForm button').mouseover(function () {
			$(this).addClass("btnHover");		
			}).mouseout(function () {
			$(this).removeClass("btnHover");			
		});	

	}; 			

	var pn = $('#productThumbnails li').size();
    if(pn > 16) {
	   $('#productList').css("padding", "0 24px")	
	   $('<a href="#" class="prev_product">Precedente</a>').insertBefore("#productList h3.productName");
	   $('<a href="#" class="next_product">Successivo</a>').insertAfter("#productThumbnails");

	   $("#productList a.next_product").bind("click", function(e){
	       e.preventDefault();
	       $("#productThumbnails").scrollTo('+=34px', 300 );
	   });
	
	   $("#productList a.prev_product").bind("click", function(e){
	       e.preventDefault();
	       $("#productThumbnails").scrollTo('-=34px', 300 );
	   });
	};

	$(function() {
	   $('a.lightbox').lightBox({
			overlayBgColor: '#ffffff',
			imageLoading: contextPath+"/css/images/lightbox-ico-loading.gif",
			imageBtnClose: contextPath+"/css/images/lightbox-btn-close.gif",
			imageBtnPrev: contextPath+"/css/images/lightbox-btn-prev.gif",
			imageBtnNext: contextPath+"/css/images/lightbox-btn-next.gif"
	   });
	}); 
	
	$(function() {
	   $('a.lightbox2').lightBox({
			overlayBgColor: '#ffffff',
			imageLoading: contextPath+"/css/images/lightbox-ico-loading.gif",
			imageBtnClose: contextPath+"/css/images/lightbox-btn-close.gif",
			imageBtnPrev: contextPath+"/css/images/lightbox-btn-prev.gif",
			imageBtnNext: contextPath+"/css/images/lightbox-btn-next.gif"
	   });
	}); 
	
	$(function() {
		
		var msie6 = $.browser == 'msie' && $.browser.version < 7; 
		
		if(!msie6) {
		  	var top = $('#nav').offset().top - parseFloat($('#nav').css('marginTop').replace(/auto/, 0));
			$(window).scroll(function (event) {
			  // what the y position of the scroll is
			  var y = $(this).scrollTop();

			  // whether that's below the form
			  if (y >= top) {
			    // if so, ad the fixed class
			    $('#nav').addClass('fixed');
			  } else {
			    // otherwise remove it
			    $('#nav').removeClass('fixed');
			  }
			});
		 }
	});

	$(function() {
		
		var msie6 = $.browser == 'msie' && $.browser.version < 7; 
		
		if(!msie6) {			
			var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/, 0));
			$(window).scroll(function (event) {
			  // what the y position of the scroll is
			  var y = $(this).scrollTop();

			  // whether that's below the form
			  if (y >= top) {
			    // if so, ad the fixed class
			    $('#sidebar').addClass('fixed');
			  } else {
			    // otherwise remove it
			    $('#sidebar').removeClass('fixed');
			  }
			});
		 }
	});

	if($("#galleryThumbs #thumbs *").size()>0){
		$("#galleryThumbs").scrollable({
			clickable: false,
			items: '#thumbs',
			size: 1,
			speed: 400
		}); 
	}else{
		$("#galleryNav").hide();
	}
	
	$("#thumbs a").click(function(e) {
		e.preventDefault();
		var wrap = $("#productImage").css('opacity', 0);
		var url = $(this).attr("href");
		var img = new Image();
	
		img.onload = function() {
			wrap.fadeTo("slow", 1);
			wrap.find("img").attr("src", url);
		};
	
		img.src = url;
	
	}).filter(":first").click();
	
	
	// Gestisce il sottomenu

	var secondLevel = $('.secondLev').parent().find('a');
	if ($('.secondLev').length > 0 ) $(secondLevel).not($('.secondLev li a')).addClass('piu');

	var thirdLevel = $('.thirdLev').parent().find('a');
	if ($('.thirdLev').length > 0 ) $(thirdLevel).not($('.thirdLev li a')).addClass('piu');

	function open_close_submenu (e) {
		e.preventDefault;
		var submenu = $(this).siblings(); 
		if ($(submenu).hasClass('open') == false) {
			$(submenu).addClass('open');
			$(this).removeClass('piu').addClass('meno');
		} else {
			$(submenu).removeClass('open');	
			$(this).removeClass('meno').addClass('piu');	
		}
	}
	
	$(secondLevel).not($('.secondLev li a')).bind('click', open_close_submenu);

	$(thirdLevel).not($('.thirdLev li a')).bind('click', open_close_submenu);	
	
	$(".secondLev a.active").parents("li").each(function(){
		$(this).find("a.piu").click();
	});
	
	$(".thirdLev a.active").parents("li").each(function(){
		$(this).find("a.piu").click();
	});

}); 

$(function(){
	$.fn.supersized.options = {  
		startwidth: 640,  
		startheight: 480,
		vertical_center: 0,
		slideshow: 1,
		navigation: 0,
		transition: 1, // 0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
		pause_hover: 0,
		slide_counter: 1,
		slide_captions: 1,
		slide_interval: 300  
	}; 
    $('#supersize').supersized(); 
});