/*DelayedFilter = new Class({
    current_filter:{},
    next_filter:{},
    
    action_timer:null,
    
    form:null,
    
    filter_toggle_checkbox:function(name , value , status){
        if(this.action_timer) $clear(this.action_timer);
    },
    
    initialize: function(form){
        form.getElements("input").each(function(el){
            //If the input is checkbox
            if(el.get("type") == "checkbox"){
                if(!(this.current_filter)) this.current_filter[el.get("name")] = [];
                if(el.checked)
                    this.current_filter[el.get("name")].push(el.value);
                    
                
            }
            
        }.bind(this));
        
        consoel.log(this.current_filter);
    }
});

window.addEvent("domready" , function(){
    delayed_filter = new DelayedFilter($("event_type_filter"));
    alert("test");
})*/

$.fn.delayed_update = function(name){
    that = this;
    
    that.startup = [];
    that.current = [];
    that.name = name;
    that.timer = null;
    
    that.refresh = function(){
        var base_address = that.parent("form").attr("action");
        
        $(that.current).each(function(){
            if(base_address.indexOf("?")>=0)
                base_address += "&";
            else
                base_address += "?";
            base_address += that.name + "=" + this;
        });
        
        window.location.href = base_address;
    }
    
    that.checkbox_click = function(){
        if(that.timer) clearTimeout(that.timer);
        that.timer = null;
        
        if(this.checked){
            that.current.push(this.value);
        } else {
            that.current.remove(this.value);
        }
        
        that.current.sort();
        
        if(that.startup.join("-") != that.current.join("-")){
            that.timer = setTimeout(that.refresh , 2000)
        }
    };
    
    this.find("input[name="+that.name+"]").each(function(){
        
        
        if(this.checked){
            that.startup.push(this.value);
            that.current.push(this.value);
        }
        
        $(this).bind("click",that.checkbox_click);
        
    });
    
    that.startup.sort();
    
}

$('document').ready(function(){
    $("#event_filter").delayed_update('types');
});