(function() {
    function HoverMenu(jqObj, settings) {
    	var $this 	= this;
    	$this.jqObj 	= $(jqObj);
    	$this.settings 	= settings;
    	$this.HoverEls 	= new Array();
    	
    	if ($this.settings.autoRender) {
    		$this.settings.children	= "ul";
    		
    		$this.jqObj.children('li').each(function() {
    			$this.HoverEls.push(new HoverMenuItem($(this), $this));
    		});
    	} else {
    		$($this.settings.root).each(function() {
        		$this.HoverEls.push(new HoverMenuItem($(this), $this));
        	});	
    	}

    	return $this;
    }

    HoverMenu.prototype.hideAll = function() {
    	var i;

    	for (i = 0; i < this.HoverEls.length; i++) {
    		this.HoverEls[i].hide();
    	}

    	return this;
    }

    HoverMenu.prototype.hide = function(index) {
    	return this.HoverEls[index].hide();
    }

    HoverMenu.prototype.show = function(index) {
    	return this.HoverEls[index].show();
    }


    function HoverMenuItem(jqObj, pObj) {
    	this.jqObj  = jqObj; 	//The current menu item jQuery selected DOM object
    	this.pObj   = pObj;		//The parent HoverMe object
    	this.settings	= pObj.settings;

    	var $this   	= this;
    	var position 	= this.jqObj.position();
    	
    	if (typeof(this.pObj.settings.topOffset) == "function") { 
    		this.jqObj.children(this.pObj.settings.children).css("top", position.top + this.pObj.settings.topOffset(this.jqObj));
    	} else {
    		this.jqObj.children(this.pObj.settings.children).css("top", position.top + this.pObj.settings.topOffset);
    	}
    	
    	if (typeof(this.pObj.settings.leftOffset) == "function") {
    		this.jqObj.children(this.pObj.settings.children).css("left", position.left + this.pObj.settings.leftOffset(this.jqObj));
    	} else {
    		this.jqObj.children(this.pObj.settings.children).css("left", position.left + this.pObj.settings.leftOffset);
    	}
		    

    	if ((this.pObj.settings.timeout != null) && (this.pObj.settings.timeout > 0)) {
    		this.jqObj.hoverIntent({ 
    			over 	: function() {
    				$this.pObj.hideAll();
    				$this.show();
    			},
    			out 	: function() {},
    			timeout	: $this.pObj.settings.timeout,
    			sensitivity	: $this.pObj.settings.sensitivity,
    			interval	: $this.pObj.settings.interval
    		});
    		this.jqObj.children(this.pObj.settings.children).hoverIntent({
    			over	: function() {}, 
    			out	: function() {
    				$this.hide();
    			},
    			timeout	: $this.pObj.settings.timeout,
    			sensitivity	: $this.pObj.settings.sensitivity,
    			interval	: $this.pObj.settings.interval
    		});
    	} else {
    		this.jqObj.mouseenter(function() { 
    			$this.pObj.hideAll();
    			$this.show();
    		});
    		this.jqObj.children(this.pObj.settings.children).mouseleave(function() {
    			$this.hide();
    		});
    	} 

    	if (this.settings.autoRender) {
    		$('ul', this.jqObj).each(function() {
        		$(this).hoverMe($this.settings);
        	});
    	}

    	return this;
    }

    HoverMenuItem.prototype.hide = function() {
    	return this.jqObj.children(this.pObj.settings.children).hide();
    }

    HoverMenuItem.prototype.show = function() {
    	return this.jqObj.children(this.pObj.settings.children).show();
    }

   
    $.hoverMe = { 
   		HoverMees: new Array(),

   		settings: {
   			autoRender	: false,
   			children	: ".hoverItem",
   			root    	: ".root",
   			topOffset  	: function(obj) {
   				return $(obj).innerHeight();
   			},
   			leftOffset	: function(obj) { 
   				return 0; 
   			},
   			timeout 	: null,
   			sensitivity	: 7,
   			interval	: 100
   		},

   		init: function(jqObj, params) {
   			var settings = $.extend($.hoverMe.settings, params || {});

   			$.hoverMe.HoverMees.push(new HoverMenu(jqObj, settings));
        }
    }

    $.fn.hoverMe = function(params) {
    	return this.each(function() {
    		$.hoverMe.init(this, params); 
    	});
    }    
})(jQuery);
