﻿$(document).ready(function () {
    $("#clearProducts").click(function () {
        clear();
    });

    var cookies = $.cookie("vasqueCompare");
    if (cookies != null) {
        cookies = cookies.split(",");
        $(".compareBox").each(function (index, box) {
            if ($.inArray($(box).val().toString(), cookies) != -1) {
                $(box).attr("checked", true);
                $(box).parent().attr("class", "mod_compare button active");
            }
        });
    }

    $(".compareBox").click(function () {
        var productID = $(this).val();
        var checked = $(this).attr("checked");

        var currentCookie = $.cookie("vasqueCompare");
        if (checked) {
            if (currentCookie != null) {
                if (currentCookie.split(",").length === 4) {
                    $("#modal_techdetail").overlay({
                        effect: 'apple',
                        closeOnClick: false,
                        load: true
                    });
                } else {
                    $.cookie("vasqueCompare", currentCookie + "," + productID, { path: '/' });
                    $(this).parent().attr("class", "mod_compare button active");
                }
            } else {
                $.cookie("vasqueCompare", productID, { path: '/' });
                $(this).parent().attr("class", "mod_compare button active");
            }
        } else {
            $.cookie("vasqueCompare", currentCookie.replace(productID + ",", "").replace("," + productID, "").replace(productID, ""), { path: '/' });
            $(this).parent().attr("class", "mod_compare button");
        }
    });
});

function clear() {
    $(".compareBox").removeAttr('checked');
    $.cookie("vasqueCompare", null, { path: '/' });
};
