﻿$(document).ready(function() {
    //    ReSetHeadInfo();
    lazyLoad.init();
    $(".imgAddItemsToCart").each(function() {
        jQuery(this).removeAttr("disabled", "");
    });
    ReSetShoppingCartInfo();
    GetMiniCartData();

});

function ReSetShoppingCartInfo() {
    if ($.cookie("ShoppingItem")) {
        $("#MiniCartItems").html($.cookie("ShoppingItem"));
    } else {
        $("#MiniCartItems").html("0");
    }
    if ($.cookie("Subtotal")) {
        $("#MiniCartSubtotal").html($.cookie("Subtotal"));
    } else {
        $("#MiniCartSubtotal").html("$0");
    }
}

function GetMiniCartData() {
    var StockItemsID = $("#HFStockItemsID").val();
    if (!StockItemsID) {
        StockItemsID = "";
    }
    $.ajax({
        url: $("#HomeLink").val() + '/Ajax/GetMiniShoppingCartInfo.ashx',
        type: 'POST',
        dataType: 'xml',
        data: { StockItemsID: StockItemsID, Url: escape(location.href), time: (new Date()).getTime() },
        error: function(error) {
            location.href = $("#HomeLink").val() + "/Error/GenericErrorpage.aspx";
        },
        success: function(data) {
            if ($(data).find("ErrorMessage").attr("message")) {
                location.href = $("#HomeLink").val() + "/Error/GenericErrorpage.aspx";
            } else {
                if ($(data).find("ShoppingCart").attr("Count")) {
                    var IsLogin = $(data).find("ShoppingCart").attr("IsLogin");
                    if (IsLogin == "true") {
                        $("#CustomerLogin").html("Welcome! <span style=\"color:#E3D11E;\">" + $(data).find("ShoppingCart").attr("Email") + "</span>, <a href=\"javascript:void(0);\" onclick=\"javascript:SignOut();\" style=\"text-decoration:none;\">Sign Out!</a>");
                    } else {
                        $("#CustomerLogin").html("Welcome! <span style=\"color:#E3D11E;\">Guest!</span>");
                    }
                    var TotalCount = $(data).find("ShoppingCart").attr("Count");
                    var TotalPrice = $(data).find("ShoppingCart").attr("TotalPrice");
                    $("#ShoppingItem").html(TotalCount);
                    $("#Subtotal").html(TotalPrice);
                    $("#MiniCartItems").html(TotalCount);
                    $("#MiniCartSubtotal").html(TotalPrice);
                    if (parseInt(TotalCount) <= 10) {
                        $("#MiniCartDisplaying").html(TotalCount);
                    }
                    var MiniCartStr = "";
                    $(data).find("Item").each(function() {
                        var ItemsStr = "<table style=\"padding:auto;margin:0 5px 0 5px;\"><tr><td align=\"left\" style=\"padding:0 4px 0 4px;\" colspan=\"2\"><a href=\"" + $("#HomeLink").val() + "/Items.aspx?id=" + $(this).attr("ItemsID") + "\">" + $(this).attr("ItemsEName") + "</a></td></tr><tr><td><a href=\"" + $("#HomeLink").val() + "/Items.aspx?id=" + $(this).attr("ItemsID") + "\"><img width=\"70\" src=\"http://img.gbeads.cn/" + $(this).attr("ImagePath") + "\" alt=\"\" /></a></td><td><table><tr><td>NO.:" + $(this).attr("Number") + "</td></tr><tr><td>Type: " + $(this).attr("ItemType") + "</td></tr><tr><td>Quantity:" + $(this).attr("Quantity") + "</td></tr><tr><td>At <span style=\"color:red;\">$" + $(this).attr("UnitPrice") + "</span> each</td></tr></table></td></tr></table>"
                        MiniCartStr += ItemsStr;
                    });

                    if (MiniCartStr == "") {
                        MiniCartStr = "<div style=\"margin:30px 0 30px 0;color:red;\">Your shopping cart is empty.</div>";
                    }
                    $("#MiniCartProductList").html(MiniCartStr);

                }
                $(data).find("StockItems").each(function() {
                    var inventory = $(this).attr("inventory");
                    var id = $(this).attr("id");
                    $("#Stock" + id).html(" " + inventory);

                    $("#txtNum" + id).val(inventory);
                });
            }
        }
    });
}


lazyLoad = (function() {
    var timer, elems, hide_elems, count, cb, delay = 30, hide_img_def = 'hide_init_src', init_src_def = 'init_src', arr_df_tag = ['img', 'iframe'], doc_body = document.body, doc_element = document.compatMode == 'BackCompat' ? doc_body : document.documentElement;
    function _onChange() {
        (typeof cb == 'function') && (cb(elems, hide_elems));
        !timer && (timer = setTimeout(_load, delay));
    };

    function _isVisible(e) {
        var offset = (window.MessageEvent && !document.getBoxObjectFor) ? doc_body.scrollTop : doc_element.scrollTop;
        var bottom = offset + doc_element.clientHeight;
        var eOffsetTop = e.offsetTop;
        while (e = e.offsetParent) {
            eOffsetTop += e.offsetTop;
        }
        return eOffsetTop <= bottom;
    };

    function _load(force) {
        if (count < 1) {
            $(window).unbind("scroll", _onChange);
            $(window).unbind("resize", _onChange);
            return;
        }
        for (var i = 0, j = elems.length; i < j; i++) {
            if (!elems[i]) {
                continue;
            }
            if (_isVisible(elems[i]) || force) {
                elems[i].src = elems[i].getAttribute(init_src_def);
                delete elems[i];
                count--;
            }
        }
        timer = 0;
    };

    function init(obj) {
        obj = obj || {};
        if (typeof obj != 'object') {
            return;
        }
        var tagNames;
        if ($.isArray(obj)) {
            tagNames = obj || arr_df_tag;
        } else {
            tagNames = obj['tagNames'] || arr_df_tag;
        }
        timer = 0;
        elems = [];
        hide_elems = [];
        count = 0;
        (typeof obj['beforeCb'] == 'function') && (obj['beforeCb']());
        for (var i = 0, j = tagNames.length; i < j; i++) {
            var es = document.getElementsByTagName(tagNames[i]);
            for (var n = 0, m = es.length; n < m; n++) {
                if (typeof es[n] == 'object' && es[n].getAttribute(init_src_def)) {
                    elems.push(es[n]); count++;
                } else if (typeof es[n] == 'object' && es[n].getAttribute(hide_img_def)) {
                    hide_elems.push(es[n]);
                }
            }
        }
        (typeof obj['afterCb'] == 'function') && (obj['afterCb'](elems, hide_elems));
        (typeof obj['changeCb'] == 'function') && (cb = obj['changeCb']);
        $(window).bind("scroll", _onChange);
        $(window).bind("resize", _onChange);
        _load();
        var lazyTime = obj['lazyTime'] || 0;
        (lazyTime > 0) && (setTimeout(function() {
            _load(true);
        }, lazyTime * 1000));
    };
    return { init: init };
})();
