
// This function is being called from the swf when the close animation is finished
function animationFinished(id) {
	// reg is an instance of the Popup.registry array which holds a referance to
	// all the popup objects created with the constructor
	var reg = Popup.registry;
	var obj = Popup.registry[id];
	
	// Hide the popup
	obj.popupNode.style.visibility = "hidden";
}

// This function is being called from the swf to declare itself loaded
function flashLoaded(id) {
	// reg is an instance of the Popup.registry array which holds a referance to
	// all the popup objects created with the constructor
	var reg = Popup.registry;
	var obj = Popup.registry[id];
	
	//
	if (obj.callflashLoadfunction) {
		obj.callflashLoadfunction = false;
		obj.flashLoaded = true;
		//if (obj.over) {
			Popup.callExternalInterface(id,"open");
		//}
	}
	
	//var debugNode2 = document.getElementById('debug02');
	//debugNode2.innerHTML = "flashLoaded id: " + id;
	
}

// =================================================================================
// Class properties
// =================================================================================
Popup.registry = [];
Popup.animationLength = 200;
Popup.minCPUResolution = 10;
Popup.hideDelay = 100;
Popup.alphaAmount = 90;
// =================================================================================
// End Class properties
// =================================================================================


// =================================================================================
// Define constructor
// =================================================================================
function Popup(id) {
	// Define instance properties
	
	// Class variables
	this.id = id;
	Popup.registry[id] = this;
	
	// HTML variables
	this.htmlVariablesSet = false;
	this.popup_rootNode = null;
	this.popupNode = null;
	this.height = null;
	this.width = null;
	
	// Animation variables
	this.hideTimer = false;
	this.aniTimer = false;
	this.startTime = 0;
	this.open = false;
	this.over = false;
	this.flashLoaded = false;
	this.callflashLoadfunction = true;
}
// =================================================================================
// End define constructor
// =================================================================================

// =================================================================================
// Define Class methods
// =================================================================================
Popup.initialize = function(node) {
	// id is a string value of the node id from the current popup
	var id = node.id;
	
	// thisPopup is an instance of the object controling the current popup
	var thisPopup = Popup.registry[id];
	
	// Save some html variables if they not already saved
	if (!thisPopup.htmlVariablesSet) {
		// Set flag to true so we are only doing this once
		thisPopup.htmlVariablesSet = true;
		
		// Save the root popup node
		thisPopup.popup_rootNode = node;
		
		// Find the first DIV child and save it, it will be our popup
		var kids = thisPopup.popup_rootNode.childNodes;
		var numKids = kids.length;
		
		for(var i = 0; i < numKids ; i++){
			if (kids[i].nodeName == 'DIV') {
				// Save the first DIV child, this is our popup
				thisPopup.popupNode = kids[i];
				
				// Exit the main for loop
				break;
			}
		}
		
		// Save the width and height
		thisPopup.width = thisPopup.popupNode.offsetWidth;
		thisPopup.height = thisPopup.popupNode.offsetHeight;
		
		// Get the popup links from the html and replace it with a swf
		var anchorHref = "";
		var anchorTarget = "";
		var anchorText = "";
		var numberOfLinks = 0;
		
		// Look for anchor tags in our popup DIV
		// Setup the loop variables
		var kids = thisPopup.popupNode.childNodes;
		var numKids = kids.length;
		
		// Do the loop
		for(var i = 0; i < numKids ; i++){
			if (kids[i].nodeName == "A") {
				// Save the anchor href
				anchorHref += (kids[i].href) + "|";
				
				// Keep track of the number of links
				numberOfLinks ++;
				
				// Save the anchor target
				anchorTarget += (kids[i].target) + "|";
				
				// Loop through the children of the anchor tag and
				// look for text inside the <a></a> tags
				// Setup the loop variables
				var kids_A = kids[i].childNodes;
				var numKids_A = kids_A.length;
				
				// Do the loop
				for(var t = 0; t < numKids_A ; t++){
					// nodeType == 3 is a text node
					if (kids_A[t].nodeType == 3) {
						// Save the anchor text
						anchorText += (kids_A[t].data) + "|";
					}
				}
				
			}
		}
		
		// Delete every child in the popup DIV so we can add the swf
		// Setup the loop
		var kids = thisPopup.popupNode.childNodes;
		var numKids = kids.length;
		// Do the loop, we must loop backwords in order for this to work
		for(var i = numKids-1; i >= 0; i--){
			thisPopup.popupNode.removeChild(kids[i]);
		}
		
		// Setup the width and height for the swf
		var extraFlashHeightForTheArrow = 17;
		var width = thisPopup.width;
		var height = thisPopup.height + extraFlashHeightForTheArrow;
		
		// Setup the variables that we need to pass to flash
		var flashVars = "flashVarsHref=" + anchorHref;
		flashVars += "&flashVarsTarget=" + anchorTarget;
		flashVars += "&flashVarsText=" + anchorText;
		flashVars += "&flashVarsWidth=" + width;
		flashVars += "&flashVarsHeight=" + height;
		flashVars += "&flashVarsID=" + thisPopup.id;
		flashVars += "&flashVarsLength=" + Popup.animationLength;
		flashVars += "&flashVarsAlpha=" + Popup.alphaAmount;
		
		
		// Create the swf
		thisPopup.popupNode.innerHTML  = 
		'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="'+ id +'flash">'
		+ '<param name="movie" value="popupMenu.swf">'
		+ '<param name="quality" value="high">'
		+ '<param name="FlashVars" value="'+ flashVars +'">'
		+ '<param name="allowScriptAccess" value="always">'
		+ '<param name="wmode" value="transparent">'
		//+ '<param name="bgcolor" value="cccccc">'
		+ '<embed src="popupMenu.swf" width="'+width+'" height="'+height+'" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" wmode="transparent" name="'+ id +'flash" FlashVars="'+flashVars+'"></embed>'
		+ '</object>';
		
		// Debug
		thisPopup.popupNode.style.visibility = "visible";
		//alert(id +"flash");
		
		// Create mouseover and mouseout functions for the root popup node
		thisPopup.popup_rootNode.onmouseover=Popup.onMouseOver;
		thisPopup.popup_rootNode.onmouseout=Popup.onMouseOut;
		
		// Call Popup.onMouseOver
		node.onmouseover();
	}
}


Popup.onMouseOver = function(mouseEvent) {
	// id is a string value of the node id from the current popup
	var id = this.id;
	//alert(id);
	// reg is an instance of the Popup.registry array which holds a referance to
	// all the popup objects created with the constructor
	var reg = Popup.registry;
	var obj = Popup.registry[id];
	
	// Set the over flag
	obj.over = true;
	
	// Loop through all the popups in the registry and close other popups.
	for (popup in reg) if (id != popup) Popup.hide(popup);
	
	// If this popup is scheduled to close, cancel it.
	if (obj.hideTimer) { obj.hideTimer = window.clearTimeout(obj.hideTimer) }
	
	// If this popup is closed, open it.
	if (!obj.open && !obj.aniTimer) reg[id].startAnimation(true);
}


Popup.onMouseOut = function(mouseEvent) {
	// id is a string value of the node id from the current popup
	var id = this.id;
	
	// Schedules the popup to close after <hideDelay> ms, which
	// Gives the user time to cancel the action if they accidentally moused out
	var obj = Popup.registry[id];
	if (obj.hideTimer) window.clearTimeout(obj.hideTimer);
	obj.hideTimer = window.setTimeout("Popup.hide('" + id + "')", Popup.hideDelay);
}


// Call a function registered as callAnimate in the SWF named movieName.
Popup.callExternalInterface = function (id,animationState) {
	// Set the movieName
	var movieName = id + "flash";
	
	// The Popup.registry array holds a referance to all the popup objects
	// created with the constructor
	var obj = Popup.registry[id];
	
	// If the flash is loaded then use the ExternalInterface
	if (obj.flashLoaded) {
		Popup.getMovieName(movieName).callAnimate(animationState);
	} 
	
}


// This utility function resolves the string movieName to a Flash object reference based on browser type.
Popup.getMovieName = function(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}


Popup.hide = function(id) {
	// The Popup.registry array holds a referance to all the popup objects
	// created with the constructor
	var obj = Popup.registry[id];
	
	// Set the over flag
	obj.over = false;
	
	// If this popup is scheduled to close, cancel it.
	if (obj.hideTimer) window.clearTimeout(obj.hideTimer);
	
	// flag that this scheduled event has occured.
	obj.hideTimer = 0;
	
	// if this popup is open, close it.
	if (obj.open && !obj.aniTimer) obj.startAnimation(false);
}
// =================================================================================
// End define Class methods
// =================================================================================

// =================================================================================
// Define Class instance methods
// =================================================================================
Popup.prototype.startAnimation = function(open) {
	this[open ? "onactivate" : "ondeactivate"]();
	this.open = open;
	
	var self = this;
	this.startTime = (new Date()).getTime();
	this.aniTimer = window.setInterval(function(){self.animation();},Popup.minCPUResolution);
}


Popup.prototype.endAnimation = function() {
	this.aniTimer = window.clearTimeout(this.aniTimer);
	if (this.open) {
		//
	} else {
		this.popupNode.style.visibility = "hidden";
	}
	
	if ((this.open && !this.over) || (!this.open && this.over)) {
		this.startAnimation(this.over);
	}
	
}


Popup.prototype.animation = function() {
	var elapsed = (new Date()).getTime() - this.startTime;
	if (elapsed > Popup.animationLength) this.endAnimation();
	/*
	else {
		// Open or close the popup by animation
		var percentElapsed = elapsed/Popup.animationLength; // percentElapsed is a decimal value
		var animationHeight = Math.round(this.height * percentElapsed);
		percentElapsed = Math.round(100 * percentElapsed);
		percentElapsed = percentElapsed > 88 ? percentElapsed = 88 : percentElapsed = percentElapsed;
		
		if (this.open) { 
			// open it
		} else {
			// close it
		}
		
	}
	*/
}


// Events
Popup.prototype.onactivate = function() {
	this.popupNode.style.visibility = "visible";
	Popup.callExternalInterface(this.id,"open");
}


Popup.prototype.ondeactivate	= function() {
	Popup.callExternalInterface(this.id,"close");
}
// =================================================================================
// End define Class instance methods
// =================================================================================
