
jQuery(document).ready(function(){

  mainmenu();
  $("a[lang]").removeClass("selected")
  if( $.cookie("GUEST_LANGUAGE_ID") === null ){
    $("a[lang='it-IT']").addClass("selected")
  }else{
    $("a[lang='" + $.cookie("GUEST_LANGUAGE_ID").replace("_","-") + "']").addClass("selected")
  }  
  $("#message a").hover(
    function(){
      $(this).clearQueue().animate({height:"35px"})
    },
    function(){
      $(this).clearQueue().animate({height:"6px"})
    }
  )
  
  $(function () { // run this code on page load (AKA DOM load)
   
  	/* set variables locally for increased performance */
  	var scroll_timer;
  	var displayed = false;
  	var $message = $('#message a');
  	var $window = $(window);
  	var top = $(document.body).children(0).position().top;
   
  	/* react to scroll event on window */
  	$window.scroll(function () {
  		window.clearTimeout(scroll_timer);
  		scroll_timer = window.setTimeout(function () { // use a timer for performance
  			if($window.scrollTop() <= top) // hide if at the top of the page
  			{
  				displayed = false;
  				$message.fadeOut(500);
  			}
  			else if(displayed == false) // show if scrolling down
  			{
  				displayed = true;
  				$message.stop(true, true).show().click(function () { $message.fadeOut(500); });
  			}
  		}, 100);
  	});
  });
  
  $("a[name='top']").click(function(){
      $("html, body").animate({ scrollTop: 0 }, "slow");
  })
  
  $(".more").click( function(){
    
    var barra = $(this).parent("div").parent("div")
    var articoli = barra.next("div.hide:eq(0)");
    var on = 0;
    var off = 0;
    
    if( articoli.html() == "" ){
      articoli = articoli.siblings("div.hide:eq(0)")
    }
    
    if( articoli.is(":visible") ){
      
      articoli.slideUp("fast")
      
      on = "0px"; off = "-50px";
      
    }else{
    
      articoli.slideDown("fast")
      
      on = "-100px"; off = "-150px";
      
    }
    
//     $(this).unbind();
    $(this).hover(
      function(){
        $(this).css({"background-position":"0 " + off })
      },
      function(){
        $(this).css({"background-position":"0 " + on })
      }
    )
      
    // scroll
    $("html,body").animate({
        scrollTop : barra.offset().top
      }, 
      500
    )     
    
  })
  
  //  Posizione menu prodotti
  $(function () {
    if( $("#menu-prodotti").length != 0 ){
      
      var m = $("#menu-prodotti");
      var offY = m.offset().top;
      var posY = m.position().top;
      m.css({"top":posY+"px"})
      
      $(window).scroll( function(){
        var wPosY = $(this).scrollTop();
        var cPosY = m.offset().Y;  
        
        if( wPosY > offY ){
          
          m.clearQueue().animate({"top": ( wPosY - offY + posY ) + "px"}, 200)
          
        }else{
            
          m.clearQueue().animate({"top": posY + "px"}, 200)
          
        }
        
      })
      
    }
  });

//  Slides
	$(function(){
		$('#slides').slides({
			preload: true,
			preloadImage: '/al7-theme/immagini/loading.gif',
			play: 8000,
			pause: 2500,
			hoverPause: true,
			pagination: true,
			effect: 'fade',
			crossfade: true,
			fadeSpeed: 500,
			animationStart: function(current){
				$('.caption').animate({
					opacity: 0
				},100);
			},
			animationComplete: function(current){
				$('.caption').animate({
					opacity:1
				},200);
// 				if (window.console && console.log) {
// 					// example return of current slide number
// 					console.log('animationComplete on slide: ', current);
// 				};
			},
			slidesLoaded: function() {
				$('.caption').animate({
					opacity:1
				},200);
			}
		});
		
		$(".pagination").css({"width" : (14 * $(".slide").length ) + "px" })
		
	});
//  Fine slides

//  Navigazione
$("ul.child-menu").each( function(){
  $(this).css({"width" : $(this).parent("li").width() +"px" });
})
  
});

function mainmenu(){

  jQuery(".navigazione ul").css({display: "none"}); // Opera Fix
  
  jQuery(" .navigazione li").hover(
    function(){
  	jQuery(this).find('ul:first:hidden').css({visibility: "visible",display: "none"}).slideDown(200);
  	},
    function(){
  	jQuery(this).find('ul:first').slideUp(200).css({display: "none"});
  	}
  );
  
}
function navProdottiSubItems(){
  // Inserisce nel menu di navigazione dei prodotti i sotto elementi  
  var subItems = "";
  
  $(".wrapper-prodotto a[name]").each( function(){
    var name = $(this).attr("name")
    subItems += "<li><a href='#" + name + "'>" + name.replace(/-/, " ")+ "</a></li>"
  })
  $("#menu-prodotti li a.selected").parent("li").append("<ul class='inner'>" + subItems + "</ul>")
  
}
