/*
   ================================================================================
        script: parallax-III
        author: Gerard Ferrandez - [Ge1doot]
          date: June 2, 2008
          site: http://www.dhteumeuleu.com
       license: CC-BY-NC - do not remove this notice
       images from: http://www.webdesignerwall.com/tutorials/parallax-gallery/
   ================================================================================
*/

var prx = function () {
	/* //////////// ==== private vars & methods ==== //////////// */
	var P = [];
	var mv = false;
	var xm = ym = xc = yc = nw = nh = 0;
	var N, force, attenuation, tags;
	/* ===== crossbrowsers addEvent ==== */
	function addEvent (o, e, f) {
		if (window.addEventListener) o.addEventListener(e, f, false);
		else if (window.attachEvent) r = o.attachEvent('on' + e, f);
	}
	/* ===== main parallax function ==== */
	function pos() {
		/* ---- mouse move? ---- */
		if (Math.abs(xm - xc) > 1 || Math.abs(ym - yc) > 1) {
			/* ---- move ease ---- */
			xc += (xm - xc) / 5;
			yc += (ym - yc) / 5;
			mv = true;
			/* ---- parallaxize all tags ---- */
			var i = N;
			while (i--) {
				var o = P[i];
				var x = Math.round(xc * o.Z / o.L);
				var y = Math.round(yc * o.Z / o.L);
				o.style.marginLeft   = x + 'px';
				o.style.marginTop    = y + 'px';
				o.style.marginRight  = -x + 'px';
				o.style.marginBottom = -y + 'px';
			}
			/* ---- loop ---- */
			setTimeout(pos, 16);
		/* ---- no move (zero CPU) ---- */
		} else mv = false;
	}
	/* ===== on mouse move ==== */
	function move (e) {
		/* ---- get mouse coordinates ---- */
		e = e || window.event;
		xm = Math.round(((nw * 2) - e.clientX) / 10);
	//	ym = Math.round(((nh * .5) - e.clientY) / 10);
		/* ---- re-launch animation if loop stopped (idle) ---- */
		if (!mv) pos();
	}
	/* ==== add parallax node  ==== */
	function addNode(node) {
		if (tags.indexOf(node.tagName) >= 0 || (node.style && node.style.zIndex)) {
			P.push(node);
			node.Z = node.style.zIndex || 1;
			node.L = force;
		}
	}
	/* ==== traverse DOM (recursive method) ==== */
	function traverseDom(node) {
		addNode(node);
		if (node.hasChildNodes) {
			force *= attenuation;
			var child = node.firstChild;
			while (child) {
				traverseDom(child);
				child = child.nextSibling;
			}
		}
		force *= 1 / attenuation;
	}
	/* ==== document.body dimensions ==== */
	function resize () {
		if( typeof( window.innerWidth ) == 'number' ) {
			nw = window.innerWidth;
			nh = window.innerHeight;
		} else if( document.documentElement && document.documentElement.clientHeight ) {
			nw = document.documentElement.clientWidth;
			nh = document.documentElement.clientHeight;
		} else if( document.body && document.body.clientHeight ) {
			nw = document.body.clientWidth;
			nh = document.body.clientHeight;
		}
	}

	/* //////////// ==== public methods ==== //////////// */
	return {
		/* ==== initialize script ==== */
		init : function (t, f, a) {
			tags = t;
			force = f;
			attenuation = a;
			traverseDom(document.body);
			N = P.length;
			resize();
			pos();
			/* ---- window events ---- */
			addEvent(window, 'resize', resize);
			addEvent(window.document, 'mousemove', move);		
			
		}
	}
}();

/* ==== init parallax engine ==== */
onload = function () {	

	var fg4_targ = Math.round(document.body.clientWidth*-.3);
	var fg3_targ = Math.round(document.body.clientWidth*-.07);
	
	
	var targ = Math.round(-640 + document.body.clientWidth*.5);

//	new Effect.Move('frm', {x:targ, y:30, mode:'absolute', duration:.1, afterFinish: function (){ prx.init("DIV IMG A SPAN", 2, 1.2); }});
	
//	new Effect.Move('fg4', {x:fg4_targ, y:0, mode:'absolute', duration:4, afterFinish: function (){ prx.init("DIV IMG A SPAN", 2, 1.2); }});
//	new Effect.Move('fg3', {x:fg3_targ, y:0, mode:'absolute', duration:4 });

	Effect.Fade('loading', {duration:.5, queue: 'front'});
	
	Effect.Appear('fg4', {duration:1});
	Effect.Appear('fg3', {duration:2});
	Effect.Appear('fg2', {duration:3});
	Effect.Appear('fg1', {duration:4});
	Effect.Appear('mg', {duration:5});
	Effect.Appear('bg', {duration:6});
	
	Effect.Appear('album', {duration:2});
	Effect.Appear('nav', {duration:2});
	
	prx.init("DIV", 2, 1.2);
}