/* collections.js, polarrose.com, johanb@polarrose.com */

var collection = {
    config: {
        target: "results",
        limit: 20,
        /*sortBy: window.location.search.substring(1).split('=')[1] == "date" ? "date" : "name",*/
        sortBy: window.location.search.toQueryParams()['paging.sortBy'] == "date" ? "date" : "name"
    },
    shield: {
        progress: function(what){
            this.el = $$(".shield").last();    
            if(what == "start"){
                this.tmp = this.el.innerHTML;
                this.el.addClassName("loading");
                this.el.update();
            } else if(what == "stop"){
                this.el.removeClassName("loading");
                this.el.update(this.tmp);
                this.disable(this.el);
            }
        },
        disable: function(el){
            el.addClassName("disabled");
            var link = el.down(".link_add_more")
            link.removeClassName("link_add_more");
            link.removeAttribute("href");
        },
        scroll: function(el){
            $$("body").first().removeClassName("height100"); // remove 100% height before we scroll
            window.opera ? Element.scrollTo(el,{
                duration: 1,
                offset: -10
            }) : Effect.ScrollTo(el,{
                duration: 1,
                offset: -10
            });
        }
    },
    add: function(str){
        this.request(this.hash(str));
    },
    hash: function(str){
        return str.split("#")[1];
    },
    total: function(n){
        $("count").update(n);
    },
    inject: function(html){
        $(this.config.target).insert(html);
    },
    request: function(n){
        $("ajax_results").request({
            parameters: {
                "paging.pageSize" : this.config.limit,
                "paging.pageNo" : n,
                "paging.sortBy" : this.config.sortBy
            },
            onCreate: function(){
                this.shield.progress("start");
            }.bind(this),
            onComplete: function(xhr){
                this.shield.progress("stop");
                this.inject(xhr.responseText);
                this.total($$(".mugshot").length); // update totals
                this.shield.scroll($$(".shield.disabled").last()); //scrolls to mugshot next to disabled shield
                Event.addBehavior.reload(); // reload events
            }.bind(this)
        })
    }
}

Event.addBehavior({
    '.mugshot:mouseover': function(){
        this.addClassName('hover');
    },
    '.mugshot:mouseout': function(){
        this.removeClassName('hover');
    },
    '.link_add_more:click': function(){
        collection.add(this.hash);
        return false;
    },
    '#link_top:click': function(){
        Effect.ScrollTo("header", {duration:1});
        return false;
    },
    '#button_new_search:click': function(){
        if($$(".mugshot").length > collection.config.limit){
            Effect.ScrollTo("header",{
                duration: 1,
                afterFinish: function(){ $("input_search").select(); }
            });
        } else {
            $("input_search").select();
        }
        return false;
    }
});

/* dom ready */
document.observe("dom:loaded", function(){
    results_width.change();
    Event.observe(document.onresize ? document : window, "resize", results_width.change.bind(results_width));
});