/* jQuery Mega Menu v1.02
* Last updated: June 29th, 2009. This notice must stay intact for usage 
* Author: JavaScript Kit at http://www.javascriptkit.com/
* Visit http://www.javascriptkit.com/script/script2/jScale/ for full source code
*/

var $g = jQuery.noConflict();

var jkmegamenu={

effectduration: 0, //duration of animation, in milliseconds
delaytimer: 0, //delay after mouseout before menu should be hidden, in milliseconds

//No need to edit beyond here
megamenulabels: [],
megamenus: [], //array to contain each block menu instances
zIndexVal: 1000, //starting z-index value for drop down menu
$gshimobj: null,

addshim:function($g){
	$g(document.body).append('<IFRAME id="outlineiframeshim" src="'+(location.protocol=="https:"? 'blank.htm' : 'about:blank')+'" style="display:none; left:-40px; top:0; z-index:999; position:absolute; filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)" frameBorder="0" scrolling="no"></IFRAME>')
	this.$gshimobj=$g("#outlineiframeshim")
},

alignmenu:function($g, e, megamenu_pos){
	var megamenu=this.megamenus[megamenu_pos]
	var $ganchor=megamenu.$ganchorobj
	var $gmenu=megamenu.$gmenuobj
	var menuleft=($g(window).width()-(megamenu.offsetx-$g(document).scrollLeft())>megamenu.actualwidth)? megamenu.offsetx : megamenu.offsetx-megamenu.actualwidth+megamenu.anchorwidth //get x coord of menu
	//var menutop=($g(window).height()-(megamenu.offsety-$g(document).scrollTop()+megamenu.anchorheight)>megamenu.actualheight)? megamenu.offsety+megamenu.anchorheight : megamenu.offsety-megamenu.actualheight
	var menutop=megamenu.offsety+megamenu.anchorheight  //get y coord of menu
	$gmenu.css({left:menuleft+"px", top:menutop+"px"})
	this.$gshimobj.css({width:megamenu.actualwidth+"px", height:megamenu.actualheight+"px", left:menuleft+"px", top:menutop+"px", display:"block"})
},

showmenu:function(e, megamenu_pos){
	var megamenu=this.megamenus[megamenu_pos]
	var $gmenu=megamenu.$gmenuobj
	var $gmenuinner=megamenu.$gmenuinner
	if ($gmenu.css("display")=="none"){
		this.alignmenu(jQuery, e, megamenu_pos)
		$gmenu.css("z-index", ++this.zIndexVal)
		$gmenu.show(this.effectduration, function(){
			$gmenuinner.css('visibility', 'visible')
		})
	}
	else if ($gmenu.css("display")=="block" && e.type=="click"){ //if menu is hidden and this is a "click" event (versus "mouseout")
		this.hidemenu(e, megamenu_pos)
	}
	return false
},

hidemenu:function(e, megamenu_pos){
	var megamenu=this.megamenus[megamenu_pos]
	var $gmenu=megamenu.$gmenuobj
	var $gmenuinner=megamenu.$gmenuinner
	$gmenuinner.css('visibility', 'hidden')
	this.$gshimobj.css({display:"none", left:0, top:0})
	$gmenu.hide(this.effectduration)
},

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

render:function($g){
	for (var i=0, labels=this.megamenulabels[i]; i<this.megamenulabels.length; i++, labels=this.megamenulabels[i]){
		if ($g('#'+labels[0]).length!=1 || $g('#'+labels[1]).length!=1) //if one of the two elements are NOT defined, exist
			return
		this.megamenus.push({$ganchorobj:$g("#"+labels[0]), $gmenuobj:$g("#"+labels[1]), $gmenuinner:$g("#"+labels[1]).children('ul:first-child'), revealtype:labels[2], hidetimer:null})
		var megamenu=this.megamenus[i]	
		megamenu.$ganchorobj.add(megamenu.$gmenuobj).attr("_megamenupos", i+"pos") //remember index of this drop down menu
		megamenu.actualwidth=megamenu.$gmenuobj.outerWidth()
		megamenu.actualheight=megamenu.$gmenuobj.outerHeight()
		megamenu.offsetx=megamenu.$ganchorobj.offset().left
		megamenu.offsety=megamenu.$ganchorobj.offset().top
		megamenu.anchorwidth=megamenu.$ganchorobj.outerWidth()
		megamenu.anchorheight=megamenu.$ganchorobj.outerHeight()
		$g(document.body).append(megamenu.$gmenuobj) //move drop down menu to end of document
		megamenu.$gmenuobj.css("z-index", ++this.zIndexVal).hide()
		megamenu.$gmenuinner.css("visibility", "hidden")
		megamenu.$ganchorobj.bind(megamenu.revealtype=="click"? "click" : "mouseenter", function(e){
			var menuinfo=jkmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))]
			clearTimeout(menuinfo.hidetimer) //cancel hide menu timer
			return jkmegamenu.showmenu(e, parseInt(this.getAttribute("_megamenupos")))
		})
		megamenu.$ganchorobj.bind("mouseleave", function(e){
			var menuinfo=jkmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))]
			if (e.relatedTarget!=menuinfo.$gmenuobj.get(0) && $g(e.relatedTarget).parents("#"+menuinfo.$gmenuobj.get(0).id).length==0){ //check that mouse hasn't moved into menu object
				menuinfo.hidetimer=setTimeout(function(){ //add delay before hiding menu
					jkmegamenu.hidemenu(e, parseInt(menuinfo.$gmenuobj.get(0).getAttribute("_megamenupos")))
				}, jkmegamenu.delaytimer)
			}
		})
		megamenu.$gmenuobj.bind("mouseenter", function(e){
			var menuinfo=jkmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))]
			clearTimeout(menuinfo.hidetimer) //cancel hide menu timer
		})
		megamenu.$gmenuobj.bind("click mouseleave", function(e){
			var menuinfo=jkmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))]
			menuinfo.hidetimer=setTimeout(function(){ //add delay before hiding menu
				jkmegamenu.hidemenu(e, parseInt(menuinfo.$gmenuobj.get(0).getAttribute("_megamenupos")))
			}, jkmegamenu.delaytimer)
		})
	} //end for loop
	if(/Safari/i.test(navigator.userAgent)){ //if Safari
		$g(window).bind("resize load", function(){
			for (var i=0; i<jkmegamenu.megamenus.length; i++){
				var megamenu=jkmegamenu.megamenus[i]
				var $ganchorisimg=(megamenu.$ganchorobj.children().length==1 && megamenu.$ganchorobj.children().eq(0).is('img'))? megamenu.$ganchorobj.children().eq(0) : null
				if ($ganchorisimg){ //if anchor is an image link, get offsets and dimensions of image itself, instead of parent A
					megamenu.offsetx=$ganchorisimg.offset().left
					megamenu.offsety=$ganchorisimg.offset().top
					megamenu.anchorwidth=$ganchorisimg.width()
					megamenu.anchorheight=$ganchorisimg.height()
				}
			}
		})
	}
	else{
		$g(window).bind("resize", function(){
			for (var i=0; i<jkmegamenu.megamenus.length; i++){
				var megamenu=jkmegamenu.megamenus[i]	
				megamenu.offsetx=megamenu.$ganchorobj.offset().left
				megamenu.offsety=megamenu.$ganchorobj.offset().top
			}
		})
	}
	jkmegamenu.addshim($g)
}

}

jQuery(document).ready(function($g){
	jkmegamenu.render($g)
})
