$( function()
{


	/*!
	 * Choix des couvertures
	 */
	// couverture selectionnées
	var s = [];

	// toutes les couvertures
	var $covers = $("#killme_covers td")
		.bind('mouseenter', function() { $("#killme_preview_c").css('background-image','url('+$(this).attr('data-rel')+')').show(); })
		.bind('mouseleave', function() { $("#killme_preview_c").css('background-image','url('+$(this).attr('data-rel')+')').hide(); })
		.not(':last').click( function()
		{
			$(this).toggleClass('selected');

			var id = $(this).attr('id');

			if ( $(this).hasClass('selected') )
			{
				$(this).css('opacity',1);
				s.push( id );
				if ( s.length > 6 )
				{
					var id = s.shift();
					$("#"+id).removeClass('selected').animate( { opacity: 0.6 } );
				}
			}
			else
			{
				for( var i in s ) if ( s[i] == id ) s.splice(i,1);
			}
		})
		.bind('mouseenter', function() { if ( !$(this).hasClass('selected') ) $(this).stop().animate( { opacity:  1 } ); } )
		.bind('mouseleave', function() { if ( !$(this).hasClass('selected') ) $(this).stop().animate( { opacity:  0.6 } ); } )
		.css('opacity',0.6)
		.end().filter(':last').addClass('selected').end();

	$("#killme_preview div").css({ position: 'absolute', width: '300px', height: '268px' }).filter(':last').css('position','static');

	function showA()
	{
		$("#killme_preview_a")
			.css('background-image','url(' + $covers.eq( Math.floor( Math.random() * $covers.length ) ).attr('data-rel')+')')
			.fadeIn('slow');

		$("#killme_preview_b").fadeOut('slow', function()
		{
			setTimeout( showB, Math.random() * 1000 + 1000 );
		});
	}


	function showB()
	{
		$("#killme_preview_b")
			.css('background-image','url('+$covers.eq( Math.floor( Math.random() * $covers.length ) ).attr('data-rel')+')')
			.fadeIn('slow');

		$("#killme_preview_a").fadeOut('slow', function()
		{
			setTimeout( showA, Math.random() * 1000 + 1000 );
		});
	}

	showA();


	$("#killme_buy").submit( function( evt )
	{
		if ( s.length != 6 )
		{
			evt.preventDefault();
			alert('Please choose '+(6-s.length)+' more cover'+(s.length==5 ? '' : 's')+' !');
			return false;
		}

		if ( !$("#killme_buy input:radio:checked").length )
		{
			evt.preventDefault();
			$("#killme_buy label").effect('highlight',{color:'#ff0000'});
			return false;
		}

		$("#killme_covers td.selected span").each( function( i )
		{
			i = i + 2;
			$("#cover_"+i).val( '- cover ' + $(this).text() );
		});
	}).find("input:radio").click(function()
	{
		$("#killme_buy input[name=item_name_9]").val('Shipping ('+$(this).attr('data-label')+')');
	});
});

