HeaderCart = {
    init: function (container) {
        this.container = $(container);
        this.element = this.container.up(0);
        this.elementHeader = this.container.previous(0);
        this.duration = 4000;
        this.timer = null;

        this.element.observe('mouseout', this.startTimer.bindAsEventListener(this));
        this.element.observe('mouseover', this.stopTimer.bindAsEventListener(this));
        this.elementHeader.observe('mouseover', this.headerCartOnMouseover.bindAsEventListener(this));

    },

    startTimer: function () {
        if($(this.elementHeader).hasClassName('expanded')) {
            this.timer = setTimeout(this.hideCart.bind(this), this.duration);
        }
    },

    stopTimer: function () {
        if (this.timer !== null) {
             clearTimeout(this.timer);
             this.timer = null;
        }
    },

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

    showCart: function (timePeriod) {
        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.timer = 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.timer !== null) {
            clearTimeout(this.timer);
            this.timer = null;
        }
        $(this.elementHeader).removeClassName('expanded');
    }
};

