/* 
	THUMBSCROLLER 1.0
	
	Copyright Erwin Eggenberger
	dev07@keyboardrocker.com
	
	This program is distributed under the terms of the GPL v2
	http://www.gnu.org/copyleft/gpl.html

	If you find this useful please let me know.
	Bug reports and feature requests are also welcome ;-)
*/

function ThumbScroller() {
	//configuration variables

	this.scrollboxitem = 'submenu';
	this.scrolleritem = 'subscroller';
	
	this.maxitems = 7;
	this.step = 87;
	
	this.refreshrate = 0.05;


	this.damping = 4.5;
	this.freq = 2.5;
	
	
	// don't change the following variables
	this.items = 0;
	this.targetoffset = 0;
	this.position = 0;
	this.offset = 0;
	this.etime = 0;
	this.timeout = null;

}

ThumbScroller.prototype = new Object();
ThumbScroller.prototype.constructor = ThumbScroller();

/* function to setup all the stuff we need */

ThumbScroller.prototype.init = function ( menuid ) {
	// identify the menu
	this.menuid= menuid;

	// get the elements we need
	this.scrollbox = document.getElementById(this.scrollboxitem);
	this.scroller = document.getElementById(this.scrolleritem);
	this.leftarrow = document.getElementById('toleft');
	this.rightarrow = document.getElementById('toright');

	// hide scrollbars
	this.scrollbox.style.overflow = 'hidden';

	// count number of menuitems
 	this.items = this.scroller.childNodes.length;

	// get the last saved position for this menu
	regexp = new RegExp("slmenuid\\["+this.menuid+"\\]=(\\w+)", "i");
	if( regexp.test(document.cookie) ) {
		result = regexp.exec(document.cookie);
		this.position = parseInt(result[1]);
	}
	else
		this.position=0;
	
	// move the menu to the last saved position
	this.offset = this.targetoffset = - this.position*this.step;
	this.scroller.style.marginLeft = '' + this.offset+ 'px';

	// unhide navigation arrows if needed
	if( this.items  > this.maxitems || this.position != 0 ) {
		this.rightarrow.style.visibility = 'visible';
		this.leftarrow.style.visibility = 'visible';
	}
}

/* Function to move one thumb to the right */

ThumbScroller.prototype.toright = function( ) {
	// clear existing timeout that may exist
	window.clearTimeout(this.timeout);
	
	// calculate new position and save it to a cookie
	this.position = Math.min( this.items-this.maxitems, this.position+1);
	document.cookie = "slmenuid["+this.menuid+"]="+this.position;
	
	// calculate the new offset the menu should move to
	this.targetoffset = - this.position*this.step;
	
	// reset elapsed time
	this.etime = 0;
	
	// calculate initial amplitude
	this.omax = this.offset - this.targetoffset;
	
	//start scrolling
	this.scroll();
}

/* Function to move one thumb to the left */

ThumbScroller.prototype.toleft = function( ) {
	// clear existing timeout that may exist
	window.clearTimeout(this.timeout);
	
	// calculate new position and save it to a cookie
	this.position = Math.max( 0, this.position-1);
	document.cookie = "slmenuid["+this.menuid+"]="+this.position;

	// calculate the new offset the menu should move to
	this.targetoffset = - this.position*this.step;

 	// reset elapsed time
	this.etime = 0;
	
	// calculate initial amplitude
	this.omax = this.offset - this.targetoffset;
	
	//start scrolling
	this.scroll();
}

/* this function moves the menu */

ThumbScroller.prototype.scroll = function() {
	// update elapsed time
	this.etime += this.refreshrate;
	
	// calculate the current amplitude an the resulting position of the menu
	y = Math.round(this.omax*Math.pow(Math.E, -this.damping*this.etime)*Math.cos(2*Math.PI*this.freq*this.etime));
	offset = this.targetoffset+ y;

	// test if we need to continue
	if( this.offset == offset && this.offset == this.targetoffset ) {
// 		this.infobox.innerHTML = 'done';
	}
	else{
		// apply new position for menu
		this.offset = offset;
		this.scroller.style.marginLeft = '' + offset+ 'px';

		// execute this function again after a timeout
		this.timeout = window.setTimeout( 'thumbscroller.scroll( )', this.refreshrate*1000);
	}
}



