﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="Library/jquery-1.4.2.min.js" />
/// <reference path="common.js" />

var projectorPoint = function() {
    var pub = {};

    pub.alert = function(message, callback) {
        common.alert(message, callback);
    };

    pub.displayError = function(message, callback) {
        common.displayError(message, callback);
    };

    pub.ajaxRequestFailed = function(error) {
        common.ajaxRequestFailed(error);
    };

    pub.confirm = function(message, callback) {
        var confirmed = false;
        if (confirm(message)) {
            confirmed = true;
        }
        if (typeof (callback) == "function") {
            callback(confirmed);
        }
    };

    pub.setupCart = function($cart) {
        $cart.find("tbody tr").removeClass("alt");
        $cart.find("tbody tr:odd").addClass("alt");
        $cart.find(".remove a").click(function(event) {
            event.preventDefault();
            var $this = $(this);
            pub.confirm("Are you sure you wish to remove this item from your cart?", function(confirmed) {
                if (confirmed) {
                    ProjectorPoint.Web.WebServices.DefaultService.RemoveFromBasket($this.attr("data-basketItemId"), function() {
                        $this.parents("tr:first").fadeOut("normal", function() {
                            pub.refreshMiniCart();
                        });
                    }, projectorPoint.ajaxRequestFailed);
                }
            });
        });
        $cart.find(".clearCart").click(function(event) {
            event.preventDefault();
            pub.confirm("Are you sure you want to clear your cart?", function(confirmed) {
                if (confirmed) {
                    ProjectorPoint.Web.WebServices.DefaultService.ClearCart(function() {
                        pub.refreshMiniCart();
                    }, projectorPoint.ajaxRequestFailed);
                }
            });
        });
    }

    pub.refreshMiniCart = function() {
        ProjectorPoint.Web.WebServices.DefaultService.RenderMiniCart(function(htmlResult) {
            var $currentCart = $("#sideBar .miniCart");
            var $newCart = $(htmlResult);
            pub.setupCart($newCart);
            $currentCart.after($newCart);
            $currentCart.remove();
        }, projectorPoint.ajaxRequestFailed);
    };

    pub.setupHeaderMenus = function() {
        $("#header li.menuItem").hover(function() {
            $(this).addClass("hover");
        }, function() {
            $(this).removeClass("hover");
        });


        $("#header .dropMenu").each(function() {
            var $this = $(this);
            var subCategoryCount = $this.find(".subCategories li").size();
            var manufacturerCount = $this.find(".manufacturers li").size();
            if (subCategoryCount > manufacturerCount) {
                $this.find(".subCategories li:last").addClass("last");
            }
            else if (subCategoryCount == manufacturerCount) {
                $this.find(".subCategories li:last").addClass("last");
                $this.find(".manufacturers li:last").addClass("last");
            }
            else {
                $this.find(".manufacturers li:last").addClass("last");
            }
        });

    };

    return pub;
} ();

$(document).ready(function() {
//    $("#header .searchField").keypress(function(e) {
//        var code = (e.keyCode ? e.keyCode : e.which);
//        if (code == 13) {
//            $(this).next().click();
//        }
//    });
    projectorPoint.setupCart($("#sideBar .miniCart"));
    projectorPoint.setupHeaderMenus();
    $(".ie6 li.menuItem").hover(function() {
        $("select.hideForMenuItem").css({ visibility: "hidden" });
    }, function() {
        $("select.hideForMenuItem").css({ visibility: "visible" });
    });

    $(".categoryImage img").load(function() {
        $(this).wrap(function() {
            return '<span class="roundImage" style="background:url(' + $(this).attr('src') + ') no-repeat center center; width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px;" />';
        });
        $(this).css("opacity", "0");
    });

});
