//Written by Nathan Faubion: http://n-son.com
//Use this or edit how you want, just give me
//some credit!

function jsScroller (o, w, h,f) {

	if(f==null) f=false;


	var self = this;
	var list = o.getElementsByTagName("div");
	for (var i = 0; i < list.length; i++) {
		if (list[i].className.indexOf("Scroller-Container") > -1) {
			o = list[i];
		}
	}
	
	//Private methods
	this._setPos = function (x, y) {
		if (x < this.viewableWidth - this.totalWidth) 
			x = this.viewableWidth - this.totalWidth;
		if (x > 0) x = 0;
		if (f==true && y < this.viewableHeight - this.totalHeight) 
			y = this.viewableHeight - this.totalHeight;
			
		if(f==true && y>0) y=0;
		
		var sel = document.getElementById("sel");
		var tot = document.getElementById("tot");
		marginefinale=(sel.value-tot.value+4)*(this.totalHeight/(tot.value));
		
		if (f==false && sel.value!=0 && (tot.value-sel.value)<5 && y>(this.totalHeight/(tot.value))*(sel.value-1)-marginefinale)
		
			y = (this.totalHeight/(tot.value))*(sel.value-1)-marginefinale;
		
		if (f==false && sel.value!=0 && (tot.value-sel.value)>4 && y>(this.totalHeight/(tot.value))*(sel.value-1)) 
		
			y = (this.totalHeight/(tot.value))*(sel.value-1);
		

		if (f==false && sel.value!=0 && (tot.value-sel.value)<5 && y<-(this.totalHeight/(tot.value))*(tot.value-sel.value)-marginefinale)
			
			y = (-1)*(this.totalHeight/(tot.value))*(tot.value-sel.value)-marginefinale;


		if (f==false && sel.value!=0 && (tot.value-sel.value)>4 && y<-(this.totalHeight/(tot.value))*(tot.value-sel.value))
			
			y = (-1)*(this.totalHeight/(tot.value))*(tot.value-sel.value);
		
		
		this._x = x;
		this._y = y;
		with (o.style) {
			left = this._x +"px";
			top  = this._y +"px";
		}
	};
	
	//Public Methods
	this.reset = function () {
		this.content = o;
		this.totalHeight = o.offsetHeight;
		this.totalWidth	 = o.offsetWidth;
		this._x = 0;
		this._y = 0;
		with (o.style) {
			left = "0px";
			top  = "0px";
		}
	};
	
	this.scrollBy = function (x, y) {

			this._setPos(this._x + x, this._y + y);
	};
	
	this.scrollTo = function (x, y) {
		this._setPos(-x, -y);
	};
	
	this.stopScroll = function () {
		if (this.scrollTimer) window.clearInterval(this.scrollTimer);
	};
	
	this.startScroll = function (x, y) {
		this.stopScroll();
		this.scrollTimer = window.setInterval(
			function(){ self.scrollBy(x, y); }, 40
		);
	};
	
	this.swapContent = function (c, w, h) {
		o = c;
		var list = o.getElementsByTagName("div");
		for (var i = 0; i < list.length; i++) {
			if (list[i].className.indexOf("Scroller-Container") > -1) {
				o = list[i];
			}
		}
		if (w) this.viewableWidth  = w;
		if (h) this.viewableHeight = h;
		this.reset();
	};
	
	//variables
	this.content = o;
	this.viewableWidth  = w;
	this.viewableHeight = h;
	this.totalWidth	 = o.offsetWidth;
	this.totalHeight = o.offsetHeight;
	this.scrollTimer = null;
	this.reset();
};
