jQuery(function($){

	var speed = 3,
		maxItemWidth = 150,
		maxItemHeight = 185,
		ztime = 500,
		minItemWidth = $('.Cell:first').width(),
		minItemHeight = parseInt(minItemWidth*maxItemHeight/maxItemWidth),
		cPadding = parseInt($('.Cell').css('padding-left')) + parseInt($('.Cell').css('padding-right')),
		olditem = null;

	$('#ScrollContent').css({width:$('#ScrollContent .Cell').length * maxItemWidth+'px'});
		
	$('.Scroll').hover(
		function(){
			var dest = this.id=='SLeft'? 0:
				$('.Cell:last').offset().left + $('.Cell:last').width() + cPadding - $('.Cell:first').offset().left - $('#ScrollArea').width();
			$('#ScrollContent').animate({ marginLeft:-dest+"px" }, (dest+((this.id=='SRight')*2-1)*parseInt($('#ScrollContent').css('margin-left')))*speed);
		},
		function(){ $('#ScrollContent').stop() }
	)
	
	$('.Cell').hover(
		function(){ $(this).addClass('CellHover') },
		function(){ $(this).removeClass('CellHover') }
	).click(function(){
		var ie6 = $.browser.msie && parseFloat($.browser.version)<7,
			cMaxW = maxItemWidth + cPadding,
			cMinW = minItemWidth + cPadding,
			v = $(this).offset().left - $('#ScrollArea').offset().left - (cMaxW-cMinW)/2 ,
			l1 = $('#ScrollArea').width() - cMaxW,
			l2 = ($('#ScrollContent .Cell').length-1)*cMinW+cMaxW-$('#ScrollArea').width();

		if (olditem==this) return;

		v = $('.Cell').index(this) * cMinW - (v<0? 0: (v<l1? v: l1));
		v = (v<0? 0: (v<l2? v: l2));

		if (olditem) {
			$(olditem).stop().animate({
				width:minItemWidth+'px',
				paddingTop:'20px',
				height:'180px',
				fontSize:'10px'
			},ztime)
			if (ie6) $('.pngFix',olditem).animate({width:minItemWidth+'px',height:minItemHeight+'px'},ztime);
		}
		olditem = this;
		
		//$('.cellName').empty().append($('.Marca,.Modello',this)).find('.Marca').append(' ');
		$('.cellName').empty().append($('.Marca,.Modello',this).clone()).find('.Marca').append(' ');
		
		$(this).animate({
			width:maxItemWidth+'px', 
			height:'195px',
			paddingTop:'5px',
			fontSize:'15px'
		},ztime,'swing', function(){
			var opz = this.className.split('pz-')[1].split(/\s/)[0];
			if ($.browser.safari) $('#Content').css('min-height',$('#Content').height());
			$( '.DettaglioCell, #opzioniGP '+ (opz!='none'?'tr':'') ).hide();
			$(  '.DettagliCell, #'+this.id.replace('c-','d-') + ', #opzioniGP .'+opz + (opz!='none'?', #opzioniGP ':'') ).show();
			if ($.browser.safari) $('#Content').css('min-height','');
		});
		if (ie6) $('.pngFix',this).animate({width:maxItemWidth+'px',height:maxItemHeight+'px'},ztime);
		$('#ScrollContent').stop().animate({ marginLeft:-v },ztime);
		
	});
});


