
(function(jQuery) {

jQuery.fn.ajaxList = function(url, options) {
    var el = this;

    this.list_index = 0;
    this.page_count = 1;
    this.page_display = 7;
    this.url = url;

    this.paging_element = options.paging_element;
    this.current_page_class = options.current_page_class;
    this.current_sort_class = options.current_sort_class;

    this.params = {
        list_size: options.list_size
    }

    //setup archiving request params
    if (options.arch_year)
        if (options.arch_year != "None")
            this.params = jQuery.extend({arch_year: options.arch_year}, this.params);
    if (options.arch_month)
        if (options.arch_month != "None")
            this.params = jQuery.extend({arch_month: options.arch_month}, this.params);

    // initialize the paging controls
    this.paging_element.find('[href=#next]').click(function() {return el.navigateDirection('next');});
    this.paging_element.find('[href=#prev]').click(function() {return el.navigateDirection('prev');});
    this.pages_element = this.paging_element.find('.pages');

    function loading() {
        // calculate the position of the loading icon
        var el_pos = el.position();
        var el_top = el_pos.top - 16 + el.height()/2;
        var el_left = el_pos.left - 16 + el.width()/2;

        // insert the loading icon and fade it in
        el.before('<img class="indicator" src="/indicator.gif" style="display:none;" />');
        var indicator = el.siblings('img.indicator');
        indicator.css({'top':el_top, 'left':el_left, 'position':'absolute'});
        indicator.toggle();

        // fade the element background out
        //el.toggle();
    };

    function done() {
        el.siblings('img.indicator').toggle().remove();
        //el.toggle();
    };

    function setupPagingLinks() {
        // determine the set of pages
        var median = Math.floor(el.page_display / 2);
        var cursor = Math.max(1, (el.current_page - median));
            if ((cursor + el.page_display) >= el.page_count) {
                cursor = Math.max(1, el.page_count - el.page_display);
            }

        el.pages_element.children('a').each(function() {
            var link_el = $(this);
            link_el.text(cursor);
            if (el.current_page_class) {
                link_el.removeClass(el.current_page_class);
                if (cursor == el.current_page) {
                    link_el.addClass(el.current_page_class);
                }
            }
            link_el.unbind('click');
            link_el.click(function() {return el.navigatePage(parseInt($(this).text()));});
            cursor++;
        });
        el.paging_element.find('[href=#next]').text("Next");
        el.paging_element.find('[href=#prev]').text("Previous");
    };

    this.sort = function(link_el, options) {
        var link_el = link_el;
        loading();

        this.params = jQuery.extend(this.params, options);

        this.load(this.url, this.params, function() {
            el.page_count = parseInt(el.find('div.page-count').text());
            el.current_page = parseInt(el.find('div.current-page').text());

            if (el.current_sort_class && link_el != null) {
                link_el.siblings('a').removeClass(el.current_sort_class);
                link_el.addClass(el.current_sort_class);
            };

            if (el.pages_element) {
                el.pages_element.empty();

                // create all the paging links
                if (el.page_count > 1)
                {
                    var page_link_count = Math.min(el.page_count, el.page_display)
                    for(var n = 0; n < page_link_count; n++) {
                        el.pages_element.append('<a href="#"></a>');
                    };
                    setupPagingLinks();
                }

                // set the class of the first page link
                if (el.current_page_class) {
                    el.pages_element.children('a:first').addClass(el.current_page_class);
                };
            };
            done();
        });

        return false;
    };

    this.navigateDirection = function(direction) {
        loading();

        //scroll to top of page
        scrollTo(0,0);

        var params = jQuery.extend({
            list_index: this.list_index,
            list_nav: direction
        }, this.params);

        this.load(this.url, params, function() {
            el.page_count = parseInt(el.find('div.page-count').text());
            el.current_page = parseInt(el.find('div.current-page').text());
            el.list_index = parseInt(el.find('div.list-index').text());

            setupPagingLinks();
            done();
        });

        return false;
    };

    this.navigatePage = function(page) {
        loading();
        
        //scroll to top of page
        scrollTo(0,0);

        var params = jQuery.extend({
            list_page: page
        }, this.params);

        this.load(this.url, params, function() {
            el.current_page = parseInt(el.find('div.current-page').text());
            el.list_index = parseInt(el.find('div.list-index').text());

            setupPagingLinks();
            done();
        });

        return false;
    };

    return this.each(function() {
        var obj = $(this);
    });
};

})(jQuery);
