var running = false;
var scrolltimer;
var scrolldiff = 0;
var mousex = 0;
var img = new Image;


document.onmousemove = getMouseCoordinates;


function getMouseCoordinates(event)
{
	ev = event || window.event;
	mousex = ev.clientX;
}


function scroll_left()
{
	if(parseInt(document.getElementById("gallery").style.left) > -(parseInt(document.getElementById("gallery").offsetWidth) - parseInt(document.getElementById("container").offsetWidth)))
	{
		var pos = parseInt(document.getElementById("gallery").style.left) - scrolldiff;
		if(pos < -(parseInt(document.getElementById("gallery").offsetWidth) - parseInt(document.getElementById("container").offsetWidth)))
			pos = -(parseInt(document.getElementById("gallery").offsetWidth) - parseInt(document.getElementById("container").offsetWidth));
		document.getElementById("gallery").style.left = pos + "px";;
	}
}


function scroll_right()
{
	if(parseInt(document.getElementById("gallery").style.left) < 0)
	{
		var pos = parseInt(document.getElementById("gallery").style.left) - scrolldiff;
		if(pos > 0)
			pos = 0;
		document.getElementById("gallery").style.left = pos + "px";;
	}
}


function scroll()
{
	if(running)
	{
		var xpos = mousex - ((parseInt(document.body.offsetWidth) - 950)/2) + 25;
		scrolldiff = (xpos - (parseInt(document.getElementById("container").offsetWidth)/2))/50;
		if(scrolldiff < 0)
			scroll_right();
		else if(scrolldiff > 0)
			scroll_left();
		window.clearTimeout(scrolltimer);
		scrolltimer = window.setTimeout("scroll()", 25);
	}
}


function stop_scrolling()
{
	running = false;
}


function start_scrolling()
{
	if(!running)
	{
		running = true;
		scroll();
	}
}


function open_image(path, width, height)
{
	width += 16;
	height += 16;
	if(width < 1600)
		window.open('viewer.php?path='+path, 'iviewer', 'width='+width+', height='+height+', toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no');
	else
		window.open(path, 'iviewer', 'width=800, height=600, toolbar=yes, location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, resizable=yes');
}

