var maxZIndex;
if(typeof projects == "undefined")
	var projects = new Array();

addEvent(window, "load", function() {loadProjectData(projects)});
			
function showTab(id) {
	var curr = document.getElementById(id);
	
	var width = curr.getAttribute("hideWidth");
	var currWidth;
	var mainEl = curr.parentNode;
	/*
		**** Note: can't use width in stylesheet becuase padding/border add to the total width the parent
					needs to be, causing the tab image to wrap.
	if(curr.style.width == "") {
		if (document.defaultView && document.defaultView.getComputedStyle) {
			curr.style.width = document.defaultView.getComputedStyle(curr, '').getPropertyValue('width');
		} else if(document.all && document.getElementById && curr.currentStyle) {
			curr.style.width = curr.currentStyle.width;
		}
	}
	width = parseInt(curr.style.width);
	*/
	if(width) {
		currWidth = mainEl.clientWidth;
	}
	
	if(!curr.style.display || curr.style.display == "none") {
		if(width)
			mainEl.style.width = (parseInt(currWidth) + parseInt(width)) + "px";
		curr.style.display = "block";
		if(mainEl.style.zIndex == "") {
			if (document.defaultView && document.defaultView.getComputedStyle) {
				mainEl.style.zIndex = document.defaultView.getComputedStyle(mainEl, '').getPropertyValue('z-index');
			} else if(document.all && document.getElementById && mainEl.currentStyle) {
				mainEl.style.zIndex = mainEl.currentStyle.zIndex;
			}
		}
		if(typeof maxZIndex == "undefined")
			maxZIndex = parseInt(mainEl.style.zIndex);
		maxZIndex++;
		mainEl.style.zIndex = maxZIndex;
	} else {
		if(width)
			mainEl.style.width = (parseInt(currWidth) - parseInt(width)) + "px";
		curr.style.display = "none";
		mainEl.style.zIndex = 10;
	}
}

function imgOver(imgEl) {
	if(!imgEl.overImg) {
		currSrc = imgEl.src;
		newSrc = currSrc.replace(/(.*)\d{1}(\..*$)/,"$12$2");
		imgEl.outImg = new Image();
		imgEl.outImg.src = currSrc;
		imgEl.overImg = new Image();
		imgEl.overImg.src = newSrc;
	}
	
	imgEl.src = imgEl.overImg.src;;
}


function imgOut(imgEl) {
	if(!imgEl.overImg) {
		currSrc = imgEl.src;
		newSrc = currSrc.replace(/(.*)\d{1}(\..*$)/,"$11$2");
		imgEl.outImg = new Image();
		imgEl.outImg.src = newSrc;
		imgEl.overImg = new Image();
		imgEl.overImg.src = currSrc;
	}
	imgEl.src = imgEl.outImg.src;
}

function showNews(xml, parent) {
	var news = xml.getElementsByTagName("news");
	if(!news.length)
		return;
	if(typeof division != "undefined") {
		var outStr = "<H1>Latest " + division + " News</H1>";
	} else {
		var outStr = "<H1>Latest News</H1>";
	}
	
	for(var i=0; i < news.length; i++) {
		var curr = news[i];
		var path = curr.getAttribute("path");
		var title = "";
		var desc = "";
		var graphic = "";
		for(var j=0; j < curr.childNodes.length; j++) {
			var child = curr.childNodes[j];
			if(child.hasChildNodes()) {
				switch(child.tagName.toLowerCase()) {
					case "title":
						title = xmlUnescape(child.firstChild.data);
						break;
					case "description":
						desc = xmlUnescape(child.firstChild.data);
						break;
					case "graphic":
						graphic = child.firstChild.data;
						break;
				}
			}
		}
		outStr += '<div class="newsitem">';
		if(graphic.length)
			outStr += '<img class="eventimg" src="' + graphic + '" align="left" />';
		outStr += '<div class="newstitle"><a href="' + path + '">' + title + '</a></div>';
		outStr += desc + '<a class="newsmore" href="' + path + '">Read more...</a>';
		outStr += '<div style="height: 1px; clear: both;"></div></div>'
	}
	parent.innerHTML = outStr;	
}
function addEvent(el, evname, func) {
	if (typeof el.addEventListener == "function") {
		el.addEventListener(evname, func, true);
	} else {
		el.attachEvent("on" + evname, func);
	}
}

function doreq(url, callbackFunction, isAsync) {
	if(typeof arguments[2] == "undefined")
		isAsync = true;
		
	function xmlReq(url) {
		var req;
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");  
		}
		return req;
	}
	var req = new xmlReq;
	var fn = callbackFunction;
	if(isAsync) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {
					//alert(req.responseText);
					fn(req.responseXML, req.responseText);
					
				}
			}
		}
	}
	
	//document.getElementById("temp").innerHTML += url + "<BR>";
	req.open("GET",url, isAsync);  // false means synchronous, so no resulthandler function
	req.send("");
	if(!isAsync)
		fn(req.responseXML, req.responseText);
}
function xmlEscape(str) {

	var fnReplace = function(str) {
		switch(str) {
			case "<": return "&lt;"
			case ">": return "&gt;"
			case "&": return "&amp;"
		}
	}
	return s.replace(/\<|\>|\&/g, fnReplace) ;
}

function xmlUnescape(str) {
	str = str.replace(/\&amp\;/g, "&").replace(/\&lt\;/g, "<").replace(/\&gt\;/g,">").replace(/\&quot\;/g, '"').replace(/\&apos\;/g, "'");
	return str;
}

function loadProjectData(projectArr) {
	if(projectArr.length) {
		var randIndex = Math.round(Math.random()*(projectArr.length-1));
		var url = projectArr[randIndex].replace(/(.*)\..{3,4}$/, "$1projectdata.xml");
		doreq(url, function(xml, text) {
			var data = xml.getElementsByTagName("project")[0];
			var path = data.getAttribute("path");
			var par = document.getElementById("side1inks3");
			var title = "";
			if(data.getElementsByTagName("title").length)
				title = data.getElementsByTagName("title")[0].firstChild.nodeValue;
			var str = "<h3>" + xmlUnescape(title) + "</h3>"
			var graphic = "";
			if(data.getElementsByTagName("graphic").length)
				graphic = data.getElementsByTagName("graphic")[0].firstChild.nodeValue;
			if(graphic.length)
				str += '<img src="' + xmlUnescape(data.getElementsByTagName("graphic")[0].firstChild.nodeValue) + '" />';
			var summary = "";
			if(data.getElementsByTagName("summary").length)
				summary = data.getElementsByTagName("summary")[0].firstChild.nodeValue;
			str += "<p>" + xmlUnescape(summary) + "</p>"
			str += '<a href="' + path + '" class="moreLink">more...</a>';
			par.innerHTML = str;
		} );
	}
}
