var temp, temp2, cookieArray, cookieArray2, cookieCount;
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(minus.gif)":"url(plus.gif)"):"url(plus.gif)";
      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++;
    }
    else{
      temp2 = document.createElement("span");
      temp2.className = "symbols";
      /*temp2.style.backgroundImage = "url(page.gif)";*/
      temp.getElementsByTagName("li")[o].insertBefore(temp2,temp.getElementsByTagName("li")[o].firstChild);
    }
  }
}

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(minus.gif)":"url(plus.gif)";
}
function linkout(el,linkurl){
	if (el.getElementsByTagName("ul")[0].style.display=="block"){
		location.replace(linkurl);
	}
}

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();
}

function writemenu(){
	var menutext='<ul id="containerul"><li class="main"> <a href="index.htm" >Home</a></li><li class="main"> <a href="about.htm">About Us </a></li><li class="main"> <a href="#" onclick=\'showhide(this.parentNode); writeCookie();  linkout(this.parentNode,"products.htm")\'>Products</a><ul class="menu"><li class="main2"> <a href="#" onclick=\'showhide(this.parentNode); writeCookie();\'>Lighting</a><ul class="menu2"><li> <a href="gls.htm">GLS Lamps</a> </li><li> <a href="candle.htm">Candle Lamps</a></li><li> <a href="round.htm">Round Lamps</a> </li><li> <a href="reflector.htm">Reflector Lamps</a> </li><li> <a href="pygmy.htm">Pygmy Lamps</a> </li><li> <a href="fireglow.htm">Fireglow/Colour GLS</a> </li><li> <a href="lowenergy.htm">Low Energy</a> </li><li> <a href="halogen.htm">Halogen</a> </li><li> <a href="accessorybulb.htm">Accessory/Others</a> </li></ul></li><li class="main2"> <a href="#" onclick=\'showhide(this.parentNode); writeCookie();\'>Accessories</a><ul class="menu2"><li> <a href="plugs.htm">Plugs</a> </li><li> <a href="extensions.htm">Extensions</a></li><li> <a href="fuses.htm">Fuses</a> </li><li> <a href="alarms.htm">Alarms</a> </li><li> <a href="torches.htm">Torches</a> </li><li> <a href="other.htm">Others</a> </li></ul></li></ul></li><li class="main"> <a href="#" onclick=\'showhide(this.parentNode); writeCookie();\'>Services</a><ul class="menu"><li class="main2"> <a href="lab.htm">In-House Laboratory</a> </li><li class="main2"> <a href="distribution.htm">Distribution Network</a> </li><li class="main2"> <a href="pack.htm">Bespoke Packaging Design</a> </li></ul></li><li class="main"> <a href="contact.htm">Contact Us</a></li></ul>';
	document.write(menutext);
	initiate();
};