/* 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
*/

//jQuery.noConflict();

var jkmegamenu={

effectduration: 0, //duration of animation, in milliseconds
delaytimer: 100, //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
$mm_shimobj: null,

addshim:function($mm_){
	$mm_(document.body).append('<IFRAME id="outlineiframeshim" src="'+(location.protocol=="https:"? 'blank.htm' : 'about:blank')+'" style="display:none; left:0; top:0; z-index:999; position:absolute; filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)" frameBorder="0" scrolling="no"></IFRAME>')
	this.$mm_shimobj=$mm_("#outlineiframeshim")
},

alignmenu:function($mm_, e, megamenu_pos){
	var megamenu=this.megamenus[megamenu_pos]
	var $mm_anchor=megamenu.$mm_anchorobj
	var $mm_menu=megamenu.$mm_menuobj
	var menuleft=($mm_(window).width()-(megamenu.offsetx-$mm_(document).scrollLeft())>megamenu.actualwidth)? megamenu.offsetx : 10+megamenu.offsetx-megamenu.actualwidth+megamenu.anchorwidth //get x coord of menu
       // alert('window width '+$mm_(window).width()+' megamenu actualwidth '+megamenu.actualwidth)
	//var menutop=($mm_(window).height()-(megamenu.offsety-$mm_(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
	$mm_menu.css({left:menuleft+"px", top:menutop+"px"})
	this.$mm_shimobj.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 $mm_menu=megamenu.$mm_menuobj
	var $mm_menuinner=megamenu.$mm_menuinner
	if ($mm_menu.css("display")=="none"){
		this.alignmenu(jQuery, e, megamenu_pos)
		$mm_menu.css("z-index", ++this.zIndexVal)
		$mm_menu.show(this.effectduration, function(){
			$mm_menuinner.css('visibility', 'visible')
                        
		})
	}
	else if ($mm_menu.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 $mm_menu=megamenu.$mm_menuobj
	var $mm_menuinner=megamenu.$mm_menuinner
	$mm_menuinner.css('visibility', 'hidden')
	this.$mm_shimobj.css({display:"none", left:0, top:0})
	$mm_menu.hide(this.effectduration)
},

definemenu:function(anchorid, menuid, revealtype){
    	this.megamenulabels.push([anchorid, menuid, revealtype])
},



render:function($mm_){
	for (var i=0, labels=this.megamenulabels[i]; i<this.megamenulabels.length; i++, labels=this.megamenulabels[i]){
		if ($mm_('#'+labels[0]).length!=1 || $mm_('#'+labels[1]).length!=1) //if one of the two elements are NOT defined, exist
			return
		this.megamenus.push({$mm_anchorobj:$mm_("#"+labels[0]), $mm_menuobj:$mm_("#"+labels[1]), $mm_menuinner:$mm_("#"+labels[1]).children('ul:first-child'), revealtype:labels[2], hidetimer:null})
		var megamenu=this.megamenus[i]
		megamenu.$mm_anchorobj.add(megamenu.$mm_menuobj).attr("_megamenupos", i+"pos") //remember index of this drop down menu
                //cisi fix to chrome and safari not returning outerWidth correctly
                mmwid=megamenu.$mm_menuobj.attr('mmwid');
                if(mmwid==undefined){mmwid=3}
                //mmwid=2;
                //alert(mmwid)
		megamenu.actualwidth=mmwid * 200;//megamenu.$mm_menuobj.outerWidth()
                
		megamenu.actualheight=megamenu.$mm_menuobj.outerHeight()
		megamenu.offsetx=megamenu.$mm_anchorobj.offset().left
		megamenu.offsety=megamenu.$mm_anchorobj.offset().top
		megamenu.anchorwidth=megamenu.$mm_anchorobj.outerWidth()
		megamenu.anchorheight=megamenu.$mm_anchorobj.outerHeight()
		$mm_(document.body).append(megamenu.$mm_menuobj) //move drop down menu to end of document
		megamenu.$mm_menuobj.css("z-index", ++this.zIndexVal).hide()
		megamenu.$mm_menuinner.css("visibility", "hidden")
		megamenu.$mm_anchorobj.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.$mm_anchorobj.bind("mouseleave", function(e){
			var menuinfo=jkmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))]
			if (e.relatedTarget!=menuinfo.$mm_menuobj.get(0) && $mm_(e.relatedTarget).parents("#"+menuinfo.$mm_menuobj.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.$mm_menuobj.get(0).getAttribute("_megamenupos")))
				}, jkmegamenu.delaytimer)
			}
		})
		megamenu.$mm_menuobj.bind("mouseenter", function(e){
			var menuinfo=jkmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))]
			clearTimeout(menuinfo.hidetimer) //cancel hide menu timer
		})
		megamenu.$mm_menuobj.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.$mm_menuobj.get(0).getAttribute("_megamenupos")))
			}, jkmegamenu.delaytimer)
		})
	} //end for loop
	if(/Safari/i.test(navigator.userAgent)){ //if Safari
		$mm_(window).bind("resize load", function(){
			for (var i=0; i<jkmegamenu.megamenus.length; i++){
				var megamenu=jkmegamenu.megamenus[i]
				var $mm_anchorisimg=(megamenu.$mm_anchorobj.children().length==1 && megamenu.$mm_anchorobj.children().eq(0).is('img'))? megamenu.$mm_anchorobj.children().eq(0) : null
				if ($mm_anchorisimg){ //if anchor is an image link, get offsets and dimensions of image itself, instead of parent A
					megamenu.offsetx=$mm_anchorisimg.offset().left
					megamenu.offsety=$mm_anchorisimg.offset().top
					megamenu.anchorwidth=$mm_anchorisimg.width()
					megamenu.anchorheight=$mm_anchorisimg.height()
				}
			}
		})
	}
	else{
		$mm_(window).bind("resize", function(){
			for (var i=0; i<jkmegamenu.megamenus.length; i++){
				var megamenu=jkmegamenu.megamenus[i]
				megamenu.offsetx=megamenu.$mm_anchorobj.offset().left
				megamenu.offsety=megamenu.$mm_anchorobj.offset().top
			}
		})
	}
	jkmegamenu.addshim($mm_)
}

}

jQuery(document).ready(function($mm_){
    
	jkmegamenu.render($mm_)
    
})


