/*
	paginator.js
	@author: matei stefanescu [matei.stefanescu at gmail.com]
	usage: 
		<style tye="text/css">
			div#your_content {
				overflow: hidden;
				width: whatever px; 
				height: whatever px;
			}
		</style> (can be external)
		....
		<script src="prototype.js"></script>
		<script src="effects.js"></script>
		<script src="paginator.js"></script>
		....
		<div id="your_container">
			<div id="your_content">
				Lorem ipsum....
			</div>
		</div>
		....
		<a href="#" id="prevpage">Previous Page</a>
		<a href="#" id="nextpage">Next Page</a>
		....
		<script type="text/javascript">
			var paginator = new Paginator("your_container", "your_content", "prevpage", "nextpage");
		</script>
*/
var Paginator = Class.create();
Paginator.prototype = {
	container: '',
	content: null,
	nextpage: null,
	prevpage: null,
	to: null,
	current_frame: 0,
	setContent: function(content) {
		var element = this.getElement(content);
		if (!element) {
			alert("Paginator error: could not find content div with id: " + content);
			return ;
		}
		this.content = element;
	},
	
	setContainer: function(container) {
		var element = this.getElement(container);
		if (!element) {
			alert("Paginator error: could not find container div with id: " + container);
			return ;
		}
		this.container = element;
	},
	
	setPrevpage: function(prevpage) {
		var element = this.getElement(prevpage);
		if (!element) {
			alert("Paginator error: could not find prevpage link with id: " + prevpage);
			return;
		}
		element.paginator = this;
		this.prevpage = element;
	},
	
	setNextpage: function(nextpage) {
		var element = this.getElement(nextpage);
		if (!element) {
			alert("Paginator error: could not find nextpage link with id: " + nextpage);
			return;
		}
		element.paginator = this;
		this.nextpage = element;
	},
	
	initialize: function(container, content, prevpage, nextpage) {
		this.setContainer(container);
		this.setContent(content);
		this.setPrevpage(prevpage);
		this.setNextpage(nextpage);
		this._init();
	},
	
	getHeight: function(div) {
		return parseInt(div.getStyle('height'));
	},
	
	getYPos: function(div) {
		if (div.getStyle('top') == null)
		{
			return 0;
		}
		else
		{
			return parseInt(div.getStyle('top'));
		}
	},
	
	getElement: function(element) {
		if (typeof element == "string") {
			if ($(element)) {
				return $(element);
			}
		} else if (typeof element == "object") {
			return element
		} 
		return false;
	},
	
	_init: function() {
		this.nextpage.observe('click', this.nextPage);
		this.prevpage.observe('click', this.prevPage);
	},
	
	nextPage: function() {
		new Effect.Move(this.paginator.content, {x: 0, y: this.paginator.getUnitScrollDown()});
	},
	
	prevPage: function() {
		new Effect.Move(this.paginator.content, {x: 0, y: this.paginator.getUnitScrollUp()});
	},
	
	getMaxPages: function() {
		return Math.ceil(this.getHeight(this.container)/this.getHeight(this.content));
	},
	
	getUnitScrollDown: function() {
		var minmax = this.getYPos(this.container) + this.getHeight(this.container) - this.getHeight(this.content);
		if (this.getYPos(this.content) <= minmax) {
			return 0;
		} else {
			return -(this.getHeight(this.container));
		}
	},
	
	getUnitScrollUp: function() {
		if (this.getYPos(this.content) >= this.getYPos(this.container)) {
			return 0;
		} else {
			return this.getHeight(this.container);
		}
	}
}