var currentlyHidingDiv = "";
var popupWindow, hoverWindow;
var fadeOutDone = true;

pageInit = function() {
	/* Popup summary */

	/* Popup large window first */
	popupWindow = new YAHOO.widget.Panel("popupWindow",
	  {
		width: "500px",
		fixedcenter: true,
		constraintoviewport: true,
		underlay: "none",
		close: true,
		visible: false,
		draggable: false,
		modal: true,
		effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: .25}
	  }
	);
	YAHOO.util.Event.addListener("popupOk", "click", popupWindow.hide, popupWindow, true);
	popupWindow.render();


	/* Simple overlay */
	hoverWindow = new YAHOO.widget.Overlay("hoverWindow",
	  {
		width: "300px",
		visible: false,
		effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: .25}
	  }
	);
	hoverWindow.hideEvent.subscribe(hoverOutDone);
	hoverWindow.render();


	var els = YAHOO.util.Dom.getElementsByClassName('popupModel', 'a');
	for (var i = 0; i < els.length; i++) {
		/* Get the hover div */
		sDivId = els[i].getAttribute('hoverId');
		sDiv = document.getElementById(sDivId);
		YAHOO.util.Dom.setStyle(sDiv, 'opacity', '0');

		/* Unhide the Summary layer when mouse over/out */
		YAHOO.util.Event.addListener(els[i], "mouseover", hoverOver);
		YAHOO.util.Event.addListener(els[i], "mouseout", hoverOut);
		YAHOO.util.Event.addListener(els[i], "mouseup", clickOn);
	}
}

/* This function is to unhide the div, then do a smooth very web2.0 fade-in */
function hoverOver() {
	var sDivId = this.getAttribute('hoverId');
	var sDiv = document.getElementById(sDivId);
	onDivId = sDiv;

	if (!fadeOutDone) {
		fadeRequest = this;
		return false;
	}
	fadeRequest = null;

	hoverWindow.setBody(sDiv.innerHTML);
	hoverWindow.cfg.setProperty('context', [this, 'br', 'bl']);
	hoverWindow.show();
}

/* This function is to unhide the div, then do a smooth very web2.0 fade */
function hoverOut() {
	fadeRequest = null;

	if (fadeOutDone == false)
		return false;
	hoverWindow.hide();
	fadeOutDone = false;
}

function hoverOutDone() {
	/* Fade out is finally done, we can now fade in any other divs
	 * that have been queued
	 */
	fadeOutDone = true;
	if (fadeRequest != null) {
		hoverOver.call(fadeRequest);
	}
}

function clickOn() {
	var sDivId = this.getAttribute('hoverId');
	var sDiv = document.getElementById(sDivId+'_pop');

	popupWindow.setBody(sDiv.innerHTML);
	popupWindow.show();
}

YAHOO.util.Event.addListener(window, "load", pageInit);
