/*********************************************************
 * WHO:			Michael Philippone
 * 
 * WHAT:		collection of functions and algorithms 
 * 				for helping the OpenPopup function
 * 
 * WHEN:		04 Aug 2009
 * 
 * VERSION:		1.0
 * 
 * COPYRIGHT:	2009 Corporate Zen
 * 
 * HOW:		
 * 		PROPERTIES / GLOBALS:
 * 			--> window.UIX
 * 			--> window.titleBarText
 * 			--> window.popUpCloseAction 
 * 			--> window.popUpCloseRedirect
 * 
 * 		FUNCTIONS:
 * 			--> getScrollXY
 * 			-->	getElementsByClassName
 * 			--> makeRedirect
 * 			--> addElement
 * 			--> deleteElement
 * 			--> getUniqueID
 * 			--> fillInPopUpComponents
 *  
 * 
 * CHANGELOG:	
 * 	-> 04 AUG 2009: (by michael@corporatezen.com)
 * 		-> created / imported from OLD version(s)
 * 	 
 *********************************************************/

//--------------------------------------------------------------------------------
/*
* Michael Philippone CorporateZen
* michael@corporatezen.com
* The following function returns a 2-element array of 
* the horizontally and vertically scrolled values 
* for the page.
* [xX] property --> the scrolled over value on the X axis
* [yY] property --> the scrolled over value on the Y axis
* 
* The following function returns an object with the X and Y values of the
* horizontally and vertically scrolled values 
* for the page.
* It is accessible via: getScrollXY().x or getScrollXY().y  
*/
function getScrollXY() 
{
  var scrOfX = 0;
  var scrOfY = 0;
  var retObj = new Object(); // the return type
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  retObj.x = scrOfX;
  retObj.X = scrOfX;
  retObj.y = scrOfY;
  retObj.Y = scrOfY;
  
  return retObj;
}
//--------------------------------------------------------------------------------
/*
 * Get all the elements with a given class name
 */
function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}
//--------------------------------------------------------------------------------
/*
 * Get all the elements with a given class name
 */
function makeRedirect(redir){
	var retFunction = function(){
		var closure = 
			function(){ 
				window.location.href = redir };
				setTimeout( closure , 1100);
				ShowPage();
	};
	return retFunction;
}
//--------------------------------------------------------------------------------
/* Add the element 
 * PARAMS:
 * parentID - the id of the parent to which to add the element
 * type - what type of HTML element should this be?
 * txt - what the innerHTML of the element be?
 * properties - an object of key-value pairs for properties and their values
 */
function addElement(parentID,type,txt,properties) 
{
	var n = document.createElement(type);
	for(var P in properties)
		n.setAttribute(P,properties[P]);
	if(txt) n.innerHTML = txt;	
	if(parentID) document.getElementById(parentID).appendChild( n );
	else document.getElementsByTagName("body")[0].appendChild( n );	
	return n;
}
//--------------------------------------------------------------------------------
/* the user has chosen to remove a query clause */
function deleteElement( parentID , item) { $(parentID).removeChild( item ); }
//--------------------------------------------------------------------------------
function getUniqueID() {
	window.UIX = ((!window.UIX) ? (0) : (window.UIX)); // if it doesn't exist, create it!
	var IX = parseInt( window.UIX );
	window.UIX = IX+1;
	return IX;
}
//--------------------------------------------------------------------------------
/*
 * Fills in the header, close button, etc for the popup
 * Like this:
 * 	<div class="popUpTitleBar">
 * 		<span class="titleBarCloseBtn">  <a href="javascript:;" onclick="javascript:ShowPage();">[X]</a>  </span>
 * 	</div>
 * 	<div class="jsPopUpContent">
 * 		<h3>Sample PopUp</h3>
 * 	</div>
 */
function fillInPopUpComponents(Pid , txt ) {
	
	var UIX = getUniqueID();
		
	var titleProps = new Object();
	titleProps.id = "jsPopUpTitleBar"+UIX;
	titleProps["className"] = "jsPopUpTitleBar";
	titleProps["class"] = "jsPopUpTitleBar";
	var title = addElement(Pid , 'div' , null , titleProps);
	
	if(window.titleBarText) { 
		var titleTxtProps = new Object();
		titleTxtProps.id = "jsPopUpTitleBarText"+UIX;
		titleTxtProps["className"] = "jsPopUpTitleBarText";
		titleTxtProps["class"] = "jsPopUpTitleBarText";
		var titleTxt = addElement(titleProps.id , 'span' , window.titleBarText , titleTxtProps);
	}

	var closerProps = new Object();
	closerProps["id"] = "titleBarCloseBtn"+UIX;
	closerProps["className"] = "titleBarCloseBtn";
	closerProps["class"] = "titleBarCloseBtn";
	var closer = addElement( "jsPopUpTitleBar"+UIX , 'span' , null , closerProps);  



	var aProps = new Object();
	aProps["href"] = "javascript:;";
	aProps["id"] = "titleBarCloseLink"+UIX;
	aProps['onclick'] = ShowPage;
	var link = addElement( "titleBarCloseBtn"+UIX , 'a' , '[X]' , aProps );
	
	
	// if the user has specified a redirect location:
	//	set up the redirect to occurr after the popup has faded (uses a setTimeout)
	if (window.popUpCloseRedirect)
		link.onclick = makeRedirect(window.popUpCloseRedirect);
		
	// There was an action specified to perform before / during the popup hiding
	else 
	{
		if (window.popUpCloseAction) {
			link.onclick = 
				function(){
					eval(window.popUpCloseAction);
					ShowPage();
				};
		}
		// Just show the page (no special details)
		else 
 			if (ShowPage) link.onclick = function(){ ShowPage(); };
			else alert('error');
	}
	
	// After all the titlebar components have been added:
   var titleBarClearBreak = addElement(Pid , 'br' , '' , {});
   titleBarClearBreak.setAttribute('class' , 'clear');
   titleBarClearBreak.setAttribute('className' , 'clear');
	
	var contentProps = new Object();
	contentProps["id"] = 'jsPopUpContent'+UIX;
	contentProps["class"] = 'jsPopUpContent';
	contentProps["className"] = 'jsPopUpContent'; 
	var content = addElement(Pid , 'div' , txt , contentProps);
}
//--------------------------------------------------------------------------------
