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 (r != 'not-in-stock' && r != 'not-enough') {
            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);            
        } else if (r == 'not-in-stock') {
            alert('Tento produkt/variantu nelze objednat, protože momentálně není skladem.');
        } else {
            alert('Požadované množství variant/produktů nelze objednat, protože jich není dostatek skladem.');
        }
    },
    hide: function() {
        this.upd.active = false;
        this.upd.className = 'hidden';
    }
}

STMA.CatItems = {
    ref: null,
    defOrder: 'ASC',
    load: function() {
        this.by = STMA.Forms.getV('OrderBy');
        this.order = STMA.Forms.getV('OrderDir');
        this.page = STMA.Forms.getV('Page');
        this.onlyAct = STMA.Forms.getV('OnlyAct');
    },
    init: function(i) {
        this.ref = $g(i);
        var site = 1;
        
        var h = STMA.parseHash();
        if (h['site']) {
            site = h['site'];
        }
        STMA.AJAX.init('category.items',{node: i},'objType=1&refId='+STMA.Core.refId+'&page='+site);
        
        $('#ajaxDummy').remove();

       $(window).bind( 'hashchange', function(e) {
           var h = STMA.parseHash();
           var s = 1;
           if (h['site']) {
               s = h['site'];
           }
           STMA.CatItems.update(s, 'Page');
       });
    },
    update: function(v,c) {      
        $g('formInput_'+c).value = v;
        if (c != 'Page') {
            $g('formInput_Page').value = 1;    
        }
        this.load();
        STMA.AJAX.init('category.items', {node: this.ref.id}, 'refId='+STMA.Core.refId+';orderBy='+this.by+';orderDir='+this.order+';page='+this.page+';onlyAct='+this.onlyAct);
    },
    addListener: function() {
        
    }
    
}
