/**
 * @author jacksongabbard
 * 
 * This class requires jQuery and was written when 
 * jQuery 1.2.6 was the most current release.
 * 
 * This is not an AJAX class, though it simulates
 * AJAX by hiding/showing content dynamically based
 * on which "page" is requested.
 * 
 */
var MiniPager = {
		
	/* METHODS */
	init:function () {
		try {
			$; // check for the existence of jQuery
		} catch (e) {
			throw "The MiniPager class requires jQuery.";
		}
		
		var mp = this;
		
		$(this.pageClass()).each(function (i) {
			
			mp._pages[i] = this;
			
			if (i > 0) {
				$(this).hide();
			}
		});
		
		$(this.nextClass()).click(function () {
			mp.advance(true);
			return false;
		});
		
		$(this.prevClass()).click(function () {
			mp.rewind(true);
			return false;
		});
		
		$(this.pauseClass()).click(function () {
			mp.autoRotate(false);
			return false;
		});
		
				
		
	},
	
	
	advance:function (pause) {
		if (pause != null && pause == true) {
			this.autoRotate(false);
		}
		
		this.position("+1");
		
	},
	
	rewind:function (pause) {
		if (pause != null && pause == true) {
			this.autoRotate(false);
		}
		
		this.position("-1");
		
	},

	
	/* PRIVATE PROPERTIES */
	_rotationInterval:null,
	_pages:[],
	
	/* GETTERS / SETTERS */
	/* auto rotation */
	autoRotate:function(val) {
		
		if (val != null) {
			
			if (val == false) {
				this._autoRotate = false;
			} else if (val == true) {
				this._autoRotate = true;
			} else if (val > 0 && val < 99) {
				this._autoRotate = true;
				this.autoRotationDuration = val;
			}
			
			if (this._autoRotate) {
				if (this._rotationInterval) {
					clearInterval(this._rotationInterval);
				}
				var mp = this;
				this._rotationInterval = setInterval(function(){
					mp.advance();
				}, this.autoRotationDuration()*1000);
			} else {
				clearInterval(this._rotationInterval);
			}
			
		} else {
			
			return this._autoRotate;
			
		}
	},
	_autoRotate:false, // private
	
	/* page class */
	pageClass:function(val) {
		if (val != null) {
			this._pageClass = val;
		} else {
			return this.cssNamespace()+this._pageClass;
		}
	},
	_pageClass:"page",
	
	/* next button class */
	nextClass:function(val) {
		if (val != null) {
			this._nextClass = val;
		} else {
			return this.cssNamespace()+this._nextClass;
		}
	},
	_nextClass:"next",
	
	/* next button class */
	prevClass:function(val) {
		if (val != null) {
			this._prevClass = val;
		} else {
			return this.cssNamespace()+this._prevClass;
		}
	},
	_prevClass:"prev",
	
	/* next button class */
	pauseClass:function(val) {
		if (val != null) {
			this._pauseClass = val;
		} else {
			return this.cssNamespace()+this._pauseClass;
		}
	},
	_pauseClass:"pause",
	
	
	/* css namespace class */
	cssNamespace:function(val) {
		if (val != null) {
			this._cssNamespace = val;
		} else {
			return this._cssNamespace;
		}
	},
	_cssNamespace:".miniPager_",
	
	
	/* auto rotation time (seconds) */
	autoRotationDuration:function(val) {
		if (val != null) {
			this._autoRotationDuration = val;
			
			// update the timer...
			
		} else {
			return this._autoRotationDuration;
		}
	},
	_autoRotationDuration:4,
	
	/* position */
	position:function (val) {
		
		var mp = this;
		if (val != null) {
			
			var jump;
			
			if (typeof(val) == 'string' && val.indexOf("+") == 0) {
				
				jump = parseInt(val.substr(1));
				
				if (jump + this.position() >= this._pages.length) {
					
					this.position((jump + this.position()) % this._pages.length);
				} else {
					this.position(this._position + jump);
				}
				
			} else if (typeof(val) == 'string' && val.toString().indexOf("-") == 0) {
				
				jump = parseInt(val.substr(1));
				
				
				
				if (this.position() - jump < 0) {
					jump = this.position() % this._pages.length;
					
					// console.log(">>"+jump);
					this.position(this._pages.length - 1 - jump);
					
				} else {
					this.position(this._position - jump);
				} 
				
			} else if (val >= 0 && val < this._pages.length) {
				
				// console.log(val);
				
				mp._position = val;
				
				$.each(mp._pages, function (i) {
					
					if (i != mp._position) {
						$(this).hide();
					} else {
						$(this).show();
					}
				});
			}
			
			this.afterPaging();
			
		} else {
			return this._position;
		}
	},
	_position:0,
	
	addPaging:function (element, container) {
		if (this._pages.length) {
			var mp = this;
			for (var i = 0; i < this._pages.length; i++) {
				$(element.replace(/~num~/, i+1)).addClass("pager_"+i).appendTo(container).click(function() {
					var regex = /pager_([0-9]{1,})/;
					var page = regex.exec($(this).attr('class'));
					mp.autoRotate(false);
					mp.position(parseInt(page[1]));
					return false;
				});	
			}
		}
	},
	
	afterPaging:function () {
		
	}
	
	
	
};
