//START READY FUNCTION
$(document).ready(function(){

	var queryFooter = "<Query><Where><Eq><FieldRef Name='ContentCategory'/><Value Type='Text'>Footer</Value></Eq></Where></Query>";
	var queryHeader = "<Query><Where><Eq><FieldRef Name='ContentCategory'/><Value Type='Text'>Header</Value></Eq></Where></Query>";
	var queryFooterEn = "<Query><Where><Eq><FieldRef Name='ContentCategory'/><Value Type='Text'>Footer EN</Value></Eq></Where></Query>";
	var queryFooterPl = "<Query><Where><Eq><FieldRef Name='ContentCategory'/><Value Type='Text'>Footer PL</Value></Eq></Where></Query>";
	var queryFooterDeu = "<Query><Where><Eq><FieldRef Name='ContentCategory'/><Value Type='Text'>Footer Deu</Value></Eq></Where></Query>";


	$().SPServices({
	  operation: "GetListItems",
	  async: false,
	  webURL: "/",
	  listName: "Reusable Content",
	  CAMLQuery: queryFooter,
	  CAMLViewFields: "<ViewFields><FieldRef Name='ReusableHtml' /></ViewFields>",		  
	  completefunc: function (xData, Status) {
	    $(xData.responseXML).find("[nodeName='z:row']").each(function() {
	      var liHtml = "" + $(this).attr("ows_ReusableHtml") +"";
	      $(".ft_content").append(liHtml);
	    });
	  }
	});

	$().SPServices({
	  operation: "GetListItems",
	  async: false,
	  webURL: "/",
	  listName: "Reusable Content",
	  CAMLQuery: queryFooterEn,
	  CAMLViewFields: "<ViewFields><FieldRef Name='ReusableHtml' /></ViewFields>",		  
	  completefunc: function (xData, Status) {
	    $(xData.responseXML).find("[nodeName='z:row']").each(function() {
	      var liHtml = "" + $(this).attr("ows_ReusableHtml") +"";
	      $(".ft_contentEn").append(liHtml);
	    });
	  }
	});

	$().SPServices({
	  operation: "GetListItems",
	  async: false,
	  webURL: "/",
	  listName: "Reusable Content",
	  CAMLQuery: queryFooterPl,
	  CAMLViewFields: "<ViewFields><FieldRef Name='ReusableHtml' /></ViewFields>",		  
	  completefunc: function (xData, Status) {
	    $(xData.responseXML).find("[nodeName='z:row']").each(function() {
	      var liHtml = "" + $(this).attr("ows_ReusableHtml") +"";
	      $(".ft_contentPl").append(liHtml);
	    });
	  }
	});
	
	$().SPServices({
	  operation: "GetListItems",
	  async: false,
	  webURL: "/",
	  listName: "Reusable Content",
	  CAMLQuery: queryFooterDeu,
	  CAMLViewFields: "<ViewFields><FieldRef Name='ReusableHtml' /></ViewFields>",		  
	  completefunc: function (xData, Status) {
	    $(xData.responseXML).find("[nodeName='z:row']").each(function() {
	      var liHtml = "" + $(this).attr("ows_ReusableHtml") +"";
	      $(".ft_contentDeu").append(liHtml);
	    });
	  }
	});
	

	
	$().SPServices({
	  operation: "GetListItems",
	  async: false,
	  webURL: "/",
	  listName: "Reusable Content",
	  CAMLQuery: queryHeader,
	  CAMLViewFields: "<ViewFields><FieldRef Name='ReusableHtml' /></ViewFields>",		  
	  completefunc: function (xData, Status) {
	    $(xData.responseXML).find("[nodeName='z:row']").each(function() {
	      var liHtml = "" + $(this).attr("ows_ReusableHtml") +"";
	      $(".linkSocial").append(liHtml);
	    });
	  }
	});
	
	
	/*Gestione ticker*/
	
	var location = window.location.href.toLowerCase();
	var urlTicker;
	if (location.indexOf('/it/')!= -1) {
		urlTicker = '/ticker_IT.aspx';
	}
	else {
		urlTicker = '/ticker_EN.aspx';	
	}
	var loadTicker = urlTicker + ' #ticker01';

	$('.tickers').load(loadTicker, function() 
		{	
			$("#ticker01").liScroll();  	 
		}
	);

	/*Nascondi voci orfane menu*/
	
	$('span.menu-item-text').each(function() {
		if (jQuery.trim ($(this).text()) == "") $(this).parent().parent().css('display','none');
	});	
	
	/*Modifica Box Contatti*/
	$('div.contatti').find('a[title="CONTATTI"]').each(function() {
		var location = window.location.href.toLowerCase();
		if (location.indexOf('/en/')!= -1) {
					$(this).replaceWith('<a title="CONTACTS" href="#">CONTACTS</a>');
		}
	});	
		
		

	 	
	//function TICKER ORIZZONTALE
	jQuery.fn.liScroll = function(settings) {
			settings = jQuery.extend({
			travelocity: 0.03
			}, settings);		
			return this.each(function(){
					var $strip = jQuery(this);
					$strip.addClass("newsticker")
					var stripWidth = 0;
					var $mask = $strip.wrap("<div class='mask'></div>");
					var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
					var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
					$strip.find("li").each(function(i){
					stripWidth += jQuery(this, i).outerWidth(true); // thanks to Michael Haszprunar
					});
					$strip.width(stripWidth);			
					var totalTravel = stripWidth+containerWidth;
					var defTiming = totalTravel/settings.travelocity;	// thanks to Scott Waye		
					function scrollnews(spazio, tempo){
					$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
					}
					scrollnews(totalTravel, defTiming);				
					$strip.hover(function(){
					jQuery(this).stop();
					},
					function(){
					var offset = jQuery(this).offset();
					var residualSpace = offset.left + stripWidth;
					var residualTime = residualSpace/settings.travelocity;
					scrollnews(residualSpace, residualTime);
					});			
			});	
	};

	
	

	//scelta lingue
  $('.piu').click(function() {
						   
		if($(this).hasClass('meno')) $(this).removeClass('meno')			
		else  $(this).addClass('meno')
						   
    var $marginLefty = $('.inner');
    $marginLefty.animate({marginLeft: parseInt($marginLefty.css('marginLeft'),10) == 0 ? $marginLefty.outerWidth() : 0});
  });
  
	$("ul.dis").quickPager({
	pageSize:10
	});	
	
  
 /* INIZIO AGGIORNAMENTO 2 marzo 2011*/
  // MENU istituzionale header	
	if($(".mn_03")) {
	
		$(".mn_03 ul ul > li").addClass('due');
		$(".mn_03 ul ul ul > li").addClass('tre').removeClass('due');
		$(".mn_03 ul ul ul ul > li").addClass('quattro').removeClass('tre due');
		$(".mn_03 ul ul ul ul span").css('padding-left', '6px');
		
		var icona = '<div style="width:25px; height:26px; position:absolute; cursor:pointer; margin-left:180px; text-align:center; font-size:16px; font-weight:500; color:#FFFFFF; padding-left:6px;" "title="espandi" class="icona">+</div>';
		
		if ($(".mn_03 ul li ul > li a").next('ul')){
			 $(".mn_03 ul li ul > li a").next('ul').parent('li').prepend(icona); 
		}
		
var timeout = true;
	var mouseIn = false;
	
	  // INIZIO MENU apertura due livelli	
			$('.mn_03 li').children('.icona').click(function(ev) {
					mouseIn = true;
					//alert (mouseIn);
					//if ($(this).children('ul').length > 0){

						//alert ($(this).next().next().html());
						
						/*APERTURA SECONDO LIVELLO*/
						if ( $(this).parent().hasClass('due')){
							
							
							
							if (timeout == true) {
								if ( $(this).next().next().is(':visible')){
								return false;
								}else{
									$('.due').children('ul').slideUp(1000);
									
									$('.due').children('.icona').fadeIn(500);
									$(this).next().next().slideDown(1000);	
									$(this).fadeOut(500);	
									
									
									setTimeout(function(){
											   timeout = true;
										}, 800);
										timeout = false;
								}
									
							} else {
								   ev.preventDefault();
							}		
						}			  
						/*APERTURA SECONDO LIVELLO*/
						if ( $(this).parent().hasClass('tre')){
							if (timeout == true) {
								if ( $(this).next().next().is(':visible')){
								return false;
								}else{
									$('.tre').children('ul').slideUp(1000);
									
									$('.tre').children('.icona').fadeIn(500);
									$(this).next().next().slideDown(1000);	
									$(this).fadeOut(500);
									setTimeout(function(){
											   timeout = true;
										}, 800);
										timeout = false;
								}
									
							} else {
								   ev.preventDefault();
							}		
						}	
						
						
			  		//}
			});
	 // FINE MENU apertura due livelli
		
		function apertura() {	
		if ($(".mn_03 li").hasClass('selected')){
			if ($(".mn_03 .selected").hasClass('quattro')){
				$(".mn_03 .selected").parent().parent().parent().css({display:'block'});
				$(".mn_03 .selected").parent().parent().css({display:'block'});
				$(".mn_03 .selected").parent().parent().children('.icona').fadeOut(1);
				$(".mn_03 .selected").parent().parent().parent().parent().children('.icona').fadeOut(1);
				$(".mn_03 .selected").css({background:'#2b2b2b'});
			}else{
				$(".mn_03 .selected").parent().parent().css({display:'block'});
				$(".mn_03 .selected").children('ul').css({display:'block'});
				$(".mn_03 .selected").css({background:'#2b2b2b'});
				$(".mn_03 .selected").children('.icona').fadeOut(1);
			}
		}
	}

	apertura();
		
	
	}
	/*fine IF*/
	  /* FINE AGGIORNAMENTO 2 marzo 2011*/		  
 
	
	// ACCORDION
	$('#accordion div').css('display', 'none');	
	$('#accordion div').eq(0).css('display', 'block');
	$('#accordion a').eq(0).addClass('selected');
	
	$("#accordion a").click(function() {
		if ($(this).next().is(':visible')){							 
			return false;
		}else{
			$('#accordion a').removeClass('selected');
			$(this).addClass('selected');
			$('#accordion div').slideUp(700);
			$(this).next().slideDown(700);
		}
	
	});
	
	// ACCORDION PRODOTTO
	$('#accordion_prodotto div.FAQ').css('display', 'none');	
	$('#accordion_prodotto div.FAQ').eq(0).css('display', 'block');
	$('#accordion_prodotto a').eq(0).addClass('selected');
	
	$("#accordion_prodotto a").click(function() {
		if ($(this).next().is(':visible')){							 
			return false;
		}else{
			$('#accordion_prodotto a').removeClass('selected');
			$(this).addClass('selected');
			$('#accordion_prodotto div.FAQ').slideUp(700);
			$(this).next().slideDown(700);
		}
	
	});
	
	// FOOTER box company info	
	$("#info_lk").click(function() {
			if ($("#info").is(":hidden")) {
			  	$(this).addClass('hover');
				$("#info").slideToggle(500);
			}else{
				$(this).removeClass('hover');
				$("#info").slideToggle(500);
			}
	});

	$("#info .chiudi").click(function() {
			$('#info_lk').removeClass('hover');
			$("#info").slideToggle(500);
	});
	
	/*Gestione continenti in inglese*/
	$('div.europa').find('div.balloon').each(function() {
		var location = window.location.href.toLowerCase();
		if (location.indexOf('/en/')!= -1) {
				$(this).replaceWith('<div class="balloon"><span>EUROPE</span></div>');
		}
	});
	
	$('div.nord_america').find('div.balloon').each(function() {
		var location = window.location.href.toLowerCase();
		if (location.indexOf('/en/')!= -1) {
				$(this).replaceWith('<div class="balloon"><span>NORTH AMERICA</span></div>');
		}
	});	
	
	$('div.latina').find('div.balloon').each(function() {
		var location = window.location.href.toLowerCase();
		if (location.indexOf('/en/')!= -1) {
				$(this).replaceWith('<div class="balloon"><span>LATIN AMERICA</span></div>');
		}
	});	
		
	
	// DISTRIBUTORI mappa
	 //
	 $('table.paesi').css('display', 'none');
	 $('div.distributori').css('display', 'none');
	
 
 	$('#mappa div').mouseover(function() {	   						   
									   
		 var title = $('#mappa div').attr("title");
		 var title_2 = $('.paesi').attr('title');
		   
		    if (title!= title_2){ 
				//$(this).hide();
			}else {	
				$(this).next('.over').hide();
				$(this).find('.balloon').hide();
			}					  
			
			//$('#mappa div').find('.over').hide();
			$(this).next('.over').fadeIn('slow');
			$(this).find('.balloon').fadeIn('slow');	
	});
	
	$('#mappa div').click(function() {
			var title = $(this).attr("title");
			// Mostra stati del continente.
			var selectorOn = 'table.paesi[title=' + title + ']';
			var selectorOff = 'table.paesi[title!=' + title + ']';
			
			$(selectorOn).css('display', 'block'); 
			$(selectorOff).css('display', 'none'); 
			
			$(selectorOn).addClass('acceso'); 
			$(selectorOff).removeClass('acceso');
			
			// Nascondi distributori.
			$('div.distributori').css('display','none');
			
			
			$('#mappa div').find('.over').hide();
			$(this).find('.over').show();
			
		    $('html, body').animate({scrollTop: '360px'}, 600);		
 	 });
	  
	  
	  $('a.paesi').click(function() {
			$('a.paesi').removeClass('active');						   
			$(this).addClass('active');					   
			
			// Mostra stati del continente.
			var stato = $(this).attr('title');
			var selectorOn = 'div.distributori[title=' + stato + ']';
			var selectorOff = 'div.distributori[title!=' + stato + ']';
			$(selectorOn).css('display', 'block'); 
			$(selectorOff).css('display', 'none'); 

			var documentHeight = $(document).height();
			var scrollBottom = documentHeight + 'px'
			//alert (scrollBottom);
			
			$('html, body').animate({scrollTop:  scrollBottom }, 1500);
 	 });


	 $('.area').mouseout(function() {
		var title = $(this).parent().attr("title");	
		var title_2 = $('.acceso').attr("title"); 
		 if ($('.paesi').hasClass('acceso') ){
			if (title != title_2){
					$(this).next('.over').fadeOut('fast');
					$(this).find('.balloon').fadeOut('fast'); 
				}else{
					return true;
				}	
			
		 }else{
			$(this).next('.over').fadeOut('fast');
			$(this).find('.balloon').fadeOut('fast'); 
		}		

			
  	 });
  	 
	
/* inizio 1 AGGIORNAMENTO 3 febbraio 2011*/	
	// FOOTER mappa sito
		
	$('#mappa_lk').click(function() {
		if ($(this).hasClass('hover')){
			$(this).removeClass('hover');
			
				var layerHeight = $('#ft_mappa').height();
				var scrollSu = '-' + layerHeight + 'px';
				$('#ft_mappa').slideUp(2000);
				 $('html, body').animate({scrollTop:  scrollSu }, 4300);
		}else{
			$(this).addClass('hover');	
						var documentHeight = $(document).height();
						var scrollBottom = documentHeight -80 + 'px';
						//alert (scrollBottom);
					if ($('.map').length > 0 ){	
						//$('html, body').animate({scrollTop:  scrollBottom }, 3000);	
						$('#ft_mappa').slideDown(2000);
					}else{
						var location = window.location.href.toLowerCase();
						//var substring = location.substring(21);
						var urlMappa;
						if (location.indexOf('/it/')!= -1) {
							urlMappa = '/MappaSito_IT.aspx';
						}
						if (location.indexOf('/pl/')!= -1) {
							urlMappa = '/MappaSito_PL.aspx';
						}	
						if (location.indexOf('/en/')!= -1) {
							urlMappa = '/MappaSito_EN.aspx';
						}	
						if (location.indexOf('/deu/')!= -1) {
							urlMappa = '/MappaSito_Deu.aspx';
						}																						
						$('#ft_mappa').load(urlMappa, function() {
						 	$('#ft_mappa').slideDown(2000);
							
								window.setTimeout(function() {
								}, 500);
						});

					
						
					
						$("head").append("<link>");
						css = $("head").children(":last");
						css.attr({
						  rel:  "stylesheet",
						  type: "text/css",
						  href: "/SiteAssets/Brembo/css/mappa.css"
						});
					}
					 $('html, body').animate({scrollTop:  scrollBottom }, 2000);
					
			}
 	 	});
 	 	
/* fine 1 AGGIORNAMENTO 3 febbraio 2011*/	
	});	
//END READY FUNCTION

//SharePoint dialog

function OpenDialog(formUrl) 
		{ 
	        var options = SP.UI.$create_DialogOptions();	
	        options.url = formUrl;
	        SP.UI.ModalDialog.showModalDialog(options);        
	    }
		function OpenDialog(formUrl,width,height,title) 
		{ 
	        var options = SP.UI.$create_DialogOptions();	
	        options.url = formUrl;
	        options.width = width;
	        options.height = height;
	        options.title = title;
	        SP.UI.ModalDialog.showModalDialog(options);        
	    }
