﻿/*extern $, Dom, Event, YAHOO */
/*global PROVIDENCE */
/*members RootPath, Scope, addClass, addListener, childNodes, className, 
    close, closeEmail, emailPage, exec, getAncestorByTagName, 
    getElementsByClassName, getTarget, hasClass, href, initMenus, 
    interactiveMenus, isNull, lang, length, location, mailWindow, nodeName, 
    onDOMReady, open, preventDefault, removeClass, title, toLowerCase, 
    toString, toggleInteractiveMenu
*/

PROVIDENCE = {
	
	onDOMReady: function() {
		this.initMenus();
	},

	initMenus: function() {
		var x, y, menu;
		var links = [];
		var menus = [];

		x = this.interactiveMenus.length; 
		
		while(x--) {
			menu = $('I' + this.interactiveMenus[x]);
			if (!menu) { continue; }
			menus[menus.length] = menu;
			for (y = 0; y < menu.childNodes.length; y++) {
				if (menu.childNodes[y].nodeName.toLowerCase() === 'a') {
					links[links.length] = menu.childNodes[y];
					break;
				}
			}
		}

		Event.addListener(links, 'click', Event.preventDefault, Event, true);
		Event.addListener(menus, 'click', this.toggleInteractiveMenu, this, true);
	},

	toggleInteractiveMenu: function(e) {
		var li     = Event.getTarget(e);
		if (li.nodeName.toLowerCase() !== 'li') { li = Dom.getAncestorByTagName(li, 'li'); }
		var tierRE = /\bT(\d+)\b/;
		var tier   = tierRE.exec(li.className);
		var tierN  = parseInt(tier[1], 10);
		var x, ul = null;

		x = li.childNodes.length; while (x--) {
			if (li.childNodes[x].nodeName.toLowerCase() === 'ul') {
				ul = li.childNodes[x];
				break;
			}
		}

		if (Dom.hasClass(li, 'Active')) {

			// Avoid closing this menu if it contains a current item.
			if (ul !== null && Dom.getElementsByClassName('Current', 'li', li).length > 0) { return; }

			Dom.removeClass(li, 'Active');
			Dom.removeClass(li, tier[0] + 'Active');
			if (ul === null) { return; }
			Dom.removeClass(ul, 'Active');
			Dom.removeClass(ul, 'T' + (tierN + 1).toString() + 'Active');
		} else {
			Dom.addClass(li, 'Active');
			Dom.addClass(li, tier[0] + 'Active');
			if (ul === null) { return; }
			Dom.addClass(ul, 'Active');
			Dom.addClass(ul, 'T' + (tierN + 1).toString() + 'Active');
		}
	},


	emailPage: {
		mailWindow: null,
		RootPath: '/Content/MailPage/',
				
		emailPage: function(Augustine) {
			var sURL;
			if(Augustine == 1) {
			// this is a tweak to replace the subject line for augustine pages email (the subject line uses the page title).
				var pageTitle = document.title.substring(23,document.title.length)
				sURL = this.RootPath + 'Window-MailPage.asp?URL=' + encodeURIComponent(document.location.href) + '&Title=Augustine Health Group - ' + encodeURIComponent(pageTitle);
			} else {
				sURL = this.RootPath + 'Window-MailPage.asp?URL=' + encodeURIComponent(document.location.href) + '&Title=' + encodeURIComponent(document.title);			
			}
						
			this.mailWindow = window.open(sURL, 'MailPage', 'height=575,width=500,toolbar=no,scrollbars=yes,resizable=yes');
			if (YAHOO.lang.isNull(this.mailWindow) === false) {
				this.mailWindow.Scope = this;
			} else {
				alert('Your popup blocker has prevented us from opening a new window.\nPlease disable your popup blocker and try again.');
			}
		},
		
		closeEmail: function() {
			if (YAHOO.lang.isNull(this.mailWindow) === false) { this.mailWindow.close(); }
		}

	}


};
Event.onDOMReady(PROVIDENCE.onDOMReady, PROVIDENCE, true);


