
var visibleMenus = new Array(0);
var actualMenu = -1;
var actualItem = -1;




function getTree(menuNum, itemNum) {
	// Array index is the menu number. The contents are null (if that menu is not a parent)
	// or the item number in that menu that is an ancestor (to light it up).

	var itemArray = new Array(0);
    var item;

	while(1) {
		item = new Array(2);
		item[0] = menuNum;
		item[1] = itemNum;		
		arrayPush(itemArray,item);		
		if (menuNum == 0) return itemArray;
		
		itemNum = menu[menuNum][0].parentItem;
		menuNum = menu[menuNum][0].parentMenu;
	}
}

function getTree2(menuNum, itemNum) {
	// Array index is the menu number. The contents are null (if that menu is not a parent)
	// or the item number in that menu that is an ancestor (to light it up).

	itemArray = new Array(0);

	while(1) {	        
        if (menuNum != 0) arrayPush(itemArray,menuNum);

		// If we've reached the top of the hierarchy, return.
		if (menuNum == 0) return itemArray;
		menuNum = menu[menuNum][0].parentMenu;
	}
}


// *** MENU CONSTRUCTION FUNCTIONS ***

function Menu() {
	// Parent menu and item numbers, indexed later.
	this.parentMenu = null;
	this.parentItem = null;
	// Reference to the object's (set later).
	this.ref = null;
	this.lnk = null;
}


function Item(text, href, length, target, cat) {
	this.text = text;
	this.href = href;
	this.length = length;
	this.target = target;
	// Reference to the object's style properties (set later).
	this.ref = null;
	
	this.catDest=cat;
}



function writeNewMenus(currMenu, tElement) {
    // do not create all menus !! Create menu on demand

    with (menu[currMenu][0]) {

        // Variable for holding HTML for items and positions of next item.
        var str = '';

       	// Remember, items start from 1 in the array (0 is menu object itself, above).
        // Also use properties of each item nested in the other with() for construction.

        for (currItem = 1; currItem < menu[currMenu].length; currItem++) {
        	with (menu[currMenu][currItem]) {
        		var itemID = 'm' + currMenu + 'i' + currItem;
        		var refID = 'h' + currMenu + 'i' + currItem;

        		str += '<li id="' + itemID + '">';

           		if (target > 0) {
            	    str +=  '<a id="' + refID + '" class="categoryNotLeaf" href="#" onClick="popSubMenu(' + currMenu + ',' + currItem + '); return false;"><span>+</span>' + text +  '</a>';
           		} else {
           		    str +=  '<a id="' + refID + '" class="categoryLeaf" href="' + href + '">' + text +  '</a>';
           		}

                str +=  '</li>';
                
            	if (target > 0) {
           			// Set target's parents to this menu item.
           			menu[target][0].parentMenu = currMenu;
           			menu[target][0].parentItem = currItem;
           	    }
           	}
        }

  		var newElement = document.createElement('ul');
       	//newElement.setAttribute("class", "menuGroup");
       	if(tElement == 0) {
       	    document.getElementById('categoryMenu').appendChild(newElement);
       	} else {
          	document.getElementById(tElement).appendChild(newElement);
        }

       	newElement.id = currMenu;
       	newElement.innerHTML = str;
       	ref = newElement;

    }

    with(menu[currMenu][0]) {
       	ref.className = 'open';
    }
}
        
        
        
function popSubMenu(menuNum, itemNum) {
    if (menuNum == actualMenu && itemNum == actualItem) {
        var linkID = 'h' + menuNum + 'i' + itemNum;
        var link = document.getElementById(linkID)

        if (link.className == 'categoryNotLeafOpened') {
            hideAllExcept(actualMenu);
          	return;
        }
        //return;
    } else {        
        actualMenu = menuNum;
        actualItem = itemNum;
    }

    hideAllExcept(menuNum);

    targetNum = menu[menuNum][itemNum].target;

    if (targetNum > 0) {
        isPushed = false;
        // create menu on demand 
        //if don't exist
        if (!menu[targetNum][0].ref) {

            targetElement ='m' + menuNum + 'i' + itemNum;
        	writeNewMenus(targetNum, targetElement);
        	menu[targetNum][0].lnk='h' + menuNum + 'i' + itemNum;

        	arrayPush(visibleMenus,targetNum);
   		    isPushed = true;  
   		}

        var refID = 'h' + menuNum + 'i' + itemNum;
        document.getElementById(refID).className='categoryNotLeafOpened';

   		with (menu[targetNum][0].ref) {
            className = 'open';

            if (!isPushed && visibleMenus[visibleMenus.length -1] != targetNum) 
                arrayPush(visibleMenus,targetNum);
        }

    }
}


function hideAllExcept(menuNum) {  

    var keepMenus = getTree2(menuNum, 1);
    var newVisibleMenus = new Array(0);		
    for (count1 = 0; count1 < visibleMenus.length; count1++) {

        pop = true;
        for (count2 = 0; count2 < keepMenus.length; count2++) {   
            if (keepMenus[count2] == visibleMenus[count1]) {
                pop = false;
          	}    	        
        }
        if (pop) {
            var link = document.getElementById(menu[visibleMenus[count1]][0].lnk);
            if (link.className == 'categoryNotLeafOpened')
                link.className = 'categoryNotLeaf';
                menu[visibleMenus[count1]][0].ref.className = 'close';
        } else {
            arrayPush(newVisibleMenus,visibleMenus[count1]);   
        }
    }

    visibleMenus = newVisibleMenus;
}


/* functions to open category in menu */
function Pair(a, b) {
	this.a = a;
	this.b = b;
}

function openCategory(dest) {
/*
[0-catMenus][0=menuInfo; 1-subCatMenus]
*/

    if(!dest) return;

    var clickList = new Array(0);
    
    for (i = 0; i < menu.length; i++) {
        for (j=1; j < menu[i].length; j++) {
            if(menu[i][j].catDest == dest)
            {
                var selId = 'h' + i + 'i' + j;
            
                if(menu[i][j].target != 0)
                    arrayPush(clickList, new Pair(i,j));
                
                var m = new Pair(i, j);
                while(m.a != 0) {
                    m = getMenuId(i);
                    arrayPush(clickList, m);
                }

                for (q=clickList.length - 1; q >= 0 ; q--) {
                    //need to use popSubMenu because writeNewMenus don't refresh extra information
                    popSubMenu(clickList[q].a, clickList[q].b);
                }
                
                var selObj = document.getElementById(selId);                
                if (selObj != null && selObj.className == 'categoryLeaf')
                    selObj.className += ' categorySelected';
                
                return;
            }
        }
    }
}

function getMenuId(tar) {
    for (i = 0; i < menu.length; i++) {
        for (j=1; j < menu[i].length; j++) {
            if(menu[i][j].target == tar)
            {
                return new Pair(i,j);
            }
        }
    }
    return null;
}
