var multiplier = 0;
var containerLeft = 0;
var containerTop = 0;
var containerDimensions = {};
var clearWidth = 200;
var windowWidth = 0;
var currentID = 0;
function initDimensions()
{
	var container = $('gallery');
	var containerArr = container.viewportOffset();
	containerLeft = containerArr[0];
	containerTop = containerArr[1];
	containerDimensions = container.getDimensions();
	windowWidth = document.viewport.getDimensions().width;
	//alert( windowWidth );
}
	 
function getcordsInDiv(e){

	//get the mouse coordinates
	mouseX = Event.pointerX(e);
	mouseY = Event.pointerY(e);
	
	//calculate the absolute mouse position in the div,
	//by mouseposition minus left position of the container
	horizontalPosition = mouseX - containerLeft;
	verticalPosition = mouseY - containerTop;


	//use prototypes function to get the dimension
	//this is a VERY usefull function because it also checks for borders
	height   = containerDimensions.height;
	width = containerDimensions.width;
	
	//check if the mouse is out or inside the div
	//this if statement checks if the cursor is inside the div
	 
	if(horizontalPosition > 0 && verticalPosition > 0 && mouseX < (width + containerLeft) && mouseY < (height + containerTop))
	{	
		var middle = ( windowWidth - containerLeft )/2  + containerLeft;
  		if( horizontalPosition > ( middle + clearWidth/2 ) || horizontalPosition < ( middle - clearWidth/2 ) ) {
			multiplier = (horizontalPosition-middle)/3;
		}else
		{
			multiplier = 0;
		}	
	}
	else
	{
			multiplier = 0;
	}
}

function tweenDiv(){
	if ( multiplier == 0 )
		return;
		
	var pos = 'false';
	var posl = $('sw').getStyle('marginLeft');

	if ( posl == null )
	{
		posl = 0;
		$('sw').setStyle({'marginLeft': posl+'px'});
	}
	
	posl = parseInt(posl );
	
	var speedAdjuster = multiplier/3;
 	if((posl-speedAdjuster) > 0 && speedAdjuster < 0) 
	{
		$('sw').setStyle({'marginLeft': 0+'px'});
	}
 	else if( posl <= - ($('sw').getWidth()-containerDimensions.width) && speedAdjuster > 0 ) 
	{
		$('sw').setStyle({'marginLeft': -($('sw').getWidth()-containerDimensions.width)+'px'});
	}
	else {
		pos = posl-speedAdjuster;
		$('sw').setStyle({'marginLeft': pos+'px'});
	}
}

function scrollToID(ID) {
	var posl = $('sw').getStyle('marginLeft');

	if ( posl == null )
	{
		posl = 0;
		$('sw').setStyle({'marginLeft': posl+'px'});
	}
	else if ( ID == currentID ) {
		ID++;	
	}
	var dims = $('photo_'+ID).positionedOffset();
	//var dimx = $('sw').getWidth()-containerDimensions.width;
	var dimx = -dims[0]+150+parseInt(posl);
	//alert(dims);
	//new Effect.Move('sw', { x: dimx, y: 0, mode: 'absolute' });
	$('sw').morph({marginLeft: dimx + 'px'});
	currentID = ID;
	}

//Event.observe(window, 'load', function(){ initDimensions(); window.setInterval("tweenDiv()",30); });
Event.observe(window, 'load', function(){ initDimensions(); });
Event.observe(window, 'scroll', function(){ initDimensions(); });
Event.observe(window, 'resize', function(){ initDimensions(); });

//Event.observe(document, 'mousemove', getcordsInDiv);