$().ready(function() {


    $("a.submit").click(function(e){
	e.preventDefault();
	$(this).closest("form").submit();
    })

    if ($.isFunction($.fn.lightbox)) {	
	$('a.lightbox').lightbox(
	{
	    fileLoadingImage: "/images/lightbox/loading.gif",
	    fileBottomNavCloseImage: "/images/lightbox/closelabel.gif"
	});
    }

    $("ul.productPhotoThumb a").click(function(e) {
	e.preventDefault();
	var id = $(this).attr("rel");
	if (!id) {
	    return;
	}
	$("div.productPhotoList a").each(function(){
	    $(this).addClass("hide");
	})
	$("#" + id).removeClass("hide");
    })

    $.extend($.validator.messages, {
	required: "Поле обязательно к заполненнию",
	email: "Неверный email"
    });

    $(".required").prev().append(' <span class="star">*</span>');
    $(".needValidate").validate({
	errorClass: 'errorForm',
	errorElement: 'div'
    });

    $("#agree").change(function(){
	if ($(this).is(":checked")) {
	    $("div.agreeWarning").hide();
	}
	else {
	    $("div.agreeWarning").show();
	}
    })

    $("#doCheckout").click(function(e){
	var elemAgree = $("#agree");
	if (!elemAgree.is(":checked")) {
	    e.preventDefault();
	    $("div.agreeWarning").show();
	    return;
	}
    })

    $("a.compareProduct").click(function(e){
	e.preventDefault();
	var productId = $(this).data("productId");	
	var block = $(this).siblings("div.actionResult");	
	$.post(
	    '/ajax/compare.php',
	    {
		productId:productId,
		action: "add"
	    },
	    function(response) {
		var message = response.message;
		block.html(message);
		if (response.success) {		    
		    block.addClass("success");
		}
		else if (response.error) {
		    block.addClass("error");
		}		
	    },
	    "json"
	    );
    });

    $("a.compareProductRemove").click(function(e){
	e.preventDefault();
	var productId = $(this).data("productId");	
	$.post(
	    '/ajax/compare.php',
	    {
		productId:productId,
		action: "remove"
	    },
	    function(response) {
		$("table.productCompare td[data-product-id=" + productId + "]").remove();
	    },
	    "json"
	    );
    });

    $("#resetFilter").click(function(e){
	e.preventDefault();
	var form = $(this).closest("form")[0];
	form.reset();
    });
});
