var J = jQuery.noConflict();
var ie6 = (J.browser.msie && J.browser.version.substring(0, 1) === '6') ? true : false;
var ie7 = (J.browser.msie && J.browser.version.substring(0, 1) === '7') ? true : false;
var ie8 = (J.browser.msie && J.browser.version.substring(0, 1) === '8') ? true : false;
var ie = (J.browser.msie) ? true : false;
var saf = (J.browser.safari) ? true : false;

J(document).ready(function ($) {
    render_cart_header("CartItemsLBGet", "CartTotalLBGet");
    $('a.popup-box-video').click(function () {
        var id = $(this).attr('id');
        load_productvideo(id);

        return false;
    });
});

function load_productvideo(id) {
    var url = '/product_video.aspx?id=' + id;
    J.ajax({
        url: url,
        success: function (html) {
            J('.popup-video').html(html);
            initPopup('.popup-video', '.popup-video .close');
        },
        error: function (e) {
            alert(e.responseText);
        }
    });
}

function initPopup(obj, close) {
    J('.wrapper').height(J(document).height());
    J('.wrapper').width(J(window).width());
    J('.wrapper, ' + obj).show();
    J(close + ', .wrapper').click(function () {
        J('.wrapper, ' + obj).hide();
    });

}

function RefreshCart() {
    J.ajax({
        url: '/store/MiniCart.aspx',
        success: function (html) {
            J('.my-bag .borders').html(html);
        }
    });
}

function UpdateShoppingBag() {
    J.ajax({
        url: '/store/MiniCartAjax.aspx',
        success: function (html) {
            J('.bag-count').html(html);
        }
    });
}

function load_minicart_contents() {
    RefreshCart();
}

function UpdateShoppingBagCount(qty, price) {
    var CartItems;
    var CartItemsHidden;

    CartItems = getElementByID_Master("CartItemsLBGet");
    CartItemsHidden = getElementByID_Master("ctl00$ShoppingBag1$CartItemsHidden");

    if (CartItems != null)
        CartItems.innerHTML = parseInt(CartItems.innerHTML) + parseInt(qty);
}

function SetMyBagHover() {
    if (!ie6) J('.top-menu UL LI .my-bag').css('opacity', 0);
    J('.top-menu UL LI').hover(function () {
        var bag = J('.my-bag', this);
        if (!J(bag).length) return;
        J(bag).stop(true);
        J(bag).css({ 'display': 'none', 'height': 'auto' });
        if (ie6) {
            J(bag).animate({
                height: 'toggle'
            }, 500);
        } else {
            J(bag).animate({
                opacity: 1,
                height: 'toggle'
            }, 500);
        }

        return false;
    }, function () {
        var bag = J('.my-bag', this);
        if (!J(bag).length) return;

        J(bag).stop(true);
        if (ie6) {
            J(bag).animate({
                height: 'toggle'
            }, 300, function () { J(bag).css('height', 'auto'); });
        } else {
            J(bag).animate({
                opacity: 0,
                height: 'toggle'
            }, 300, function () { J(bag).css('height', 'auto'); });
        }

        return false;
    });
}

function UnbindMyBag() {
    J('.info-box UL LI.shopping-cart').unbind('mouseenter mouseleave');
}

function ShowShoppingBag() {
    UnbindMyBag();
    var st;
    var bag = J('.my-bag');
    if (!J(bag).length) return;
    if (ie6) {
        J(bag).animate({
            height: 'toggle'
        }, 500, function () { st = setTimeout(HideShoppingBag, 3000); });
    } else {
        J(bag).animate({
            opacity: 1,
            height: 'toggle'
        }, 500, function () { st = setTimeout(HideShoppingBag, 3000); });
    }

    // temp binding
    setTimeout(function () {
        J('.info-box UL LI.shopping-cart').hover(function () {
            var bag = J('.my-bag');
            if (!J(bag).length) return;
            J(bag).stop(false);
            clearTimeout(st);
            return false;
        }, function () {
            var bag = J('.my-bag');
            if (!J(bag).length) return;
            J(bag).stop(true);
            st = setTimeout(HideShoppingBag, 300);
            return false;
        });
    }, 0);

    return false;
}

function HideShoppingBag() {
    UnbindMyBag();
    var bag = J('.my-bag');
    if (!J(bag).length) return;
    J(bag).stop(true);
    if (ie6) {
        J(bag).animate({
            height: 'toggle'
        }, 300, function () { J(bag).css('height', 'auto'); SetMyBagHover(); });
    } else {
        J(bag).animate({
            opacity: 0,
            height: 'toggle'
        }, 300, function () { J(bag).css('height', 'auto'); SetMyBagHover(); });
    }

    return false;
}

function RemoveItem(recid, qty) {
    var url = '/store/removeminicartitem.aspx?recid=' + recid;
    J('#minicart-message').show();
    J('#minicart-message').html('processing...');
    J.ajax({
        url: url,
        success: function (data) {
            var notice = J('#minicart-message');
            notice.html('Item has been removed from your Bag!');
            setTimeout(function () { notice.hide('slow'); }, 2000);
            setTimeout(function () { RefreshCart(); }, 1000);
            if (document.getElementById('ShoppingBag1_CartItemsCountLB') == null) {
                document.getElementById('Header1_ShoppingBag1_CartItemsCountLB').innerHTML = parseInt(document.getElementById('Header1_ShoppingBag1_CartItemsCountLB').innerHTML) - qty;
            } else {
                document.getElementById('ShoppingBag1_CartItemsCountLB').innerHTML = parseInt(document.getElementById('ShoppingBag1_CartItemsCountLB').innerHTML) - qty;
            }
        },
        error: function (err) {
            return false;
        }
    });
}

function SetMyBagHover() {
    if (!ie6) J('.my-bag').css('opacity', 0);
    J('.info-box UL LI.shopping-cart').hover(function () {
        var bag = J('.my-bag', this);
        if (!J(bag).length) return;
        J(bag).stop(true);
        J(bag).css({ 'display': 'none', 'height': 'auto' });
        if (ie6) {
            J(bag).animate({
                height: 'toggle'
            }, 500);
        } else {
            J(bag).animate({
                opacity: 1,
                height: 'toggle'
            }, 500);
        }

        return false;
    }, function () {
        var bag = J('.my-bag', this);
        if (!J(bag).length) return;

        J(bag).stop(true);
        if (ie6) {
            J(bag).animate({
                height: 'toggle'
            }, 300, function () { J(bag).css('height', 'auto'); });
        } else {
            J(bag).animate({
                opacity: 0,
                height: 'toggle'
            }, 300, function () { J(bag).css('height', 'auto'); });
        }

        return false;
    });
}
