﻿var prevX = -1;
var prevY = -1;
var focusAreaWidth = 960;
var focusAreaDepth = 490;
var minX = 0;
var maxX = focusAreaWidth;
var maxTravelX = 50;
var maxTravelY = 35;
var allLevels = new Array();
var mouseTrackingActive = false;
var mouseTrackTimeout = null;

function showFeaturePanel() {
	if(!isIE) {
		if($) {
			setTimeout("$('#homeVisual').fadeTo(1000,1);",500);
		} else {
			document.getElementById("homeVisual").style.display = "block";
		}
	}
}

// jQuery horizontal parallax scrolling (non-touch devices)
$(document).ready(function() {
	if(isIE) {
		document.getElementById("homeVisual").style.display = "block";
	} else {
		$('#homeVisual').fadeTo(0,0);
	}
	mouseTrackingActive = true;
	
	$('#homeVisual .homeVisualLevel').each(function(index) {
		allLevels[index] = new Object();
		allLevels[index].div = $(this);
		position = $(this).position();
		allLevels[index].x = position.left;
		allLevels[index].z = $(this).zIndex();
	});
	
	$(document).mousemove(function(e) {
		if(mouseTrackingActive) {
			var mX = e.pageX;
			var mY = e.pageY;
			mouseTrackingActive = false;
			mouseTrackTimeout = setTimeout("mouseTrackingActive = true;", 100);
			if(mY > focusAreaDepth) mY = focusAreaDepth;
			var pageWidth = $("#fullBrowserWidth").width();
			if(pageWidth > focusAreaWidth) {
				minX = Math.floor((pageWidth - focusAreaWidth) / 2);
				maxX = minX + focusAreaWidth;
			} else {
				minX = 0;
				maxX = focusAreaWidth;
			}
			if(mX > maxX) {
				mX = focusAreaWidth;
				xPercentage = 100;
			} else if(mX > minX) {
				mX -= minX;
				xPercentage = (mX / focusAreaWidth) * 100;
			} else {
				mX = 0;
				xPercentage = 0;
			}
			if(mX != prevX || mY != prevY) {
				// x
				prevX = mX;
				if(xPercentage >= 50) {
					xTravel = (maxTravelX/50) * (xPercentage-50);
				} else {
					xTravel = 0 -((maxTravelX/50) * (50 - xPercentage));
				}
				// y
				prevY = mY;
				yTravel = (maxTravelY/focusAreaDepth) * mY;
				// update positions
				for(i=0; i < allLevels.length; i++) {
					// stop any old animation
					$(allLevels[i].div).stop(false,false);
					// set new animation
					newX = Math.floor(allLevels[i].x + ((xTravel/100)*allLevels[i].z));
					newY = Math.floor((yTravel/100)*allLevels[i].z);
					$(allLevels[i].div).animate({top: newY, left: newX}, 200);
				}
			}
		}
	});
	
});
