var popup = null;
function add2cart(productId) {

    getJson("/ws.asmx/Add2Cart", "{productId:" + productId + "}", function (d) {
        var data = d.d;
        if (data.result == 'ok') {
            $('.viewCart').replaceWith(data.content);
            enableMiniCart();
            window.scrollTo(0, 0);
            showMiniCart();
        }
        else if (data.result == 'redirect')
            location = data.content;
        else if (data.result == 'popup') {
            if (!popup) popup = $("<div/>", { id: 'popup' }).appendTo('body').css({ background: '#fff' });
            popup.html(data.content).dialog({ resizable: false, modal: true, height: 520, width: 820 });
            $('#frqv').ajaxForm({ dataType: 'script' });
        }
    }, function (e, err, ethrown) {
        alert('Sorry! There was a Technical Error!');
    });
    return false;

}

function quickView(productId) {
    getJson("/ws.asmx/QuickView", "{productId:" + productId + "}", function (d) {
        if (!popup) popup = $("<div/>", { id: 'popup' }).appendTo('body').css({ background: '#fff' });
        popup.html(d.d).dialog({ resizable: false, modal: true, height: 520, width: 820 });
        $('#frqv').ajaxForm({ dataType: 'script' });
    });
    return false;
}

function added2cart(success) {
    if (success) {
        getJson("/ws.asmx/MiniCart", null, function (d) {
            $('#popup').dialog('close');
            var data = d.d;
            $('.viewCart').replaceWith(data.content);
            enableMiniCart();
            window.scrollTo(0, 0);
            showMiniCart();

        }, function (e, err) {
            alert('Sorry! There was a Technical Problem.');
        });
    }
    else
        alert('There was an error!\nPlease try again later.');

}

function getJson(path, data, cb, err) {
    $.ajax({
        url: path,
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: data,
        success: cb,
        error: err
    });
}

function getE(name) {
    if (document.getElementById)
        var elem = document.getElementById(name);
    else if (document.all)
        var elem = document.all[name];
    else if (document.layers)
        var elem = document.layers[name];
    return elem;
}

var smTimer = null;
var hmTimer = null;
var menuIndex = -1;
$(function () {
    $(".sideMenu ul > li").mouseenter(function (event) {
        var el = $(this).next();

        if (el.hasClass("subMenu")) {
            if (el.is(":visible") && hmTimer) {
                clearTimeout(hmTimer);
                hmTimer = null;
            }
            else {
                if (smTimer) {
                    clearTimeout(smTimer);
                    smTimer = null;
                }
                smTimer = setTimeout(function () {
                    showSubMenu(el);
                }, 250);
            }
        }
    }).mouseout(hideSubMenu);

    $(".subMenu").mouseover(function () { if (hmTimer) { clearTimeout(hmTimer); hmTimer = null; } });
});

function hideSubMenu() {
    if (smTimer) {
        clearTimeout(smTimer);
        smTimer = null;
    }
    if (!hmTimer)
        hmTimer = setTimeout(function () {
            hmTimer = null;
            $(".subMenu:visible").hide()
        }, 250);
}

function showSubMenu(item) {
    smTimer = null;
    $(".subMenu:visible").hide();
    item.show() //Needed for proper positioning!
                    .position({ of: item.prev(), at: "left bottom", my: "left top", offset: "0px 0px" })
                    .show("slide", { direction: "down" }, 150);
}

$(function () {
    $('#nav > li').each(function (index) {
        this.index = index;
        $(this).mouseenter(function () {
            var el = $(".hsMenu:eq(" + index + ")");
            el[0].index = index;

            if (hmTimer) {
                clearTimeout(hmTimer);
                hmTimer = null;
            }

            if (!el.is(":visible")) {

                menuIndex = index;
                if (smTimer) {
                    clearTimeout(smTimer);
                    smTimer = null;
                }
                var parent = $(this);
                $("#nav >li >a").removeClass("hover");
                parent.find("a").addClass("hover");
                smTimer = setTimeout(function () {
                    showMenu(index, parent);
                }, 50);
            }
        }).mouseout(function () {
            menuIndex = -1;
            hmTimer = setTimeout(function () { hideMenu(index); }, 250);
        });

        $(".hsMenu")
        .mouseover(function () { menuIndex = this.index; if (smTimer) { clearTimeout(smTimer); smTimer = null; } if (hmTimer) { clearTimeout(hmTimer); hmTimer = null; } })
        .mouseout(function () { menuIndex = -1; var index = this.index; if (!hmTimer) hmTimer = setTimeout(function () { hideMenu(index); }, 250); });

    })
});

function hideMenu(index) {
    hmTimer = null;
    if (index != menuIndex) {
        $(".hsMenu:visible").each(function (i) { if (this.index == index) $(this).hide() });
        $("#nav > li:eq(" + index + ") > a").removeClass("hover");
    }
}

function showMenu(index, parent) {
    smTimer = null;
    var el = $(".hsMenu:eq(" + index + ")");
    $(".hsMenu:visible").hide();//.each(function (i) { $("#nav > li:eq(" + $(this).index + ") > a").removeClass("hover"); });
    if(el.find('li').length>0)
    if(index < 4)
    el.show()
        .position({ of: parent, at: "left bottom", my: "left top", offset: "0px -1px" })
        .css('z-index',50);
    else
        el.show().position({ of: parent, at: "left bottom", my: "left top", offset: "0px -1px" });

}

//ViewCart POPUP
var hciTimer = null;
function enableMiniCart() {
    $("a.viewCartBtn,a.chkoutButton").mousemove(function () {
        if (hciTimer) {
            clearTimeout(hciTimer);
            hciTimer = null;
        }
        $(".viewCart").addClass("viewCartHover");
        $(".cartInfo:hidden").slideDown('fast').position({ of: $(this).parent(), at: "right bottom", my: "right top", offset: "0px -3px" });
    }).mouseout(function () {
        hciTimer = setTimeout(function () { $(".cartInfo").hide(); $(".viewCart").removeClass("viewCartHover"); }, 250);
    });

    $(".cartInfo").mouseover(function () {
        if (hciTimer) {
            clearTimeout(hciTimer);
            hciTimer = null;
        }
    }).mouseout(function () {
        hciTimer = setTimeout(function () { $(".cartInfo").hide(); $(".viewCart").removeClass("viewCartHover"); }, 250);
    });
}

function showMiniCart() {
    $(".cartInfo:hidden").slideDown('slow').position({ of: $('.viewCart'), at: "right bottom", my: "right top", offset: "0px -3px" });
    $(".viewCart").addClass("viewCartHover");
    hciTimer = setTimeout(function () { $(".cartInfo").slideUp('fast'); $(".viewCart").removeClass("viewCartHover"); }, 3000);
}
$(enableMiniCart);

function getCookies() {
    var cks = new Object();
    var ckList = document.cookie.split("; ");
    for (var i = 0; i < ckList.length; i++) {
        var ck = ckList[i].split("=");
        cks[ck[0]] = unescape(ck[1]);
    }
    return cks;
}

function ttclick(a) {
    var cookies = getCookies();
    if (cookies["Nop.CustomerSessionGUIDCookie"]) {
        location = a.href + "?cc=" + cookies["Nop.CustomerSessionGUIDCookie"];
        return false;
    }
}



