/**
 * @author batman
 */

	var $jq = jQuery.noConflict();
	jQuery.fn.exists = function(){return jQuery(this).length>0;}
	
	$jq.exists = function(selector) {return ($(selector).length > 0);}
	
	$jq(document).ready(
		function() {

			// caroselli
		  $jq("#content").jCarouselLite({
		      btnNext: "#right_arrow",
		      btnPrev: "#left_arrow",
		      easing: "easeout",
					visible: 1,
		      speed: 800
		  });
		
		  $jq("#content2").jCarouselLite({
		    btnNext: "#right_arrow2",
		    btnPrev: "#left_arrow2",
		    easing: "easeout",
					visible: 1,
		    speed: 800
		  });
		
		  $jq("#technical_scroll").jCarouselLite({
		    btnNext: "#right_arrow_tech",
		    btnPrev: "#left_arrow_tech",
		    easing: "easeout",
				visible: 1,
		    speed: 1000
		  });

			$jq(".gr_wallpaper").each(
			
				function() {

/*					var slider = $jq(this).find(".gr_downloadable_slider div:first");*/
					var slider = $jq(this).find("ul").parent().parent();
					var quanti = slider.find('li').size();
					$jq(this).next(".gr_downloadables_slider_dati").find(".gr_contatore").text(quanti);

					if(quanti>1) {
						var sinistra = $jq(this).find("a:first");
						var destra = $jq(this).find("a:last");
					}
					else {
						$jq(this).find("a:first").click(
							function() {
								return false;
							}
						);
						$jq(this).find("a:last").click(
							function() {
								return false;
							}
						);
					}
					
					//alert(sinistra.html());

				  slider.jCarouselLite({
				    btnNext: destra,
				    btnPrev: sinistra,
				    easing: "easeout",
				    speed: 1000,
						visible: 1,
				    afterEnd: function(a,b) {
						$jq(a).parent().parent().parent().parent().next(".gr_downloadables_slider_dati").find(".gr_contatorefirst").text( b );
				    }
				  });

				}			

			);
			

			$jq(".gr_handler").click(
				function() {
					$jq(".gr_handler").not(this).next(".gr_pulsantone_links :visible").slideUp();
					$jq(this).next(".gr_pulsantone_links:hidden").slideDown();

					$jq(".gr_handler").removeClass("gr_pulsantone_sel");
					$jq(this).addClass("gr_pulsantone_sel");

					return false;
				}
			);
			
			
			// tabs
			$jq('.gr_tab_mesi a').click(
				function() {
					var theindex = $jq('.gr_tab_mesi a').index(this);
					$jq('#gr_box_fotografi .gr_un_fotografo:visible').hide();
					$jq('.gr_tab_mesi a').removeClass('gr_tabs_sel');
					$jq('#gr_box_fotografi .gr_un_fotografo').eq(theindex).show();
					$jq(this).addClass('gr_tabs_sel');
					return false;
				}
			);
/*
			$jq('.gr_tab_equipment a').click(
				function() {
					var theindex = $jq('.gr_tab_equipment a').index(this);
					$jq('#gr_box_equipment .gr_un_equipment:visible').hide();
					$jq('.gr_tab_equipment a').removeClass('gr_tabs_sel');
					$jq('#gr_box_equipment .gr_un_equipment').eq(theindex).show();
					$jq(this).addClass('gr_tabs_sel');
					return false;
				}
			);
*/

			// lightbox
			if( typeof jQuery.fn.lightBox == 'function' ) {
				
		  	$jq('.gr_mylightbox a, .gr_mylightbox').each(
					function() {
						$jq(this).lightBox(
							{
						  		fixedNavigation: true,
						  		imageLoading: '/jsp/jahia/templates/gitzo/corporate_portal_templates_v2/custom/static/include/jquery/jquery.lightbox.js/images/lightbox-ico-loading.gif',
						  		imageBtnClose: '/jsp/jahia/templates/gitzo/corporate_portal_templates_v2/custom/static/include/jquery/jquery.lightbox.js/images/lightbox-btn-close.gif',
						  		imageBtnNext: '/jsp/jahia/templates/gitzo/corporate_portal_templates_v2/custom/static/include/jquery/jquery.lightbox.js/images/lightbox-btn-next.gif',
						  		imageBtnPrev: '/jsp/jahia/templates/gitzo/corporate_portal_templates_v2/custom/static/include/jquery/jquery.lightbox.js/images/lightbox-btn-prev.gif',
						  		imageBlank: '/jsp/jahia/templates/gitzo/corporate_portal_templates_v2/custom/static/include/jquery/jquery.lightbox.js/images/lightbox-blank.gif'
						
							}
						);						
					}
				);

		  }

			// slider
			/* ridimensiona slider */
			$jq('.gr_products_box_wrapper').each (
				function() {
			  	var ul =$jq(this).find('ul:first');

					singlewidth = ul.find('li').outerWidth();
					quantielementi = ul.find('li').size();
					ul.width(singlewidth*quantielementi);

			  	
			  	var itemsWidthss = ul.innerWidth() - $jq(this).outerWidth();
					/*alert(singlewidth + ' ' + quantielementi + ' ' + itemsWidthss);*/
					
					$jq(this).find('.slider:first').slider({
						minValue: 0,
						maxValue: itemsWidthss,
						handle: '.handle',
						stop: function(ev, ui){
							ul.animate({
								'left': ui.value * -1
							}, 500);
						},
						slide: function(event, ui){
							ul.css('left', ui.value * -1);
						}
					});

				}
			);

/*
			$jq('.gr_myslider').each (
				function() {
					singlewidth = $jq(this).find('li').outerWidth();
					quantielementi = $jq(this).find('li').size();
					$jq(this).width(singlewidth*quantielementi);
				}
			);

		  var container1 = $jq('#giggi');
			if (container1.length > 0) {
		  	var ul1 = $jq('ul', container1);
		  	
		  	itemsWidthss = ul1.innerWidth() - container1.outerWidth();
				
				$jq('.slider', container1).slider({
					minValue: 0,
					maxValue: itemsWidthss,
					handle: '.handle',
					stop: function(event, ui){
						ul1.animate({
							'left': ui.value * -1
						}, 500);
					},
					slide: function(event, ui){
						ul1.css('left', ui.value * -1);
					}
				});
			}

		  var container2 = $jq('#pappa');
			if(container2.length>0) {
			  var ul2 = $jq('ul', container2);
			  
			  var itemsWidthzz = ul2.innerWidth() - container2.outerWidth();
			
				$jq('.slider', container2).slider({
			      minValue: 0,
			      maxValue: itemsWidthzz,
			      handle: '.handle2',
			      stop: function (event, ui) {
			          ul2.animate({'left' : ui.value * -1}, 500);
			      },
			      slide: function (event, ui) {
			          ul2.css('left', ui.value * -1);
			      }
			  });
			}
*/


			$jq(".gr_family_description a").click(
				function() {
					var display = $jq("#gr_family_description_box").css("display");
					if ( display == 'none' ) $jq(".gr_family_description a img").attr("src","/jsp/jahia/templates/gitzo/corporate_portal_templates_v2/custom/static/img_all/frecce_giu_su.png");
					else $jq(".gr_family_description a img").attr("src","/jsp/jahia/templates/gitzo/corporate_portal_templates_v2/custom/static/img_all/frecce_giu.png");
					$jq("#gr_family_description_box").slideToggle(1500);
				}
			);

			$jq(".gr_pulsantone a.gigi").click( 
				function() {
					var display = $jq(this).next(".gr_pulsantone_links").css("display");
					if ( display == 'none' ) $jq(this).find(".fr_tab").attr("src","/jsp/jahia/templates/gitzo/corporate_portal_templates_v2/custom/static/img_all/frecce_su.png");
					else $jq(this).find(".fr_tab").attr("src","/jsp/jahia/templates/gitzo/corporate_portal_templates_v2/custom/static/img_all/frecce_sotto.png");
					$jq(this).next(".gr_pulsantone_links").slideToggle("slow");
				}
			);
			
			$jq("#gr_tasca_linguetta a").click( 
				function() {
				
					var top = $jq("#gr_tasca_mask").attr("style");
		
					if(top.indexOf("20px") > 0 ) {
						newheight = "160px";
						newtop = "-210px";
					}
					else {
						newheight = "20px";
						newtop = "-70px";
					}
					
					$jq("#gr_tasca_mask").animate(
						{top:newtop, height:newheight},
						1500
					);
		
				}
		
			);		
		
			$jq("#outerScroller, #gr_tiene_frecce").mouseover( 
				function() {
					$jq("#gr_tiene_frecce").show();
				}
			);		
		
			$jq("#outerScroller").mouseout( 
				function() {
					$jq("#gr_tiene_frecce").hide();
				}
			);
		
		
		
		
		
			var selezionato = $jq(".thesel").attr("id");
			if(typeof(selezionato) != 'undefined') {
				var array_selezionato = selezionato.split("_");
			
				var thesel_livello1 = "#grlink_"+array_selezionato[1];
				if(array_selezionato.length>1) var thesel_livello2 = "#grlink_"+array_selezionato[1]+"_"+array_selezionato[2];		
			}
		
			/* funzione che riporta il menu allo stato iniziale */
			function showSelected() {
				if (typeof(selezionato) != 'undefined') {
					$jq("#gr_livello1 div a").removeClass("livello1_sel");
					$jq(thesel_livello1).addClass("livello1_sel");
					$jq("#gr_livello2 div:visible").hide();
					$jq(whichMenu(thesel_livello1)).show();
					
					if (thesel_livello2 != 'undefined') {
						$jq("#gr_livello3 div:visible").hide();
						$jq(whichMenu(thesel_livello2)).show();
						
						$jq('#gr_livello2 div a').removeClass("livello2_sel");
						$jq(thesel_livello2).addClass("livello2_sel");
					}
				}
				else {
					$jq("#gr_livello1 div a").removeClass("livello1_sel");
					$jq("#gr_livello2 div:visible").hide();
					
					$jq("#gr_livello3 div:visible").hide();
					$jq('#gr_livello2 div a').removeClass("livello2_sel");
				}
			}
		
		
			/* genera gli ID delle sezioni */
			$jq("#gr_livello1 div a").each(
				function(index) {
					var nuovo = index+1;
					$jq(this).attr("id","grlink_"+nuovo);
				}
			);
		
			$jq("#gr_livello2 div").each(
				function() {
					$jq(this).children().each(
						function(index) {
							var nuovo = index+1;
							$jq(this).attr("id", whichLink($jq(this).parent().attr("id"))+"_"+ nuovo);
						}
					);								
				}
			);
		
			/* gestisce i comportamenti sui mouseover */
			$jq("#gr_livello1 div a:not('.gr_vuoto')").mouseover (
				function() {
					$jq('#gr_livello1 div a').removeClass("livello1_sel");
					$jq(this).addClass("livello1_sel");
					$jq("#gr_livello2 div:visible").hide();
			$jq('#'+whichMenu($jq(this).attr('id'))).show();
			$jq('#gr_livello2 div a').removeClass("livello2_sel");
			$jq("#gr_livello3 div:visible").toggle();
		}
	);

	$jq('#gr_livello2 div a').mouseover (
		function() {
			$jq('#gr_livello2 div a').removeClass("livello2_sel");
			$jq(this).addClass("livello2_sel");
			$jq("#gr_livello3 div:visible").hide();
			$jq('#'+whichMenu($jq(this).attr('id'))).toggle();
		}
	);

	/* torna allo stato iniziale se esco col mouse dall'area principale */
	$jq("#gr_menu_tutto").bind(
		"mouseleave",
		function() {
			showSelected();
		}
	);
	
	/* apri layer */
	$jq('.gr_mylayer').click (
		function() {
			flashFunction($jq(this).attr("href"));
			return false;
		}
	);
	
	$jq('.gr_single_photo').click(
		function() {
			
			if( $(this).closest('.gr_vintage_item').length>0 ) {
				flashLightbox($jq(this).attr("href"));
			}
			return false;
			
		}
	);
	
	/* pupop busta */
	$jq('#gr_contatti_busta').click(
		function() {			
			if($jq('#busta_envelope').length==0) $jq('#gr_contatti_busta').after("<div id='busta_envelope'></div>");
			$jq('#busta_envelope').load(
				$jq(this).attr('href')
			);
			return false;
		}
	);

	/* pupop login */
	$jq('#gr_contatti_login').click(
		function() {			
			if($jq('#login_envelope').length==0) $jq('#gr_contatti_login').after("<div id='login_envelope' style='position:absolute;top:25px;'></div>");
			$jq('#login_envelope').load(
				$jq(this).attr('href')
			);
			return false;
		}
	);

	/* 
	 * pupop login2
	 * 
	 * creato il 22/06/09 da businarolo stefano
	 * 
	 * questo id viene utilizzato nella pagina SERVICES
	 * dentro al box MY GITZO
	 * serve per apripe il popup di login via jquery
	 * 
	 *  */
	$jq('#gr_contatti_login2').click(
		function() {			
			if($jq('#login_envelope').length==0) $jq('#gr_contatti_login2').after("<div id='login_envelope2'></div>");
			$jq('#login_envelope2').load(
				$jq(this).attr('href')
			);
			return false;
		}
	);

	/* pupop lingua */
	$jq('#gr_languages').click(
		function() {			
			if($jq('#language_envelope').length==0) $jq('#gr_languages').after("<div id='language_envelope'></div>");
			$jq('#language_envelope').load(
				$jq(this).attr('href')
			);
			return false;
		}
	);
	
	/* select where to buy */
	if ($jq('#gr_search_box_result').length > 0) {
	//alert($jq('#gr_search_select :selected').val());
  	$jq('#gr_search_box_result').load('/service/distributors/search?siteKey=gitzo&output=html&country='+$jq('#gr_search_select :selected').val());

		$jq('#gr_search_select').change(
			function() {
				selvar = $jq('#gr_search_select :selected').val();
				//alert(selvar);
				
				//$jq('.wtb_item').not('#wtb_'+selvar).hide();
				//$jq('#wtb_'+selvar).show();
				$jq('#gr_search_box_result').load('/service/distributors/search?siteKey=gitzo&output=html&country='+$jq('#gr_search_select :selected').val());
			}
		);
		
	
  }
  
  	$jq('.switchTable').html("switch to imperial");
  	
  	$jq('.switchTable').click(
  		function(){
  			if($jq('#gr_table_famiglia_imp').css("display")=="none"){
  				/*
$jq('#gr_table_famiglia_imp').show();
  				$jq('#gr_table_famiglia_dec').hide();
*/
  				$jq('.imperial').show();
  				$jq('.metric').hide();
  				$jq('.switchTable').html("switch to metric");
  			}else{
  				/*
$jq('#gr_table_famiglia_imp').hide();
  				$jq('#gr_table_famiglia_dec').show();
*/
  					$jq('.imperial').hide();
  				$jq('.metric').show();
  				$jq('.switchTable').html("switch to imperial");
  			}
  		}
  	)
  	
  
 	 /* mesi dell'anno nel calendario*/
	//var my_date = new Date();
	//$jq('#calendar_box').load('calendar.php?month='+my_date.getMonth()+'&year='+my_date.getFullYear());
	/* mesi dell'anno nel calendario*/
	

	/* mostra lo stato iniziale alla prima apertura */
	showSelected();
	

	var flashvars = {};
	var params = {
	  scale: "noscale",
	  wmode: "transparent"
	};
	var attributes = {
	};
	
	var larghezza = 600;

		$jq(document).pngFix();
			
	

			
		}
	);
	
	
	
	
