﻿var pages = new Array();
pages.push(18);
var defaultDesc = '';
var defaultPageGifts = '';
var currentSubShop = "";

var whenUserClickPreviousButton = new Array();
whenUserClickPreviousButton.push("" + window.location);
whenUserClickPreviousButton.push(0);
whenUserClickPreviousButton.push(0);
whenUserClickPreviousButton.push(0);

function clearArray(array) {
    //    if (array != null && array.length > 0) {
    //        for (var i = 0; i < array.length; i++) {
    //            array.pop();
    //        }
    //    }
    array.length = 0;
}

$(window).unload(function() {
    var page = "" + window.location;
    if (page.indexOf("detail.jsp") == -1) {
        if (whenUserClickPreviousButton.length == 4) {
            window.name = whenUserClickPreviousButton[0] + "\\";
            if (whenUserClickPreviousButton[1] != 0) {
                if (whenUserClickPreviousButton[3] == 0) {
                    window.name += "show('" + whenUserClickPreviousButton[1] + "','" + whenUserClickPreviousButton[2] + "');";
                }
                else {
                    window.name += "show('" + whenUserClickPreviousButton[1] + "','" + whenUserClickPreviousButton[2] + "'," + whenUserClickPreviousButton[3] + ");";
                }
            }
            else if (whenUserClickPreviousButton[3] != 0) {
                window.name += "page(" + whenUserClickPreviousButton[3] + ");";
            }
            else {
                // Does nothing.
            }
            var currentshopname = $("#CurrentShop").val();
            if (currentshopname != "" && currentshopname != undefined) {
                if (currentSubShop != "") {
                    currentshopname = currentshopname + " > " + currentSubShop;
                }
                window.name += "@" + currentshopname;
            }
        }
        else {
            window.name = "";
        }
    }
});

function show(id, sender, pageIndex) {
    // 1 is used to store show() method first param
    // 2 is used to store show() method second param.
    // 3 is used to store default page index.
    whenUserClickPreviousButton[1] = id;
    whenUserClickPreviousButton[2] = sender;
    whenUserClickPreviousButton[3] = typeof (pageIndex) == "undefined" ? 0 : pageIndex; ;

    $("#productDetailContent").hide();
    $("#indexPageContent").show();
    var obj = $("#" + id);
    if (sender) {
        sender = $("#" + sender).get(0);
    }
    if (sender && $(sender).attr("_title").length > 0) {
        $("#recipentDesc").html(BuzzminderFramework.Utility.htmlDeCode($(sender).attr("_title")).replace(/\n/gm, "<br/>"));
        $(sender).parent().find("a").removeClass("currentGift").addClass("recItem"); //.css({ "text-decoration": "none", "color": "#1b57c4" });
        $(sender).removeClass("recItem").addClass("currentGift"); //.css({"text-decoration": "underline","color":"");
        currentSubShop = $(sender).html();
    }
    else {
        //        if (typeof($("#productDetailContent").get(0))=="undefined")
        //        {
        //            $("#recipentDesc").html(defaultDesc);
        //            $("#productContent").html(defaultPageGifts);
        //        }
        //        else {
        if (id == "parent") {
            window.location = "/gift/index.jsp";
            return;
        }
        //        }
    }
    if (obj.get(0) != null) {
        $("#recipientPanel").html(obj.html());
    }
    //    else {
    if (id && !isNaN(parseInt(id.replace("__", ''), 10))) {
        if (id.indexOf("__") != -1) {
            id = parseInt(id.replace("__", ''), 10);
        }
        if (pages.length == 0 || pages[0] != id) {
            clearArray(pages);
            pages.push(id);

            //                $.ajax({
            //                    cache: false,
            //                    type: "post",
            //                    url: "/gift/GetPages.jsp",
            //                    async: false,
            //                    data: { QuickShopId: parseInt(id, 10) },
            //                    error: function() {
            //                        alert("Server error.");
            //                    },
            //                    success: function(data) {
            //                        var obj = eval(data);
            //                        if (obj.isSuccess) {
            //                            var arr = obj.pages;
            //                            for (var i = 0; i < arr.length; i++) {
            //                                pages.push(arr[i].text);
            //                            }
            //                        }
            //                        else {
            //                            $("#productContent").html("get error data");
            //                        }
            //                    }
            //                });
        }
        $(".giftImg").find("img").attr("src", "../../Image/loadding_indicator_medium.gif");
        $(".giftDetail").html("loading...");
        $(".giftTitle").html("");
        //            if (pages.length > 0) {
        var nodeId = typeof (pageIndex) == "undefined" ? 1 : pageIndex;
        $.ajax({
            cache: false,
            type: "post",
            url: "/gift/QuickShop.jsp",
            //                    async: false,
            data: { BrowseNodeId: nodeId, shopId: parseInt(id, 10) },
            //                    data: { BrowseNodeId: pages[1] },
            error: function() {
                alert("Server error. Show(id)");
            },
            success: function(data) {
                $("#productContent").html(data);
                generatePages();
            }
        });
        //            }
    }
    //    }
}

function page(i) {
    whenUserClickPreviousButton[3] = i;

    $("#pageHeader").html("");
    $(".giftImg").find("img").attr("src", "../../Image/loadding_indicator_medium.gif");
    $(".giftDetail").html("loading...");
    $.ajax({
        cache: false,
        type: "post",
        url: "/gift/QuickShop.jsp",
        //                    async: false,
        //            data: { BrowseNodeId: pages[i] },
        data: { BrowseNodeId: i, shopId: pages[0] },
        error: function() {
            alert("Server error. page(i)");
        },
        success: function(data) {
            $("#productContent").html(data);
        }
    });

    $("#pageHeader").find("a").removeClass("currentPage");
    //    for (var j = 1; j < pages.length; j++) {
    //        $(".page" + j).removeClass("currentPage");
    //    }
    $("#pageHeader").find("a").eq(i - 1).addClass("currentPage");
}

function generatePages() {
    //    if (pages.length > 1) {
    //        var pageFooter = '';
    //        for (var i = 1; i < pages.length; i++) {
    //            pageFooter += '<a href="javascript:void(0);" onclick="javascript:page(' + i + ')" class="page'+i+'">&lt;' + i + '&gt;</a> ';
    //        }
    //        $("#pageHeader").html(pageFooter);
    //        $("#pageFooter").html(pageFooter);
    //    }
    //    else {
    //        $("#pageHeader").html("");
    //        $("#pageFooter").html("");
    //    }
}

$().ready(function() {
    defaultPageGifts = $("#productContent").html();


    if (window.name != null && window.name != "") {
        var specialCharIndex = window.name.indexOf("\\");
        if (specialCharIndex != -1) {
            var location = window.name.substring(0, specialCharIndex);
            if (location == "" + window.location) {
                var cont = window.name.replace(location + "\\", "");
                if (cont.length > 0) {
                    var specialAtIndex = cont.indexOf("@");
                    if (specialAtIndex != -1) {
                        cont = cont.substring(0, specialAtIndex);
                    }
                    if (cont.length > 0) {
                        eval(cont);
                    }
                }
            }
        }
    }

})

