window.onload = function () {

		var $hoverSliderContainer= $('.carouselContainer');
		var $hoverSlider = $('ul',$hoverSliderContainer);
		var $btnArrowL= $('.carouselContainer .carousel_arrowL a');
		var $btnArrowR= $('.carouselContainer .carousel_arrowR a');
		
		var total_width = ($('li:not(.carousel_icon)',$hoverSlider).width()+(23*2))*($('li:not(.carousel_icon)',$hoverSlider).length)*3;
		var visible_width= $('.carousel_view',$hoverSliderContainer).width()+1;
		var offset= ($('li:not(.carousel_icon)',$hoverSlider).width()+23*2)*5;
		var x=0;
		
		$btnArrowR.click(function(){
			if((x+visible_width+offset) >= total_width)
			{
				$btnArrowR.css('background-position','left top');
				$btnArrowR.css('cursor','default');
				
			}
		
		
			if((x+visible_width) >= total_width)
			{
				$hoverSlider.animate({left: -(total_width-visible_width) }, 1000); 
				//console.log(visible_width);
				
			}
			else
			{
				x= x+ offset;
				$hoverSlider.animate({left: -x }, 1000); 
				//console.log(x);
				$btnArrowL.css('background-position','left bottom');
				$btnArrowL.css('cursor','pointer');
			}	
			$('.carousel_view').css('background-position','60px 100px');// Elimina la freccia sotto le immagini al Click

		});
		
		$btnArrowL.click(function(){
			if((x-offset) <= 0)
			{
				$btnArrowL.css('background-position','left top');
				$btnArrowL.css('cursor','default');
			}
			
			if(x <= 0)
			{
				$hoverSlider.animate({left: 0 }, 1000); 
				
			}
			else
			{
				x= x- offset;
				$hoverSlider.animate({left: -x }, 1000); 
				//console.log(x);
				$btnArrowR.css('background-position','left bottom');
				$btnArrowR.css('cursor','pointer');
			}	
			$('.carousel_view').css('background-position','60px 100px');// Elimina la freccia sotto le immagini al Click
		});
		
		/* Descrizione Icona */
		
		$('.carousel_icon a').mouseover(function(){
			$('.hover_txt',this).show();									 
			$(this).parent().prev().children("a").css('z-index','10');
			$(this).parent().next().children("a").css('z-index','10');
			$(this).css('z-index','100');
			
		});
		$('.carousel_icon a').mouseout(function(){
			$('.hover_txt',this).hide();	
			$(this).parent().prev().children("a").css('z-index','auto');
			$(this).parent().next().children("a").css('z-index','auto');
			$(this).css('z-index','auto');
		});
		
		/* GALLERY */
		
		$('.carousel_view li a').click(function(){
												
			var src = $(this).attr("href");
			var arrow_offset = parseInt($('.more_info .arrow_position',this).text())+"px";
			
			
			if($(this).parent().hasClass('carousel_icon')){ //icone
				var h2_content = $('.more_info .h2_content',this).html();
				var p_content = $('.more_info .p_content',this).html();
				$('.galleryContainer h2').css('display','block').html(h2_content);	
				$('.galleryContainer p').css('display','block').html(p_content);
				
			}
			else{ //hardware
				$('.galleryContainer h2').css('display','none');
				$('.galleryContainer p').css('display','none');
			}
			$('.galleryContainer img').attr('src',src);
			$('.carousel_view').css('background-position',arrow_offset+' bottom');

			return false;
		});
		
		/*  Radio Buttons  */
		
		$('.fake_radio input').click(function(){
											 	
			if(!$(this).parent().hasClass("checked"))
			{
				$(this).parent().addClass("checked");
				$(this).parent().siblings('.fake_radio').removeClass("checked");
				if($(this).attr("value") == "black"){
					$(this).parents(".prod_box").children("img").attr("src","http://www.tim.it/showimg/98153.jpg");	
					if($(this).parents('.prod_box').children('span').attr('id') == 'iPhone3Gs_16'){
						$('#iphone16_lnk').attr("href","/consumer/c365/i70113/o90626/prodotto.do");	
					}
					else if($(this).parents('.prod_box').children('span').attr('id') == 'iPhone3Gs_32'){
						$('#iphone32_lnk').attr("href","/consumer/c365/i70113/o90621/prodotto.do");	
					}
				}
				else {
					$(this).parents(".prod_box").children("img").attr("src","http://www.tim.it/showimg/98154.jpg");	
					if($(this).parents('.prod_box').children('span').attr('id') == 'iPhone3Gs_16'){
						$('#iphone16_lnk').attr("href","/consumer/c365/i70113/o90624/prodotto.do");	
					}
					else if($(this).parents('.prod_box').children('span').attr('id') == 'iPhone3Gs_32'){
						$('#iphone32_lnk').attr("href","/consumer/o90623/prodotto.do");	
					}
				}
			}
		});
		
		
		/* Overlay Details */
		
		$('.prod_box .more a').mouseover(function(){
				$(this).parents('.prod_box').children('.overlay_details').stop(true, true).fadeIn('normal');
		});
		$('.prod_box .more a').mouseout(function(){ 
				$(this).parents('.prod_box').children('.overlay_details').stop(true, true).fadeOut('normal');
		});
		
		/* Scroller Servizi */
		$('#scrollerContents').find('div').jScrollPane({showArrows:true, arrowSize:20, scrollbarWidth:21, dragMinHeight:80, dragMaxHeight:81, scrollbarMargin:10});
        
        $('.jScrollPaneContainer:eq(1)').hide();
        
        $('#descrizione').click(function(){
            $(this).addClass('descrizioneActive');
            $('#costi').removeClass('costiActive');
            $('.jScrollPaneContainer:eq(1)').hide();
            $('.jScrollPaneContainer:eq(0)').show();
        });
        
        $('#costi').click(function(){
            $('#descrizione').removeClass('descrizioneActive');
            $(this).addClass('costiActive');
            $('.jScrollPaneContainer:eq(0)').hide();
            $('.jScrollPaneContainer:eq(1)').show();
        });
}
