Array.prototype.remove = function(from, to){
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};



var clicker = function(){
    var target_div = "#scContent_inner", paging_div = "#sc_paging", count_div = "#sc_count", queried_tag, page = 1, json_response, items_per_page = 49;
    var that = {};
    var set_tag = function(slug){
        queried_tag = [slug];
    };
    
    
    var set_paging_click = function(event){
        if (String(typeof event.data) !== "undefined") {
            page = event.data.page;
        }
        ajax_request();
    };
    /**
     *
     * @param {Object} event
     */
    var set_click = function(){
    
        var li = $(this);
        li.addClass("current").siblings().removeClass("current");
        set_tag(li.attr("id"));
        page = 1;
        ajax_request();
        
    };
    /**
     *
     */
    var set_paging = function(){
        $(paging_div).empty();
        if (json_response.html.length <= items_per_page) {
        
        
        
            //$(count_div).html(json_response.html.length + " von " + json_response.count);
                        
            var pages = Math.ceil(parseInt(json_response.count) / items_per_page);
            
            
            var max = page * items_per_page;
            var min = max - items_per_page + 1;
            if (page === pages) {
                max = json_response.count;
				min=max-json_response.html.length + 1;
            }
            $(count_div).html(min + " - " + max);
            
            var paging_ul = $("<ul>");
            for (var i = 1; i <= pages; i++) {
                var number = i;
                var li = $("<li>").html(i);
                if (i !== page) {
                    li.bind("click", {
                        page: i
                    }, set_paging_click);
                }
                else {
                    li.addClass("current");
                }
                li.appendTo(paging_ul);
                
            }
            
            paging_ul.appendTo(paging_div);
        }
    };
    /**
     *
     */
    var create_images = function(){
        $.each(json_response.html, function(i, item){
            var img = new Image();
            img.src = item.src;
            
            var a = $("<a>").attr({
                "class": "thumb",
                "href": item.href,
                "rel": queried_tag.join(","),
                "title": typeof item.link != "undefined" ? '<a href=\'' + item.link + '\'>>> Bild auf Quer-durch-asien.de</a>' : queried_tag.join(",")
            }).colorbox({
                height: "90%"
            });
            
            $(img).appendTo(a).load(function(){
                $(this).parent().fadeIn();
            });
            $(a).appendTo(target_div);
        });
    };
    /**
     *
     */
    var ajax_request = function(){
    
        $(target_div).empty();
        
        
        
        $.post("/ci/jax/index/json", {
            slug: queried_tag,
            page: page
        }, function(data){
            if (data.error === true) {
                $(target_div).html(data.message);
            }
            else {
                json_response = data;
                set_paging();
                create_images();
                
                
            }
            
        }, "json");
    };
    that.set_click = set_click;
    return that;
};
