// ##############################################################################################################################
// Instructions page: http://www.dynamicdrive.com/dynamicindex1/ddlevelsmenu/
// Last Edit by: Maikel Doeze
//
// ## OTYS Change log ###########################################################################################################
// -Removed theme system - Maikel Doeze - 06-08-2010
// -Removed Ajax load function - Maikel Doeze - 06-08-2010
// -Removed Shade effect duo to quirks bug - Maikel Doeze - 06-08-2010
// -Replaced images for spans for easy editing - Maikel Doeze - 06-08-2010
// ##############################################################################################################################

var jssmoothmenu={
	//Specify classnames for down and right arrow images (23 is padding-right added to top level LIs with drop downs):
	arrowimages: {down:['downArrow', 23], right:['rightArrow']}, // SPAN Class names used for arrows
	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

	// DONT EDIT BELOW ///////////////////////////////////////////////////////////////////////////////////////////////////////////

	buildmenu:function($, setting){
		var smoothmenu=jssmoothmenu
		var $mainmenu=$("#"+setting.mainmenuid+">ul") //reference main menu UL
		$mainmenu.parent().get(0).className=setting.classname+'-'+setting.orientation || "jssmoothmenu"
		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 && setting.orientation!='v'? this._dimensions.h+"px" : 0})
			
			if(setting.arrows || setting.arrows === undefined)
			{
				$curobj.children("a:eq(0)").css(this.istopheader? {paddingRight: smoothmenu.arrowimages.down[1]} : {}).append( //add arrow images
					'<span class="' + (this.istopheader && setting.orientation!='v'? smoothmenu.arrowimages.down[0] : smoothmenu.arrowimages.right[0])
					+ '"></span>'
				)
			}
			
			$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 && setting.orientation!='v'? 0 : header._dimensions.w
						menuleft=(header._offsets.left+menuleft+header._dimensions.subulw>$(window).width())? (header.istopheader && setting.orientation!='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'}, jssmoothmenu.transition.overtime)
						}
					}, jssmoothmenu.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'}, jssmoothmenu.transition.outtime)
					}, jssmoothmenu.showhidedelay.hidedelay)
				}
			) //end hover
		}) //end $headers.each()
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
	},

	init:function(setting){
		jQuery(document).ready(function($){
			jssmoothmenu.buildmenu($, setting)
		})
	}
}
