﻿var bannerInterval;

$(document).ready(function () {
    // fixTrademarkSymbol();

    if ($("#sidebar_maincat_right").length > 0) {
        $("#sidebar_maincat_right li:last a").addClass("end");
    }

    $("#ajaxIframe").load(function () {
        updateShoppingCartSummary();
    });

    $("a.action_btn").click(function () {
        if ($(this).closest("form").length > 0) {
            if ($(this).hasClass("cart-add")) {

                if ($(this).closest("form").find("input[name=quantity]").val() == "") {
                    $(this).closest("form").find("input[name=quantity]").val(1);
                }

                var addCartButton = $(this);
                $("#ajaxIframe").unbind("load");
                $("#ajaxIframe").load(function () {
                    updateShoppingCartSummary();
                    $(this).unbind("load");
                    if ($("#cart-form").length > 0) {
                        location.href = $("#cart-form").attr("action");
                    }
                    addCartButton.fadeOut(500, function () {
                        addCartButton.siblings("div.add-success-message").fadeIn(500, function () {
                            addCartButton.siblings("div.add-success-message").fadeOut(500, function () {
                                addCartButton.fadeIn();
                            });
                        });
                    });
                });
            }
            if ($(this).attr("id") == "checkout-button") {
                $(this).closest("form").attr("action", $(this).closest("form").attr("action") + "?checkout=1");
            }
            $(this).closest("form").submit();
            return false;
        }
    });
    $("#page-option-print, #page-option-taf, #page-option-share").unbind("click");
    $("#page-option-print").click(function () {
        window.print();
        return false;
    });
    $("#page-option-taf").click(function () {
        return false;
    });
    $("#page-option-share").click(function () {

    });

    $("#nav_sc").mouseenter(function () {
        $(this).attr("style", "cursor:pointer");
    });
    $("#nav_sc").click(function () {
        location.href = "/cart/";
    });

    $("#main_nav > ul > li").mouseenter(function () {
        $(this).find("ul").show();
        if ($.browser.msie && $.browser.version == "6.0") {
            $("#Advanced select").hide();
        }
    });
    $("#main_nav > ul > li").mouseleave(function () {
        $(this).find("ul").hide();
        if ($.browser.msie && $.browser.version == "6.0") {
            $("#Advanced select").show();
        }
    });

    $("#browse-by-benefits-dropdown-handle").mouseenter(function () {
        $(this).attr("style", "cursor:pointer");
        $("#browse-by-benefits-dropdown").show();
    });
    $("#browse-by-benefits-dropdown-handle").mouseleave(function () {
        $("#browse-by-benefits-dropdown").hide();
    });

    if ($("#taf")) {
        try {
            $("#taf").dialog({
                autoOpen: false,
                height: 390,
                width: 390,
                modal: true,
                resizable: false,
                draggable: false
            });
        } catch (ex) { }

        $("#page-option-taf").click(function () {

            $("#taf").find("input.text, textarea").each(function () {
                this.value = "";
            });

            $("#taf-formview").show();
            $("#taf-successview").hide();
            $("#taf").dialog("open");

            return false;
        });

        $("#taf-submit").click(function () {
            $.each($("#taf-formview :input"), function () {
                if ($(this).attr("name").length > 0) {
                    $(this).parent().parent().removeClass("error");
                }
            });
            $("#friend_details").parent().parent().removeClass("error");

            $.ajax({
                url: "/" + $(this).closest("form").find("input[name=site]").val() + "/products/taf",
                type: "post",
                data: $("#taf_form").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data.isvalid == false) {
                        $.each(data.err_fields, function (key, value) {
                            $("#taf-formview input[name='" + value + "']").parent().parent().addClass("error");
                            $("#taf-formview textarea[name='" + value + "']").parent().parent().addClass("error");
                            $("#taf-formview table[id='" + value + "']").parent().parent().addClass("error");
                        });
                    } else if (data.isvalid == true) {
                        $("#taf-formview").hide();
                        $("#taf-successview").show();
                    }
                }
            });

            return false;
        });

        $("#taf-cancel").click(function () {
            $("#taf").dialog("close");
            return false;
        });
    }

    if ($("#icandy").length > 0) {
        $("#icandy").find("div").hide();
        $("#icandy").find("div:eq(0)").show();
        bannerInterval = setInterval(showBanner, 5000);
        numBanners = $("#icandy").find("div:not(ul div)").length;
        if (numBanners > 1) {
            for (i = 1; i <= numBanners; i++) {
                $("#banner-page-buttons").append('<li><a href="#" class="' + ((i == 1) ? "active" : "") + '">' + i + '</a></li>');
            }
            $("#banner-page-buttons").show();
            $("#banner-page-buttons a").click(function () {
                var clickIndex = $("#banner-page-buttons a").index($(this));
                jumpToBanner(clickIndex);
                clearInterval(bannerInterval);
                return false;
            });
        }
    }

    $("#update-cart-button").click(function () {
        $(this).closest("form").submit();
        return false;
    });

});

function jumpToBanner(index) {
    var icandy = $("#icandy");
    var numBanners = icandy.find("div:not(ul div)").length;

    var currentIndex = icandy.find("div").index(icandy.find("div:visible"));
    var nextIndex = index;
    if (nextIndex < 0) {
        nextIndex = (currentIndex + 1 < numBanners) ? currentIndex + 1 : 0;
    }
    icandy.find("div:eq(" + currentIndex + ")").hide();
    icandy.find("div:eq(" + nextIndex + ")").show();
    $("#banner-page-buttons a.active").removeClass("active");
    $("#banner-page-buttons a:eq(" + nextIndex + ")").addClass("active");
}

function showBanner() {
    jumpToBanner(-1);
}

function updateShoppingCartSummary() {
    if ($("#ajaxIframe").contents().find("div#cart-item-count").text() != "") {
        $("#cart-count").find("span:first").html($("#ajaxIframe").contents().find("div#cart-item-count").text());
        $("#cart-sub-total").html($("#ajaxIframe").contents().find("div#sub-total").text());
        $("#cart-delivery").html($("#ajaxIframe").contents().find("div#delivery").text());
        $("#cart-grand-total").html($("#ajaxIframe").contents().find("div#grand-total").text());

        $("#ajaxIframe").contents().find("div.subtotal").each(function () {
            $("#" + $(this).attr("id")).html($(this).text());
        });
    }
}

function fixTrademarkSymbol() {
    var f = $("body");
    f.html(f.html().replace(/®/g, "<sup>®</sup>"));
    f.html(f.html().replace(/&reg;/g, "<sup>&reg;</sup>"));
}
