
function com_blindacre_navigationmenu(o){
	
	// First, define all the properties of this new object.
	this.elementBound = null;
	
	// Any options currently set for this object.
	this.options = jQuery.extend(o, {
		overduration: 10,
		outduration: 10
	});
	
	this.init = function(){
		if(!this.isbound()) return;
		
		//var $mainmenu=$("#"+menuid+">ul")
		
		var $headers = this.elementBound.find("ul").parent()
		var obj = this;
		$headers.each(function(i){
			var $curobj=$(this);
			var $subul=$(this).find('ul:eq(0)');
			this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false
			$subul.css({top:this.istopheader? this._dimensions.h+"px" : 0})
			$curobj.hover(
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					var obj = $targetul[0].com_blindacre_navigationmenu;
					this._offsets={left:$(this).offset().left, top:$(this).offset().top}
					var menuleft=this.istopheader? 0 : this._dimensions.w
					menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
					$targetul.css({left:menuleft+"px"}).fadeIn(obj.options.overduration)
				},
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					var obj = $targetul[0].com_blindacre_navigationmenu;
					$(this).children("ul:eq(0)").fadeOut(obj.options.outduration)
				}
			) //end hover
		}) //end $headers.each()
		this.elementBound.find("ul").css({display:'none', visibility:'visible'})
	};
	
	this.attachToElement = function(el){
		if(typeof(el) == 'undefined')
			this.unbind();
		else
			this.bind(el);
	};
	
	this.bind = function(el){
		
		this.elementBound = jQuery(el);
		this.elementBound[0].com_blindacre_navigationmenu = this;
		
		// Also bind to every subul in the system, this will save time on lookups during mouseover/out events.
		var obj = this;
		this.elementBound.find('ul').each(function(){
			this.com_blindacre_navigationmenu = obj;
		});
		
	};
	
	this.unbind = function(){
		if(this.elementBound == null) return;
		if(typeof(this.elementBound.com_blindacre_navigationmenu) != 'undefined') this.elementBound.com_blindacre_navigationmenu = null;
		this.elementBound = null;
	};
	
	this.isbound = function(){
		return (this.elementBound != null);
	};

};

(function(jQuery) {
	jQuery.extend(jQuery.fn, {
		// jQuery wrapper around the global handler object.
		navigationmenu : function(options) {
		  
			// Ensure that options is *something*.
			if (options == undefined) options = {};
			
			// Run through each element given in by the programmer.
			jQuery(this).each(function(){
				// Try to reuse an existing object if it is bound to the dom node.
				if(typeof(this.com_blindacre_navigationmenu) != 'undefined'){
					obj = this.com_blindacre_navigationmenu;
				}
				else{
					obj = new com_blindacre_navigationmenu(options);
					obj.attachToElement(this);
				}
				obj.init();
			});
			return this;
		}
	});
})(jQuery);
