STMA.root = '/frontend/';

STMA.Basket = {
    active: false,
    nodeId: 'basketInner',
    upd: null,
    ref: null,
    init: function(o,c,i) {
        if (this.active && this.ref != i || typeof(o) == 'undefined') {
            obj = $g(this.nodeId);
            obj.parentNode.removeChild(obj);
            this.active = false;
            this.ref = null;            
        }
        if (typeof(o) != 'undefined') {
            if (!this.active) {
                this.ref = i;
                div = document.createElement('div');
                div.id = this.nodeId;
                div.className = c;
                
                o.parentNode.insertBefore(div, o);
                
                STMA.AJAX.init('basket', {node: this.nodeId},'refId='+i);            
            } else {
                o = $g(this.nodeId);
                o.parentNode.removeChild(o);
            }
            this.active = (this.active ? false : true);            
        }

    },
    add: function() {
        amount = STMA.Forms.getV('Amount');
        color = STMA.Forms.getV('Color');
        size = STMA.Forms.getV('Size');
        STMA.AJAX.init('basket',{onComplete: function(r){STMA.Basket.finish(r)}},'refId='+this.ref+';action=addItems;amount='+amount+';color='+color+';size='+size);
    },
    finish: function(r) {
        if (this.upd == null) {
            this.upd = $g('basketUpdated');
        }
        $g('basketContent').innerHTML = r;
        if (!this.upd.active) {
            this.upd.className = '';
        }
        this.upd.active = true;
        this.init();
        window.clearTimeout(this.timer);
        this.timer = window.setTimeout(function(){
            STMA.Basket.hide();
        }, 9000);
    },
    hide: function() {
        this.upd.active = false;
        this.upd.className = 'hidden';
    }
}
