
var WindowSize = Class.create({
    width: window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth),
    height: window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight)	
});

Prototype.Browser.IE7=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==7;

var KMegaMenu = Class.create({
	effectduration: 0.3, //duration of animation, in milliseconds
	delaytimer: 200, 	//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: 		5000, //starting z-index value for drop down menu
	shimobj: 		null,	
	
	initialize: function(){
		
		
	},
	addshim:function(){
	
		$(document.body).insert('<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.shimobj = $("outlineiframeshim");
	},	
	alignmenu:function(e, megamenu_pos){
		var ws = new WindowSize();
		var doc = document.viewport.getScrollOffsets();
		
		var megamenu = this.megamenus[megamenu_pos];
		var anchor   = megamenu.anchorobj;
		var menu	 = megamenu.menuobj;
		
		//console.log('align:   '+ws.width + ' ' + doc.left + ' ' +megamenu.offsetx   +  ' ' + megamenu.actualwidth);
		
		var menuleft = (ws.width -(megamenu.offsetx-doc.left)>megamenu.actualwidth)? megamenu.offsetx : megamenu.offsetx-megamenu.actualwidth+megamenu.anchorwidth; //get x coord of menu

		
		//var menutop=($(window).height()-(megamenu.offsety-$(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
		menu.setStyle({left:menuleft+"px", top:menutop+"px"});
		this.shimobj.setStyle({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 menu		= megamenu.menuobj;
		var menuinner	= megamenu.menuinner;
		
		
	
		for(i=0;i<this.megamenus.length;i++){
			
			if(i != megamenu_pos && menu.id.indexOf("_") == -1){
				this.hidemenu(null, i);
			}	
		}

		if (!menu.visible()){
	
			this.alignmenu(e, megamenu_pos);
			menu.setStyle({"zIndex" : ++this.zIndexVal});
			

			if(Prototype.Browser.IE7){
			
				menu.show();
					menuinner.setStyle({
						'visibility': 'visible'
					});				
			}
			else{
				new Effect.Grow(menu,{direction:'top-left', duration: this.effectduration, afterFinish: function(){
					menuinner.setStyle({
						'visibility': 'visible'
					});			
				}.bind(this) });
}
			
			/*menu.show(this.effectduration, function(){
				menuinner.setStyle({
					'visibility': 'visible'
				});
			})*/
		}
		else if (menu.getStyle("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 menu=megamenu.menuobj;
		var menuinner=megamenu.menuinner;
		menuinner.setStyle({'visibility' : 'hidden'});
		this.shimobj.setStyle({display:"none", left:0, top:0});
		
		
		menu.hide();
		
		/*new Effect.Fold(menu,{direction:'top-left', duration: this.effectduration, afterFinish: function(){
			menuinner.setStyle({
				'visibility': 'hidden'
			});			
		}.bind(this) });*/


	},
	
	definemenu:function(anchorid, menuid, revealtype){
		this.megamenulabels.push([anchorid, menuid, revealtype]);
		
	},
	
	render:function(){
		//console.log(this.megamenus);
		for (var i=0, labels=this.megamenulabels[i]; i<this.megamenulabels.length; i++, labels=this.megamenulabels[i]){
			if (!$(labels[0]) || !$(labels[1])) //if one of the two elements are NOT defined, exist
				continue;
			
			this.megamenus.push({	anchorobj:$(labels[0]),  //link die activeert
									menuobj:$(labels[1]),    //submenu div
									menuinner:$(labels[1]).select('ul').first() , //submenu ul 
									revealtype:labels[2],    //mouseover
									hidetimer:null
								});
							
			var megamenu			= this.megamenus[i];
			var dimensions = megamenu.menuobj.getDimensions();		
				
			megamenu.anchorobj.insert(megamenu.menuobj).writeAttribute("_megamenupos", i+"pos"); //remember index of this drop down menu

			megamenu.actualwidth	= dimensions.width;
			megamenu.actualheight	= dimensions.height;

			megamenu.offsetx		= megamenu.anchorobj.cumulativeOffset().left;
			megamenu.offsety		= megamenu.anchorobj.cumulativeOffset().top;
			
			//set coordinates for subsubmenus
			
			if ((megamenu.offsetx==0 && megamenu.offsety==0) || megamenu.offsetx == -1) {
				
				if(megamenu.anchorobj.up('.megamenu').id){
					arrays = megamenu.anchorobj.up('.megamenu').id.split("_");
					
		
					megamenu.topnr			= arrays[arrays.length-1];
					//console.log(megamenu.topnr);
				}

					
				
				megamenu.topanchor		= $('hmegamenulink_'+megamenu.topnr);
				//console.log('hmegamenulink_'+megamenu.topnr);
				//console.log(megamenu.anchorobj.up('.megamenu').id);
				//megamenu.topanchor = megamenu.anchorobj.up('.megamenu');
				megamenu.offsetx		= megamenu.topanchor.cumulativeOffset().left + megamenu.actualwidth - 35;
				megamenu.offsety		= megamenu.topanchor.cumulativeOffset().top + 25;
			}
			
			//console.log('render '+ i +' x:'+megamenu.offsetx+' y:'+megamenu.offsety +' w:'+megamenu.actualwidth+' h:'+megamenu.actualheight);
			
			var anchor_dim = megamenu.anchorobj.getDimensions();
			
			megamenu.anchorwidth	= anchor_dim.width;
			megamenu.anchorheight	= anchor_dim.height;
			
			$(document.body).insert(megamenu.menuobj) //move drop down menu to end of document
			megamenu.menuobj.writeAttribute("_megamenupos", i+"pos");
			megamenu.menuobj.setStyle({"zIndex" : ++this.zIndexVal}).hide();
			megamenu.menuinner.setStyle({"visibility": "hidden"});
			
			
		
			
			var _this = this;
			

			megamenu.anchorobj.observe(megamenu.revealtype == "click" ? 'click' : 'mouseenter',function(e){
				var el = Event.element(e);
				var pos = parseInt(el.readAttribute("_megamenupos").replace("pos", ""));
				
				var menuinfo=_this.megamenus[pos];
				clearTimeout(menuinfo.hidetimer); //cancel hide menu timer
				return _this.showmenu(e, pos);					
			});
	

			megamenu.anchorobj.observe('mouseleave',function(e){
				
		
		
				var el 			= Event.element(e);
				var pos 		= parseInt(this.readAttribute("_megamenupos").replace("pos", ""));
				var menuinfo 	= _this.megamenus[pos];
				


				if (e.relatedTarget != menuinfo.menuobj && (!$(e.relatedTarget).up("#"+menuinfo.menuobj.id)) ){

					menuinfo.hidetimer = setTimeout(function(){ //add delay before hiding menu
						_this.hidemenu(e, pos)
					}, _this.delaytimer);
					
				}				
			},this);

			megamenu.menuobj.observe('mouseenter',function(e){
				var el = Event.element(e);
				
				if (el.tagName == "DIV") {
								
					var pos = parseInt(el.readAttribute("_megamenupos").replace("pos", ""));
					
					var menuinfo = _this.megamenus[pos];
					clearTimeout(menuinfo.hidetimer); //cancel hide menu timer
				}				
			});

			megamenu.menuobj.observe('click',function(e){
				var el = Event.element(e);
				
				if (el.tagName != "DIV") {
					var el = el.up('div.megamenu');	
				}

				if (el) {
				
					var pos = parseInt(el.readAttribute("_megamenupos").replace("pos", ""));
					
					var menuinfo = _this.megamenus[pos];
					menuinfo.hidetimer = setTimeout(function(){ //add delay before hiding menu
						_this.hidemenu(e, pos)
					}, _this.delaytimer);
				}		
			});
			
			megamenu.menuobj.observe('mouseleave',function(e){
					hide = true;
					hideparent = false;
					var el = Event.element(e);
					
					if (el.tagName != "DIV") {
						var el = el.up('div.megamenu');	
					}	
					
					if (el) {
						if(el.readAttribute("_megamenupos")){
							var pos = parseInt(el.readAttribute("_megamenupos").replace("pos", ""));
						
							var menuinfo = _this.megamenus[pos];
					
							//nr = menuinfo.anchorobj.id.slice(-1);
							
							nr = el.id.replace("megamenu","");
							nr_array = nr.split("_");
							
							if (nr_array.length<3) {	//no subsubmenu
								submenu = $$("div[id^='megamenu"+nr+"_']"); 
								if (submenu.length>0) {
									for (var i=0; i<submenu.length; i++){
										if (submenu[i].getStyle('display')=='block') {
											hide=false;
										}
									}
								}
							} else {
								pos2 = $('megamenu'+nr).readAttribute("_megamenupos").replace("pos", "");
								hideparent = true;
							}
							
							
							//hide child first, check secondly if we are still on top of parent?
							
							if (hide==true) {	
								menuinfo.hidetimer = setTimeout(function(){ //add delay before hiding menu
									_this.hidemenu(e, pos);
									
									if (hideparent==true) {
										//menuinfo.hidetimer = setTimeout(function(){ //add delay before hiding menu
											_this.hidemenu($('megamenu'+nr), pos2)
										//}, _this.delaytimer);
									}									
									
								}, _this.delaytimer);
							}							
							

							

							
						}
					}	
				
			});			


		} //end for loop
		

		
		if(/Safari/i.test(navigator.userAgent)){ //if Safari
		
			Event.observe(window,'load',function(){
				for (var i=0; i< this.megamenus.length; i++){
					var megamenu = this.megamenus[i];
					var anchorisimg=(megamenu.anchorobj.children().length==1 && megamenu.anchorobj.children().eq(0).is('img'))? megamenu.anchorobj.children().eq(0) : null;
					if (anchorisimg){ //if anchor is an image link, get offsets and dimensions of image itself, instead of parent A
						megamenu.offsetx	= anchorisimg.cumulativeOffset().left;
						megamenu.offsety	= anchorisimg.cumulativeOffset().top;
						megamenu.anchorwidth= anchorisimg.getWidth();
						megamenu.anchorheight=anchorisimg.getHeight();
					}
				}
			}.bind(this));
			
			Event.observe(window,'resize',function(){
				for (var i=0; i<this.megamenus.length; i++){
					var megamenu=this.megamenus[i];
					var anchorisimg=(megamenu.anchorobj.children().length==1 && megamenu.anchorobj.children().eq(0).is('img'))? megamenu.anchorobj.children().eq(0) : null;
					if (anchorisimg){ //if anchor is an image link, get offsets and dimensions of image itself, instead of parent A
						megamenu.offsetx=anchorisimg.offset().left;
						megamenu.offsety=anchorisimg.offset().top;
						megamenu.anchorwidth=anchorisimg.width();
						megamenu.anchorheight=anchorisimg.height();
					}
				}
			}.bind(this));					


		}
		else{
			
			Event.observe(window,'resize',function(){
			

				for (var i=0; i< this.megamenus.length; i++){
					var megamenu= this.megamenus[i];
					
					megamenu.offsetx		= megamenu.anchorobj.cumulativeOffset().left;
					megamenu.offsety		= megamenu.anchorobj.cumulativeOffset().top;
					

					//set coordinates for subsubmenus
					
					if ((megamenu.offsetx==0 && megamenu.offsety==0) || megamenu.offsetx == -1) {
						
						if(megamenu.anchorobj.up('.megamenu').id){
							arrays = megamenu.anchorobj.up('.megamenu').id.split("_");
							
				
							megamenu.topnr			= arrays[arrays.length-1];
							//console.log(megamenu.topnr);
						}
		
							
						
						megamenu.topanchor		= $('hmegamenulink_'+megamenu.topnr);
						//console.log('hmegamenulink_'+megamenu.topnr);
						//console.log(megamenu.anchorobj.up('.megamenu').id);
						//megamenu.topanchor = megamenu.anchorobj.up('.megamenu');
						megamenu.offsetx		= megamenu.topanchor.cumulativeOffset().left + megamenu.actualwidth - 35;
						megamenu.offsety		= megamenu.topanchor.cumulativeOffset().top + 25;
					}
								
					

				}
			}.bind(this));

		}

		this.addshim();
	}	
	
	
});   




