El = {
    toggle : function (id, opt) {
        el = $('#El_'+id+'_container');

        el.find('input[type!=hidden],button,textarea,select').each(function() {
            if(opt!=undefined){
                this.disabled = opt ?  "disabled" : null;
            } else {
                if(!this.disabled){
                    this.disabled = "disabled";
                } else {
                    this.disabled = null;
                }
            }
        });
        
        return el;
    },

    show : function (id) {
        el = $('#El_'+id+'_container');
        el.show();
    },

    hide : function (id) {
        el = $('#El_'+id+'_container');
        el.hide();
    },

    group : {
        enable : function (id) {
            El.group.toggle(id, false)
        },

        disable : function (id) {
            El.group.toggle(id, true);
        },
        
        toggle : function (id, opt) {
            $('#' + id).find('input[type!=hidden],button,textarea,select').not('.disabless').each(function() {
                if(opt!=undefined){
                    this.disabled = opt ?  "disabled" : null;
                } else {
                    if(!this.disabled){
                        this.disabled = "disabled";
                    } else {
                        this.disabled = null;
                    }
                }
            });
        }
    },

    range : {
        check : function (id, obj) {
            clickedOn = (obj.id == id + '_min') ? 'min' : 'max';

            $emin = $('#' + id + '_min');
            $emax = $('#' + id + '_max');

            min = parseFloat($emin.val());
            max = parseFloat($emax.val());

            if(clickedOn == 'min') {
                if(min>max) {
                    $emax.val(min);
                }
            } else if(clickedOn == 'max') {
                if(max<min) {
                    $emin.val(max);
                }
            }

            $emin.find('option').each(function(){
                v = this.value;
                if(v>max) {
                    $(this).addClass('over-under');
                } else {
                    $(this).removeClass('over-under');
                }
            });
            $emax.find('option').each(function(){
                v = this.value;
                if(v<min) {
                    $(this).addClass('over-under');
                } else {
                    $(this).removeClass('over-under');
                }
            });


        }
    },

    select : {
        options : {
            remove : function (id) {
                $('#' + id).children().remove();
            },

            addFromJson : function(id, json) {
                $select = $('#' + id);

                for(i in json) {
                    $select.append('<option value="'+i+'">'+json[i]+'</option>');
                }
            },

            replaceFromJson : function(id, json) {
                El.select.options.remove(id);
                El.select.options.addFromJson(id, json);
                $('#'+id).effect('highlight');
            }
        }
    },

    geo : {
        changeCountry : function (obj, targetId, loadData) {
            if(obj.value == 'load-less' || obj.value == 'load-more') {
                if(obj.value == 'load-less') {
                    mode = 'less';
                    nextload = 'more';
                } else if(obj.value == 'load-more') {
                    mode = 'more';
                    nextload = 'less';
                }

                $.getJSON('/dict/countries', {mode : mode}, function (json) {
                    El.select.options.replaceFromJson(obj.id, json);
                });
                return true;
            }

            El.geo.change(obj, targetId, loadData);
        },

        change : function (obj, targetId, loadData) {
            
            $o = $(obj);
            iso = $o.val();

            switch (loadData) {
                case 'region':
                    $.getJSON('/dict/regions', {iso : iso}, function (json) {
                        El.select.options.replaceFromJson(targetId, json);
                    });
                    
                    break;

                default:
                    break;
            }
        }
    },

    tabs : {
        activate : function (obj, tabIndex, tabCount) {
            for(i=0; i<tabCount; i++) {
                if(i==tabIndex) {
                    $('#tab-trigger-'+i).attr('class', 'active');
                    $('#tab-'+i).show();
                } else if(i<tabIndex) {
                    $('#tab-trigger-'+i).attr('class', 'prev');
                    $('#tab-'+i).hide();
                } else {
                    $('#tab-trigger-'+i).attr('class', 'next');
                    $('#tab-'+i).hide();
                }
            }






            return false;
        }
    }

}
