var Imtech = {};
Imtech.Pager = function() {
    this.paragraphsPerPage = 3;
    this.currentPage = 1;
    this.pagingControlsContainer = '#pagingControls';
    this.pagingContainerPath = '#content';
	this.pagerName = 'pager';
	this.showButtons = false;
	this.leftEnabledImg = '';
	this.leftEnabledActiveImg = '';
	this.leftDisabledImg = '';
	this.rightEnabledImg = '';
	this.rightEnabledActiveImg = '';
	this.rightDisabledImg = '';
	this.pageChanged = null;
	
    this.numPages = function() {
        var numPages = 0;
        if (this.paragraphs != null && this.paragraphsPerPage != null) {
            numPages = Math.ceil(this.paragraphs.length / this.paragraphsPerPage);
        }
        return numPages;
    };
    this.showPage = function(page) {
        this.currentPage = page;
        var html = '';
        //this.paragraphs.slice((page-1) * this.paragraphsPerPage,
        //    ((page-1)*this.paragraphsPerPage) + this.paragraphsPerPage).each(function() {
        //    html += $(this).outerHTML();
        //});
        html = this.paragraphs.slice((page-1) * this.paragraphsPerPage,
            ((page-1)*this.paragraphsPerPage) + this.paragraphsPerPage).clone();
        $(this.pagingContainerPath).html(html);
        this.renderControls(this.pagingControlsContainer);
		
		if(this.pageChanged != null && this.pageChanged != undefined)
			this.pageChanged(this.currentPage, this.numPages());
    }
    this.renderControls = function(container) {
		if(numPages != 1)
		{
			var pagingControls = 'Page: <ul>';
			var numPages = this.numPages();
			for (var i = 1; i <= numPages; i++) {
				if(i <= 3 || i >= numPages - 2 || Math.abs(this.currentPage-i) < 2)
					if (i != this.currentPage) {
						pagingControls += '<li><a href="#" onclick="' + this.pagerName + '.showPage(' + i + '); return false;">' + i + '</a></li>';
					} else {
						pagingControls += '<li>' + i + '</li>';
					}
				else if(Math.abs(this.currentPage - 4) > 1 && i == 4)
					pagingControls += '<li>...</li>';
				else if(this.currentPage > 2 && this.currentPage < (numPages - 3) && Math.abs(this.currentPage - (numPages - 3)) > 1 && i == numPages - 3)
					pagingControls += '<li>...</li>';
			}
			pagingControls += '</ul>';
			
			if(this.showButtons)
			{
				if(this.currentPage == 1)
				{
					pagingControls += '<span class="leftArrows"><img alt="No Previous" src="' +
						this.leftDisabledImg + '" /></span>';
				}
				else
				{
					pagingControls += '<a href="#" onclick="' + this.pagerName + '.showPage(' + (this.currentPage - 1) +
						'); return false;" class="leftArrows"><img alt="Previous" src="' + this.leftEnabledImg +
						'" onmouseover="this.src=\'' + this.leftEnabledActiveImg +
						'\'" onmouseout="this.src=\'' + this.leftEnabledImg + '\'" /></a>';
				}
				
				if(this.currentPage == numPages)
				{
					pagingControls += '<span href="#" class="rightArrows"><img alt="No Next" src="' +
						this.rightDisabledImg + '" /></span>';
				}
				else
				{
					pagingControls += '<a href="#" onclick="' + this.pagerName + '.showPage(' + (this.currentPage + 1) +
						'); return false;" class="rightArrows"><img alt="Next" src="' + this.rightEnabledImg +
						'" onmouseover="this.src=\'' + this.rightEnabledActiveImg +
						'\'" onmouseout="this.src=\'' + this.rightEnabledImg + '\'" /></a>';
				}
			}
                        if(container instanceof Array)
                        {
                            for(c in container)
                            {
								$(container[c]).html(pagingControls);
                            }
                        }
                        else
                        {
							$(container).html(pagingControls);
                        }
		}
		else
		{
                        if(container instanceof Array)
                        {
                            for(c in container)
                            {
								$(container[c]).html('');
                            }
                        }
                        else
                        {
							$(container).html('');
                        }
		}
    }
}

//jQuery hack to add outterHTML
jQuery.fn.outerHTML = function(s) {
    return s
        ? this.before(s).remove()
        : jQuery("<p>").append(this.eq(0).clone()).html();
};
