// globals
var sa_popup;

// onload
$(function(){	
	$("#ca_manager a.button-grey").click(function(){
		var w = 600;
		var h = 500;
		var u = $(this).attr("href");
		if (sa_popup)
			sa_popup.close();
		sa_popup = window.open(
			u,
			"sa_popup",
			"top="+((screen.height/2)-(h/2))+",left="+((screen.width/2)-(w/2))+",width="+w+",height="+h+",resizable,scrollbars"
		);
		return false;
	});
});

$(function(){
	$("a.dashboard").click(function(){
		window.open(
			$(this).attr("href"),
			"gap_dashboard",
			"width=950,height="+screen.height+",top=0,left="+((screen.width/2)-(950/2))+",scrollbars"
		);
		return false;
	});
});

var hdanz = {

	/*
	 * contains reference to the admin menu ul element that the script uses
	 * to show or hide on mouse over for IE because of not supporting the 
	 * :hover pesudoclass properly.
	 */
	adminMenu : null,

	startList : function() {
		if (document.getElementById("nav-second")) {
			navRoot = document.getElementById("nav-second");
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					for (x=0; x<node.childNodes.length; x++) {
						y = node.childNodes[x];
						if (y.nodeName=="SPAN") {
							y.onmousedown=function() {
								hdanz.collapseAll();
								this.parentNode.className+=" over active";
							}
						}
					}
				}
			}
		}
	},

	collapseAll : function() {
		if (document.getElementById("nav-second")) {
			navRoot = document.getElementById("nav-second");
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.className=node.className.replace(" over", "");
					node.className=node.className.replace(" active", "");
				}
			}
		}
	},

	/*
	 * Add the event handlers that show and hide the admin menu when the user
	 * moves the mouse over the "client login" button.
	 */
	adminMenu : function() {
		if (document.getElementById('nav-first')) {
			var menuItems = document.getElementById('nav-first').getElementsByTagName('li');
			for (var i = 0; i < menuItems.length; i++) {
				if (menuItems[i].className == 'login') {
					var adminMenu = menuItems[i].getElementsByTagName('ul');
					if (adminMenu.length == 1) {
						hdanz.adminMenu = adminMenu[0];
						menuItems[i].onmouseover = function() {
							hdanz.adminMenu.style.display = 'block';
						}
						menuItems[i].onmouseout = function() {
							hdanz.adminMenu.style.display = 'none';
						}
					}
				}
			}
		}
	},

	/**
	 * 
	 */
	printPage : function(title) {
		var i, a;
		for (i = 0; (a = document.getElementsByTagName('link')[i]); i++) {
			if (a.getAttribute('title')) {
				a.disabled = (a.getAttribute('title') == title) ? false : true;
			}
		}
	}

}

window.onload = function() {
	hdanz.startList();
	hdanz.adminMenu();
}
