/*!
 * AWI Mouse Navigator
 * http://www.aw-industries.com
 *
 * Copyright 2010, Alexander HL Wong
 * Licensed under the MIT license.
 * http://www.aw-industries.com/files/license.txt
 *
 * Date: Sunday, March 14 2010
 */

/*!
 * USAGE
 * initialize: var g = new mouseNavigate(targetID);
 * , where [targetID] is a CSS selector string
 * corresponding to the background container.
 */

mouseNavigate = function(targetID) {
	this.init(targetID);
}
$.extend(mouseNavigate.prototype, {
	targetID: null,
	timeout: null,
	mouseX: null,
	mouseY: null,
	windowE: null,
	scrollTop: null,
	scrollLeft: null,
	init: function(targetID) {
		if(mouseNavigate.objList == undefined) {
			//alert("undefined");
			mouseNavigate.objList = new Array();
		}
		this.objIndx = mouseNavigate.objList.length;
		mouseNavigate.objList[this.objIndx] = this;
		
		//this.windowE = new Function('e','mouseNavigate.objList['+this.objIndx.toString()+'].update(e);');
		this.targetID = targetID;
		this.timeout = null;
		this.mouseX = $(window).width() / 2;
		this.mouseY = $(window).height() / 2;
		this.scrollTop = $(this.targetID).offset().top;
		this.scrollLeft = $(this.targetID).offset().left;
		//this.start();
		this.start(this.objIndx.toString());
	},
	start: function(index) {
		this.timeout = setInterval('mouseNavigate.objList['+this.objIndx.toString()+'].scroll();', 65);
		//$(window).bind('mousemove',this.windowE);
		$(document).bind('mousemove',function(event){
			mouseNavigate.objList[index].update(event);
		});
	},
	kill : function(index) {
		clearTimeout(this.timeout);
		//$(window).unbind('mousemove',this.windowE);
		$(document).unbind('mousemove',function(event){
			mouseNavigate.objList[index].update(event);
		});
		delete(mouseNavigate.objList[this.objIndx]);
	},
	update: function(e) {
		//$('#bla').html("Test"+e.pageX);
		this.mouseX = e.pageX;
		this.mouseY = e.pageY;
	},
	scroll: function() {
		
		var yCenter = $(window).height() / 2;
		var xCenter = $(window).width() / 2;
		var yOffset = yCenter - this.mouseY + this.scrollTop;
		var xOffset = xCenter - this.mouseX + this.scrollLeft;
		var yTarget = $(this.targetID).position().top;
		var xTarget = $(this.targetID).position().left;
		var hTarget = $(this.targetID).height();
		var wTarget = $(this.targetID).width();
		var hWindow = $(window).height();
		var wWindow = $(window).width();

		if(yOffset > 25 && yTarget <= 0) {
			$(this.targetID).css('top', Math.min(0, yTarget + this.delta(yOffset / yCenter)));
		}else if(yOffset < -25 && yTarget >= hWindow - hTarget) {
			$(this.targetID).css('top', Math.max(hWindow - hTarget, yTarget - this.delta(yOffset / yCenter)));
		}
		if(xOffset > 25 && xTarget <= 0) {
			$(this.targetID).css('left', Math.min(0, xTarget + this.delta(xOffset / xCenter)));
		} else if(xOffset < -25 && xTarget >= wWindow - wTarget) {
			$(this.targetID).css('left', Math.max(wWindow - wTarget, xTarget - this.delta(xOffset / xCenter)));
		}
	},
	delta: function(ratio){
		return Math.min(75, Math.pow(Math.abs(ratio) * 9, 2));
	}
});
