var scrollAmount = 2;
var scrollDelay = 20;


var current_x;
var max_x;

var container;
var target;

var timer_id;
var change;
var current_fn;

function initScroll()
{
	container = $('#wn');
	target = $('#lyr1');

	max_x = target.width() - container.width();
	current_x = target.scrollLeft();

	$('#scroll-left').hover(startScrollLeft, stopScroll);
	$('#scroll-right').hover(startScrollRight, stopScroll);
	target.css('visibility', 'visible');
}

function startScrollLeft()
{
	current_fn = startScrollLeft;
	change = scrollAmount;

	timer_id = setTimeout(performScroll, 0);
}

function startScrollRight()
{
	current_fn = startScrollRight;
	change = -scrollAmount;

	timer_id = setTimeout(performScroll, 0);
}

function stopScroll()
{
	change = 0;
	current_dir = '';
	clearTimeout(timer_id);
}

function performScroll()
{
	current_x += change;

	if (Math.abs(current_x) > max_x)
		current_x = -max_x;
	else if (current_x > 0)
		current_x = 0;

	target.css('left', current_x + 'px');

	clearTimeout(timer_id);
	timer_id = setTimeout(performScroll, scrollDelay);
}


$(document).ready(function () {
	initScroll();
});
