﻿$().ready(function () {
    // header
    $.ajax({
        type: "get",
        url: "/templete/header.aspx",
        async: false,
        success: function (result) {
            $("#head").append(result);

            // 購物車清單
            $("#popup_cart #bd").html(getPopupCartHtml());
            $("#icon_list .cart").mouseover(function () {
                if ($("#popup_cart").css("display") != "none") return false;
                setBasketHtml();
                $("#popup_cart").show();
            })

            // 隱藏購物車清單
            var hidePopupCartPanel = 0;
            $("#icon_list .cart, #popup_cart").mouseover(function () {
                hidePopupCartPanel = 0;
                $("#popup_cart").stopTime("tm_PopupCart");
            }).mouseout(function () {
                $("#popup_cart").everyTime(500, "tm_PopupCart", function (i) {
                    hidePopupCartPanel++;
                    if (hidePopupCartPanel >= 2) {
                        $("#popup_cart").stopTime("tm_PopupCart");
                        $("#popup_cart").hide();
                    }
                });
            });

            // Google Search
            $("#btn_search").click(function () {
                submit_search();
                return false;
            });
        }
    });

    // h-menu
    $.ajax({
        type: "get",
        url: "/templete/hmenu.aspx",
        async: false,
        success: function (result) {
            $("#menu").html(result);

            // 產品分類
            updateCategories("cat_l1");
        }
    });


    // f-menu
    $.ajax({
        type: "get",
        url: "/templete/fmenu.aspx",
        async: false,
        success: function (result) {
            $("#fmenu").prepend(result);
            updateCategories("cat_l2");
        }
    });

    // link
    $.ajax({
        type: "get",
        url: "/templete/ent_link.aspx",
        async: false,
        success: function (result) {
            $("#fmenu").after(result);
        }
    });

    // footer
    $.ajax({
        type: "get",
        url: "/templete/footer.aspx",
        async: false,
        success: function (result) {
            $("#foot").prepend(result);
        }
    });

    // language - onchange
    $("#ddl_lang").change(function () {
        window.location.href = $(this).val();
        return false;
    });

    // nav - check
    headNav = $("#head2 #nav");
    nav_l1 = $(headNav).find("li:first");
    if (nav_l1.length == 0) headNav.remove();
    else {
        if (nav_l1.text() == "") headNav.remove();
    }
});

// product categories
function updateCategories(target_id) {
    $.getJSON("/ajax/categories.aspx", { pi: 0 }, function (data) {
        _target = $("#" + target_id);
        _target.html("");
        $.each(data, function (i, item) {
            htmStr = "<li><a href=\"/category/" + item.custom_id + "\">" + item.name + "</a></li>";
            _target.append(htmStr);
        });
    });
}

// 頁籤
function addBookmark() {
    name = "Rexmed - Medical & Laboratory Equipment";
    url = "http://www.rexmed.com";

    if (window.sidebar) { // Mozilla Firefox
        window.sidebar.addPanel(name, url, "");
    }
    else if (window.external) { // IE
        window.external.AddFavorite(url, name);
    }
    else if (window.opera && window.print) {
        window.external.AddFavorite(url, name);
    }
    else {
        alert('not supported');
    }
    return false;
}

// 取得 popup 購物車的 html
function getPopupCartHtml() {
    var d = new Date();
    var html = $.ajax({ url: "/ajax/popup_cart.aspx?t=" + d.getTime(), async: false }).responseText;
    return html;
}

// 更新購物車 (Client Side)
function setBasketHtml() {
    var cartItem = $("#icon_list .cart");
    $("#popup_cart #bd").html(getPopupCartHtml());
    $("#popup_cart").css("top", cartItem.offset().top + 5);
    $("#popup_cart").css("right", "-10px");
}
// 更新購物車 (Server Side)
function basket_upd(sender, args) {
    $("#" + args + " :text").each(function (i, obj) {
        pid = $(this).attr("id").substr(("popupCart_qty_").length);
        qty = Number($(this).val());
        $.post("/ajax/basket_upd.aspx", { pid: pid, qty: qty }, function () { setBasketHtml(); });
    });
    return false;
}

// Google Search
function submit_search() {
    v = $("#tb_search").val();
    $("#google_search input[name='q']").val(v);
    $("#cse-search-box").submit();
}
