/* 
*  Author:  David Gailey
*  File name: DivPopUp.js
*
*  provides class and methods to assist in creation of pop-up div that can 
*  replace js alerts and/or provide additional information to the user.
*  Can also be used to create any number of embedded divs each with their own content and classes
*  usage: 	var ud = new ultimateDiv();
*  			ud.createDiv(parentElm|required|,'myNewDivId'|optional|);
*			ud.addContent('content'|required|, nodeNumber|required|);
*  			
*  REQUIRES className.js   
*
*
*
*/


var masterDivIdArray = [];

/*********************
	ultimateDiv class
*********************/
function ultimateDiv(uDivCloseButton,uDivClassNames,uDivParents)
{
	//set up CSS classNames in multidimensional Array so we can have multiple classes on each node if needed, 
	// you can make your own like in the following example:
	// var ud = new ultimateDiv(); uDivClassNames = [['myownclass','bold'],['validation_border'],['text10']];
	if(uDivClassNames == undefined)
	{uDivClassNames = [['ud_node0'],['ud_node1_shadow'],['ud_node2'],['ud_node3_close'],['ud_node4']]; //default Set 1 in style sheet
	}
	this.uDivClassNames = uDivClassNames;


	//set up array to tell createDiv method to append as siblings or children
	if(uDivParents == undefined)
	{uDivParents = ['parentElm',0,0,2,2]; //default parents
	}
	this.uDivParents = uDivParents;
	
	
	//set this argument to true if you want the default close button
	if(uDivParents == undefined)
	{uDivCloseButton = false;
	}
	this.uDivCloseButton = uDivCloseButton;
	
	
	//set up an array to hold id's of any created divs
	var uDivIdArray = new Array();
	this.uDivIdArray = uDivIdArray;
	
	
	
	/*********************
		createDiv method
	*********************/
	this.createDiv = createDiv;
	function createDiv(parentElm,handle)
	{
		//check uDivIdArray to see if there is already a created div out there
		//we don't want to create more than one so we can limit one per object and have ids be more manageable
		if(this.uDivIdArray.length > 0)
		{
			console.info('uDivIdArray.length: ',uDivIdArray.length,' ...div not created');
			return false;
		}
		
		
		var nodeCount,i,n,nodeRef;
		nodeRef = new Array();  //nodeRef hold a direct reference to each node
	
		//create all defined nodes as per length of uDivClassNames Array, add ids, add classes, append to parent
		for(nodeCount=0;nodeCount<uDivClassNames.length;nodeCount++)
		{
			nodeRef[nodeCount] = document.createElement('div');
		
			//set node0's id to the handle if one is declared, otherwise create a random id
			if(nodeCount == 0)nodeRef[nodeCount].id = (!handle)? 'node0_' + Math.floor(Math.random()*10000).toString() : handle;
			else nodeRef[nodeCount].id = 'node' + nodeCount + '_' + Math.floor(Math.random()*10000).toString();
			
			//make sure the div id isn't already used, check master Array
			var redoIdFlag = false;
			do
			{
				for(i=0;i<masterDivIdArray.length;i++)
				{
					for(n=0;n<masterDivIdArray[i].length;n++)
					{
						if(nodeRef[nodeCount].id == masterDivIdArray[i][n])redoIdFlag = true;
					}
				}
				if(redoIdFlag) nodeRef[nodeCount].id = 'node' + nodeCount + '_' + Math.floor(Math.random()*10000).toString();
				else redoIdFlag = false;
			}
			while(redoIdFlag)
			
			//add id to uDivIdArray, holds ids for just this one ultimate Div object
			uDivIdArray.push(nodeRef[nodeCount].id);
			
			//attach node to parent, parentElm is required so node0 has a parent to append to.  
			//Other nodes get appended to node defined in uDivParents array.
			if(nodeCount == 0)parentElm.appendChild(nodeRef[nodeCount]); //append node 0 to parentElm
			else nodeRef[uDivParents[nodeCount]].appendChild(nodeRef[nodeCount]); //append to parent node defined in uDivParents array
			
			//add classNames to node
			for(i=0;i<uDivClassNames[nodeCount].length;i++)
			{
				//addClassName(elementId, className)
				addClassName(nodeRef[nodeCount].id,uDivClassNames[nodeCount][i]);
				//console.info('id: ',nodeRef[nodeCount].id);
				//console.info('class: ',uDivClassNames[nodeCount][i]);
			}
			
			
			//temp dummy content
			//nodeRef[nodeCount].innerHTML = nodeCount;
			
			
		}//end for loop
		
		//add close button
		if(uDivCloseButton)
		{
			var content3 = '<a href="#" id="close_button">Close Window <img width="15" height="15" border="0" align="absmiddle" alt="Close Window" src="/wcsstore/B2BDirectStorefrontAssetStore/images/prodPg_closeWindow.gif"/></a>';
			nodeRef[3].innerHTML = content3;
			document.getElementById('close_button').onclick = function(){destroyDiv();showHideElements('select','visible',document.body,true)};
		}
		
		//push all ids into masterDivIdArray, this will keep track of all the node ids of all ultimate div objects on any one page
		//the first created ultimate div's node ids would be stored in masterDivIdArray[0] and it's outer node id would be in masterDivIdArray[0][0]
		masterDivIdArray.push(uDivIdArray);
		
		return true;
		
	}
	
	
	/*********************
		addContent method
	*********************/
	this.addContent = addContent;
	function addContent(content,nodeNumber)
	{
		document.getElementById(uDivIdArray[nodeNumber]).innerHTML = content;
	}

	/*********************
		destroyDiv method
	*********************/
	this.destroyDiv = destroyDiv;
	function destroyDiv()
	{
		var outerDiv = document.getElementById(uDivIdArray[0]);
		outerDiv.parentNode.removeChild(outerDiv);
		uDivIdArray.splice(0,uDivIdArray.length);
	}
	
	/*********************
		eraseContent method
	*********************/
	this.eraseContent = eraseContent;
	function eraseContent(nodeNumber)
	{
		document.getElementById(uDivIdArray[nodeNumber]).innerHTML = '';
	}
	
	/*********************
		showHideElements method 
	*********************/
	this.showHideElements = showHideElements;
	function showHideElements(tagName,visibleHidden,withinRef,IE6only)
	{
		if(IE6only)
		{
			if(document.body.className.match('ie6')) // className is added to body in includes/CachedHeaderDisplay.jsp
			{
				var i;
				var x = withinRef.getElementsByTagName(tagName);
				for(i=0;i<x.length;i++)
				{
					x[i].style.visibility = visibleHidden;
				}
			}
		}
		else
		{
			var i;
			var x = withinRef.getElementsByTagName(tagName);
			for(i=0;i<x.length;i++)
			{
				x[i].style.display = blockNone;
			}
		}
	}
	
}




