﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("Windows");

Windows.ToolTipBehavior = function(popup, trigger) {
	this.popupElement = popup;
	this.triggerElement = trigger;
	this.initialize();
}

Windows.ToolTipBehavior.prototype = {
    initialize: function() {
        $addHandler( this.triggerElement, 'mouseover', new Function.createDelegate( this, this.triggerOver ) );
        $addHandler( this.triggerElement, 'mouseout', new Function.createDelegate( this, this.triggerOut ) );
        $addHandler( this.popupElement, 'mouseover', new Function.createDelegate( this, this.popupOver ) );
        $addHandler( this.popupElement, 'mouseout', new Function.createDelegate( this, this.popupOut ) );
        this.hideDelegate = Function.createDelegate( this, this.hidePopup );
        Sys.Application.add_unload( Function.createDelegate(this, this.dispose) );
        this.hidePopup();
    },	
    dispose: function() {        
        this.popupElement = null;
        this.triggerElement = null;
        this.popupTimer = null;
    },
    triggerOver : function( args ) {
		clearTimeout(this.popupTimer);
		this.showPopup();
    },
    triggerOut : function( args ) {
		clearTimeout(this.popupTimer);
		this.popupTimer = setTimeout( this.hideDelegate, 10 );
    },
    popupOver : function( args ) {
		args.stopPropagation();
		clearTimeout(this.popupTimer);
		this.showPopup();
    },
    popupOut : function( args ) {
		args.stopPropagation();
		clearTimeout(this.popupTimer);
		this.popupTimer = setTimeout( this.hideDelegate, 10 );
    },
    showPopup : function() {
		this.popupElement.style.visibility = "visible";
		this.popupElement.style.display = "block";
    },
    hidePopup : function() {
		this.popupElement.style.visibility = "hidden";
		this.popupElement.style.display = "none";
    }
}
Windows.ToolTipBehavior.registerClass('Windows.ToolTipBehavior');

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

// recursive function that loops through all elements in the "root" element, 
// looking for class="popup" and adding SAPopup behavior to those controls
function createPopups( root )
{
	if ( root.className != "popupDiv" ) {
	    for (var i = 0; i < root.childNodes.length; i++) {
		// loop through root's childnotes
	        if (root.childNodes[i].className == "popup") {
			// if a node with class 'popup' is found, create new div element
				var absoluteDiv = document.createElement("div");
				absoluteDiv.className = "popupDiv";

				// modify DOM for popup:
				// add a new child to the root (i.e. in the same container as the 'popup')
				root.appendChild(absoluteDiv);
				absoluteDiv.style.position = "absolute";
				// move the 'popup' inside of the new child
				absoluteDiv.appendChild(root.childNodes[i]);

				// initialize popup:
				// THE PARENT IS ACTUALLY THE TRIGGER, SO THE POPUPS SHOULD NOT SHARE THE SAME PARENT!
				new Windows.ToolTipBehavior(absoluteDiv, root);
			}
			else {
			// if root's current childnode is not a 'popup', run createPopups on root's next childNode
				createPopups( root.childNodes[i] );
			}
		}
	}
}