Ext.namespace('PBS.utils');

PBS.utils.Paginator = function(container_id, configuration) {
    this.page_request = new Ext.util.Event();

    var paginator_links = new Array();
    var paginator_elements = new Array();

    // Setup first link
    if(configuration.show_first) {
        if(configuration.first + 1 == configuration.page_numbers[0]) {
            paginator_elements.push({tag: "a", id: container_id+"_first", href: "javascript:void(0)", html: configuration.first }, ' ');
            paginator_links.push({id: container_id+"_first", page: configuration.first});
        }
        else {
            paginator_elements.push({tag: "a", id: container_id+"_first", href: "javascript:void(0)", html: configuration.first}, {tag: "span", cls: "skip", html: " &hellip; "});
            paginator_links.push({id: container_id+"_first", page: configuration.first});
        }
    }

    // Setup page number list
    for(var j=0; j<configuration.page_numbers.length; j++) {
        var page_number = configuration.page_numbers[j];
        if(page_number == configuration.page) {
            paginator_elements.push({tag:"strong", cls: "current", html: page_number}, " ");
        }
        else {
            paginator_elements.push({tag: "a", id: container_id+"_page"+page_number, href:"javascript:void(0);", html:page_number}, " ");
            paginator_links.push({id: container_id+"_page"+page_number, page: page_number});
        }
    }

    // Setup last link
    if(configuration.show_last) {
        if(configuration.last == configuration.page_numbers[configuration.page_numbers.length-1] + 1) {
            paginator_elements.push({tag: "a", id: container_id+"_last", href: "javascript:void(0)", html: configuration.last});
            paginator_links.push({id: container_id+"_last", page: configuration.last});
        }
        else {
            paginator_elements.push({tag: "span", cls: "skip", html: "&hellip; "});
            paginator_elements.push({tag: "a", id: container_id+"_last", href: "javascript:void(0)", html: configuration.last});
            paginator_links.push({id: container_id+"_last", page: configuration.last});
        }
    }

    var num_per_page_elements = new Array();
    var num_per_page_links = new Array();

    // Create Num Per Page
    for(var j=0; j<configuration.num_per_page_options.length; j++) {
        var num_per_page = configuration.num_per_page_options[j];
        var num_per_page_text = num_per_page;
        if(num_per_page == 0) {
            num_per_page_text = "All";
        }

        if(num_per_page == configuration.num_per_page) {
            num_per_page_elements.push({tag: "strong", cls: "current", html: num_per_page_text}, " ");
        }
        else {
            num_per_page_elements.push({tag: "a", id: container_id+"_num"+num_per_page, href: "javascript:void(0);", html: num_per_page_text}, " ");
            num_per_page_links.push({id:container_id+"_num"+num_per_page, num_per_page: num_per_page});
        }
    }
    num_per_page_elements.push({tag: "span", html: " per page"});

    // Create Paginator
    var list = Ext.DomHelper.overwrite(container_id, {
        tag: 'div', children: [
                {tag: 'p', cls: 'results', html: ' Courses found', children: [
                        {tag: 'strong', html: ''+configuration.hits}
                 ]},
                {tag: 'div', cls: 'yui-g paginator_bar', children: [
                        {tag: 'div', cls: 'yui-u first', children: [
                                {tag: 'div', cls: 'pagination', children: [
                                        {tag: 'strong', html: 'Page: '},
                                        {tag: 'span', children: paginator_elements}
                                 ]}
                         ]},
                        {tag: 'div', cls: 'yui-u', children: [
                                {tag: 'div', cls: 'results_per_page align_right', children: [
                                        {tag: 'strong', html: 'Show: '},
                                        {tag: 'span', children: num_per_page_elements}
                                 ]}
                     ]}
                 ]}
        ]});

    for(var k=0; k<paginator_links.length; k++) {
        var link = paginator_links[k];
        link.num_per_page = configuration.num_per_page;
        Ext.get(link.id).on("click", function(e, el) {
            this.event.fire(this.link);
        }, {event: this.page_request, link:link});
    }

    for(var k=0; k<num_per_page_links.length; k++) {
        var link = num_per_page_links[k];
        link.page = configuration.page;
        Ext.get(link.id).on("click", function(e, el) {
            this.event.fire(this.link);
        }, {event: this.page_request, link:link});
    }
};


/*
var paginator = new PBS.utils.Paginator(["paginator"], ["num_per_page"], data.paginator);
paginator.page_request.addListener(function(kwargs) {
    if(kwargs.page) {
        PBS.myresources.request_config.params[0].page = kwargs.page;
    }
    if(kwargs.num_per_page >= 0) {
        PBS.myresources.request_config.params[0].num = kwargs.num_per_page;
    }
    PBS.myresources.request_data();
});
*/
