﻿var scroller = {};
scroller.current = null;

function scroller_stop() {
	if (scroller.current != null)
		scroller.current.sa = 0;
}
function scroller_start() {
	var s = scroller.current;
	if (s == null || s.sa == 0) return;

	var t = s.innerPart;
	var pos = t.style.left;
	pos = parseInt(pos.substring(0, pos.length - 2), 10); // remove px from end
	pos += s.sa;

	if (pos > 0) pos = 0;
	else if (Math.abs(pos) > t.clientWidth - s.w)
		pos = -(t.clientWidth - s.w);

	t.style.left = pos + "px";
	s.curPos = pos;

	setTimeout(scroller_start, s.tick);

	scroller_showstatus(s);
}

function scroller_showstatus(s) {
	if (s.curPos >= 0)
		s.leftButton.src = "images/scroller/arrow_left_disabled.gif";//disabled
	else if (s.sa > 0)
		s.leftButton.src = "images/scroller/arrow_left_over.gif";
	else
		s.leftButton.src = "images/scroller/arrow_left.gif";

	
	if (Math.abs(s.curPos) >= s.innerPart.clientWidth - s.w)
		s.rightButton.src = "images/scroller/arrow_right_disabled.gif"; //off
	else if (s.sa < 0)
		s.rightButton.src = "images/scroller/arrow_right_over.gif"; //over
	else
		s.rightButton.src = "images/scroller/arrow_right.gif"; //normal
}

function scroller_mouseover(s, mb) {
	scroller.current = s;
	s.sa = mb;
	setTimeout(scroller_start, s.tick);
}
function scroller_mouseout(s) {
	scroller_stop();
	if (scroller.current != null)
		scroller_showstatus(scroller.current);
		
	scroller.current = null;
}


function scroller_register(innerPart, leftButton, rightButton, width) {
	var s = {};
	s.sa = 0; 	// by how much we are moving this instance
	s.tick = 10; 	// timer in ms
	s.moveBy = 4; 	// by how much to move on each click in pixels
	s.w = width; // width that this element is defined as
	s.curPos = 0; // current position in scrolling
	s.leftButton = document.getElementById(leftButton);
	s.rightButton = document.getElementById(rightButton);
	s.innerPart = document.getElementById(innerPart);

	s.leftButton.onmouseover = function() { scroller_mouseover(s, s.moveBy); }
	s.rightButton.onmouseover = function() { scroller_mouseover(s, -s.moveBy); }
	
	s.leftButton.onmouseout = scroller_mouseout;
	s.rightButton.onmouseout = scroller_mouseout;

	s.leftButton.onclick = function() { s.sa *= 2; }
	s.rightButton.onclick = function() { s.sa *= 2; }

	s.leftButton.style.cursor = "pointer";
	s.rightButton.style.cursor = "pointer";

	s.leftButton.style.display = "";
	s.rightButton.style.display = "";


	return s;
}