window.addEvent('domready', function() {
    function filter(event) {
        var period = $('period').get('alt')
        var activity_types = $$('.checkbox[checked]');
        var current_url = document.baseURI;
        var start = current_url.search(/\/\d+\//) + 1;
        var user_id = current_url.slice(start, -1);
        var data = "";
        activity_types.each(function(el){
            data += "&activity_type="+el.value;
        });
        data += "&period="+period
        var jsonRequest = new Request.JSON({
        url: "/activity/filter/"+user_id+"/",
            method: 'get',
            data: data,
            onComplete: function(json){
                $('column_left').empty();
                if(json.actions.length){
                json.actions.each(function(item){
                    if (item[3] == 1) {
                        var action = "<span style='color: #636363; font-weight: bold;'>commented</span>";
                    }else if(item[3] == 4) {
                        var action = "<span style='color: #9F9F96; font-weight: bold;'>answered</span>";
                    }else if(item[3] == 2) {
                        var action = "<span style='color: #3B93FF; font-weight: bold;'>code added</span>";
                    }else if(item[3] == 3) {
                        var action = "<span style='color: red; font-weight: bold;'>code edited</span>";
                    }
                    var table = new Element("table", {class: "history", html: "<tr><td width='130'><div class='history_date'><div class='date_brick'>"+item[1]+"<br />"+item[2]+"</div>"+item[0]+"</div></td><td><span class='accept-answer-link'>"+action+"</span></td><td><a style='color: #498b38;' href='/code/"+item[4]+"/'>"+item[5]+"</a></td></tr>"});
                    (table).inject($('column_left'));
                });
            }else{
                var el = new Element("div", {html: "<span>No activity found<span>"});
                el.inject($('column_left'));
            }
            }
        }).send();
    }
    $$('.checkbox').addEvent('click', filter);
    $$('.range').addEvent('click', function(event){
        $('period').removeProperty('id');
        this.set('id', 'period');
        event.stop();
        filter(event);
    });
});


