// Pepperio Functions

/***************************************************************/
/* returns the current menu item (if there is one) in the tree */
/***************************************************************/

function returnSelected(menuDiv)
{
   var url = window.location.href.toLowerCase();
   var menu = document.getElementById(menuDiv);
   if( menu!=null )
   {
	  var items = menu.getElementsByTagName("a");
	  for( var i=0; i<items.length; i++ )
	  {
         //alert('Window Url: '+url+'\nLink Url: '+items[i].href.toLowerCase()+"\n");
		 if( items[i].href.toLowerCase()==url )
         {
            return items[i];
			break;
		 }
	  }
	  return false;
   }
}

/***************************************************************/
/* sets the parent list item of the current list and any       */
/* parents of it that are list items to selected               */
/***************************************************************/

function setSelected( menuDiv,selectedItem )
{
   if( selectedItem!=false )
   {
      selectedItem.className = selectedItem.parentNode.className+" selected";
	  var parent = selectedItem.parentNode;
      while( true )
	  {
		 if( parent==null )
		 {
			break; 
		 }
		 else if( parent.id==menuDiv )
		 {
			break;
         }
         else if( parent.tagName.toLowerCase()=="li" )
         {
            parent.className = parent.className+" selected";
         }
         parent = parent.parentNode;
	  }
   }
}

/***************************************************************/
/* expand menu                                               */
/***************************************************************/

function expandMenu( menuDiv,selectedItem )
{
   if( selectedItem!=false )
   {
      var menu = document.getElementById(menuDiv);
      if( menu!=null )
      {
         var items = menu.getElementsByTagName("a");
         var lastSetAncestor = null;
		 for( var i=0; i<items.length; i++ )
		 {
            if( items[i]===selectedItem )
			{
                //alert("Found Selected Item, 4 levels up id is: "+items[i].parentNode.parentNode.parentNode.parentNode.id);
				if( items[i].parentNode.parentNode.parentNode.parentNode===menu )
				{
                   
                   var childLists = items[i].parentNode.parentNode.getElementsByTagName("UL");
				   if( childLists.length>0 )
				   {
					  //alert("Number of Child Lists: "+childLists.length);
					  lastSetAncestor = childLists[0];
                      //alert("lastSetAncestor tag name is: "+lastSetAncestor.tagName);
				   }
				   else
				   {
                      lastSetAncestor = false;
					  //alert("There are no Child Lists");   
				   }
				}
                else
				{
				   //alert("Found Selected Item");
				   lastSetAncestor = items[i].parentNode.parentNode.parentNode;
				   //alert("lastSetAncestor: "+lastSetAncestor.tagName);
				   break;
				}
			}
		 }
		 if( lastSetAncestor!=null )
		 {
		    var menuChildNodes = menu.childNodes;
			var subMenus = null;
			for( var i=0; i<menuChildNodes.length; i++ )
			{
			    if( menuChildNodes[i].tagName.toLowerCase()=="ul" )
				{
				   subMenus = menuChildNodes[i].getElementsByTagName("UL");
				   break;
				}
			}
			
			if( subMenus!=null )
			{
			   //alert(subMenus.length+" submenus found");
               for( var i=0; i<subMenus.length; i++ )
		       {
                  if( subMenus[i]===lastSetAncestor )
			      {
                     subMenus[i].style.display = "block";
					 break;
			      }
			   }
		    }			 
		 }
      }
   }
}

//*************************************************************
//* Popups
//*************************************************************

var newWindow = null;

function addEvent(elm, evType, fn, useCapture){
	if(elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent){
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else{
		elm['on' + evType] = fn;
	}
}

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	
	closeWin();
		
	type = type.toLowerCase();
	
	if (type == "fullscreen"){
		strWidth = screen.availWidth;
		strHeight = screen.availHeight;
	}
	var tools="";
	if (type == "standard") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console" || type == "fullscreen") tools = "resizable,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}

function doPopUp(e)
{
//set defaults - if nothing in rel attrib, these will be used
var t = "standard";
var w = "780";
var h = "580";
//look for parameters
attribs = this.rel.split(" ");
if (attribs[1]!=null) {t = attribs[1];}
if (attribs[2]!=null) {w = attribs[2];}
if (attribs[3]!=null) {h = attribs[3];}
//call the popup script
popUpWin(this.href,t,w,h);
//cancel the default link action if pop-up activated
if (window.event) 
	{
	window.event.returnValue = false;
	window.event.cancelBubble = true;
	} 
else if (e) 
	{
	e.stopPropagation();
	e.preventDefault();
	}
}

function findPopUps()
{
var popups = document.getElementsByTagName("a");
for (i=0;i<popups.length;i++)
	{
	if (popups[i].rel.indexOf("popup")!=-1)
		{
		// attach popup behaviour
		popups[i].onclick = doPopUp;
		// add popup indicator
		if (popups[i].rel.indexOf("noicon")==-1)
			{
			popups[i].style.backgroundImage = "url(pop-up.gif)";
			popups[i].style.backgroundPosition = "0 center";
			popups[i].style.backgroundRepeat = "no-repeat";
			popups[i].style.paddingLeft = "15px";
			}
		// add info to title attribute to alert fact that it's a pop-up window
		popups[i].title = popups[i].title + " [Opens in pop-up window]";
		}
	}
}


/***************************************************************/
/* startup - perform startup functions                         */
/***************************************************************/

function startup()
{
   var menuName = 'leftnavigation';
   var menu = document.getElementById(menuName);
   if( menu!=null )
   {
      var selected = returnSelected(menuName);
      setSelected(menuName,selected);
      expandMenu(menuName,selected);
   }
   findPopUps();
}










