/* START - $.fn.Bounds */

(function($){

	$.fn.Bounds = function () {
		var useOuter = (arguments.length == 1 && typeof arguments[0] == 'boolean' && arguments[0]) ? true : false; //Check whether to use outer or inner dimensions
		var e = this.length == 1 ? this : this.eq(0); //Get reference to first element in jQuery search
		var sOffset = e.offset();//Get offset
		var dims = useOuter ? {height: e.outerHeight(),width: e.outerWidth()} : {height: e.innerHeight(),width: e.innerWidth()}; 
		return{width: dims.width,height: dims.height,left: sOffset.left,right: Math.round(sOffset.left + dims.width),top: sOffset.top,bottom: Math.round(sOffset.top + dims.height)};
	};
	
})(jQuery);

/* END - $.fn.Bounds */

/* START - $.fn.WatchBounds */

(function($){
	
$.fn.WatchBounds = function(options) {
			
	if(typeof $.fn.Bounds != 'function')throw new Error('$.WatchBounds() expects the object \'$.Bounds()\'');
	
	var o = $.extend({
	
		hitClass: 'hit',			//Indicates whether element has been hit
		preventReHit: true,			//indicates whether to prevent the rehit of element or not
		inBounds: function(){},		//Function to be called when whithin bounds of element
		outBounds: function(){}	//Function to be called when outside bounds of element
		
	},options);
	
	
	function hit(x,y,e)//Checks whether mouse is whithin boundaries of element
	{
		var b = $(e).Bounds(true);//Get specs for bounds
		if(y > b.top && y < b.bottom && x > b.left && x < b.right)return true;//If whithin bounds
		return false;	
	}
	
	function watchMove($e)//Tracks whether an element in the collection has been hit
	{
		$(this).each(function(){//Loop through each element
			
			var e = $(this);
			
			if(hit($e.pageX,$e.pageY,this))//If whithin boundaries of element
			{
				if(o.preventReHit && e.hasClass(o.hitClass))return;//If the hit event for this item has been intiated return
				e.addClass(o.hitClass);//Set the item as initiated
				o.inBounds($e,e);
			} else {
				if(o.preventReHit && !e.hasClass(o.hitClass))return;//If the hit event for this item has been intiated return
				e.removeClass(o.hitClass);//Unset the item as initiated	
				o.outBounds($e,e);
			}	
		});
	}

	$(document.body).bind('mousemove',$.proxy(watchMove,this));//Attach mousemove event to the body of the document
	
	this.release = function(){//unbinds the mouse move event to the body of the document
		$(this).removeClass(o.hitClass);//Unset the item as initiated	
		$(document.body).unbind('mousemove',watchMove);	
	};
	
	return this
		.each(function($key,$value){
			$(this).addClass('watch-bounds-' + $key);
		});

};

})(jQuery);

/* END - $.fn.WatchBounds */
