
var removeLastMenuNode = true;
var defaultTarget = 'main_frame';
var vMenuNodeTableId = 'menu_left';
var hMenuNodeTableId = 'menu_top';
var hMenuNodeTableBottomId = 'h_menu_table_bottom';
var pathTableId = 'path_table';

var rootMenu = null;

var nodeId = 1;
var arrayId = 1;

var activeVNodeId = -1;
var activeHNodeId = -1;

function HMenuNodeArray() {
	this.id = arrayId;
	arrayId++;
	this.elements = new Array();
	this.addNode = function(newNode) {
		var i = this.elements.length;
		this.elements[i] = newNode;
		newNode.index = i;
	}
}

function HMenuNode(pText, pHref, pTarget, width, pDbId) {
	this.text = pText;
	this.child = null;
	
	if (!pHref) {
		this.href = null;
	}
	else {
		this.href = pHref;
	}
	if (!pTarget) {
		this.target = defaultTarget;
	}
	else {
		this.target = pTarget;
	}
	if (!width) {
		this.width = null;
	}
	else {
		this.width = width;
	}
	//this.id = nodeId;
	//nodeId++;
	this.id = pDbId;
	this.index = 0;
	this.childId = 0;
	this.addChildNodeArray = function (childNodeArray) {
		this.child = childNodeArray;
		this.childId = childNodeArray.id;
	}
}


function VMenuNodeArray() {
	this.id = arrayId;
	arrayId++;
	this.elements = new Array();
	this.addNode = function(newNode) {
		var i = this.elements.length;
		this.elements[i] = newNode;
		newNode.index = i;
	}
}

function VMenuNode(pText, pHref, pTarget, width, pDbId) {
	this.text = pText;
	if (!pHref) {
		this.href = null;
	}
	else {
		this.href = pHref;
	}
	if (!pTarget) {
		this.target = defaultTarget;
	}
	else {
		this.target = pTarget;
	}
	if (!width) {
		this.width = null;
	}
	else {
		this.width = width;
	}
	this.child = null;
	this.level = 0;
	this.isopen = false;
	//this.id = nodeId;
	//nodeId++;
	this.id = pDbId;
	this.addChildNodeArray = function (childNodeArray) {
		this.child = childNodeArray;
		if ((this.text != 'root') && (childNodeArray != null)) {
			for (var i=0; i<childNodeArray.elements.length; i++) {
				childNodeArray.elements[i].level = this.level + 1;
			}
		}
	}
}

function findMenuNode(nodeArray, pId) {
	var cArray = nodeArray.elements;
	for (var i=0; i<cArray.length; i++) {
		if (cArray[i].id == pId) {
			return cArray[i];
		}
		else if (cArray[i].child != null) {
			var rObj = findMenuNode(cArray[i].child, pId);
			if ( rObj != null ) {
				return rObj;
			}
		}
	}
	return null;
}

function findHMenuNode(nodeArray, pId) {
	var cArray = nodeArray.elements;
	//alert('find: '+pId+'; array length: '+cArray.length);
	for (var i=0; i<cArray.length; i++) {
	//alert(cArray[i].id);
		if (cArray[i].id == pId) {
			return cArray[i];
		}
	}
	return null;
}

function setDefaultTarget(pTarget) {
	defaultTarget = pTarget;
}

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function ceaateHMenuTable(menuArray) {
	if (vMenuArray != null) {
		for (var i=0; i<vMenuArray.length; i++) {
			
		
		}
	} 
}
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	 {
	 objXMLHttp=new XMLHttpRequest()
	 }
	else if (window.ActiveXObject)
	 {
	 objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	 }
	return objXMLHttp
}
function getRequest() {
    try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {}
    try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {}
    try { return new XMLHttpRequest(); } catch(e) {}
    return null;
}

function sendMenuRequest(url){
	if( url != null ){
		requestUrl = url;
		xmlHttp = getRequest();
		if(xmlHttp != null){
			if(url.indexOf('?') >= 0)
				url += '&sid=' + Math.random();
			else
				url += '?sid=' + Math.random();
				
			xmlHttp.open('GET', url, true);
			xmlHttp.onreadystatechange = requestReady;
			xmlHttp.send(null);
		}
	}
}

var xmlHttp;
var requestUrl;

function requestReady(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
 	//alert(xmlHttp.responseText);
 	document.getElementById('content').innerHTML = xmlHttp.responseText;
 	
 	contentLoaded(requestUrl);
 
 } 
}

	var divName = 'pictures';
	var images1 = new Array();
	var sizes = new Array();

function contentLoaded(url){

	if(url.indexOf('pictures.php') >= 0){
	
		gallery = url.substr(url.indexOf('=') + 1);
		
		sendPictRequest('pictures_xml.php?g=' + gallery);	
		getImage(images1, divName);
	}
	//resizeAll();
}

function sendPictRequest(url){
	if( url != null ){
		requestUrl = url;
		xmlHttpReq = getRequest();
		if(xmlHttpReq != null){
			url += '&sid=' + Math.random();
	
			xmlHttpReq.open('GET', url, true);
			xmlHttpReq.onreadystatechange = pictRequestReady;
			xmlHttpReq.send(null);
		}
	}
}

var xmlHttpReq;

function pictRequestReady(){

if (xmlHttpReq.readyState==4 || xmlHttpReq.readyState=="complete")
 {
   xmlDoc = xmlHttpReq.responseXML;

    var x = xmlDoc.getElementsByTagName("image");
	images1.length = 0;
	
    for (var i=0; i<x.length; i++)
    { 
    	images1[i] = x[i].attributes["name"].value;
    	sizes[i] = x[i].attributes["height"].value;
    	loadMyImages(x[i].attributes["name"].value);
    }  
    getImage(images1, divName);
 } 
}

function showPage(nodeId, pHref) {
//	if(!setActive(nodeId)) {
//		return;
//	}
	setActive(nodeId);
	activeVNodeId = nodeId;
	//alert('showPage(' + pHref + ')');
	//sendMenuRequest(pHref);
	window.location.href = pHref;
}

function showHPage(nodeId, pHref) {
	if(!setHActive(nodeId)) {
		return;
	}
	removeAllVMenu();
	activeVNodeId = -1;
	//alert('showHPage(' + pHref + ')');
	//document.getElementById("content").src = pHref;
	//window.location.href = pHref;
	sendMenuRequest(pHref);
}

function noCommand(nodeId) {
	if(!setActive(nodeId)) {
		return;
	}
	activeVNodeId = nodeId;
	alert('noCommand()');
}

function noHCommand(nodeId) {
	alert('noHCommand()');
}

function removeAllSubMenu(nodeArray) {
	var cArray = nodeArray.elements;
	for (var i=0; i<cArray.length; i++) {
		if ((cArray[i].isopen) &&  (cArray[i].level > 0)) {
			var menuRowObject = window.document.getElementById('row_' + cArray[i].id);
			if (menuRowObject != null) {
				window.document.getElementById(vMenuNodeTableId).deleteRow(menuRowObject.rowIndex);
				cArray[i].isopen = false;
			}
		}
		if (cArray[i].child != null) {
			removeAllSubMenu(cArray[i].child);
		}
	}
}

function createTableNodes(menuId) {
	if(!setActive(menuId)) {
		return;
	}
	//alert('createTableNodes id:'+menuId);
	removeAllVMenu();
	activeVNodeId = menuId;
	var hMenuNode = findMenuNode(rootMenu.child, menuId);
	if (hMenuNode != null) {
		//rootMenu = new VMenuNode('root');
		//rootMenu.addChildNodeArray(hMenuNode);
		//alert(hMenuNode.child);
		
		var childObjectArray = hMenuNode.child;
		if (childObjectArray != null) {
			for (var i=(childObjectArray.elements.length - 1); i>=0; i--) {
				createTableNode(rootMenu, childObjectArray.elements[i]);
			}
		}
	} //alert('createTableNodes hmenunode is null');
}

function createTableNodes_orig(menuId) {
	if(!setActive(menuId)) {
		return;
	}
	activeVNodeId = menuId;
	var menuObject = findMenuNode(rootMenu.child, menuId);
	if (menuObject != null) {
		if((removeLastMenuNode) && (menuObject.level == 0)) {
			removeAllSubMenu(rootMenu.child);
		}
		var childObjectArray = menuObject.child;
		if (childObjectArray != null) {
			for (var i=0; i<childObjectArray.elements.length; i++) {
				createTableNode(menuObject, childObjectArray.elements[i]);
			}
		}
	} 
}

function removeAllVMenu() {
	var vTableObject = window.document.getElementById(vMenuNodeTableId);
	if (vTableObject != null) {
		var nrOfRows = vTableObject.rows.length;
		if (nrOfRows > 0) {
			for (var i=0; i<nrOfRows; i++) {
				vTableObject.deleteRow(0);
			}
		}
	}
}

function createRootNodes(pHMenuId, pHMenuIndex) {
	if(!setHActive(pHMenuId)) {
		//return;
	}
	removeAllVMenu();
	activeVNodeId = -1;
	var hMenuNode = null;
	if(pHMenuId == 0){
		hMenuNode = hMenuTop.elements[pHMenuIndex].child;
	}else{
	//alert('createroot nodes: '+pHMenuId);
		hMenuNode = findHMenuNode(hMenuTop, pHMenuId).child;
	}
	if (hMenuNode != null) {
		rootMenu = new VMenuNode('root');
		rootMenu.addChildNodeArray(hMenuNode);
		var childObjectArray = hMenuNode;
		if (childObjectArray != null) {
			for (var i=(childObjectArray.elements.length - 1); i>=0; i--) {
				createTableNode(rootMenu, childObjectArray.elements[i]);
			}
		}//else alert('childobjectarray is null');
	} //else alert('hmenunode is null');
}

function addPath(link, id, text){
	var pathTable = document.getElementById(pathTableId);
	
	var pathRow = pathTable.rows[0];

	var menuObject = findMenuNode(rootMenu.child, id);
	if(menuObject && menuObject.level + 2 == pathRow.cells.length)
		removeLastPath();
		
	
	var pathCell = pathRow.insertCell(pathRow.cells.length);
	var cellId = 'cell_' + id;
	pathCell.id=cellId;
	
	var commandLink = unescape(link);
	
	commandLink = "removePath('"+cellId+"');removeAllVMenu();"+commandLink;
	
	pathCell.align = "left";
	
	var newCommandSpanId = 'span_path_' + id;
	
	var spanTxt = '<span id="' + newCommandSpanId + '" class="menuTextActive" ';
	spanTxt += 'onmouseover="javascript:setOverMouse(\'' + newCommandSpanId + '\', this);" ';
	spanTxt += 'onmouseout="javascript:setOutMouse(\'' + newCommandSpanId + '\', this);" ';
	spanTxt += 'onclick="' + commandLink + '" ';
	spanTxt += '>' + text + ' > ';
	spanTxt += '</span>';
	
	//alert('add path: '+spanTxt);
	pathCell.innerHTML = spanTxt;  
}


function removePath(cellId){
	var pathTable = document.getElementById(pathTableId);
	
	var pathRow = pathTable.rows[0];
	
	var pathCell = document.getElementById(cellId);
	
	for(var i=pathRow.cells.length-1; i>pathCell.cellIndex; i--){
		pathRow.deleteCell(i);
	}
	
}

function removeAllPath(){
	var pathTable = document.getElementById(pathTableId);
	
	var pathRow = pathTable.rows[0];
		
	for(var i=pathRow.cells.length-1; i>=0; i--){
	
		pathRow.deleteCell(i);
	}
	
}

function removeLastPath(){
	var pathTable = document.getElementById(pathTableId);
	
	var pathRow = pathTable.rows[0];

	pathRow.deleteCell(pathRow.cells.length-1);
	
}
function clearContent(){
	document.getElementById('content').innerHTML = '';
}
function createTableNode(parentObject, childObject) {
	var vMenuTable = window.document.getElementById(vMenuNodeTableId);
	//alert('createTableNode parent:'+parentObject.text+'; child:'+childObject.id);
	if ( vMenuTable == null ) {
		return;	
	}

	var vMenuTableRow = null;
	var vMenuTableRowIndex = 0;
	var vMenuTableCell = null;
	
	if (parentObject == null) {
/* root node */
		vMenuTableRowIndex = 0;
	}
	else {
		var parenRowObject = window.document.getElementById('row_' + parentObject.id);
		if (parenRowObject == null) {
			vMenuTableRowIndex = 0;
		}
		else {
			vMenuTableRowIndex = parenRowObject.rowIndex + 1;
		}
	}
	
	vMenuTableRow = vMenuTable.insertRow(vMenuTableRowIndex);
	vMenuTableRow.setAttribute("id", 'row_' + childObject.id);
	vMenuTableCell = vMenuTableRow.insertCell(0);
	vMenuTableCell.align = "left";

	var tableObject = null;
	
	tableObject = window.document.createElement("table");
	vMenuTableCell.appendChild(tableObject);
	tableObject.border = "0";
	tableObject.cellPadding = "0";
	tableObject.cellSpacing = "1";
	
	var cellIndex = 0;
	var newCell = null;
	
	var commandLink = null;
	if (childObject.child != null && childObject.href != null) {
		commandLink = "createTableNodes('" + childObject.id + "','" + childObject.index + "');" +
		"javascript:showPage('" + childObject.index + "','" + childObject.href + "');";
	}
	else if (childObject.href != null) {
	
		commandLink = "javascript:showPage('" + childObject.id + "','" + childObject.href + "');";
	}
	else if (childObject.child != null) {
		commandLink = "clearContent();createTableNodes('" + childObject.id + "');";
	}
	else {
		commandLink = "clearContent();javascript:noCommand('" + childObject.id + "');";
	}
	
	commandLink = "addPath('"+escape(commandLink)+"','"+childObject.id+"','"+childObject.text+"');"+commandLink;
		
	var newRow = tableObject.insertRow(0);
/*	for (var i=0; i<childObject.level; i++) {
		newCell = newRow.insertCell(cellIndex++);
		newCell = newRow.insertCell(cellIndex++);
	}
*/
	var newCommandSpanId = 'span_' + childObject.id;

	var spanTxt = '<span id="' + newCommandSpanId + '" class="menuText" ';
	spanTxt += 'onmouseover="javascript:setOver(' + childObject.id + ', this);" ';
	spanTxt += 'onmouseout="javascript:setOut(' + childObject.id + ', this);" ';
	spanTxt += 'onclick="' + commandLink + '" ';
	spanTxt += '>' + childObject.text;// + ':' + childObject.level;
	spanTxt += '</span>';
	
		
	newCell = newRow.insertCell(cellIndex++);

	newCell.innerHTML = spanTxt;

	childObject.isopen = true;
	
}

function createHTableTopNodes(hMenuArray) {
	if (hMenuArray != null) {
		if (hMenuArray.elements != null) {
			var hMenuTable = window.document.getElementById(hMenuNodeTableId);
			if ( hMenuTable == null ) {
				return;	
			}
			for (var i=(hMenuArray.elements.length - 1); i>=0; i--) {
				createHTableNode(hMenuTable, hMenuArray.elements[i]);
			}
		}
	} 
}

function createHTableBottomNodes(hMenuArray) {

	if (hMenuArray != null) {
		if (hMenuArray.elements != null) {
			var hMenuTable = window.document.getElementById(hMenuNodeTableBottomId);
			if ( hMenuTable == null ) {
				return;	
			}
			for (var i=(hMenuArray.elements.length - 1); i>=0; i--) {
				createHTableNode(hMenuTable, hMenuArray.elements[i]);
			}
		}
	} 
}


function createHTableNode(hMenuTable, menuObject) {

	var rowObject = null;
	if ( hMenuTable.rows.length < 1 ) {
		rowObject = hMenuTable.insertRow(0);
	}
	else {
		rowObject = hMenuTable.rows[0];
	}

	var cellObject = rowObject.insertCell(1);
	
	var commandLink = null;
/*	
	if (menuObject.child != null && menuObject.href != null) {
	
		commandLink = "createRootNodes('" + menuObject.id + "','" + menuObject.index + "');" +
		"javascript:showHPage('" + menuObject.index + "','" + menuObject.href + "');";
	}
	else*/
	
	if (menuObject.width != null){
		cellObject.width = menuObject.width;
	} 
	if (menuObject.href != null) {
		commandLink = "javascript:showHPage('" + menuObject.id + "','" + menuObject.href + "');";
	}
	else if (menuObject.child != null) {
		commandLink = "clearContent();createRootNodes('" + menuObject.id + "','" + menuObject.index + "');";
	}
	else {
		commandLink = "clearContent();javascript:noHCommand('" + menuObject.index + "');";
	}
	
	commandLink = "removeAllVMenu();"+
		"addPath('"+escape(commandLink)+"','"+menuObject.id+"','"+menuObject.text+"');"+
		commandLink;
	
	var newCommandSpanId = 'span_' + menuObject.id;

	var spanTxt = '<span id="' + newCommandSpanId + '" class="menuHText" ';
	spanTxt += 'onmouseover="javascript:setHOver(' + menuObject.id + ', this);" ';
	spanTxt += 'onmouseout="javascript:setHOut(' + menuObject.id + ', this);" ';
	spanTxt += 'onclick="removeAllPath();' + commandLink + '" ';
	spanTxt += '>' + menuObject.text;
	spanTxt += '</span>';

	cellObject.innerHTML = spanTxt;
	
}

function setOver(nodeId, spanObject) {
	if (nodeId != activeVNodeId) {
		spanObject.className="menuTextHover";
		spanObject.style.cursor='pointer';
	}
	
}

function setOut(nodeId, spanObject) {
	if (nodeId != activeVNodeId) {
		spanObject.className="menuText";
		spanObject.style.cursor='default';
	}	
}

function setOverMouse(nodeId, spanObject) {
		spanObject.style.cursor='pointer';
}

function setOutMouse(nodeId, spanObject) {
		spanObject.style.cursor='default';
}

function setActive(nodeId) {
	if (nodeId == activeVNodeId) {
		return false;
	}
	
	var activeNode = window.document.getElementById('span_' + activeVNodeId);
	if (activeNode != null) {
	
		activeNode.className="menuText";
	}
	var newActiveNode = window.document.getElementById('span_' + nodeId);
	if (newActiveNode != null) {
		newActiveNode.className="menuTextActive";
	}
	activeVNodeId = nodeId;
	return true;
}

function setHOver(nodeId, spanObject) {
	if (nodeId != activeHNodeId) {
		spanObject.className="menuHTextHover";
		spanObject.style.cursor='pointer';
	}
	
}

function setHOut(nodeId, spanObject) {
	if (nodeId != activeHNodeId) {
		spanObject.className="menuHText";
		spanObject.style.cursor='default';
	}	
}

function setHActive(nodeId) {
	if (nodeId == activeHNodeId) {
		return false;
	}
	
	var activeNode = window.document.getElementById('span_' + activeHNodeId);
	if (activeNode != null) {
		activeNode.className="menuHText";
	}
	var newActiveNode = window.document.getElementById('span_' + nodeId);
	if (newActiveNode != null) {
		newActiveNode.className="menuHTextActive";
	}

	activeHNodeId = nodeId;
	return true;
}

function activateMenu(id){
	activeNodePath.reverse();
	
	createRootNodes(activeNodePath[2], 0);
	var menuObject = findHMenuNode(hMenuTop, activeNodePath[2]);
	commandLink = 'clearContent();createRootNodes('+menuObject.id+','+menuObject.index+');';
	addPath(escape(commandLink),menuObject.id,menuObject.text);
	
	for(i=3;i<activeNodePath.length-1;i++){
		createTableNodes(activeNodePath[i]);
		var menuObject = findMenuNode(hMenuTop, activeNodePath[i]);
		commandLink = 'clearContent();createTableNodes('+menuObject.id+');';
		addPath(escape(commandLink),menuObject.id,menuObject.text);
		
	}
	aNodeId=activeNodePath[activeNodePath.length-1];
	setActive(aNodeId);
	activeVNodeId = aNodeId;
}
function startMenu(activeId) {
	startTop(activeId);
	//startBottom();
}

function startTop(activeId) {
	createHTableTopNodes(hMenuTop);
	
	if(activeId){
		activateMenu(activeId);
	}else{
		createRootNodes(0, 0);
		var menuObject = hMenuTop.elements[0];
		commandLink = 'clearContent();createRootNodes('+menuObject.id+','+menuObject.index+');';
		addPath(escape(commandLink),menuObject.id,menuObject.text);
	}
}

function startBottom() {
	createHTableBottomNodes(hMenuBottom);
}
