/**********
*  Author: Reed Evans
*    Date: 9/18/2008
*  This program uses the site structure to build out
*  navigation elements using the applied CSS styles
*  given
***********/

var nodeList = new Array();		// Array of all nodes in the site structure
var breadcrumb = new Array();		// Array of all nodes in the breadcrumb trail

// The function that creates the asset Node objects is in topNav.js now


//this function wraps first level items in the left nav
//Changes the background image if the text in the left nav wraps
$(document).ready( function(){
  /* $("ul.list-expander > li > ul > li > a").each( function() {
	var height=$(this).height();
	if(height > 27){
	  $(this).css("line-height","15px");
          $(this).addClass("a-wrapped");
	}
   }); */

   $("ul.list-expander > li > h3").each( function() {
      var height=$(this).height();
      if(height > 45)
      {
         $(this).parent().css("background", "url(/common/images/bg-active-collapse01_38.gif) no-repeat");
	 $(this).addClass("wrapped-nav");
	 $(this).children("a").css("line-height","15px");
      }
   });
});



// This function creates breadcrumb objects
function breadcrumbNode(id,isActive)
{
  this.id = id;
  this.isActive=isActive;
}

function doSubSubMenu(numChildren,subIsActive)
{
  var appliedStyle="";

  if(subIsActive)
    appliedStyle="display:block;";

  document.writeln("<ul class='sub-sub-nav-local' style='"+appliedStyle+"'>");

  for(i=0;i < numChildren;i++)
  {
    var appliedClass="";
    var node=nodeList.pop();

    if(breadcrumb.length > 2)
    {
      if(breadcrumb[2].id == node.id)
        appliedClass="active";
    }

    document.writeln("<li><a href='"+node.url+"' class='"+appliedClass+"' title='"+node.name+"'>"+node.name+"</a></li>");
    if(node.children > 0)
    {
      for(j=0;j < node.children;j++)
      {
        var tempNode=nodeList.pop();
        if(tempNode.children > 0)
          alert("You have added one too many levels to this site structure, please correct! \n\n Node: "+tempNode.name);
      }
    }
  }
  document.writeln("</ul>");
}

function doSubMenu(numChildren,IsActive)
{
  var appliedStyle="";

  if(IsActive)
    appliedStyle="display:block;";

  document.writeln("<ul class='sub-nav-local' style='"+appliedStyle+"'>");   //style display:block; expands the menu
  for(var i=0;i < numChildren;i++)
  {
    var subIsActive=false;   //Flag for Active URL
    var appliedClass="";

    var node=nodeList.pop();

    if(breadcrumb.length > 1)
    {
      if(breadcrumb[1].id == node.id)
      {
        appliedClass="active";
        subIsActive=true;
      }
    }
    document.writeln("<li><a href='"+node.url+"' class='"+appliedClass+"' title='"+node.name+"'>"+node.name+"</a>");

    if(node.children > 0)
      doSubSubMenu(node.children,subIsActive);
   // alert(i+" through the loop "+node.name);

    document.writeln("</li>");
  }
  document.writeln("</ul>");
}

function doMenu()
{
  var node;
  var temp;
  nodeList.reverse();  //The first element in the array is always a top level asset, therefore I reverse and pop elements off.
		      // Once an asset is used, it won't be used again. Thus, "pop it" from the array
  if(contentType=="Company" || contentType=="Careers" || contentType=="Investors" || contentType=="Sustainability")
  {
    nodeList[nodeList.length-1].children=0;
    breadcrumb.splice(0,1);//removes the first element from breadcrumb
  }

  while(nodeList.length>0)
  {
      node = nodeList.pop();//Remove first node because it will contain the parent landing page that is not in the navigation
    var appliedClass="";

    var active=false;

    if(breadcrumb.length > 0)
    {
      if(breadcrumb[0].id == node.id)
      {
        active=true;
        appliedClass="active";
      }
    }

    document.write("<li><h3><a href='"+node.url+"' class='"+appliedClass+"' title='"+node.name+"'>"+node.name+"</a></h3>");
    if(node.children > 0)
      doSubMenu(node.children,active);

    document.write("</li>");
  }


}