/**
 * @Author: RAGBIT GmbH
 * @Developer: Anatolij Rau
 * @Date: 26.11.2008
 * @Version: 1.0 Beta
 **/

/** Configurable values **/
var scrollBoxSpeed = 20; // ms
var scrollBoxStep = 1; // px
var scrollBoxSpeedUpRate = 1; // px
var scrollBoxMaxSpeedUp = 10; // px

/** Default values **/
var scrollBoxSpeedUp = 2;
var scrollBoxStart = false;
var timeout;

function scrollBoxMove(scroll_body, element_width, body_width, to) {
	var left_pos = parseInt(scroll_body.style.left); 
	switch (to) {
		case 'left':
			if (scrollBoxStart || scrollBoxSpeedUp) {
				//alert((left_pos-element_width)*(-1));
				if ((left_pos-element_width)*(-1) >= body_width) scroll_body.style.left = element_width-body_width+'px'
				else scroll_body.style.left = Math.round(left_pos-scrollBoxStep-scrollBoxSpeedUp)+'px';
				scrollBoxDoMove(scroll_body, element_width, body_width, to);
			}
		break;
		case 'right':
			if (scrollBoxStart || scrollBoxSpeedUp) {
				if (left_pos >= 0) scroll_body.style.left = 0+'px';
				else scroll_body.style.left = Math.round(left_pos*1+scrollBoxStep*1+scrollBoxSpeedUp*1)+'px';
				scrollBoxDoMove(scroll_body, element_width, body_width, to);
			}
		break;
	}
}

function scrollBoxDoMove(scroll_body, element_width, body_width, to) {
	timeout = window.setTimeout(function(){scrollBoxMove(scroll_body, element_width, body_width, to);}, scrollBoxSpeed*1);
	
	if (scrollBoxStart && scrollBoxSpeedUp < scrollBoxMaxSpeedUp) scrollBoxSpeedUp = scrollBoxSpeedUp+scrollBoxSpeedUpRate;
	else if (scrollBoxSpeedUp <= 1) scrollBoxSpeedUp = 0;
	else if (scrollBoxStart && scrollBoxSpeedUp >= scrollBoxMaxSpeedUp) scrollBoxSpeedUp = scrollBoxMaxSpeedUp;
	else scrollBoxSpeedUp = scrollBoxSpeedUp-(scrollBoxSpeedUpRate*2);
}

function scrollBox(element, move_to) {
	if (!move_to) move_to = 'left';
	scrollBoxStart = true;
	scrollBoxSpeedUp = 0;
	window.clearTimeout(timeout);

	if (typeof element == 'object') {
		box_body = document.getElementById(element.id+'_body')
		if (typeof box_body == 'object') {
			if (box_body.style.position != 'relative') box_body.style.position = 'relative';
			if (!box_body.style.left) box_body.style.left = 0+'px';
			hscrollbox_body_width = document.getElementById(element.id+'_body_width');
			scrollBoxMove(box_body, element.offsetWidth, hscrollbox_body_width.offsetWidth, move_to);
		}
	} else alert('Object error!');
}

function scrollBoxStop() {
	//alert('stop');
	scrollBoxStart = false;
}