/*
 * menuExpandable.js - implements an expandable menu based on a HTML list
 */

if (!document.getElementById) {
    document.getElementById = function() { return null; }
}

var menuCookie = "menusToExpand";
var itemCookie = "itemToHighlight";

function initializeMenu(menuId, actuatorId) {
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;

    actuator.parentNode.style.backgroundImage = "none";
    //actuator.parentNode.style.listStyleImage = "url(../images/plus.gif)";
	
    actuator.onclick = function() {
        var display = menu.style.display;
        this.parentNode.style.backgroundImage = "none";
        //this.parentNode.style.listStyleImage = (display == "block") ? "url(../images/plus.gif)" : "url(../images/minus.gif)";
        //actuator.parentNode.style.listStyleImage = (display == "block") ? "url(../images/plus.gif)" : "url(../images/minus.gif)";
        
        this.parentNode.className = (display == "block") ? "menubar" : "menubarMinus";
        actuator.parentNode.className = (display == "block") ? "menubar" : "menubarMinus";
        menu.style.display = (display == "block") ? "none" : "block";
        
        // Begin custom code for remembering expanded menus with cookies
        var menusToExpand = getCookie(menuCookie);
        if (menu.style.display == "block") {
            // set a cookie to keep the menu expanded
            if (menusToExpand == null) {
                setCookie(menuCookie,menuId);
            } else if (menusToExpand.indexOf(menuId) == -1) {
                setCookie(menuCookie,menusToExpand+","+menuId);
            }
        } else {
            // remove it from the expanded cookie list
            if (menusToExpand.indexOf(menuId) != -1) {
                // check for comma after menu
                if (menusToExpand.indexOf(menuId+",") != -1) {
                    menusToExpand = menusToExpand.replace(menuId+",","");
                } else {
                    menusToExpand = menusToExpand.replace(menuId,"");
                }
                if (menusToExpand == "") {
                    deleteCookie(menuCookie);
                } else {
                    setCookie(menuCookie,menusToExpand);
                }
            }
        }
        // End custom code
        
        return false;
    }
}

function initializeMenus() {
	var menu = document.getElementById("menuDiv");
    var links = null;
    if (menu != null) {
    	links = menu.getElementsByTagName("a");
    } 
    //check to determine what kind of menu it is
	if (menu != null && links != null) {	
		initializeNonTabMenus();
	}
	else { //it is a tab menu
		initializeTabMenus();
	}
}

// This function loops through all the <ul>'s in the document and
// initializes the menus for them if they have menu classes
function initializeNonTabMenus() {
    var menu = document.getElementById("menuDiv");
    var links = menu.getElementsByTagName("a");
    var lists = document.getElementsByTagName("ul");

    var actuators = new Array();
    var nonActuators = new Array();
    // build an array of actuators
    for (i=0; i < links.length; i++) {
        if (links[i].className == "actuator") {
            actuators[actuators.length] = links[i];
        } else {
            nonActuators[nonActuators.length] = links[i];
        }
    }

    var menus = new Array();
    // build an array of menus
    for (i=0; i < lists.length; i++) {
        if (lists[i].className == "menu" || lists[i].className == "submenu") {
            menus[menus.length] = lists[i];
        }
    }

    // initialize actuators and menus (number should be the same)
    for (i=0; i < actuators.length; i++) {
        initializeMenu(menus[i].id, actuators[i].id);
    }
    
    // Begin custom code to remember last item clicked (with cookies)
    // add an onclick event to set a cookie on the non-actuators
    // skip for those opening windows - added v3.5
    for (i=0; i < nonActuators.length; i++) {
        // retain any existing onclick handlers from menu-config.xml
        if (nonActuators[i].onclick) {
          var sOnClick = nonActuators[i].onclick.toString();
          if ( sOnClick.indexOf("window.open") < 0 )
          {
            nonActuators[i].onclick=function() {
            	highlight(this);
                setCookie(itemCookie,this.href);
                if (eval(this.getAttribute("onclick")) == false) {
                    return false;
                }
            };
          }
        } 
        else {
            nonActuators[i].onclick=function() {
            	highlight(this);
                setCookie(itemCookie,this.href);
            };
        }
    }

    // user must have cookies enabled for this to work
    expandMenus();
}


function openMenu(menuId) {
    var menu = document.getElementById(menuId);
    var actuatorId = menuId.substring(0, menuId.indexOf("Menu")) + "Actuator";
    var actuator = document.getElementById(actuatorId);
    if (menu != null) {
        var display = menu.style.display;
        menu.parentNode.style.backgroundImage = "none";
        //menu.parentNode.style.listStyleImage = "url(../images/minus.gif)";
        menu.parentNode.className = "menubarMinus";
        menu.style.display = (display == "block") ? "none" : "block";
    }
}

function expandMenus() {
    var menusToExpand = getCookie(menuCookie);
    if (menusToExpand != null) {
        // if more than one menu has been menusToExpanded,
        // create an array of menusToExpanded menus
        if (menusToExpand.indexOf(",") != -1) {
            menuArray = menusToExpand.split(",");
            for (var i=0; i < menuArray.length; i++) {
                openMenu(menuArray[i]);
            }
         } else {
            openMenu(menusToExpand);
         }
    }
    var itemToHighlight = getCookie(itemCookie);
    var links = document.getElementsByTagName("a");
    // add an onclick event to set a cookie on the non-actuators
    /*for (i=0; i < links.length; i++) {
        if (links[i].href == itemToHighlight) {
            links[i].className += " highlight";
        }
    }*/
}

// =========================================================================
//                          Cookie functions 
// =========================================================================
/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "=" 
	var start = document.cookie.indexOf(prefix) 

	if (start==-1) {
		return null;
	}
	
	var end = document.cookie.indexOf(";", start+prefix.length) 
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end) 
	return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// You can call initializeMenus() manually from your JSP
window.onload = initializeMenus;

function highlight(link){
	var itemToHighlight = getCookie(itemCookie);
	// add an onclick event to set a cookie on the non-actuators
    if(link.href == itemToHighlight) return;
    var menu = document.getElementById("menuDiv");
	var links = menu.getElementsByTagName("a");
    // add an onclick event to set a cookie on the non-actuators
    for (i=0; i < links.length; i++) {
        if (links[i].href == itemToHighlight) {
            //links[i].className = '';
            //alert(links[i].className);
        }
    }
	//link.className +='highlight';
	//alert('final '+link.className)
}

// =========================================================================
//                          Added for Tab Menu functions 
// =========================================================================
var menuItemDefaultImgLeft="../images/lblue_left.gif";
var menuItemDefaultImgRight="../images/lblue_right.gif";
var menuItemDefaultImgBG="../images/lblue_bg.gif";
var menuItemMouseOverImgLeft="../images/dblue_left.gif";
var menuItemMouseOverImgRight="../images/dblue_right.gif";
var menuItemMouseOverImgBG="../images/dblue_bg.gif";
var menuItemSelectedImgLeft="../images/purple_left.gif";
var menuItemSelectedImgRight="../images/purple_right.gif";
var menuItemSelectedImgBG="../images/purple_bg.gif";
var menuItemDefaultFontFamily="arial";
var menuItemDefaultFontSize="11px";
var menuItemSelectedFontFamily="verdana";
var menuItemSelectedFontSize="13px";
var selectedId = "";
//preload the images for better perf.
var menuItemDefaultImgLeft;
var menuItemDefaultImgBG;
var menuItemDefaultImgRight;

var tabItemCookie = "tabItemToHighlight";

function initializeTabMenus() {
	reloadSelectedIdFromCookieIfAny();
	if (!( selectedId =="")) {
		setSelectedTabLayout(selectedId);
	}
}

function reloadSelectedIdFromCookieIfAny() {

	//we need to reload so it works on reload of the page
	if ( selectedId =="") {
		selectedId = getCookie(tabItemCookie);
	}
	
	if (selectedId == null) {
		selectedId = "";
	}
}

function mouseOverMenuItem(id) {

	reloadSelectedIdFromCookieIfAny();
	if(menuItemDefaultImgLeft=="") {
		menuItemDefaultImgLeft = document.getElementById(id+'l').background;
		menuItemDefaultImgBG = document.getElementById(id+'bg').background;
		menuItemDefaultImgRight = document.getElementById(id+'r').background;
	}

	if(!(id==selectedId)) {
		document.getElementById(id+'l').background=menuItemMouseOverImgLeft;
		document.getElementById(id+'bg').background=menuItemMouseOverImgBG;
		document.getElementById(id+'r').background=menuItemMouseOverImgRight;
	}
	
}

function mouseOutMenuItem(id) {

	reloadSelectedIdFromCookieIfAny();
	if(!(id==selectedId)) {
		document.getElementById(id+'l').background=menuItemDefaultImgLeft;
		document.getElementById(id+'bg').background=menuItemDefaultImgBG;
		document.getElementById(id+'r').background=menuItemDefaultImgRight;
	}	
	
}

function selectedMenuItem(id) {

	reloadSelectedIdFromCookieIfAny();

	//clear all the rest of the tabs.
	var collection = document.getElementsByTagName('td');
	for(i=0;i<collection.length;i++) {
		collection[i].style.fontSize = menuItemDefaultFontSize;
		if(collection[i].id.substring(0, 8)=='menuItem') {
			if(collection[i].id.substr(collection[i].id.length-1, collection[i].id.length)=='l')  {
				collection[i].background=menuItemDefaultImgLeft;
			}
			else if(collection[i].id.substr(collection[i].id.length-1, collection[i].id.length)=='r') {
				collection[i].background=menuItemDefaultImgRight;
			}
			else collection[i].background=menuItemDefaultImgBG;
		}
	}

	//reset previous selected menu item's fontFamily.
	//if(selectedId!="") document.getElementById(selectedId).style.fontFamily = menuItemDefaultFontFamily;
	//if(selectedId!="") document.getElementById(selectedId).style.fontSize = menuItemDefaultFontSize;

	setSelectedTabLayout(id);
	
	//ensure that the selected tab is 'remembered'.
	selectedId = id;

	//so that it will still work on refresh
	setCookie(tabItemCookie,selectedId);

}

function setSelectedTabLayout(id) {

	var selectedItem = document.getElementById(id);
	var selectedIteml = null;
	var selectedItembg = null;
	var selectedItemr = null;
	
	if (selectedItem != null) {
		//document.getElementById(id).style.fontFamily = menuItemSelectedFontFamily;
		selectedItem.style.fontSize = menuItemSelectedFontSize;
		selectedIteml = document.getElementById(id+'l');
		selectedItembg = document.getElementById(id+'bg');
		selectedItemr = document.getElementById(id+'r');
		if (selectedIteml != null) {
			selectedIteml.background=menuItemSelectedImgLeft;
		}
		if (selectedItembg != null) {
			selectedItembg.background=menuItemSelectedImgBG;
		}
		if ( selectedItemr != null) {
			selectedItemr.background=menuItemSelectedImgRight;
		}
	}

}

function showMenuItem(layerId, idx, itemZone){
	eval(layerId+".style.visibility='visible';");
	if(idx > 0)
	{
		eval(layerId+".style.left='"+(99-(idx-1)*10)+"px';");
		//eval(layerId+".style.top='"+(35-34*(idx))+"px';");
	}
	
	eval(itemZone+".style.background='#5755A8';");
}

function hideMenuItem(layerId, itemZone){
	eval(layerId+".style.visibility='hidden'");
	eval(itemZone+".style.background='#827EBB';");
}

function selectItem(elem){
	eval(elem+".style.background='#707EE2'");
}

function cancelItem(elem){
	eval(elem+".style.background='#827EBB'");
}
