﻿function mainmenu() {
    $(" #menu ul ").css({ display: "none" }); // Opera Fix
    $(" #menu li").hover(function () {
        $(this).find('ul:first').css({ visibility: "visible", display: "none" }).show(400);
    }, function () {
        $(this).find('ul:first').css({ visibility: "hidden" });
    });
};

function menuhover() {
    $(" #menu li")
        .hover(
            function () {
                $(this)
                    .find("ul")
                    .stop(true, true)
                    .slideDown();
            },
            function () {
                $(this)
                    .find("ul")
                    .stop(true, true)
                    .slideUp();
            }
    )
};

function contheight() {
    var pathname = location.pathname;
    var h = null;
    switch (pathname) {
        case "/Home/index":
            h = 870;
            break;
        case "/Home/About":
            h = 780;
            break;
        case "/Home/thepark":
            h = 1000;
            break;
        case "/Home/dbs":
            h = 1590;
            break;
        case "/Home/giraffe":
            h = 660;
            break;
        case "/Home/gravevault":
            h = 680;
            break;
        case "/Home/reebok":
            h = 780;
            break;

    };
    $("#content").css("height", h);
};

function setfcart() {
    var width = $(window).width();
    var heght = $(window).height();
    $("#fcart").css("width", width);
    $("#fcart").css("top", heght - 30);
    $("#fcart").text("ShoppingBag");
    $(window).resize(function () {
        var elem = $(this);
        //    $("#fcart").text('window width: ' + elem.width() + ',height: ' + elem.height());
        $("#fcart").css("width", elem.width());
        $("#fcart").css("top", elem.height() - 30);
    });
    /*
    $(window).scroll(function () {
    var elem = $(this);
    $("#fcart").css("width", elem.width());
    $("#fcart").css("top", elem.height() - 30);
    });
    */
};

/*ページカートの表示切り替えを行います。

*/
function showCart() {
    $("#shopcart_container").append("<div id='cart_layer'></div>");
    
    
    //カート内容を生成
    var URL = "/Shop/GetCartItems/";
    $.ajax({
        type: "POST",
        url: URL,
        data: {},
        dataType: "json",
        beforeSend: function () {
            //
        },
        success: function (result, status) {
            //
            if (result && result.length > 0) {
                BuildHeaderCart(result);
                $("#cart_layer")
                    .filter(":not(:animated)")
                    .slideDown("slow", function () {
                        var timer = setTimeout(function () {
                            $("#cart_layer").slideUp("slow");
                            timer = null;
                        }, 5000);
                    });
            }
        }
    });
}

//ページプレカートの内容(HTML)を作成します。
function BuildHeaderCart(result) {
    //var sumQ = 0;
    //var sumA = 0;
    $("#cart_layer").setTemplateURL("../../Template/PreCart.htm?" + Math.random().toString(36).slice(-8));
    $("#cart_layer").processTemplate(result);
    //
    $("#viewcart").bind("click", function () {
        //location.href = "../../Shop/ShoppingCart?returnUrl=" + location.pathname;
        location.href = "../../Shop/ShoppingCart";
    });
    $(".cartClose").bind("click", function () {
        $("#cart_layer").slideUp();
        
    });
};

$(document).ready(function () {
    //メニュー表示
    mainmenu();
    
    /*
    //ページ高さの設定
    contheight();
    */
    //ヘッダカート表示用コンテナを追加します。
    $("#center").append("<div id='shopcart_container'></div>");
    //カートcontainerにスクロール効果を設定します。*0319 jquery.toolsと競合発生
    $("#shopcart_container").scrollFollow();


    $("ul li#util-checkout a").click(function () {
        var q = $("ul li#util-minicart a .qty").text();
        if (q == "(0)")
            //location.href = "../../Shop/ShoppingCart?returnUrl=" + location.pathname;
            location.href = "../../Shop/ShoppingCart";
        else
            location.href = "../../Account/RegistCustomer";
    });


    $("ul li#util-minicart a").hover(function () {
        //カートアイテムある場合にアクション
        //if (location.pathname.indexOf("ShoppingCart") == -1)
            showCart();
    },
    function () {
        $(this).stop();
    });

    $("ul li#util-wishlist a").click(function () {
        location.href = "../../Shop/WishList?returnUrl=" + location.pathname;
    });

});
