

CLOSED_IMAGE='./application/html/images/menu_item_closed.gif';
OPEN_IMAGE= './application/html/images/menu_item_opened.gif';
BLANK_IMAGE= './application/html/images/menu_item_blank.gif';


//Expander Class 
function MenuExpander(id, currentPage, previousPage){
	this.makeCollapsible(document.getElementById(id), currentPage, previousPage);
}	

MenuExpander.prototype.makeCollapsible = function (listElement, currentPage, previousPage){

	var child=listElement.firstChild;
	var currentPageFound = false;
	
	// loop over all LI child elements to see if current page can be found 
	while (child!=null){  	
	  	if(child.nodeType==1 && child.className == 'MenuHeading'){
	  		//Search sub menu items
	  		if(this.subMenuContainsPage(child, currentPage)){
	  			currentPageFound = true;
	  		}	
	  	}
	  	else if(child.nodeType==1){
	  		//Search this item
	  		if(this.containsPage(child, currentPage)){
	  			currentPageFound = true;
	  		}	
	  	}		
	  	
	  	child=child.nextSibling;
	} 


  // loop over all child elements of the list
  var child=listElement.firstChild;
  var startClosed;
  var started = false;
  
  
  while (child!=null){
	// only process li elements (and not text elements)
    
    
    if (child.nodeType==1 && child.className == 'MenuHeading'){
	  
	  	startClosed = true;			
	
      	// build a list of child ol and ul elements and hide them
      	var list=new Array();
      	var grandchild=child.firstChild;
      	while (grandchild!=null){
        	if (grandchild.tagName=='OL' || grandchild.tagName=='UL'){
          	list.push(grandchild);
        	}
        	grandchild=grandchild.nextSibling;
      	}
      
     	//Test if this child OL or UL contains the current page or if not previous page
     	if(this.subMenuContainsPage(child, currentPage)){
     	 	startClosed = false;
     	}
     	else if (! currentPageFound ) {
     		if(this.subMenuContainsPage(child, previousPage)){
     	 		startClosed = false;
     	 	}	
     	}		

      	// add toggle buttons to OL or UL element
      	var node=document.createElement('img');
      	if(startClosed){
      		node.setAttribute('src',CLOSED_IMAGE);
      		node.setAttribute('class','collapsibleClosed');
      		for (var i=0;i<list.length;i++){
      			list[i].style.display= 'none'; 
      		}	
      	}
      	else{
      		node.setAttribute('src',OPEN_IMAGE);
      		node.setAttribute('class','collapsibleOpen'); 
      		for (var i=0;i<list.length;i++){
      			list[i].style.display= 'block'; 
      		}		
      	}		
      	node.onclick= this.createToggleFunction(node,list);
      	child.insertBefore(node,child.firstChild);

    }
	else if(child.nodeType==1 && child.className != 'MenuHeading'){
		var node=document.createElement('img');
		node.setAttribute('src',BLANK_IMAGE);
		child.insertBefore(node,child.firstChild);
	}	
    child=child.nextSibling;
  }

}

/* createToggleFunction - returns a function that toggles the sublist display
 * 
 * toggleElement  - the element representing the toggle gadget
 * sublistElement - an array of elements representing the sublists that should
 *                  be opened or closed when the toggle gadget is clicked
 */
MenuExpander.prototype.createToggleFunction = function (toggleElement,sublistElements){

  return function(){

    // toggle status of toggle gadget
    if (toggleElement.getAttribute('class')=='collapsibleClosed'){
      toggleElement.setAttribute('class','collapsibleOpen');
      toggleElement.setAttribute('src',OPEN_IMAGE);
    }else{
      toggleElement.setAttribute('class','collapsibleClosed');
      toggleElement.setAttribute('src',CLOSED_IMAGE);
    }

    // toggle display of sublists
    for (var i=0;i<sublistElements.length;i++){
      sublistElements[i].style.display=
          (sublistElements[i].style.display=='block')?'none':'block';
    }

  }

}


/* containsPage - returns true if current page string is in LI
 * 
 * node  - a menu heading LI node
 * pageName = page name string 
 *  
 */

MenuExpander.prototype.containsPage = function(child, currentPage){
	var url = child.firstChild.href.toLowerCase();
	var str = currentPage.toLowerCase();
	if(this.currentPageMatch(url,  str)){
    	return true;	
    }	
    return false;
}	
	

/* subMenuContainsPage - returns true if current page string is in LI sub-menu items URL
 * 
 * node  - a menu heading LI node
 * pageName = page name string 
 *  
 */

MenuExpander.prototype.subMenuContainsPage = function (node,  currentPage){
  	var child=node.firstChild;
      while (child!=null){
        if (child.tagName=='OL' || child.tagName=='UL'){
         	grandchild=child.firstChild;
         	while (grandchild!=null){
        		if(grandchild.tagName == 'LI'){
          			var url = grandchild.firstChild.href.toLowerCase();
          			var str = currentPage.toLowerCase();
          			//var index = url.indexOf(str);
          			//alert(url + " | " + str);
          			if(this.currentPageMatch(url,  str)){
          				return true;	
          			}	
          		}
        		grandchild=grandchild.nextSibling;
      		}
        }
        child=child.nextSibling;
      }	
	return false;
}	

/* currentPageMatch - returns true if current page in url 
 * 
 * url
 * test string
 *  
 */
MenuExpander.prototype.currentPageMatch = function (url,  str){
	
	var fwPagPos = url.indexOf('fwpage=');
	
	if(fwPagPos > -1){
		//Regular url
		return this.currentPageMatchRegularURL(url,  str);
	}	
	else{
		//User friendle URL
		return this.currentPageMatchUserFriendlyURL(url,  str);	
	}	
}	


MenuExpander.prototype.currentPageMatchRegularURL = function (url,  str){
	var fwPagPos = url.indexOf('fwpage=');
	var fwIDPos = url.indexOf('&fwid=');
	var urlPageName;
	
	if(fwIDPos > -1){
		urlPageName = url.substr(fwPagPos + 7, fwIDPos  - (fwPagPos + 7));
	}
	else{
		urlPageName = url.substr(fwPagPos + 7);
	}		
	
	if(str == urlPageName){
		return true;
	}	
	return false;
	
	
}	

MenuExpander.prototype.currentPageMatchUserFriendlyURL = function (url,  str){
	var fwPagPos = url.lastIndexOf('/');
	var fwIDPos = url.indexOf('=');
	var urlPageName;
	
	if(fwIDPos > -1){
		urlPageName = url.substr(fwPagPos + 1, fwIDPos  - (fwPagPos + 1));
	}
	else{
		urlPageName = url.substr(fwPagPos + 1);
	}		
	
	if(str == urlPageName){
		return true;
	}	
	return false;
	
	
}	