﻿
(function ($) {

    $.extend($.fn, {
        livequery: function (type, fn, fn2) {
            var self = this, q;
            if ($.isFunction(type))
                fn2 = fn, fn = type, type = undefined;

            $.each($.livequery.queries, function (i, query) {
                if (self.selector == query.selector && self.context == query.context && type == query.type && (!fn || fn.$lqguid == query.fn.$lqguid) && (!fn2 || fn2.$lqguid == query.fn2.$lqguid))
                    return (q = query) && false;
            });

            q = q || new $.livequery(this.selector, this.context, type, fn, fn2);
            q.stopped = false;
            q.run();
            return this;
        },
        expire: function (type, fn, fn2) {
            var self = this;
            if ($.isFunction(type))
                fn2 = fn, fn = type, type = undefined;

            $.each($.livequery.queries, function (i, query) {
                if (self.selector == query.selector && self.context == query.context && (!type || type == query.type) && (!fn || fn.$lqguid == query.fn.$lqguid) && (!fn2 || fn2.$lqguid == query.fn2.$lqguid) && !this.stopped)
                    $.livequery.stop(query.id);
            });
            return this;
        }
    });

    $.livequery = function (selector, context, type, fn, fn2) {
        this.selector = selector; this.context = context || document; this.type = type; this.fn = fn; this.fn2 = fn2; this.elements = []; this.stopped = false;
        this.id = $.livequery.queries.push(this) - 1;
        fn.$lqguid = fn.$lqguid || $.livequery.guid++;
        if (fn2) fn2.$lqguid = fn2.$lqguid || $.livequery.guid++;

        return this;
    };
    $.livequery.prototype = {
        stop: function () {
            var query = this;
            if (this.type)
                this.elements.unbind(this.type, this.fn);
            else if (this.fn2)
                this.elements.each(function (i, el) { query.fn2.apply(el); });

            this.elements = [];
            this.stopped = true;
        },
        run: function () {
            if (this.stopped) return;
            var query = this;
            var oEls = this.elements,
			els = $(this.selector, this.context),
			nEls = els.not(oEls);
            this.elements = els;
            if (this.type) {
                nEls.bind(this.type, this.fn);
                if (oEls.length > 0)
                    $.each(oEls, function (i, el) { if ($.inArray(el, els) < 0) $.event.remove(el, query.type, query.fn); });
            }
            else {
                nEls.each(function () { query.fn.apply(this); });
                
                if (this.fn2 && oEls.length > 0)
                    $.each(oEls, function (i, el) { if ($.inArray(el, els) < 0) query.fn2.apply(el); });
            }
        }
    };
    $.extend($.livequery, {
        guid: 0, queries: [], queue: [], running: false, timeout: null,
        checkQueue: function () {
            if ($.livequery.running && $.livequery.queue.length) {
                var length = $.livequery.queue.length;
                while (length--)
                    $.livequery.queries[$.livequery.queue.shift()].run();
            }
        },
        pause: function () { $.livequery.running = false; },
        play: function () { $.livequery.running = true; $.livequery.run(); },

        registerPlugin: function () {
            $.each(arguments, function (i, n) {
                if (!$.fn[n]) return;
                
                var old = $.fn[n]; $.fn[n] = function () { var r = old.apply(this, arguments); $.livequery.run(); return r;
                }
            });
        },
        run: function (id) {
            if (id != undefined) {
                if ($.inArray(id, $.livequery.queue) < 0) $.livequery.queue.push(id);
            }
            else
                $.each($.livequery.queries, function (id) { if ($.inArray(id, $.livequery.queue) < 0) $.livequery.queue.push(id); });

            if ($.livequery.timeout) clearTimeout($.livequery.timeout);
            
            $.livequery.timeout = setTimeout($.livequery.checkQueue, 20);
        },
        stop: function (id) {
            if (id != undefined) $.livequery.queries[id].stop();
            else $.each($.livequery.queries, function (id) { $.livequery.queries[id].stop(); });
        }
    });

    $.livequery.registerPlugin('append', 'prepend', 'after', 'before', 'wrap', 'attr', 'removeAttr', 'addClass', 'removeClass', 'toggleClass', 'empty', 'remove');
    $(function () { $.livequery.play(); });

    var init = $.prototype.init;

    $.prototype.init = function (a, c) {
        var r = init.apply(this, arguments);

        if (a && a.selector) r.context = a.context, r.selector = a.selector;

        if (typeof a == 'string') r.context = c || document, r.selector = a;

        return r;
    };
    $.prototype.init.prototype = $.prototype;

})(jQuery);


$(document).ready(function () {
    $('#miniBasket .next').click(function () {
        if ($('#miniBasket .next').hasClass('disabled')) { return false; } 
        else { mini_basket_holder.nextPage(); this.blur(); return false; }
    });
    $('#miniBasket .prev').click(function () {
        if ($('#miniBasket .prev').hasClass('disabled')) { return false; } 
        else { mini_basket_holder.prevPage(); this.blur(); return false; }
    });
    $(".remove").livequery("click", function () { $(this).parent().prev('.item').fadeOut(); $(this).parent().fadeOut(); $(this).hide(); });
    $('#miniBasket').bind('mouseenter', function () { 
        if ($("#openMiniBasketOnHover").val() === "true") { mini_basket_holder.maximizeBasket(false); }
    });
    $('#miniBasket').hover(function () { mini_basket_holder.isMouseOverMiniBasket = true; }, 
    function () { mini_basket_holder.isMouseOverMiniBasket = false; 
    });

});

/* Class for handling miniBasket */
function miniBasketHolder() {
    this.basketLoadingOrHasLoaded = false;
    this.products = [];
    this.qtyPerPage = 3;
    this.currPage = 1;
    this.qtyPage = 1;
    this.minimizing = false;
    this.maximized = false;
    this.paging = false;
    this.minimizeAfterAddTimeout = 4000;
    this.minimizeAfterMouseLeaveTimeout = 500;
    this.productAddedTimeout = this.minimizeAfterAddTimeout;
    this.minimiseBasketTimeout = null;
    this.totalPrice = 0;
    this.totalQuantity = 0;
    this.containsVoucher = false;
    this.errorMessageHideTimeout = null;
    this.openSpeed = 700;
    this.closeSpeed = 500;
    this.itemTemplate = "";
    this.itemHighlight = "#d7d7d7";
    this.itemContainerHeight = 423;
    this.itemHeight = 137;
    this.isMouseOverMiniBasket = false;

}

miniBasketHolder.prototype.applyLayoutTemplate = function (itemData) {
    this.currPage = 1;
    var tpResult = TrimPath.parseTemplate(this.itemTemplate).process(itemData, null);
    $('#miniBasket .body.full .items').html(tpResult);
    $(".item.true").animate({ backgroundColor: mini_basket_holder.itemHighlight }, this.openSpeed);
};

miniBasketHolder.prototype.nextPage = function () {
    if (this.paging) { return; }
    this.paging = true;
    var self = this;
    this.currPage++;
    
    var currentTop = parseInt($('#miniBasket .itemsClmn').css('top'), 10);
    if (isNaN(currentTop)) { currentTop = 0; }
    
    var newTop = currentTop - mini_basket_holder.itemContainerHeight;
    
    $('#miniBasket .itemsClmn').animate({ top: newTop + 'px' }, 500, function () { self.paging = false; });
    this.doPageButtonVisibilities();
    return false;
};

miniBasketHolder.prototype.prevPage = function () {
    if (this.paging) { return; }
    this.paging = true;
    var self = this;
    this.currPage--;
    
    var currentTop = parseInt($('#miniBasket .itemsClmn').css("top"), 10);
    if (isNaN(currentTop)) { currentTop = 0; }
    
    var newTop = currentTop + mini_basket_holder.itemContainerHeight;
    
    $('#miniBasket .itemsClmn').animate({ top: newTop + 'px' }, 500, function () { self.paging = false; });
    this.doPageButtonVisibilities();
    return false;
};

miniBasketHolder.prototype.doPageButtonVisibilities = function () {
    var pnum = $(".itemsClmn > .item").size();

    var pageSign;
    if (pnum < 3) pageSign = 1;
    else {
        if (pnum % 3 == 0) { pageSign = pnum / 3; }
        else { pageSign = parseInt(pnum / 3) + 1; }
    }

    if (this.currPage == 1) {
        $('.MiniShoppingCart_bottom .prev').css({ "display": "block", "background-image": "url(/Ps/Style/Imgs/MiniShoppingCart_prev1.png)" });
        $('.MiniShoppingCart_bottom .prev').addClass('disabled');
    }
    else {
        $('.MiniShoppingCart_bottom .prev').css({ "display": "block", "background-image": "url(/Ps/Style/Imgs/MiniShoppingCart_prev.png)" });
        $('.MiniShoppingCart_bottom .prev').removeClass('disabled');
    }

    if (this.currPage == pageSign) {
        $('.MiniShoppingCart_bottom .next').css({ "display": "block", "background-image": "url(/Ps/Style/Imgs/MiniShoppingCart_next1.png)" });
        $('.MiniShoppingCart_bottom .next').addClass('disabled');
    }
    else {
        $('.MiniShoppingCart_bottom .next').css({ "display": "block", "background-image": "url(/Ps/Style/Imgs/MiniShoppingCart_next.png)" });
        $('.MiniShoppingCart_bottom .next').removeClass('disabled');
    }

};


miniBasketHolder.prototype.doError = function (errorMessage) {
    $('#miniBasketError').html(errorMessage); $('#miniBasketError').show(); $(".add-to-bag-notification").html(""); mini_basket_holder.maximizeBasket(true);
    this.errorMessageHideTimeout = setTimeout(function () { $('#miniBasketError').hide(); }, 
    mini_basket_holder.productAddedTimeout);
};
miniBasketHolder.prototype.maximizeBasket = function (notInvokedDirectlyByUser) {
    if (this.minimizing) {
        return;
    }
    this.maximized = true;
    $('#miniBasket').addClass("sel");
    $('#miniBasket .mini-bag-wrapper').slideDown(this.openSpeed);
    $('#miniBasket .top').slideDown(miniBasketHolder.openSpeed);
    var self = this;
    $('#miniBasket').hover(
        function () { clearTimeout(this.minimiseBasketTimeout); },
        function () { clearTimeout(this.minimiseBasketTimeout); this.minimiseBasketTimeout = setTimeout(function () { self.minimizeBasket(); }, self.minimizeAfterMouseLeaveTimeout); }
    );
    clearTimeout(this.minimiseBasketTimeout);
    if (notInvokedDirectlyByUser) { this.minimiseBasketTimeout = setTimeout(function () { self.minimizeBasket(); }, this.minimizeAfterAddTimeout); }
};

miniBasketHolder.prototype.resetMinimiseAfterClick = function () {
    var self = this; clearTimeout(this.minimiseBasketTimeout);
    this.minimiseBasketTimeout = setTimeout(function () { self.minimizeBasket(); }, self.minimizeAfterAddTimeout);
};

miniBasketHolder.prototype.minimizeBasket = function () {
    if (!this.maximized || this.isMouseOverMiniBasket) { return; }
    this.maximized = false; this.minimizing = true; $('#miniBasket .mini-bag-wrapper').slideUp(this.closeSpeed); $('#miniBasket .top').slideUp(this.closeSpeed);
    var self = this; 
    setTimeout(function () { $('#miniBasket').removeClass("sel"); self.minimizing = false; }, 500);
};

mini_basket_holder = new miniBasketHolder();

