//menuMaker.js
//Global vars:
var oXmlDom = new XmlDom();
var XMLProcessed = false;

//1. General functions:
oXmlDom.onreadystatechange = function () {
	if (oXmlDom.readyState == 4) {	//xml loaded
		if (oXmlDom.parseError != 0) {//load error handling
			var oError = oXmlDom.parseError;
			alert("Error in file "+fname+":\nError Code: "
				+ oError.errorCode + "\n"
				+ "Line: " + oError.line + "\n"
				+ "Line Pos: " + oError.linepos + "\n"
				+ "Reason: " + oError.reason);
		}
		else {	//xml loaded w/o error:
			XMLProcessed = processXML();	//Create arrays from loaded XML
		}
	}//end if readystate==4
};//end function oXmlDom.onreadystatechange

function getObj(id, doc) {	//doc can be an XML document object
 if (isNull(doc)) doc = document;
 with (doc) {
	if (!isNull(getElementById(id)))		return getElementById(id);
	if (!isNull(getElementsByName(id)))	return getElementsByName(id);//id is a NAME in fact
	if (!isNull(getElementsByTagName(id))) return getElementsByTagName(id)//id is a TAGNAME
	else return false
 }
}

function parseString(n,str,delim){//n>0; str:param1;param2;...;paramN
// Returns nth param of the string. Last param may or may not end with delimiter (;)
if(isNull(delim))	delim=';'	//default delimiter
if(n<1){alert('function parseString called with wrong parameter number');return ''};//never should happen
if(isNull(str))str=getHash();//hash string w/o the '#'
str=unescape(str);//i.e. '&amp;' converts to '&'
if(str.charAt(str.length-1)!=delim)str+=delim;
var i=n;
while(i>0){
retVal=str.substring(0,str.indexOf(delim));	//cut subsequent params
str=str.substring(retVal.length+1,str.length);//cut prev param
i--;
}
return retVal;
}//end function parseString *************************

function getHash(){
	var str=document.location.hash;
	return str.substring(1,str.length);//cut the '#'
}//end function getHash() ***************************

function isEmptyNode(arg) {
	reg = /\n/g;
	arg = arg.replace(reg, "");
	reg = / /g;
	arg = arg.replace(reg, "");
	if (arg.length == 0) return true;
	return false;
}//end function isEmptyNode(arg)

//2. Picture manipulation functions:

function picFileName(picTitle) {
	var picFileName=picTitle.replace(/\s/g,"").replace(/,/g,""); //removes all spaces and commas;
	if (picFileName.indexOf('.')<0) picFileName += '.jpg';
	return picFileName
}	//end function picFileName

function submenuOnmouseover(obj) {
	obj.style.backgroundColor='blue';
}

function submenuOnmouseout(obj) {
	obj.style.backgroundColor='#6699ff';
}
	
function showSubmenu(numSubmenu){
//Set position and visibility of submenu:
	var oMenu0	= getObj("L0");
	var oMenu	= getObj("menu");
	var oSub	= getObj("submenu");
	with (oSub.style){//set submenu position relative to including element (to main menu)
		sTop	= oMenu.style.top.toString();			//Top of main menu
		sTop	= sTop.substring(0,sTop.length-2);	//Cut 'px' from the end
		num	= Number(sTop)-27 + 25*numSubmenu;
		top	= num.toString()+'px';
		sLeft	= oMenu.style.width.toString();		//Left of sub = Width of main menu - 1px
		sLeft	= sLeft.substring(0,sLeft.length-2);//Cut 'px' from the end
		num	= Number(sLeft)- 1;
		left	= num.toString()+'px';

		visibility = 'visible';
	}//end with
//Regenerate items of submenu:
	var j=1;
	var i=numSubmenu;
	var fn=arrMenus[i][j];
	with(getObj("submenu")) {
		innerHTML='';
		while (arrMenus[i][j]>"") {
			innerHTML+='<a href="#'+i+';'+j+'" class="navsub" onmouseover="submenuOnmouseover(this)" onmouseout="submenuOnmouseout(this)" onclick="setImage('+i+','+j+')"> &nbsp; '+fn+'</a>';
			j++;
			fn=arrMenus[i][j];
		}//end while
	}//end with
}//end function showSubmenu(numSubmenu)

function showObj(ID){
	if (ID==2) ID='submenu';
//	getObj(ID).style.display='block';
	getObj(ID).style.visibility='visible';
}//end function showObj(ID)

function hideObj(ID){
	if (ID==2) ID='submenu';
//	getObj(ID).style.display='none';
	getObj(ID).style.visibility='hidden';
}//end function hideObj(ID)

function changeBgImage(obj, imgSrc){
	if (isNull(imgSrc)) imgSrc=folderPix[0].src;	//open folder
	if (imgSrc==0) imgSrc=folderPix[1].src;
	obj.style.backgroundImage="url("+imgSrc+")";	
}//end function changeBgImage

function processXML () {//Called before page load to create arrays from loaded XML
	root = oXmlDom.documentElement;
	createArrayFromXML(root);
	return true
}//end function processXML

function showObjects(albumNo, picNo) {//called at onload only
if (XMLProcessed) {
	showMenus();
	if (picNo==0) thumbNails(albumNo)
	else setImage(albumNo, picNo);
	return true
}
else {//Wait while processXML runs successfully and try again
	setTimeout(showObjects(albumNo, picNo) ,100);
}
return false
}//end function showObjects

function menuOnmouseover(obj) {
	getObj("menus").style.visibility='visible'
	obj.style.backgroundColor='blue';
	changeBgImage(obj);
}

function menuOnmouseout(obj) {
	hideObj(2);
	obj.style.backgroundColor='#6699ff';
	changeBgImage(obj,0);
}

function showMenus(){//!!!arrMenus may have not been defined yet when this is called!!!
	var oL0=getObj("L0");
	oL0.onmouseover=function() {showObj('menu');/*changeBgImage(oL0,'images/uparrow.gif')*/};
	oL0.onmouseout=function() {hideObj('menu'); changeBgImage(oL0,'images/downarrow.gif')};
	oL0.innerHTML = " &nbsp; &nbsp; "+arrMenus[0][0];
//Set main menu position and visibility:
 	with (getObj("menu")) {
 	 visibility = 'visible';
//submenu
	 innerHTML='';
	 for (var i=1; i<arrMenus.length; i++) {
		var fn=arrMenus[i][0];

		innerHTML+='<a href="#'+i+';0" class="navmain" onclick="thumbNails('+i+')" onmouseover="menuOnmouseover(this);showSubmenu('+i+')" onmouseout="menuOnmouseout(this)">'+fn+'<img src="images/rightarrow.gif" class="icon" alt=">" /><\/a>';
	 }//end for
	}//end with
}//end function showMenus()

function wipeTable(oTable)	{
 if (oTable.rows.length>1){// Called from thumbnail view
	while (oTable.rows.length>1)	oTable.deleteRow(oTable.rows.length-1);// Wipe all but 0th row
	var oRow = oTable.rows[0];
	while (oRow.cells.length>1)	oRow.deleteCell(oRow.cells.length-1);	// Wipe all but 0th cell
 }
}//end function wipeTable

function createArrayFromXML(node) {// Fills arrMenus[][] at node level
	var tagName = node.childNodes[1].tagName;			//tagName of a record: <e>
	var x = oXmlDom.getElementsByTagName(tagName);	//array of records with tagName

	for (var j=0;j<x.length;j++) {	//Loop through records.	noOfRecs=x.length;
	//menus:
		arrMenus[j] = new Array(); 
		if ((x[j].childNodes[0].nodeType != 1) ) continue;//a has child
		var nodes=x[j].childNodes[0];
		k=0;
		k = createSub(nodes,j,k);
	}//end for j
}//end function createArrayFromXML

function createSub(node,j,k) { 
    var numtags = 0;
    if (node.nodeType == 1) numtags++;			// node.ELEMENT_NODE
    var children = node.childNodes;				// Get all children nodes of node

    for(var i=0; i < children.length; i++) {	// Loop through the children
      k = createSub(children[i],j,k);
      val= children[i].nodeValue;     // Recurse on each one
		if (!isNull(val) && !isEmptyNode(val)) {
			arrMenus[j][k] = val; 
			k++   
		}
    }//end for
    return k;                             // Return the total number of tags
}	//end function createSub

//Preload:
var folderPix =  new Array();
folderPix[0] = new Image()
folderPix[0].src = "images/folder_open.gif"
folderPix[1] = new Image()
folderPix[1].src = "images/folder_closed.gif"

