﻿$(document).ready(function() {
    $(".addToCart").click(function() {
        var product_id = $(this).attr("product_id");
        if ($("#selProductColor").length > 0) {
            if ($("#selProductColor").val() == "") {
                alert("Please choose a color first");
                return;
            }
            add_to_cart(product_id, $("#selProductColor").val());
        } else if ($("#selAmount").length > 0) {
            add_to_cart_number(product_id, $("#selAmount").val());
        }
    });

    $(".addtowishlist").click(function() {
        if ($(this).hasClass("inWishlist")) {
            remove_from_wishlist($(this).attr('product_id'), $(this));
        } else {
            add_to_wishlist($(this).attr('product_id'), $(this));           
        }
    });

    //Write review section

    function rateOver() {
        //if($("#reviewRating").val() == 0){
        $(this).find("img").attr("src", "/images/star1.png");
        $(this).prevAll().each(function() {
            $(this).find("img").attr("src", "/images/star1.png");
        });
        $(this).nextAll().each(function() {
            $(this).find("img").attr("src", "/images/star0.png");
        });
    }

    function rateOut() {
        $("#reviewRating").val($(this).find('img').attr('alt'));
        $("#reviewRating").valid();
    }

    //rating control
    $("#selectStar>li").hover(rateOver, rateOut).click(function() {
        $("#reviewRating").val($(this).find('img').attr('alt'));
    });

    jQuery.validator.addMethod("NoZero", function(value, element, param) {
        if (value == "0") {
            return false;
        } else {
            return true;
        }
    });



    $("form:eq(0)").validate({
        errorElement: "p",
        errorElementClass: "err",
        errorClass: "inpe",
        rules: {
            reviewRating: {
                required: true,
                NoZero: true
            },
            txtReviewTitle: {
                required: true,
                maxlength: 50
            },
            txtReviewComment: {
                required: true
            },
            shareto: {
                required: true,
                validwatermark: true,
                email: true
            },
            sharefrom: {
                required: true,
                validwatermark: true,
                email: true
            },
            sharemsg: {
                required: true,
                validwatermark: true,
                maxlength: 200
            }

        },
        messages: {
            reviewRating: {
                required: "Please select your rating.",
                NoZero: "Please select your rating."
            },
            txtReviewTitle: {
                required: "Please input the title of your review.",
                maxlength: "Your title should be less than 50 characters."
            },
            txtReviewComment: {
                required: "Please input your review."
            },
            sharemsg: {
                required: "Please input your message.",
                validwatermark: "Please input your message."
            },
            shareto: {
                required: "Please input your friend's email",
                validwatermark: "Please input your friend's email",
                email: "Please input a valid email"
            },
            sharefrom: {
                required: "Please input your email.",
                validwatermark: "Please input your email.",
                email: "Please input a valid email"
            }
        }
    });

    $(".saveReview").click(function() {
        var bRet = true;
        bRet = $("#reviewRating").valid() && bRet;
        bRet = $("#txtReviewTitle").valid() && bRet;
        bRet = $("#txtReviewComment").valid() && bRet;
        if (bRet) {
            return true;
        }
        return false;
    });

    $(".reviewYes,.reviewNo").click(function() {
        var isUseful = $(this).hasClass("reviewYes") ? 1 : 0;
        var review_id = $(this).attr("review_id");
        var obj = $(this).parent().parent();
        $.post("/rate_review/", { review_id: review_id, is_useful: isUseful }, function(data) {
            eval("var ret = " + data);
            if (ret.ret == 0) {
                obj.empty();
                if (ret.count == 1) {
                    obj.html("<li class=\"txt\"><em>1</em>  person found this review helpful.</li>");
                } else {
                    obj.html("<li class=\"txt\"><em>" + ret.count + "</em>  people found this review helpful.</li>");
                }
            } else {
                alert(ret.msg);
            }
        });
    });

    $("#selProductColor").change(function() {
        $("#ulColorGroup>li a[color=" + $(this).val() + "]").trigger('click');
    });

    $("#ulColorGroup>li a").each(function(index) {
        $(this).click(function() {
            $("#pStock").html($(this).attr('stock'));
            $("#selProductColor").val($(this).attr('color'));
            if ($(".prodthumbs").find('ul').eq(index).find('li').length > 0) {
                $(".prodthumbs").show();
                $(".prodthumbs").find('ul').eq(index).show();
                $(".prodthumbs").find('ul').eq(index).siblings().hide();
            } else {
                $(".prodthumbs").hide();
            }
        });
    });
});
