/// <reference path="jquery-vsdoc.js" />

function Pager(globalName, pagerId) {
    this.mGlobalName = globalName;
    this.mPagerId = pagerId;
    this.mPageCount = 0;
    this.mCurrentPage = 0;
    this.mjPages = [];
    this.mPagerFirstCall = true;

    this.gotoPage = function(page) {
        if (page < this.mPageCount) {
		
            var htmlPager = '<div class="pager">';
			var pagerTitles = [];

			if ($('.pager').length > 0) {
				$('.pager:first *:gt(0)').each(function() {
					pagerTitles.push($(this).html());
				});
			}
			else {
				var i = 1;
				$('#pages>div:gt(0)').each(function() {
					if ($(this).attr('title') != '') {
						pagerTitles.push($(this).attr('title'));
						$(this).attr('title','');
					}
					else
						pagerTitles.push('Page ' + i);
					i++;
				});
			}
			
            if (page > 0) {
                htmlPager += '<a href="#" onclick="return gotoPage(' + this.mGlobalName + ',' + (page - 1) + ');">&laquo; Prev</a>';
            } else {
                htmlPager += '<span class="pager_current">&laquo; Prev</span>';
            }
            for (i = 0; i < this.mPageCount; i++) {
                htmlPager += '&nbsp;-&nbsp;';
				
                if (i == page) {
                    htmlPager += '<span class="pager_current">' + pagerTitles[i] + '</span>';
                } else {
                    htmlPager += '<a href="#" onclick="return gotoPage(' + this.mGlobalName + ',' + i + ');">' + pagerTitles[i] + '</a>';
                }
            }
            htmlPager += '&nbsp;-&nbsp;';
            if (page < this.mPageCount - 1) {
                htmlPager += '<a href="#" onclick="return gotoPage(' + this.mGlobalName + ',' + (page + 1) + ');">Next &raquo;</a>';
            } else {
                htmlPager += '<span class="pager_current">Next &raquo;</span>';
            }
            htmlPager += '</div>';

            $('#' + this.mPagerId + '_bottom').html(htmlPager);
            $('#' + this.mPagerId + '_top').html(htmlPager);

            var self = this;

            var fnShowCurrentPage = function() {
                var jCurrentPage = $(self.mjPages.get(page));

                if (i != page) {
                    if (self.mPagerFirstCall) {
                        jCurrentPage.show();
                    } else {
                        jCurrentPage.fadeIn('fast');
                    }
                }
            };

            /* Hide all but the current page */
            for (i = 0; i < self.mPageCount; i++) {
                var jPage = $(self.mjPages.get(i));

                if (i != page) {
                    if (self.mPagerFirstCall) {
                        jPage.hide();
                    } else {
                        if (jPage.is(':visible'))
                            jPage.fadeOut('fast', fnShowCurrentPage);
                    }
                }
            }

            if (self.mPagerFirstCall)
                fnShowCurrentPage();

            self.mCurrentPage = page;
            self.mPagerFirstCall = false;
        }

        return false;
    }


    var jPages = $('#' + pagerId);

    if (jPages.length > 0) {

        this.mjPages = jPages.find('> div');
        jPages.append('<div id="' + pagerId + '_bottom"></div>');
        jPages.prepend('<div id="' + pagerId + '_top"></div>');

        this.mPageCount = this.mjPages.length;
        if (this.mPageCount > 0) {
            this.gotoPage(0);
        }
    }
}

function gotoPage(global,page) {
    return global.gotoPage(page);
}

var gPager;

$(function() {
	gPager = new Pager('gPager','pages');
});
