var temp, temp2, cookieArray, cookieArray2, cookieCount;
var tempB, temp2B, cookieArrayB, cookieArray2B, cookieCountB;
var tempC, temp2C, cookieArrayC, cookieArray2C, cookieCountC;
var tempD, temp2D, cookieArrayD, cookieArray2D, cookieCountD;

function initiate()
{

  cookieCount=0;

  if(document.cookie)
  {
    cookieArray=document.cookie.split(";");
    cookieArray2=new Array();

    for(i in cookieArray)
	{
      cookieArray2[cookieArray[i].split("=")[0].replace(/ /g,"")]=cookieArray[i].split("=")[1].replace(/ /g,"");
    }

  }

  cookieArray=(document.cookie.indexOf("state=")>=0)?cookieArray2["state"].split(","):new Array();
																	 
  temp=document.getElementById("containerul");
								
  for(var o=0;o<temp.getElementsByTagName("li").length;o++)
  {
    if(temp.getElementsByTagName("li")[o].getElementsByTagName("ul").length>0)
	{
      temp2				= document.createElement("span");
      temp2.className			= "symbols";
      temp2.style.backgroundImage	= (cookieArray.length>0)?((cookieArray[cookieCount]=="true")?"url(../images/minus.png)":"url(../images/plus.png)"):"url(../images/plus.png)";
      temp2.onclick=function()
	  {
        showhide(this.parentNode);
        writeCookie();
      }

      temp.getElementsByTagName("li")[o].insertBefore(temp2,temp.getElementsByTagName("li")[o].firstChild)

      temp.getElementsByTagName("li")[o].getElementsByTagName("ul")[0].style.display = "none";

      if(cookieArray[cookieCount]=="true")
	  {
        showhide(temp.getElementsByTagName("li")[o]);
      }

      cookieCount++;

    }
  }
}

// This is the same as the first initiate() but is need so that IE can have 2 columns of the outline!!
function initiate2()
{
  cookieCountB = 0;

  if(document.cookie) // check if a cookie already exists??? 
  {
    cookieArrayB=document.cookie.split(";");	// fills the cookieArray with data from the COOKIE ?!
    cookieArray2B = new Array();

    for(i in cookieArrayB)
	{			// fills cookieArray2 with something!!! maybe all the data after state=
      cookieArray2B[cookieArrayB[i].split("=")[0].replace(/ /g,"")] = cookieArrayB[i].split("=")[1].replace(/ /g,"");
    }

  }//end of if

	// If there even exists "state=" in the cookie Fill cookieArray with the data from cookieArray2["state"] else make a new Array
  if(document.cookie.indexOf("state=") >= 0)
  {
  	cookieArrayB = cookieArray2B["state"].split(",");
  }
  else
  {
	cookieArrayB = new Array();
  }

	// gets an element(object) that has the "containerul" for its id, I think.
  tempB = document.getElementById("containerul2");

	// loops through the element from above which should be a <ul>
  for(var oB = 0; oB < tempB.getElementsByTagName("li").length; oB++)
  {
		//if there is a <ul> tag inside of a <li> then put a plus/minus box next to it
    if(tempB.getElementsByTagName("li")[oB].getElementsByTagName("ul").length > 0)
	{
		//creates a <span> object I think
      temp2B	= document.createElement("span");
	  
      temp2B.className = "symbols"; // would look something like this I think <span class="symbols">
	  
	  if(cookieArrayB.length > 0) // If the length of the cookieArray is > 0, not sure why it looks at this!!
	  {
		  if(cookieArrayB[cookieCountB] == "true") // And cookieArray at position cookieCount = = true
		  {									   // then change the backgroundImage to the minus
			  temp2B.style.backgroundImage = "url(../images/plus.png)";
		  }
		  else
		  {
			  temp2B.style.backgroundImage = "url(../images/minus.png)";
		  }
	  }
	  else
	  {
      	  temp2B.style.backgroundImage = "url(../images/minus.png)";
	  }
	  
      temp2B.onclick=function() // if you click on the temp2 object then do whats below?
	  {
        showhide(this.parentNode); // calls the showhide() function pointing to temp2 I think
        //writeCookie();				// calls writeCookie()
      }

		// This puts the temp2 element/object before the temp object/element, I think
      tempB.getElementsByTagName("li")[oB].insertBefore(temp2B, tempB.getElementsByTagName("li")[oB].firstChild)

		// This hides the <ul> element that happens inside of the temp element/object
      tempB.getElementsByTagName("li")[oB].getElementsByTagName("ul")[0].style.display = "block";

		// If cookieArray[cookieCount] is true then make the elements/objects inside of the temp element/object visable
      if(cookieArrayB[cookieCount] == "true")
	  {
        showhide(tempB.getElementsByTagName("li")[oB]);
      }

		//move to the next element/object with in cookieArray
      cookieCountB++;

    }

  }	// end of the for loop
  
}	// end of the initiate2() function

function initiate3()
{
  cookieCountC=0;

  if(document.cookie)
  {
    cookieArrayC=document.cookie.split(";");
    cookieArray2C=new Array();

    for(i in cookieArrayC)
	{
      cookieArray2C[cookieArrayC[i].split("=")[0].replace(/ /g,"")]=cookieArrayC[i].split("=")[1].replace(/ /g,"");
    }
  }

  cookieArrayC=(document.cookie.indexOf("state=")>=0)?cookieArray2C["state"].split(","):new Array();
																	
  tempC=document.getElementById("containerul3");
								
  for(var oC=0;oC<tempC.getElementsByTagName("li").length;oC++)
  {
    if(tempC.getElementsByTagName("li")[oC].getElementsByTagName("ul").length>0)
	{
      temp2C				= document.createElement("span");
      temp2C.className			= "symbols";
      temp2C.style.backgroundImage	= (cookieArrayC.length>0)?((cookieArrayC[cookieCountC]=="true")?"url(../images/minus.png)":"url(../images/plus.png)"):"url(../images/plus.png)";
      temp2C.onclick=function()
	  {
        showhide(this.parentNode);
        writeCookie();
      }

      tempC.getElementsByTagName("li")[oC].insertBefore(temp2C,tempC.getElementsByTagName("li")[oC].firstChild)

      tempC.getElementsByTagName("li")[oC].getElementsByTagName("ul")[0].style.display = "none";

      if(cookieArrayC[cookieCountC]=="true")
	  {
        showhide(tempC.getElementsByTagName("li")[oC]);
      }

      cookieCountC++;

    }
  }

}	// End of initiate3()

function initiate4()
{
  cookieCountD=0;

  if(document.cookie)
  {
    cookieArrayD=document.cookie.split(";");
    cookieArray2D=new Array();

    for(iD in cookieArrayD)
	{
      cookieArray2D[cookieArrayD[iD].split("=")[0].replace(/ /g,"")]=cookieArrayD[iD].split("=")[1].replace(/ /g,"");
    }
  }

  cookieArrayD=(document.cookie.indexOf("state=")>=0)?cookieArray2D["state"].split(","):new Array();
																	
  tempD=document.getElementById("containerul4");
								
  for(var oD=0;oD<tempD.getElementsByTagName("li").length;oD++)
  {
    if(tempD.getElementsByTagName("li")[oD].getElementsByTagName("ul").length>0)
	{
      temp2D				= document.createElement("span");
      temp2D.className			= "symbols";
      temp2D.style.backgroundImage	= (cookieArrayD.length>0)?((cookieArrayD[cookieCountD]=="true")?"url(../images/minus.png)":"url(../images/plus.png)"):"url(../images/plus.png)";
      temp2D.onclick=function()
	  {
        showhide(this.parentNode);
        writeCookie();
      }

      tempD.getElementsByTagName("li")[oD].insertBefore(temp2D,tempD.getElementsByTagName("li")[oD].firstChild)

      tempD.getElementsByTagName("li")[oD].getElementsByTagName("ul")[0].style.display = "none";

      if(cookieArrayD[cookieCountD]=="true")
	  {
        showhide(tempD.getElementsByTagName("li")[oD]);
      }

      cookieCountD++;

    }
  }
}	// End of initiate4()

function showhide(el)
{

  el.getElementsByTagName("ul")[0].style.display = (el.getElementsByTagName("ul")[0].style.display == "block")?"none":"block";

  el.getElementsByTagName("span")[0].style.backgroundImage = (el.getElementsByTagName("ul")[0].style.display == "block")?"url(../images/minus.png)":"url(../images/plus.png)";

}

function closeAll() // Programmed by Brendon Ezell
{					// This function will close/colapse all the visable elements that are under containerul
					// ( that get opened by the initiate() )
	reload();
	
	//for(var index = 0; /*until there are no more */; index++)	//loops until it has looked at all the elements
	//{
		/*for(i in cookieArray)
	{
      cookieArray2[cookieArray[i].split("=")[0].replace(/ /g,"")]=cookieArray[i].split("=")[1].replace(/ /g,"");
    }
		temp = document.getElementById("containerul");
		
		for(var o=0; o < temp.getElementsByTagName("li").length; o++)
		{
			el = temp.getElementsByTagName("li")[o];
		
			// if el is expanded then colapse it
			if(el.getElementsByTagName("ul")[o].style.display == "block")
			{
				el.getElementsByTagName("ul")[o].style.display = "none";
				el.getElementsByTagName("span")[o].style.backgroundImage = "url(../images/plus.png)";
			}
		}
	}*/
}

function writeCookie()
{		
// Runs through the menu and puts the "states" of each nested list into an array, the array is then joined together and assigned to a cookie.

  cookieArray = new Array()

  for(var q = 0; q < temp.getElementsByTagName("li").length; q++)
  {

    if(temp.getElementsByTagName("li")[q].childNodes.length > 0)
	{
      if((temp.getElementsByTagName("li")[q].childNodes[0].nodeName == "SPAN") && (temp.getElementsByTagName("li")[q].getElementsByTagName("ul").length > 0))
	  {

        cookieArray[cookieArray.length] = (temp.getElementsByTagName("li")[q].getElementsByTagName("ul")[0].style.display == "block");

      }
    }

  }

  document.cookie = "state="+cookieArray.join(",")+";expires="+new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString();

}

