// when the DOM is ready...
$(document).ready(function () {

   // var $panels = $('#gamma-prodotto .scrollContainer > div');
	var $panels = $('#gamma-prodotto .scrollContainer p');   
    var $container = $('#gamma-prodotto .scrollContainer');

    // if false, we'll float all the panels left and fix the width 
    // of the container
    var horizontal = true;

    // float the panels left if we're going horizontal
    if (horizontal) {
        $panels.css({
            'float' : 'left',
            'position' : 'relative' // IE fix to ensure overflow is hidden
        });

        // calculate a new width for the container (so it holds all panels)
        $container.css('width', $panels[0].offsetWidth * $panels.length);
    }

    // collect the scroll object, at the same time apply the hidden overflow
    // to remove the default scrollbars that will appear
    var $scroll = $('#gamma-prodotto .scroll').css('overflow', 'hidden');

    // apply our left + right buttons
    $scroll
        .before('<img class="scrollButtons left" src="img/scroll_left.gif" />')
        .after('<img class="scrollButtons right" src="img/scroll_right.gif" />');
        
    
    	
        
   	var $l_btn = $('img.left');
   	var $offsetMove = $panels[0].offsetWidth*4;
   	var $offsetMoveC = ($panels[0].offsetWidth * $panels.length);
   	var $Lclicable = 1;
   	var $Rclicable = 1;
   	
   	$l_btn.click(function () { 
   		var $templeft = $container.css('marginLeft');
   		$templeft = $templeft.substr(0,$templeft.length-2);
   		var $newleft = $templeft*1 + $offsetMove*1 + 4;
   		if($newleft > ($panels[0].offsetWidth*3)){
   			$Lclicable = 0;
   		}
   		//alert($newleft);
   		if($Lclicable == 1){
   	    	$container.animate({marginLeft: $newleft+"px"},1000,function(){
   	    		$Lclicable = 1;
   	    		$Rclicable = 1;
   	    	});
   	    	$Lclicable = 0;
   	    	$Rclicable = 0;
   	    }
    });
    
   	var $r_btn = $('img.right');
   	$r_btn.click(function () { 
   	    var $templeft = $container.css('marginLeft');
   		$templeft = $templeft.substr(0,$templeft.length-2);
   		var $newleft = $templeft*1 - $offsetMove*1 - 4;
   		if($newleft < -($offsetMoveC)){
   			$Rclicable = 0;
   		}
   		//alert($newleft);
   	    if($Rclicable == 1){
   	    	$container.animate({marginLeft: $newleft+"px"},1000,function(){
   	    		$Lclicable = 1;
   	    		$Rclicable = 1;
   	    	});
   	    	$Lclicable = 0;
   	    	$Rclicable = 0;
   	    }
    });


    // handle nav selection
    //$('ul.navigation li:first').find('a').addClass('selected');
        
    jQuery.each($('.panel'),function enumera(){
		$(this).attr("rel",$(this).children().length );
	});
    
    //setto il counter a 0
	var $all_prev_block = 0;
        
    function selectNav() {
        /*$(this)
            .parents('ul:first')
                .find('a')
                    .removeClass('selected')
                .end()
            .end()
            .addClass('selected');*/
            
	//al clic su una label setto il counter a 0
	$all_prev_block = 0;
	
	//seleziono i pannelli precedenti e per ognuno sommo al counter il valore del rel ottenendo gli elementi totali
	var $pattern = $(this).attr('rel');
	
	jQuery.each($($pattern).prevAll(),function enumera(){
		$all_prev_block = $all_prev_block*1 + $(this).attr("rel")*1;
	});
	
	var $newleft = -1*(($all_prev_block * $panels[0].offsetWidth)+($all_prev_block)) ;
	
	$container.animate({marginLeft: $newleft+"px"},1000,function(){
   	    		$Lclicable = 1;
   	    		$Rclicable = 1;
   	    	});
   	$Lclicable = 0;
   	$Rclicable = 0;
	
	
    }
    
    $('#gamma-prodotto .navigation').find('a').click(selectNav);
    
    $('#gamma-prodotto .navigation').find('a').mouseover(function(){
      $(this).addClass('selected');
    }).mouseout(function(){
      $(this).removeClass('selected');
    });
	
	//inserisco il nome della linea a fina co del prodoto
	/*
	jQuery.each($('.panel a'),function sei(){
		var $image_str = $(this).text();
		var $image_str_l = $(this).text().length;
		var $image_str_code = $(this).html().substring(0, $(this).html().length-$image_str_l);
		var $prd_name = $(this).parent().parent().attr('id');
		//alert($image_str_l);
		$(this).html($image_str_code + $prd_name + ' <strong>' + $image_str + '</strong>');
	});*/

});
