// Generated from http://frog.gb.int/dynamic/catalog/ajax.phtml, DO NOT EDIT!
if(typeof(CatalogController) == 'undefined')
    CatalogController = new Object();

Object.extend(CatalogController,
{
    _callback48e8dac7c6e6c: "http://frog.gb.int/dynamic/catalog/ajax.phtml",

    _getJSON: function(r, j)
    {
        j = (j || r.headerJSON || r.responseJSON);
        if(j == null)
            return null;
        if(j.errors || j.failed)
        {
            var f = function(a, c)
            {
                var e = $(c);
                if(a.length > 0)
                {
                    var s = '<ul>';
                    $(a).each(function(e){ s += '<li>'+e+'</li>'; });
                    s += '</ul>';
                    if(e){ e.update(s); e.show(); }
                }
                else
                {
                    if(e){ e.hide(); }
                }
            }
            if(!j.errors)
            {
                j.errors = {error: [], warning: [], info: []}
                if(j.failed)
                    j.errors.error[0] = j.message;
            }

            f(j.errors.error, 'globuleError');
            f(j.errors.warning, 'globuleWarning');
            f(j.errors.info, 'globuleInfo');
        }
        return j;
    },

    _proxy: function(callbackurl, methodName, args, sync)
    {
        var targs = $H();
        for(var i = 0; i < args.length-1; i++){ targs.set('args[' + i + ']', args[i]); }
        callback = args[args.length-1];
        var ar = new Ajax.Request(callbackurl + '?method=' + methodName,
        {
            method: 'post',
            asynchronous: (!sync),
            parameters: targs,
            onComplete: (sync)?null:callback,
            onException: function(request, e){console.debug(request, e);}
        });
        if(sync)
            return new Ajax.Response(ar);
    },

    countResults: function(){ this._proxy(this._callback48e8dac7c6e6c, 'countResults', arguments, false); },
    countResults_sync: function(){ return this._proxy(this._callback48e8dac7c6e6c, 'countResults', arguments, true); },

    _countCompatibles: function(){ this._proxy(this._callback48e8dac7c6e6c, '_countCompatibles', arguments, false); },
    _countCompatibles_sync: function(){ return this._proxy(this._callback48e8dac7c6e6c, '_countCompatibles', arguments, true); },

    _t: ''
});
// End of generated code

Object.extend(CatalogController,
{
    _tree: null,
    _timer: null,
    
    init: function(tree)
    {
        this._tree = tree;
        
        if($('filterForm'))
        {
            $A($('filterForm').getElementsByTagName('input')).each(function(i){
                i = $(i);
                
                if(i.type == 'text')
                    i.observe('keypress', this._delayCountFilter.bind(this));
                else if(i.type == 'checkbox' || i.type == 'radio')
                    i.observe('change', this._delayCountFilter.bind(this));
            }.bind(this));
            $A($('filterForm').getElementsByTagName('select')).each(function(i){
                if(i.options.length == 2)
                    $(i).disable();
                $(i).observe('change', this._delayCountFilter.bind(this));
            }.bind(this));
        }
    },
        
    countFilter: function()
    {
        this._timer = null;
        var f = this._getFilters();
        var q = ($('q'))?$F('q'):null;
        this.countResults(this._tree, f.toJSON(), q, function(r, j){ $('filterCount').update(j.out); } );
    },
    
    countCompatibles: function(from)
    {
        if(from != 'product')
            $('compatibility_'+from+'_loader').show();
        var q = ($('q'))?$F('q'):null;
        this._countCompatibles(this._tree, q, $F('compatibility_brand'), $F('compatibility_range'), $F('compatibility_product'), from, function(r, j)
        {
            j = this._getJSON(r, j);
            if(j == null)
                return;
            $('filterCount').update(j.out.count);
            switch(from)
            {
                case 'brand':
                var cr = $('compatibility_range');
                cr.selectedIndex = 0;
                cr.options.length = 1;
                $A(j.out.data).each(function(d){
                    cr.options[cr.options.length] = new Option(d.name, d.id);
                });
                var cp = $('compatibility_product');
                cp.selectedIndex = 0;
                cp.options.length = 1;
                $('compatibility_brand_loader').hide();
                break;
                case 'range':
                var cp = $('compatibility_product');
                cp.selectedIndex = 0;
                cp.options.length = 1;
                $A(j.out.data).each(function(d){
                    cp.options[cp.options.length] = new Option(d.name, d.id);
                });
                $('compatibility_range_loader').hide();
                break;
                case 'product':
                break;
            }
        }.bind(this));
    },
    
    applyFilter: function(base)
    {
        var f = this._getFilters();
        if(f.size() == 0)
            document.location = base;
        else
        {
            $('resultPage').value = base.gsub('^.*\\?p=', '');
            $('filterForm').submit();
        }
    },
    
    _delayCountFilter: function()
    {
        if(this._timer)
            clearTimeout(this._timer);
        this._timer = setTimeout(this.countFilter.bind(this), 300);
    },
    
    _getFilters: function()
    {
        var inputs = $A($('filterList').getElementsByTagName('input'));
        var selects = $A($('filterList').getElementsByTagName('select'));
        var f = $H();
        
        inputs.each(function(input)
        {
            if(input.type == 'hidden' || input.type == 'button' || input.type == 'submit' || input.type == 'reset')
                return;
            var v = input.value.replace(/^\s+|\s+$/, '');
            if(v.length <= 0)
                return;
            f.set(input.id, {value: v, operator: '='});
        });
        selects.each(function(select)
        {
            if(select.selectedIndex < 0 || select.options.length == 2)
                return;
            if(select.id.match(/-operator$/))
            {
                var s = select.id.replace(/-operator$/, '');
                var v = f.get(s);
                if(v == null)
                    return;
                v.operator = select.options[select.selectedIndex].value;
                f.set(s, v);
            }
            else
            {
                var v = $A();
                $A(select.options).each(function(option){ if(option.selected && option.value.length > 0){ v.push(option.value); }});
                if(v.length > 0)
                    f.set(select.id, {value: v, operator: '='});
            }
        });
        
        return f;
    },
    
    compare: function(url)
    {
        var cmpv = '?_=_';
        var checkCount = 0;
        $A($('product-list').getElementsByTagName('input')).each(function(inp)
        {
            if(inp.type == 'text')
                return;
            if(inp.checked)
            {
                cmpv += "&p[]="+inp.value;
                checkCount++;
            }
        });
        if(checkCount < 2)
            return;
        document.location = url + cmpv;
    }
});