var totalWidth = 100000;
var scrollWidth = 100000;
Event.observe(window, 'load', function() {
		totalWidth = calcCorrectWidth();
		var childn = $('sw').childElements();
		var nchildn= childn.length;
		var lastwidth = childn[nchildn-1].getWidth();
		scrollWidth = totalWidth-lastwidth;
		
} );

calcCorrectWidth	= function() {
	if (allImagesLoaded()) {
		var wrap = $('sw');
		/*wrap.setStyle({ width: '50000px' });*/
		var childs	= wrap.childElements();
		var nwidth	= 0;
		for(var i=0; i<childs.length; i++) {
			var w	= childs[i].getWidth();
			var ml	= childs[i].getStyle('margin-left');
			ml = ml.replace("px", "");
			var mr	= childs[i].getStyle('margin-right');
			mr = mr.replace("px", "");
			var marg =  parseInt(ml) + parseInt(mr);
			nwidth = nwidth + w;
			nwidth = nwidth + marg;
		}
		wrap.setStyle({ width: nwidth+'px' });
		return nwidth;
	} else {
		setTimeout("calcCorrectWidth()", 50);
	}
}

function allImagesLoaded() {
	var imagesloaded = true;
	var images = document.images;
	for (var i = 0;i<images.length;i++)
	{
			if(images[i].complete == false) {
					imagesloaded = false;
			}
	}
	return imagesloaded;
}

function getcords(e){
	mouseX = Event.pointerX(e);
	mouseY = Event.pointerY(e);
	//for testing put the mouse cords in a div for testing purposes
	$('debug').innerHTML = 'X:' + mouseX + ' / Y:' + mouseY;
}

function moveDiv(d){
	var pos = 'false';
	//var pos = $('sw').cumulativeScrollOffset();
	var posl = $('sw').getStyle('left');
	posl = parseInt(posl.replace("px", ""));
	if (d == 'right') {
		if(posl-25 > -scrollWidth) {
			pos = posl-25;
		} else {
			pos = 'false';
		}
	}
	else if (d == 'left') {
		if(posl != 0 || posl == -25) {
			pos = posl+25;
		} else {
			pos = 'false';
		}
	}
	if (pos != 'false') {
		$('sw').setStyle({'left': pos+'px'});
	}
}

function tweenDiv(){
	if ( multiplier == 0 )
		return;
		
	var pos = 'false';
	//var pos = $('sw').cumulativeScrollOffset();
	var posl = $('sw').getStyle('left');
	posl = parseInt(posl.replace("px", ""));
	if(posl-25 > -scrollWidth && (posl-multiplier/10) <= 0) {
		pos = posl-multiplier/10;
	}
	else {
		pos = 'false';
		multiplier = 0;
	}
	if (pos != 'false') {
		$('sw').setStyle({'left': pos+'px'});
	}
	else {
		multiplier = 0;
	}
}


function scrollsection(to) {
	clearInterval(moveint);
	moveint = false;
	to = $(to);
	var sw = $('sw');
	swOf	= sw.viewportOffset()[0];
	toOf	= to.viewportOffset()[0];
	var scrolltopixle	=  swOf - toOf;
	new Effect.Move(sw,{x: scrolltopixle, y: 0, duration: 0.5, mode:'absolute'
	});
}


var moveint = false;
var multiplier = 0;
function getcordsInDiv(e){
	//alert(multiplier);
	var container = $('mouse');
	//get the position of the container, prototype functionality
	var containerArr = container.viewportOffset();
	var containerLeft = containerArr[0];
	var containerTop = containerArr[1];

	//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
	containerDimensions = container.getDimensions();
	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))
	{	
		
		if(horizontalPosition > 500 || horizontalPosition < 300) {
			multiplier = (horizontalPosition-400)/3;
		}else
		{
			multiplier = 0;
		}	
	}
	else
	{
			multiplier = 0;
	}
}

Event.observe(window, 'load', function(){ window.setInterval("tweenDiv()",30); });
Event.observe(document, 'mousemove', getcordsInDiv);