//** Menu based on Smooth Navigational Menu- By Dynamic Drive DHTML code library: http://www.dynamicdrive.com
//** Menu modifications by Visual Java, Inc.: http://www.visualjava.com

var dropMenu={
	transition: {overtime:300, outtime:300}, //duration of slide in/ out animation, in milliseconds
	showhidedelay: {showdelay: 100, hidedelay: 200}, //set delay in milliseconds before sub menus appear and disappear, respectively
	
	///////Stop configuring beyond here///////////////////////////
	
	detectwebkit: navigator.userAgent.toLowerCase().indexOf("applewebkit")!=-1, //detect WebKit browsers (Safari, Chrome etc)
	detectie6: document.all && !window.XMLHttpRequest,
	
	getajaxmenu:function($, setting){ //function to fetch external page containing the panel DIVs
		var $menucontainer=$('#'+setting.contentsource[0]) //reference empty div on page that will hold menu
		$menucontainer.html("Loading Menu...")
		$.ajax({
			url: setting.contentsource[1], //path to external menu file
			async: true,
			error:function(ajaxrequest){
				$menucontainer.html('Error fetching content. Server Response: '+ajaxrequest.responseText)
			},
			success:function(content){
				$menucontainer.html(content)
				dropMenu.buildmenu($, setting)
			}
		})
	},
	
	buildmenu:function($, setting){
		var smoothmenu=dropMenu
		var $orient='h'
		var $mainmenu=$("#"+setting.mainmenuid+">ul") //reference main menu UL
		$mainmenu.parent().get(0).className=setting.classname || "dropMenu"
		var $headers=$mainmenu.find("ul").parent()
		$headers.hover(
			function(e){
				$(this).children('a:eq(0)').addClass('selected')
			},
			function(e){
				$(this).children('a:eq(0)').removeClass('selected')
			}
		)
		$headers.each(function(i){ //loop through each LI header
			var $curobj=$(this).css({zIndex: 100-i}) //reference current LI header
			var $subul=$(this).find('ul:eq(0)').css({display:'block'})
			$subul.data('timers', {})
			this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false //is top level header?
			$subul.css({top:this.istopheader && $orient!='v'? this._dimensions.h+"px" : 0})
			
			$curobj.hover(
				function(e){
					var $targetul=$subul //reference UL to reveal
					var header=$curobj.get(0) //reference header LI as DOM object
					clearTimeout($targetul.data('timers').hidetimer)
					$targetul.data('timers').showtimer=setTimeout(function(){
						header._offsets={left:$curobj.offset().left, top:$curobj.offset().top}
						var menuleft=header.istopheader && $orient!='v'? 0 : header._dimensions.w
						menuleft=(header._offsets.left+menuleft+header._dimensions.subulw>$(window).width())? (header.istopheader && $orient!='v'? -header._dimensions.subulw+header._dimensions.w : -header._dimensions.w) : menuleft //calculate this sub menu's offsets from its parent
						if ($targetul.queue().length<=1){ //if 1 or less queued animations
							$targetul.css({left:menuleft+"px", width:header._dimensions.subulw+'px'}).animate({height:'show',opacity:'show'}, dropMenu.transition.overtime)
						}
					}, dropMenu.showhidedelay.showdelay)
				},
				function(e){
					var $targetul=$subul
					var header=$curobj.get(0)
					clearTimeout($targetul.data('timers').showtimer)
					$targetul.data('timers').hidetimer=setTimeout(function(){
						$targetul.animate({height:'hide', opacity:'hide'}, dropMenu.transition.outtime)
					}, dropMenu.showhidedelay.hidedelay)
				}
			) //end hover
		}) //end $headers.each()
		
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
	},
	
	init:function(setting){
		jQuery(document).ready(function($){ //ajax menu?
			if (typeof setting.contentsource=="object"){ //if external ajax menu
				dropMenu.getajaxmenu($, setting)
			}
			else{ //else if markup menu
				dropMenu.buildmenu($, setting)
			}
		})
	}

} //end dropMenu variable

