TopCart= {
    initialize: function (container) {
        this.container = $(container);
        this.element = this.container.up(0);
        this.elementHeader = this.container.previous(0);
        this.intervalDuration = 4000;
        this.interval = null;
        this.onElementMouseOut = this.handleMouseOut.bindAsEventListener(this);
        this.onElementMouseOver = this.handleMouseOver.bindAsEventListener(this);
        this.onElementMouseClick = this.handleMouseClick.bindAsEventListener(this);
        this.element.observe('mouseout', this.onElementMouseOut);
        this.element.observe('mouseover', this.onElementMouseOver);
    },

    handleMouseOut: function (evt) {
        if($(this.elementHeader).hasClassName('expanded')) {
            this.interval = setTimeout(this.hideCart.bind(this), this.intervalDuration);
        }
    },

    handleMouseOver: function (evt) {
        if (this.interval !== null) {
             clearTimeout(this.interval);
             this.interval = null;
        }
    },

    handleMouseClick: function (evt) {
        if (!$(this.elementHeader).hasClassName('expanded') && !$(this.container.id).hasClassName('process') )  {
            this.showCart();
        }
        else {
            this.hideCart();
        }
    },

    showCart: function (timePeriod) {
        if (!$(this.elementHeader).hasClassName('expanded') && !$(this.container.id).hasClassName('process') )  {
            this.container.parentNode.style.zIndex=992;
            new Effect.SlideDown(this.container.id, { duration: 0.5,
                beforeStart: function(effect) {$( effect.element.id ).addClassName('process');},
                afterFinish: function(effect) {$( effect.element.id ).removeClassName('process'); }
                });
            $(this.elementHeader).addClassName('expanded');
            if(timePeriod) {
                this.timePeriod = timePeriod*1000;
                this.interval = setTimeout(this.hideCart.bind(this), this.timePeriod);
            }
        }
    },

    hideCart: function () {

        if (!$(this.container.id).hasClassName('process') && $(this.elementHeader).hasClassName('expanded')) {
            new Effect.SlideUp(this.container.id, { duration: 0.5,
                beforeStart: function(effect) {$( effect.element.id ).addClassName('process');},
                afterFinish: function(effect) {
                    $( effect.element.id ).removeClassName('process');
                    effect.element.parentNode.style.zIndex=1;
                    }
                });
        }
        if (this.interval !== null) {
            clearTimeout(this.interval);
            this.interval = null;
        }
        $(this.elementHeader).removeClassName('expanded');
    }
};
var $J = jQuery.noConflict();
var GRI = {
    addToCartButtons : function () {
        if ($J(".btn-cart").length > 0 || $J(".btn-cart-lg").length > 0) {
            var cart = $J('#perpetual_cart');
            $J(".btn-cart, .btn-cart-lg").click(function(e) {
                e.preventDefault();

                if ($J(this).hasClass('btn-cart-lg')) {
                    var params = '';
                    $J('#product_addtocart_form input:radio:checked, #product_addtocart_form input:not(":radio"), #product_addtocart_form select').each(function(i) {
                        if (this.value != '') {
                            if (i != 0) { params += '&' }
                            params += this.name + '=' + this.value;
                        }
                    });
                }
                else {
                    params = 'product=' + this.id + '&qty=1';
                }
                $J.ajax({
                    type: "GET",
                    url: "/ajaxCart.php",
                    beforeSend: function() {
                        $J('#cartHeader strong').addClass('loading');
                        $J('html,body').animate({'scrollTop':'0px'}, 750);
                    },
                    data: params,
                    success: function(data, textStatus){
                        if (textStatus == "error"){
                            $J('#cartHeader strong').removeClass('loading');
                            alert("There was an error adding this item to your cart.  Please call customer service for assistance.", "Error");
                            return;
                        }
                        if (data.result == "error"){
                            $J('#cartHeader strong').removeClass('loading');
                            alert("Sorry, an error occurred while adding the item to your cart.  The error was: '" + data.message + "'");
                            return;
                        }
                        $J.ajax({
                            type: "GET",
                            url: "/updateCart.php",
                            dataType: 'html',
                            success: function(msg){
                                cart.html(msg);
                                TopCart.showCart(7);
                            }
                        });
                    }
                });
            });
        }
        if ($J('#perpetual_cart .btn-remove').length > 0) {
            $J("#perpetual_cart .btn-remove").each(function() {
                $J(this).click(function(){
                    e.preventDefault();
                    $J('#cartHeader strong').addClass('loading');
                    var removeLink = $J(this).attr("href");
                    $J.ajax({
                        type: "GET",
                        url: removeLink,
                        success: function() {
                            $J.ajax({
                                type: "GET",
                                url: "/updateCart.php",
                                dataType: 'html',
                                success: function(msg){
                                    cart.html(msg);
                                }
                            });
                        }
                    });
                });
            });
        }
    }
};
$J(document).ready(function() {
    GRI.addToCartButtons();
});
