
// ===========================================================================

var MAW = new Site();

// ===========================================================================

var DHTMLMenu = Class.create(PageWidget, {
	initialize : function(menu_id, config) {
		this.node = $(menu_id);

		// bail out if the menu doesn't exist on this page
		if (!this.node || this.initialized) return;

		this.setOptions(config);

		this.submenus = { };

		// get all the top-level LIs in the menu and iterate over them, adding event handlers
		$(menu_id).immediateDescendants().each(function(item){
			item.observe("mouseover", this.mouseoverHandler.bindAsEventListener(this, item));
			item.observe("mouseout", this.mouseoutHandler.bindAsEventListener(this, item));

			this.submenus[item.id] = item.down().next();
			/* console.log(this.submenus[item.id]); */
		}.bind(this));

	},

	initialized : false,
	node : null,              // holds the DOM node of the menu
	menu_hide_timeout : null, // the JS timeout ID for hiding the menu
	menu_show_timeout : null, // the JS timeout ID for showing the menu
	last_menu_on : null,      // the DOM object of the waiting to close

	mouseoverHandler : function(e, item) {
		/* console.log(arguments);  */
		// stop the menu from closing/opening (this gets called a lot)
		clearTimeout(this.menu_hide_timeout);
		clearTimeout(this.menu_show_timeout);

		// if we're mousing over the menu for the first time, set a timeout so the menu doesn't show up accidentally.
		if (this.last_menu_on == null) {
			this.menu_show_timeout = setTimeout(this.showMenu.bind(this, item),	this.CONFIG["menu_show_time"]);

		// if there's already a menu on, then we know the user is expecting to see another one, so show it immediately.
		} else if (this.last_menu_on != item) {
			this.showMenu(item);
		}
	},

	mouseoutHandler : function(e, item) {
		/* console.log(arguments);  */
		// clear the existing show/hide timeouts (this gets called a lot)
		clearTimeout(this.menu_hide_timeout);
		clearTimeout(this.menu_show_timeout);

		// only "close" the menu in a little bit if we're over a menu with submenus, otherwise, close it right now
		if (this.submenus[item.id]) {
			this.menu_hide_timeout = setTimeout(this.hideMenu.bind(this, item),	this.CONFIG["menu_hide_time"]);

		} else {
			this.hideMenu(item);
		}
	},

	// shows the menu
	showMenu : function(item) {
		// hide the last menu shown
		if (this.last_menu_on != null) { this.hideMenu(this.last_menu_on); }

		// adding the "hover" class turns on the menu
		item.addClassName(this.CONFIG['hover_class']);

		// insert the IFRAME for IE
/*		if (Prototype.Browser.IE6) {
			// get at the submenu for dimension info
			var submenu = this.submenus[item.id];
			if (submenu) {
				var iframe = submenu.next();

				if (submenu && !iframe) {
					this.createIframe(item, {
						'left'   : submenu.offsetLeft + "px",
						'height' : submenu.offsetHeight + "px",
						'width'  : submenu.offsetWidth + "px",
						'allowtransparency'  : "true"
					});
				}
			}
		}
*/
		// store the last item on
		this.last_menu_on = item;

	}, // END: showMenu()

	// hide the menu
	hideMenu : function(item) {	
		// nothing to hide
		if (item == null) return;

		// removing the "hover" class turns off the menu
		item.removeClassName(this.CONFIG['hover_class']);

		// remove the IFRAME for IE
		//if (Prototype.Browser.IE6) {
			var submenu = this.submenus[item.id];
			if (submenu) {
				var iframe = submenu.next();
				if (submenu && iframe) {
					item.removeChild(iframe);
				}
			}
		//}

		this.last_menu_on = null;
	}, // END: hideMenu()

	// makes an IFRAME the exact size of the menu so elements underneath it are covered
	// (IE-only)
	createIframe : function(node, style) {
		new Insertion.Bottom(node, (new Template('<iframe class="' + this.CONFIG['iframe_class'] + '" frameborder="0" scrolling="no" style="width: #{width}; height: #{height}; left: #{left};"><\/iframe>')).evaluate(style));
	}
});

DHTMLMenu.CONFIG = {
	submenu_class : "SubNav", // the DOM class of the menu container
	hover_class : "Hover", // the class to give the top-level LI to "activate" the menu
	menu_hide_time : 500, // time to keep the menus on after mouseout; in ms
	menu_show_time : 100, // threshold o
	iframe_class : "IframeFix"
};

fixWebKitInheritanceBug(DHTMLMenu);

// ---------------------------------------------------------------------------

MAW.addFeature('DHTMLMenus', {
	setupElements : function(scope) {
		$$S(scope, ".DHTMLMenu").each(function(menu) {
			this.storeWidgetInstance(menu.id, new DHTMLMenu(menu.id));
		}.bind(this));
	}
});


// ===========================================================================


MAW.addFeature('TestLinkInterceptor', {
	setupElements : function() {
		var alert_msg = function() { alert("Sorry, this link is not implemented yet."); return false; };
		var debug = this._debug_mode('debug');
		var all_links = document.getElementsByTagName("A");
		for (var i=0; i < all_links.length; i++) {
			if (all_links[i].href.toUpperCase().indexOf("MAW_TEST") < 0) { continue; }
			all_links[i].onclick = alert_msg;
			if (debug)
				this._highlight_test_link(all_links[i]);
		}
	},

	_highlight_test_link : function(link) {
		link.style.borderWidth = "1px;";
		link.style.borderStyle = 'solid';
		link.style.borderColor = "#C33";
	},

	_debug_mode : function(name) {
		var pattern = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( pattern );
		var paramter = regex.exec( window.location.href );
		var debug = false;
		if (!(null == paramter))
			debug = true;

		return debug;
	}
});


// ============================================================================
function email_popup(el) {
  var url = "/layout/set/popup" + el.rel;
  var client_win = window.open(url, 'clientWin', 'width=376,height=540,toolbar=no,scrollbars=no,scrolling=no,statusbar=yes,resizable=yes');
  if (window.focus) client_win.focus();
}

// ===========================================================================


//ie6 fix for calendar hover effect	
MAW.addFeature('IEHover', {
	setupElements : function(){
		if (Prototype.Browser.IE6) {
			var events = $$('#Calendar div.event');
			events.each(function(el) {
				el.onmouseover=function() {
					el.addClassName('iehover');
				}
				el.onmouseout=function() {
					el.removeClassName('iehover');
				}
			});
		}
	}
});