/*********************************************************
 * WHO:			Michael Philippone
 * 
 * WHAT:		collection of functions and algorithms 
 * 				for creating a content panel that 
 * 				floats above the page body element
 * 
 * WHEN:		04 Aug 2009
 * 
 * VERSION:		1.0
 * 
 * COPYRIGHT:	2009 Corporate Zen
 * 
 * HOW:		
 * 		PROPERTIES / GLOBALS:
 * 			--> Prototype (included via lib/prototype.js file
 * 			--> Scriptaculous (included via lib/scriptaculous/*.js files)
 * 			--> window.titleBarText
 * 			--> window.popUpCloseRedirect
 * 			--> window.popUpCloseAction
 * 			--> window.popUpID
 * 
 * 		FUNCTIONS:
 * 			--> testIncludes
 * 			-->	OpenPopup
 * 			--> HidePopup
 * 			--> ShowOverlay
 * 			--> HideOverlay
 * 			--> ShowPage
 *  
 * 
 * CHANGELOG:	
 * 	-> 04 AUG 2009: (by michael@corporatezen.com)
 * 		-> created / imported from OLD version(s)
 * 	 
 *********************************************************/

// -------------------------------------------------------------------------------
/*
 * tests for functions that are declared in the jsPopUp_utils.js file
 * and looks for the Prototype object
 * 
 * alerts the user if the dump function is called without them
 */
function testIncludes() {
	var items = "Prototype,Scriptaculous,getScrollXY,getElementsByClassName,makeRedirect,addElement,deleteElement,getUniqueID,fillInPopUpComponents";
	
	items = items.split(",");
		
	for (var i = 0; i < items.length; i++) 
	{
		if ( !Boolean( window[ items[i] ] ) ) 
		{
			alert(
				"Please be sure you have included the files, \n" + 
				"the 'prototype' javascript framework, 'Scriptaculous' DHTML toolkit and 'jsPopUp_utils.js' BEFORE \n" + 
				"the 'jsPopUp.js' file in your page. \n " + 
				"Please be sure to include the files in the order specified above\n\n" +
				
				"There are required functions in that file that appear to be missing.");
			return false;
		}
	}
	return true;	
}

//--------------------------------------------------------------------------------
/*
 * Pop up a div in the foreground of the web page
 * 
 *  PARAMETER --> Object with named elements for all the properties of the popup:
 *   Properties
 *   	REQUIRED:
 *   
 *   	OPTIONAL:
 *  	- height (int)
 *  	- width (int)
 *  	- overlay (true/false)
 *  	- id (string: id of some div to populate and pop-up)
 *  	- draggable (true/false)
 *  	- distFromTop (int: how far from window-top should pop up appear?)
 *  	- title (string)	
 *  	- redirectOnClose (string: URL)
 *  	- altCloseAction (string: code block to exec in an EVAL statement)
 *  	- className (string: css class)
 *  	- contentLoc (string: URL for ajax content population -- BROKEN)
 *  	- content (string: pop up's content)
 */
function OpenPopup( propsObj ) 
{	

	if( !testIncludes() ) return;
	
	try {
		
		// if a distance to drop the popup from the top of the window was specified, use it, else use '100'
		propsObj.distFromTop = (Boolean(propsObj.distFromTop))?(propsObj.distFromTop): 100 ;
		
		// if a height and width were specified, use them, ELSE, use 500 for either
		( !Boolean(propsObj.height) )?(propsObj.height):(500);
		( !Boolean(propsObj.width) )?(propsObj.width):(500);
		
		// if there was a title specified, use it, otherwise, use 'Information'
		if(Boolean(propsObj.title)) window.titleBarText =  propsObj.title ;
		//window.titleBarText = (Boolean(propsObj.title)) ? propsObj.title : 'Information' ;
		
		window.popUpCloseRedirect = ( Boolean(propsObj.redirectOnClose) )?(propsObj.redirectOnClose):(null);
		
		window.popUpCloseAction = (Boolean(propsObj.altCloseAction))?(propsObj.altCloseAction):(null); 
		
		var overlayStat = (Boolean(propsObj.overlay))?( ShowOverlay() ) : 1;
		if(overlayStat < 1) alert("There was an error creating the overlay. \n(error code: " + overlayStat + ")")
		
		// If an element ID was specified to popup, use that,
		//	 otherwise, create and element on the end 
		//	 of the page body element
		if( Boolean(propsObj.id) ) 
		{ 
			propsObj.popObj = $( propsObj.id ); 
			window.popUpID = propsObj.id;
		}
		else 
		{
			propsObj.id = "popUp_" + getUniqueID();
			window.popUpID = propsObj.id;
			propsObj.popObj = addElement( null , 'div' , null , propsObj);
		}
		
		propsObj.popObj.style.display = "none";
		
		// If a CSS class for the pop up was specified, 
		//	use that, otherwise, just use "jsPopUp" 
		propsObj.className = ( Boolean(propsObj.className) ) ? (propsObj.className) : "jsPopUp" ;
		propsObj.popObj.setAttribute("class" , propsObj.className );
		propsObj.popObj.setAttribute("className" , propsObj.className );
		
	
		/* If we are creating this in an IE instance, then the 'fixed' 
			position attribute breaks, so just use 'absolute' */
		// if(Prototype.Browser.IE) 
			propsObj.popObj.style.position = 'absolute';
		//else 
			//propsObj.popObj.style.position = 'fixed';
		
		//alert(getScrollXY()['y']);
		propsObj.popObj.style.top = (getScrollXY()['y'] + propsObj.distFromTop) + 'px';
		propsObj.popObj.style.left = (getScrollXY()['x'] + ((document.body.offsetWidth - propsObj.width) /2)) + 'px';
		propsObj.popObj.style.height = propsObj.height + 'px';
		propsObj.popObj.style.width = propsObj.width + 'px';

		propsObj.popObj.innerHTML = '';
		
		if (propsObj.contentLoc) {
			var success=function(e) {				
				fillInPopUpComponents( 
					window.popUpID , 
					(e.transport.responseText.length > 0) ? ( e.transport.responseText.replace(/^[/s]+/ , '').replace(/[/s]+$/ , '') ) : ("Nothing to report.") );
				
			};
			var myAjax = new Ajax.Request( 
							propsObj.contentLoc , 
							{
								method: 'get',
								on200:success,							
								onFailure: function(){
									fillInPopUpComponents(
										window.popUpID, 
										'<h2>Something went wrong...</h2>' );
								}
							}
						);
		}
		else {
			if (propsObj.content) fillInPopUpComponents( propsObj.id, propsObj.content );
			else fillInPopUpComponents( propsObj.id , "" );
		}		
		
		if(propsObj.draggable)
			new Draggable( propsObj.id );
		
		if($(propsObj.id).appear) 
			$(propsObj.id).appear();
		else 
			$(propsObj.id).style.display = 'block';
		
	} catch(err) {
		//alert("Pop Up Error: \n" + err);
		dump(err);		
	}	
}
//--------------------------------------------------------------------------------

/*
 * hides the specified popup
 * by default, it also tries to hide the overlay
 * PARAM 01 --> id of the div to hide
 */
function HidePopup( hideMe ) { 	
	if($(hideMe).fade)
		$(hideMe).fade();
	else $(hideMe).style.display = 'none';
}
//--------------------------------------------------------------------------------
/*
 * This function renders the transparent background overlay visible
 * NO PARAMS
 * 
 * RETURNS:
 * 	-1: error assigning overlay object/element attributes
 * 	-2: error adding overlay object/element to page body element
 * 	 1: success 
 */
function ShowOverlay()
{	
	try {
		var h = window.innerHeight;
		var overlay = document.createElement('div');
		var uid = getUniqueID();	
		overlay["id"] = "overlay_"+uid;
		overlay["class"] = "overlay";
		overlay["className"] = "overlay";
		overlay.style.width = (Prototype.Browser.Gecko)?(window.innerWidth):('100%');
		overlay.style.height = '1000px';
		overlay.style.display = 'block';
		overlay.style.position='fixed';  // OR 'absolute' 
		overlay.style.top = '0'; 
		// if the user has specified a redirect location, another popUp close action or nothing:
		if ( window.popUpCloseRedirect ) 
			overlay.onclick = function(){ window.location.href = window.popUpCloseRedirect; }; 		
		else if( window.popUpCloseAction )
			overlay.onclick = window.popUpCloseAction; 
		else 
			overlay.onclick = function() { HidePopup( window.popUpID ); HideOverlay(); };	
	}
	catch(err){
		return -1;
	}
	
	try {
		document.getElementsByTagName("body")[0].appendChild(overlay);
	}catch(err2) {
		return -2;
	}
	
	return 1;
}
//--------------------------------------------------------------------------------
/*
 * This function renders the transparent background overlay INvisible
 * if one is not present, then it fails gracefully
 * NO PARAMS
 */
function HideOverlay()
{
	var overlays = getElementsByClassName("overlay" , document);
	var overlay = null;
	for(var i=0; i<overlays.length; i++)
	{
		overlay = overlays[i]
		
		if(overlay.parentElement) 
			overlay.parentElement.removeChild(overlay); // not supported by FF (why?)
		else 
			document.getElementsByTagName("body")[0].removeChild(overlay); // for FF
	}
}
//--------------------------------------------------------------------------------
/*
 * This function hides all the pop'd  divs and the overlay (reverts to 'normal' view)
 * NO PARAMS
 */
function ShowPage()
{
	var pop_arr = getElementsByClassName('jsPopUp');
	for(var i=0; i<pop_arr.length; i++) {
		HidePopup(pop_arr[i].id);	
		}	
	HideOverlay();		
}
//--------------------------------------------------------------------------------