// XML to Box rendering system library. Part of the Jump Craaaazy management system
// -Rob, Feb 20, 2007


//Define required functions if not already established in other libraries.
if (typeof(findNamedNode) != "function") { 
	
	function findNamedNode(whichElement, name) {
		
		var nodeList = new Array();
		var test = null;
		for (var j=0;j < whichElement.childNodes.length; j++) {
			if (whichElement.childNodes.item(j).nodeType == 1) {
				nodeList.push(whichElement.childNodes.item(j));
			}
		}
		
		//Remeber to check the temp node incase the element is minimized/moving/etc
		if (whichElement.tempNode != null) {
			nodeList.push(whichElement.tempNode);
		}
		
		//alert (whichElement.getAttribute("name") + "  " + name);
		if (whichElement.getAttribute("name") != name) {
			for (var j=0;j < nodeList.length; j++) {
				if (nodeList[j].getAttribute("name") == name) {
					test = nodeList[j];
					break;
					
				} else if (nodeList[j].childNodes.length > 0) {
					//interrogate(nodeList[j].childNodes);
					test = findNamedNode(nodeList[j], name);
					
					if (test != null) { //Found the node in the last search
						break;
					}
				}
				
			}
		} else {
			return whichElement;
			
		}
		
		if (test != null) {
			return test;
		}
		
	}
}
//done common functions



//function to get PX value from string
function pxToInt(stringVal) {
	return (parseInt(stringVal));
	//return (parseInt(stringVal.substring(0, stringVal.indexOf("px"))));
}

//Function to set opacity of an element
function setOpacity(ele, opacity) {

	if (ele.style.opacity != null) {
		ele.style.opacity = (opacity / 100);
	} else if (ele.style.KhtmlOpacity != null) {
		ele.style.KhtmlOpacity = (opacity / 100);
	} else if (ele.style.MozOpacity != null) {
		ele.style.MozOpacity = (opacity / 100);
	} else if (ele.style.filter != null) {
		ele.style.filter = "alpha(opacity=" + opacity + ")";
	}
}

//function to get the opacity of an element
function getOpacity(ele) {
	var theOp = 100;
	if (ele.style.opacity != null) {
		if (ele.style.opacity != "") {
			theOp = ele.style.opacity * 100;
		}
	} else if (ele.style.KhtmlOpacity != null) {
		if (ele.style.KhtmlOpacity != "") {
			theOp = ele.style.KhtmlOpacity * 100;
		}
	} else if (ele.style.MozOpacity != null) {
		if (ele.style.MozOpacity != "") {
			theOp = ele.style.MozOpacity * 100;
		}
	} else if (ele.style.filter != null) {
		if (ele.style.filter != "") {
			var alpha = "opacity=";
			var str = ele.style.filter;
			theOp = str.substring(str.indexOf(alpha) + alpha.length, str.lastIndexOf(")"));
		}
	}
	
	
	return parseFloat(theOp);
}


//Take a parameter object and create a box element out of it.
function drawBox(paramsObj) {
	//First draw the container
	var contDiv = document.createElement("div");
	contDiv.id = paramsObj.id;
	
	if (paramsObj.boxClass == null) {
		contDiv.className = "box";
	} else {
		contDiv.className = paramsObj.boxClass;
	}
	
	
	var titleClass = "headerback";
	if (paramsObj.titleClass != null) {
		titleClass = paramsObj.titleClass;
	}
	
	var contentClass = "bodyBox";
	if (paramsObj.contentClass != null) {
		contentClass = paramsObj.contentClass;
	}
	
	
	//Create the border on the top
	var topBar = document.createElement("div");
	topBar.className = titleClass;
	topBar.setAttribute("name", "headerBox");
	
	topBar.innerHTML = paramsObj.title;
	
	contDiv.appendChild(topBar);
	
	//Create the body content
	var bodyBox = document.createElement("div");
	bodyBox.setAttribute("name", "body");
	bodyBox.className = contentClass;

	if (paramsObj.overflow != false) {
		bodyBox.style.overflow = paramsObj.overflow;
		bodyBox.targetOF = paramsObj.overflow;
	}

	//Apply the body
	bodyBox.innerHTML = paramsObj.body;
	contDiv.appendChild(bodyBox);
	
	//Now setup events
	if (paramsObj.loadFunc != null) {
		contDiv.loadFunc = paramsObj.loadFunc;
	}
	
	
	//Display mode options
	if (paramsObj.dispMode == null) {
		paramsObj.dispMode = "foldOut";
	}
	
	//quick error check since fade in requires with and height
	if (paramsObj.dispMode == "fadeIn") {
		if (paramsObj.height == null || paramsObj.width == null) {
			paramsObj.dispMode = "foldOut";
		}
	}
	
	
	
	switch (paramsObj.dispMode) {
		case ("foldOut"):
			//The foldOut method is used to draw the box out horizontally, and then vertically.
			//as such, we must start the box at 0 width, 0 height
			
			if (paramsObj.height) {
				bodyBox.style.height = "1px";
				bodyBox.style.overflow = "hidden";
				contDiv.targetHeight = paramsObj.height;
				
			}
			
			if (paramsObj.width != null) {
				contDiv.style.width = "0px";
				contDiv.targetWidth = paramsObj.width;
			}
				
	
			break;
			
		case ("fadeIn"):
			//The fade in method simply starts the box at 0 opacity and fades in
			if (paramsObj.height) {
				bodyBox.style.height = paramsObj.height;
			}
			
			if (paramsObj.width != null) {
				contDiv.style.width = paramsObj.width;
			}
			
			
			setOpacity(contDiv, 0);
			
			//alert (contDiv.style.filter);
			break;
	
	}

	//alert (paramsObj.parent);
	//Apply to page
	if (paramsObj.parent) {
		paramsObj.parent.appendChild(contDiv);
		
	} else {
		document.body.appendChild(contDiv);
	}
	
	//Now perform the actual rendering
	switch (paramsObj.dispMode) {
		case ("foldOut"):
			
			initBox(contDiv.id);
			break;
			
		case ("fadeIn"):
	
			fadeBox(contDiv.id);
			break;
	}

}


//function to draw out a box
function initBox(boxEle) {
	
	var boxEle = document.getElementById(boxEle);
	
	
	
	var thisWidth = boxEle.style.width;
	var bottomBox = findNamedNode(boxEle, "body");
	var thisHeight = bottomBox.style.height;
	
	var widthComplete = false;
	

	
	if (thisWidth != "") {
		thisWidth = pxToInt(thisWidth);
		
		if (thisWidth < pxToInt(boxEle.targetWidth)) {
		
			boxEle.style.width = thisWidth + 5;
			setTimeout("initBox('" + boxEle.id + "')", 30);
		} else {
			widthComplete = true;
		}
	}

	if (widthComplete) {
		//Now do height
		var heightComplete = false;
	
		if (thisHeight != "") {
			thisHeight = pxToInt(thisHeight);
			
			if (thisHeight < pxToInt(boxEle.targetHeight)) {
				 bottomBox.style.height = thisHeight + 5;
				setTimeout("initBox('" + boxEle.id + "')", 30);
			} else {
				if (bottomBox.targetOF != null) {
					bottomBox.style.overflow = bottomBox.targetOF;
				} else {
					bottomBox.style.overflow = "";
				}
			
				heightComplete = true;
			}
		}
	}
	
	//Call back for when complete
	if (widthComplete == true && heightComplete == true) {
		boxEle.loaded = true;
		if (boxEle.loadFunc != null) {
			eval(boxEle.loadFunc + "(boxEle)");
		}
	}

}

//function to "undraw" a box
//function to draw out a box
function uninitBox(boxEle) {
	
	var boxEle = document.getElementById(boxEle);
	
	
	
	var thisWidth = boxEle.style.width;
	var bottomBox = findNamedNode(boxEle, "body");
	var thisHeight = bottomBox.style.height;
	
	var widthComplete = false;
	var heightComplete = false;
	
	if (thisHeight != "") {
		thisHeight = pxToInt(thisHeight);
	} else {
		thisHeight = bottomBox.offsetHeight;
	}
	
	if (bottomBox.style.overflow != "hidden") {	
		bottomBox.style.overflow = "hidden";
	}	
			
	if (thisHeight > 1) {
		bottomBox.style.height = thisHeight - 5;
		setTimeout("uninitBox('" + boxEle.id + "')", 30);
	} else {
		heightComplete = true;
	}

	if (heightComplete) {
		if (thisWidth != "") {
			thisWidth = pxToInt(thisWidth);
		} else {
			thisWidth = boxEle.offsetWidth;
		}
		
		
		if (thisWidth > 1) {
		
			boxEle.style.width = thisWidth - 5;
			setTimeout("uninitBox('" + boxEle.id + "')", 30);
		} else {
			widthComplete = true;
		}
	}

	//Call back for when complete
	if (widthComplete == true && heightComplete == true) {
		boxEle.unloaded = true;
		
		if (boxEle.unloadFunc != null) {
			if (typeof(boxEle.unloadFunc) == "function") {
				boxEle.unloadFunc(boxEle);
			} else {
				eval(boxEle.unloadFunc + "(boxEle)");
			}
		}
	}

}


//function to fade box in
function fadeBox(eleID) {
	var boxEle = document.getElementById(eleID);
	var theOp = getOpacity(boxEle); 
	var fadeSpeed = 5;

	if (boxEle.fadeSpeed) {
		fadeSpeed = boxEle.fadeSpeed;
	}

	if (theOp < 100) {
		setOpacity(boxEle, theOp + fadeSpeed);
		setTimeout("fadeBox('" + boxEle.id + "')", 50);
	} else {
		boxEle.loaded = true;
		if (boxEle.loadFunc != null) {
			eval(boxEle.loadFunc + "(boxEle)");
		}
	}
}


//function to fade box out
function fadeBoxOut(eleID) {
	var boxEle = document.getElementById(eleID);
	var theOp = getOpacity(boxEle); 
	var fadeSpeed = 5;

	if (boxEle.fadeSpeed) {
		fadeSpeed = boxEle.fadeSpeed;
	}



	if (theOp > 0) {
		setOpacity(boxEle, theOp - fadeSpeed);
		setTimeout("fadeBoxOut('" + boxEle.id + "')", 50);
	} else {
		boxEle.unloaded = true;
		if (boxEle.unloadFunc != null) {
			if (typeof(boxEle.unloadFunc) == "function") {
				boxEle.unloadFunc(boxEle);
			} else {
				eval(boxEle.unloadFunc + "(boxEle)");
			}
		}
	}

}




//Get the node value of a node if it exists
function getTagValue(doc, tag) {
	var res = false;
	if (doc.getElementsByTagName(tag).item(0)) {
		if (doc.getElementsByTagName(tag).item(0).firstChild.nodeValue) {
			res=doc.getElementsByTagName(tag).item(0).firstChild.nodeValue;
		}
	}
	return res;
}

//Callback to read an XML document and convert it to a box property object.
function handleXMLBox(respText, respXML) {
	//alert (respXML.getElementsByTagName("title").item(0).innerHTML);
	var boxObj = new Object();
	boxObj.id = getTagValue(respXML, "ID");
	boxObj.title = getTagValue(respXML, "title");
	boxObj.width = getTagValue(respXML, "width");
	boxObj.height = getTagValue(respXML, "height");
	boxObj.overflow = getTagValue(respXML, "overflow");
	if (getTagValue(respXML, "titleClass")) {
		boxObj.titleClass = getTagValue(respXML, "titleClass");
	}
	if (getTagValue(respXML, "contentClass")) {
		boxObj.contentClass = getTagValue(respXML, "contentClass");
	}
	if (getTagValue(respXML, "boxClass")) {
		boxObj.boxClass = getTagValue(respXML, "boxClass");
	}
	if (getTagValue(respXML, "top")) {
		boxObj.top = getTagValue(respXML, "top");
	}
	
	if (getTagValue(respXML, "left")) {
		boxObj.contentClass = getTagValue(respXML, "left");
	}
	
	if (getTagValue(respXML, "loadFunc")) {
		boxObj.loadFunc = getTagValue(respXML, "loadFunc");
	}
	
	if (getTagValue(respXML, "dispMode")) {
		boxObj.dispMode = getTagValue(respXML, "dispMode");
	}
	
	var parentID = getTagValue(respXML, "parentID");
	boxObj.parent = document.getElementById(parentID);
	
	
	boxObj.body = getTagValue(respXML, "body");
	
		
	drawBox(boxObj);
}

