$(document).ready(function() {

	// Put the jcarousel where needed
	if($('#thumbs').length) {
		$("#thumbs").jcarousel();
	}

	// Get the payment card
	$("#payment-block").each(function(index, value) {

		var order_id = $(value).data("order-id");

		$.ajax({
			type: "POST",
			url: "/ezsips/request",
			data:"orderID=" + order_id,
			success: function(msg) {
				$('#payment-block').show();
				$('#payment-block div.ajax_loader').hide();
				$('#payment-block div.content').html(msg);
			}
		});
	});

	//Actions on basket quantity buttons
	$('img[data-action=remove]').click(function(e) {
	    var item_id = $(this).data('item-id');
	    $('#basket-action').attr('name', 'RemoveProductItemButton');
	    $('#basket-action').val(item_id);
	    $('#basket-form').submit();
	});

	$('img[data-action=plus]').click(function() {
	    $('#basket-action').attr('name', 'StoreChangesButton');
	    var countValue = parseInt($(this).parent().parent().find('input[name="ProductItemCountList[]"]').val()) + 1;
	    $(this).parent().parent().find('input[name="ProductItemCountList[]"]').val(countValue);
	    $('#basket-form').submit();
	});

	$('img[data-action=minus]').click(function() {
	    $('#basket-action').attr('name', 'StoreChangesButton');
	    var countValue = parseInt($(this).parent().parent().find('input[name="ProductItemCountList[]"]').val()) - 1;
	    $(this).parent().parent().find('input[name="ProductItemCountList[]"]').val(countValue);
	    $('#basket-form').submit();
	});

	/* Validation du formulaire d'adresse du panier */


	$("#userregister").validate({
		rules: {
			"FirstName":{
				"required": true,
				"minlength": 2,
				"maxlength": 255
			},
			"LastName":{
				"required": true,
				"minlength": 2,
				"maxlength": 255
			},
			"EMail":{
				"required": true,
				"email": true,
				"maxlength": 255
			},
			"Street2":{
				"required": true,
				"minlength": 2,
				"maxlength": 255
			},
			"Zip":{
				"required": true,
				"digits": true,
				//"maxlength": 5
				"rangelength": [5, 5]
			},
			"Place":{
				"required": true,
				"minlength": 2,
				"maxlength": 255
			}
		}
	});

	$('#buy').bind('click', function(event) {
		alert('The mouse cursor is at (' + event.pageX + ', ' + event.pageY + ')');
	});

	can_fade = false;

	// Because some user click very fast for nothing

	$('ul.main_image > li:visible').each(function(i) {
		visible_id = $(this).attr('id').split('_')[1];
		id_img = visible_id;
	});




	$('.buy-button-class').click(function() {
			if ($(this).queue("fx").length == 0) {
				$(this).fadeOut('fast');
				$('#buy-image_' + id_img).parent().animate({
					left: '-=150'
				},500, function(){
					$('.thumbnails').css('opacity', .4);
					//$('.thumbnails img').removeClass('thumb');
					$('#buy-form_' + id_img).fadeIn('fast');
				});

				can_fade = true;
			}
	});

	$('.buy-close-class').click(function() {
		if ($('#buy-form_' + id_img).queue("fx").length == 0) {
			$('#buy-form_' + id_img).fadeOut('fast');
			$('#buy-image_'+ id_img).parent().animate({
				left: '+=150'
			},500, function(){
				$('#buy-button_'+ id_img).fadeIn('fast');
				$('.thumbnails').css('opacity', 1);
			});
		}
	});


	$('img.thumb').click(function () {
		id_thumb = this.id;

		id_img = id_thumb.split('_')[1];

		$('ul.main_image > li:visible').each(function(i) {

			visible_id = $(this).attr('id').split('_')[1];

			if (id_img != visible_id)
			{
				idZoom = (id_thumb).replace('thumb_', '#zoom_');

				$('#zoom_'+visible_id).hide();

				$(idZoom).show();

			}
		});
	});

	$('.buy-image').click(function() {
			if (can_fade) {
				$('#buy-form_' + id_img).fadeOut('fast');
				$('#buy-image_'+id_img).parent().animate({
					left: '+=150'
					},500, function(){
						$('#buy-button_'+ id_img).fadeIn('fast')
						$('.thumbnails').css('opacity', 1);
				});
				can_fade = false;
			}
	});

	$('#random_image').cycle({
		random: 1
	});
});
//*************************************************//
//     HIDE ALL DIV AND SHOW SELECTEDINDEX ID      //
//*************************************************//
function basketAction(index, action)
{
	if (arguments.length == 2)
	{
		// Find the element to modify
		var form = $('#basketform');

		// Get all quantities | Type is not specified to treat all types (text and hidden)
		var buttons = $('input[name="ProductItemCountList[]"]');

		// Get value of selected quantity
		var quantity = $(buttons[index]).val();

		// Based on action modify this quantity
		if (action == 'remove')
		{
			quantity = 0;
		}
		else if (action == 'plus')
		{
			quantity++;
		}
		else if (action == 'minus')
		{
			quantity--;
		}

		// Set value of selected quantity
		$(buttons[index]).val(quantity);

		// Set action in action hidden field
		$('#action').attr('name','StoreChangesButton');

		// Submit
		$('#basketform').submit();
	}
	else
	{
		alert("We cannot execute this action");
	}
}
//*************************************************//
//     HIDE ALL DIV AND SHOW SELECTEDINDEX ID      //
//*************************************************//
jQuery.extend(jQuery.validator.messages, {
	required: "Ce champ doit être rempli",
	email: "Ce champ doit contenir une adresse email valide",
	digits: "Ce champ ne doit contenir que des chiffres",
	rangelength: "Ce champ doit contenir 5 chiffres",
	minlength: jQuery.validator.format("Ce champ doit contenir au moins {0} caractéres"),
	maxlength: jQuery.validator.format("Ce champ doit contenir au maximum {0} caractéres")
});

