/*
	Paginator 4000 (3000 ?)
	- as seen on dirty.ru
	- idea by ecto (fhn.ru)
	- coded by karaboz (futurico.ru)
	- updated by Ryan Brooks (rechargemedia.com) to utilize mootools, planned replacement of concept to Drag.Move, etc
*/

Paginator = function(id, pagesTotal, pagesSpan, currentPage, baseUrl){
	this.htmlBox = $(id);
	if(!this.htmlBox || !pagesTotal || !pagesSpan) return;
	this.pagesTable;
	this.pagesTr; 
	this.sliderTr;
	this.pagesCells;
	this.slider;
	this.pagesTotal = pagesTotal;
	if(pagesSpan < pagesTotal)
	{
		this.pagesSpan = pagesSpan;
	}
	else
	{
		this.pagesSpan = pagesTotal;
		this.htmlBox.addClass('fullsize');
	}

	this.currentPage = currentPage;
	this.firstCellValue;
	this.baseUrl = (baseUrl) ? baseUrl : '#'
	this.initPages();
	this.initSlider();
	this.drawPages();
	this.initEvents();
	this.scrollToCurrentPage();
	this.setCurrentPagePoint();
} 
Paginator.prototype.initPages = function(){
	var html = "<table><tr>" 
	for (var i=1; i <= this.pagesSpan; ++i){
		html += "<td></td>"
	}
	html += "</tr>" +
	"<tr><td colspan='" + this.pagesSpan + "'>" +
	"<div class='scrollbar'>" + 
		"<div class='line'></div>" + 
		"<div class='current_page_point'></div>" + 
		"<div class='slider'>" + 
			"<div class='slider_point'></div>" + 
		"</div>" + 
	"</div>" +
	"</td></tr></table>";
	this.htmlBox.innerHTML = html;

	this.pagesTable = this.htmlBox.getFirst();
	this.pagesTr = this.pagesTable.getElements('tr')[0];
	this.sliderTr = this.pagesTable.getElements('tr')[1];
	this.pagesCells = this.pagesTr.getElements('td');
	this.scrollbar = this.sliderTr.getFirst();
	this.slider = this.pagesTable.getElement('div.slider');
	this.currentPagePoint = this.pagesTable.getElements('div.current_page_point')[0];
}
Paginator.prototype.initSlider = function(){
	this.slider.xPos = 0;
	this.slider.style.width = this.pagesSpan / this.pagesTotal * 100 + "%";	
}
Paginator.prototype.initEvents = function(){
	var _this = this;
	this.slider.addEvent('mousedown', function(e) {
		if (!e) var e = new Event(event);
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		this.dx = e.client.x - this.xPos;
		document.addEvent('mousemove', function(e) {
			if (!e) var e = new Event(event);
			_this.slider.xPos = e.client.x - _this.slider.dx;
			_this.setSlider();
			_this.drawPages();
		});
		document.addEvent('mouseup', function(e) {
			if (!e) var e = new Event(event);
			document.removeEvents('mousemove');
			document.removeEvents('mouseup');
			_this.enableSelection();
		});
		_this.disableSelection();
	});
	this.scrollbar.addEvent('mousedown', function(e) {
		//if(_this.paginatorBox.hasClass('fullsize')) return; Not used as far as I can tell.
		if (!e) var e = new Event(event);
		_this.slider.xPos = e.client.x - _this.scrollbar.getPosition().x - _this.slider.offsetWidth / 2;
		_this.setSlider();
		_this.drawPages();
	});
	window.addEvent('resize', resizePaginator);
}
Paginator.prototype.setSlider = function(){
	this.slider.setStyles({'left': this.slider.xPos});
}
Paginator.prototype.drawPages = function(){

	var percentFromLeft = this.slider.xPos / (this.pagesTable.offsetWidth);
	this.firstCellValue = Math.round(percentFromLeft * this.pagesTotal);
	var html = "";
	if(this.firstCellValue < 1)
	{
		this.firstCellValue = 1;
		this.slider.xPos = 0;
		this.setSlider();
	}
	else if(this.firstCellValue >= this.pagesTotal - this.pagesSpan)
	{
		this.firstCellValue = this.pagesTotal - this.pagesSpan + 1;
		this.slider.xPos = this.pagesTable.offsetWidth - this.slider.offsetWidth;
		this.setSlider();
	}
	var field;
	var spanContainer;
	for(var i = 0, cnt = this.pagesCells.length; i < cnt; ++i)
	{
		var currentCellValue = this.firstCellValue + i;
		var prefixLength = String(this.pagesTotal).length - String(currentCellValue).length;
		var prefix = this.makePrefix(prefixLength);
		spanContainer = new Element('span');
		
		if(currentCellValue == this.currentPage)
		{
			field = new Element('em', {'html': currentCellValue.toString()});
		}
		else
		{
			field = new Element('a', {'href': this.baseUrl.replace("REPLACEME", currentCellValue), 'html': currentCellValue.toString()});
		}
		spanContainer.innerHTML = field.getHTML();
		spanContainer.appendText(prefix.toString());
		this.pagesCells[i].innerHTML = spanContainer.getHTML();
	}
}
Paginator.prototype.scrollToCurrentPage = function(){
	this.slider.xPos = (this.currentPage - Math.round(this.pagesSpan/2)) / this.pagesTotal * this.pagesTable.offsetWidth;
	this.setSlider();
	this.drawPages();
}
Paginator.prototype.setCurrentPagePoint = function(){
	if(this.currentPage == 1)
	{
		this.currentPagePoint.setStyles({'left': 0});
	}
	else
	{
		this.currentPagePoint.setStyles({'left': this.currentPage / this.pagesTotal * this.pagesTable.offsetWidth})
	}
}
Paginator.prototype.makePrefix = function(prefixLength){
	var prefix = "";
	for (var i=0; i < prefixLength; ++i){
		prefix += "_";
	}
	return prefix;
}
Paginator.prototype.disableSelection = function(){
	document.onselectstart = function(){
		return false;
	}
	this.slider.focus();	
}
Paginator.prototype.enableSelection = function(){
	document.onselectstart = function(){
		return true;
	}
	this.slider.blur();		
}
resizePaginator = function (){
	if(typeof pag == 'undefined') return;
	pag.setCurrentPagePoint();
	pag.scrollToCurrentPage();
}