/***********************************************
* jQuery Mega Menu- by JavaScript Kit (www.javascriptkit.com)
* This notice must stay intact for usage
* Visit JavaScript Kit at http://www.javascriptkit.com/ for full source code
***********************************************/
/***********************************************
* Walter Flaschka, customization 2009
***********************************************/

var wfmenu = {
	effectduration: 100,
	delaytimer: 500,
	
	menulabels: [], // ?
	menus: [],
	zIndexVal: 110,
	
	alignmenu:function($, e, menu_pos) {
		var menu = this.menus[menu_pos]
		var $anchor  = menu.$anchorobj
		var $menu    = menu.$menuobj
		// var menuleft = ($(window).width()-(menu.offsetx-$(document).scrollLeft())>menu.actualwidth)? menu.offsetx : menu.offsetx-menu.actualwidth+menu.anchorwidth;
		var menuleft = (menu.offsetx - (menu.actualwidth / 2) + (menu.anchorwidth / 2));
		var menutop  = menu.offsety+menu.anchorheight  //get y coord of menu
		menutop = menutop; // -1; Nudge the menu up one pixel
		$menu.css({left:menuleft+"px", top:menutop+"px"})
	},
	
	showmenu:function(e, menu_pos){
		var menu  = this.menus[menu_pos]
		var $menu = menu.$menuobj
		menu.$anchorobj.parent('div.tab-knockout').addClass('tab-knockout-over');
		
		if ($menu.css("display")=="none"){
			this.alignmenu(jQuery, e, menu_pos)
			$menu.css("z-index", ++this.zIndexVal)
			$menu.show(this.effectduration)
		}
		return false
	},

	hidemenu:function(e, menu_pos){
		var menu   = this.menus[menu_pos]
		var $menu      = menu.$menuobj
		$menu.hide(this.effectduration)
		menu.$anchorobj.parent('div.tab-knockout').removeClass('tab-knockout-over');
	},

	definemenu:function(anchorid, menuid, revealtype){
		this.menulabels.push([anchorid, menuid])
	},

	render:function($){
		for (var i=0, labels=this.menulabels[i]; i<this.menulabels.length; i++, labels=this.menulabels[i]){
			if ($('#'+labels[0]).length!=1 || $('#'+labels[1]).length!=1) { return; }

			this.menus.push({
				$anchorobj  : $("#"+labels[0]), 
				$menuobj    : $("#"+labels[1]), 
				hidetimer   : null
			})
			
			var menu=this.menus[i]	

			// Collect
			menu.$anchorobj.add(menu.$menuobj).attr("_menupos", i+"pos") //remember index of this drop down menu
			menu.actualwidth  = menu.$menuobj.outerWidth()
			menu.actualheight = menu.$menuobj.outerHeight()
			menu.offsetx      = menu.$anchorobj.offset().left
			menu.offsety      = menu.$anchorobj.offset().top
			menu.anchorwidth  = menu.$anchorobj.outerWidth()
			menu.anchorheight = menu.$anchorobj.outerHeight()

			//move drop down menu to end of document
			$(document.body).append(menu.$menuobj)

			menu.$menuobj.css("z-index", ++this.zIndexVal).hide()

			menu.$anchorobj.bind("mouseenter", function(e){
				var menuinfo=wfmenu.menus[parseInt(this.getAttribute("_menupos"))]
				clearTimeout(menuinfo.hidetimer) //cancel hide menu timer
				return wfmenu.showmenu(e, parseInt(this.getAttribute("_menupos")))
			})
			menu.$anchorobj.bind("mouseleave", function(e){
				var menuinfo=wfmenu.menus[parseInt(this.getAttribute("_menupos"))]
				if (e.relatedTarget!=menuinfo.$menuobj.get(0) && $(e.relatedTarget).parents("#"+menuinfo.$menuobj.get(0).id).length==0){ //check that mouse hasn't moved into menu object
					menuinfo.hidetimer=setTimeout(function(){ //add delay before hiding menu
						wfmenu.hidemenu(e, parseInt(menuinfo.$menuobj.get(0).getAttribute("_menupos")))
					}, wfmenu.delaytimer)
				}
			})
			menu.$menuobj.bind("mouseenter", function(e){
				var menuinfo=wfmenu.menus[parseInt(this.getAttribute("_menupos"))]
				clearTimeout(menuinfo.hidetimer) //cancel hide menu timer
			})
			menu.$menuobj.bind("mouseleave", function(e){
				var menuinfo=wfmenu.menus[parseInt(this.getAttribute("_menupos"))]
				menuinfo.hidetimer=setTimeout(function(){ //add delay before hiding menu
					wfmenu.hidemenu(e, parseInt(menuinfo.$menuobj.get(0).getAttribute("_menupos")))
				}, wfmenu.delaytimer)
			})
		} //end for loop
		if(/Safari/i.test(navigator.userAgent)){ //if Safari
			$(window).bind("resize load", function(){
				for (var i=0; i<wfmenu.menus.length; i++){
					var menu=wfmenu.menus[i]
					var $anchorisimg=(menu.$anchorobj.children().length==1 && menu.$anchorobj.children().eq(0).is('img'))? menu.$anchorobj.children().eq(0) : null
					if ($anchorisimg){ //if anchor is an image link, get offsets and dimensions of image itself, instead of parent A
						menu.offsetx=$anchorisimg.offset().left
						menu.offsety=$anchorisimg.offset().top
						menu.anchorwidth=$anchorisimg.width()
						menu.anchorheight=$anchorisimg.height()
					}
				}
			})
 		} else {
			$(window).bind("resize", function(){
				for (var i=0; i<wfmenu.menus.length; i++){
					var menu=wfmenu.menus[i]	
					menu.offsetx=menu.$anchorobj.offset().left
					menu.offsety=menu.$anchorobj.offset().top
				}
			})
		}
	}

	
	
}
