$(document).ready(function() {
						   
	$("#nav li div.mega.open").css("top","-9999px");
	$("#nav li").removeClass("active");
	
	//allineamento altezza dei box
	   function equalHeight(group) {
		   tallest = 0;
		   group.each(function() {
			  thisHeight = $(this).height();
			  if(thisHeight > tallest) {
				 tallest = thisHeight;
			  }
		   });
		   group.height(tallest);
		}
		   equalHeight($('.item .table_box'));
	
	//Main Nav Prodotti: Switch dei link
	$('#clickTavola').click(function() {
		CreateSlider('scrollPaneTavola', '#PB_tavola');
		$(".active").removeClass("active");
		$('.PBoff').removeClass("PBon");
		$('#PB_tavola').addClass("PBon");		
		$(this).addClass("active");		
	});
	
	$('#clickRistorante').click(function() {
		CreateSlider('scrollPaneRistorante', '#PB_ristorante');
		$(".active").removeClass("active");
		$('.PBoff').removeClass("PBon");
		$('#PB_ristorante').addClass("PBon");		
		$(this).addClass("active");		
	});
	
	$('#clickEuropa').click(function() {
		$(".active").removeClass("active");
		$('.PBoff').removeClass("PBon");
		$('#PB_europa').addClass("PBon");
		$(this).addClass("active");
		
	});
	
	$('#clickMondo').click(function() {
		$(".active").removeClass("active");
		$('.PBoff').removeClass("PBon");
		$('#PB_mondo').addClass("PBon");
		$(this).addClass("active");
		
	});
	
	
	
	// Inizializzazione Scrollable e Centratura Navigator
	$("#browsable").scrollable().circular().navigator();
		
	var navigatorWidth = $(".navi").width() + $(".prevPage").width() + $(".nextPage").width()  ;
	$("#navigator").css('width', navigatorWidth+24); /* 24 is the margin (6+6+6+6)	*/	
	
	
	// Chiudi Mega
	$(".closeMega").click(function() {
		
		$(this).hide();
		$("#nav li.active div.mega").hide();
		$("#nav li.active").removeClass("active");
		Cufon.replace($("ul#nav h2"), {color: '#024a2b'})
	});

	//TEST
	CreateSlider('scrollPaneTavola', '#PB_tavola');
	
	/*
	$(function(PBtavolaWidth){
		var sliderWidth=0;
		$("#PB_tavola .scroll-content-item").each(function() {
			sliderWidth+=$(this).width();
		});
		$("#PB_tavola .scroll-content").css('width', sliderWidth);
		//alert("1 " + sliderWidth);
	});	
	$(function(PBRistoranteWidth){
		var sliderWidth=0;
		$("#PB_ristorante .scroll-content-item").each(function() {
			sliderWidth+=$(this).width();
		});
		//alert("2 "+sliderWidth);
		$("#PB_ristorante .scroll-content").css('width', sliderWidth);
	});
	*/
	
	//Jquery UI slider
	function CreateSlider(objectId, contentLayerId) 
	{
		//Calculate slider length
		var sliderWidth=0;
		$(contentLayerId+" .scroll-content-item").each(function() {
			sliderWidth+=$(this).width();
		});
		$(contentLayerId+" .scroll-content").css('width', sliderWidth);		

		
		//scrollpane parts
		var scrollPane = $('#'+objectId);
		var scrollContent = $('.scroll-content');
		$('.scroll-content').css('margin-left',0);
		var widthScrollPaneChild = scrollPane.children(0).width();
		
		//build slider
		var scrollbar = $("#"+objectId+"_scr").slider({
			slide:function(e, ui){
				if (widthScrollPaneChild > scrollPane.width())
				{ 
					scrollContent.css('margin-left', Math.round( ui.value / 100 * ( scrollPane.width() - widthScrollPaneChild )) + 'px'); 
				}
				else 
				{ 
					scrollContent.css('margin-left', 0); 
				}
			}
		});
		
		//Unwrap fix
		if (scrollbar.find('.ui-slider-handle').parent().attr("class")=="ui-handle-helper-parent")
			scrollbar.find('.ui-slider-handle').unwrap();
		
		//append icon to handle
		var handleHelper = scrollbar.find('.ui-slider-handle')
		.mousedown(function(){
			//	scrollbar.width( handleHelper.width() );
		})
		.mouseup(function(){
			scrollbar.width( '100%' );
		})
		//.append('<span class="ui-icon ui-icon-grip-dotted-vertical"></span>')
		.wrap('<div class="ui-handle-helper-parent"></div>').parent();
		
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css('overflow','hidden');
		
		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar()
		{
			var remainder = scrollContent.width() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = scrollPane.width() - (proportion * scrollPane.width());
			scrollbar.find('.ui-slider-handle').css({
				width: handleSize,
				'margin-left': -handleSize/2
			});
			handleHelper.width('').width( scrollbar.width() - handleSize);
		}
		
		//reset slider value based on scroll content position
		function resetValue(){
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
			var percentage = Math.round(leftVal / remainder * 100);
			scrollbar.slider("value", percentage);
		}
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent(){
				var showing = scrollContent.width() + parseInt( scrollContent.css('margin-left') );
				var gap = scrollPane.width() - showing;
				if(gap > 0){
					scrollContent.css('margin-left', parseInt( scrollContent.css('margin-left') ) + gap);
				}
		}
		
		//change handle position on window resize
		$(window)
		.resize(function(){
				resetValue();
				sizeScrollbar();
				reflowContent();
		});
		//init scrollbar size
		//setTimeout(sizeScrollbar,10);//safari wants a timeout
	};
	
});

////////// SHARE PER FB E TWITTER /////////
	
function ShareUrl(tipo, linkStatus)
{
	var myActualUrl = document.location.href;
	//myActualUrl = 'http://www.devguru.com/technologies/javascript/10822.asp';
	var myFinalUrl = '';
	var myTitle = document.title;
	var myText='Carapelli - ' + linkStatus;
	switch (tipo.toLowerCase())
	{
		case 'facebook' :
			myFinalUrl = 'http://www.facebook.com/sharer.php?u=' + myActualUrl + '&t=' + myText;
			break;
		case 'twitter' :
			myFinalUrl = 'http://twitter.com/?status=' + linkStatus + ' ' +  myActualUrl
			break;
	}
	//alert(myFinalUrl);
	//document.location.href = myFinalUrl;
	Popup(myFinalUrl,'scrollbars=1, location=1, status=1, resizable=1, toolbar=1, menubar=1');
}

function ShareOtherUrl(tipo, linkStatus ,myLink )
{
	//var myActualUrl = document.location.href;
	//myActualUrl = 'http://www.devguru.com/technologies/javascript/10822.asp';
	var myFinalUrl = '';
	var myTitle = document.title;
	var myText='Carapelli - ' + linkStatus;
	switch (tipo.toLowerCase())
	{
		case 'facebook' :
			myFinalUrl = 'http://www.facebook.com/sharer.php?u=' + myLink + '&t=' + myText;
			break;
		case 'twitter' :
			myFinalUrl = 'http://twitter.com/?status=' + linkStatus + ' ' +  myLink
			break;
	}
	//alert(myFinalUrl);
	//document.location.href = myFinalUrl;
	Popup(myFinalUrl,'scrollbars=1, location=1, status=1, resizable=1, toolbar=1, menubar=1');
}
function Popup(url, stile) 
{
	window.open(url, "popup", stile);
}

function ShowElement()
{
	//alert("mostro");
	$("ul#nav li").css("display","block");
	$("#boxHome").css("display","block");
	$("ul#nav li div.mega").css("top","-9945px");
	
	
}

function HideElement()
{
	//alert("nascondo");
	$("ul#nav li").css("display","none");
	$("#boxHome").css("display","none");
	
}