//	Parallax 0.1.8 - ©2009 John Einselen
//	in part, derived from code by Stephen Chapman and Kurt Grigg

function parallax(container,objects,speed,invert,ballance){
	var objects = $$(objects);
	var container = $(container);
	var containerD = container.getCoordinates();
	var centerx = (containerD.width/2)+containerD.left;
	var centery = (containerD.height/2)+containerD.top;
	var xx = centerx;
	var yy = centery;
	var zx = [xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx+1];
	var zy = [yy,yy,yy,yy,yy,yy,yy,yy,yy,yy,yy,yy,yy,yy,yy,yy,yy,yy,yy,yy+1];

	function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
	function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}

	function windowPosition() {
		var centerx = (window.getWidth()/2);
		var centery = ((window.getScrollTop()+window.getHeight())/2);
	}

	function followLocation(evt) {
		xx = parseInt(mouseX(evt));
		yy = parseInt(mouseY(evt));
	}

	function followDelay() {
		if(xx!=Math.round(zx[19])||yy!=Math.round(zy[19])){
		zx.each(function(item, index){
			if(index<1){
				zx[index] = xx;
				zy[index] = yy;
			}else{
				zx[index] = (zx[index]+=(zx[index-1]-zx[index])*speed);
				zy[index] = (zy[index]+=(zy[index-1]-zy[index])*speed);
			}
		});
		objects.each(function(item, index){
			item.set({'styles': {'left': ((centerx - Math.round(zx[19]))*(invert*item.get('title'))*(2-ballance)) + centerx + 'px','top': ((centery - Math.round(zy[19]))*(invert*item.get('title'))*(ballance)) + centery + 'px'},});
		});
		}
	}
	container.onmousemove = followLocation;
	window.onresize = windowPosition;
	var interval = followDelay.periodical(50);
}

window.addEvent('domready', function(){
	var new3D = parallax('cover', '#box div', '.55', '.60', '.65');
//	Settings: container ID, elements selector, speed (0 through 1), invert (1 or -1), balance (0==only X, 2==only Y)
});