/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n')
document.write('.submenu{display: none;}\n')
/*document.write('.micon{background: transparent url(file/layout/icon_collapsed.png) center left no-repeat;}\n')*/
document.write('</style>\n')
}

function menu_grp(grp_no){
var cookiename="menu_grp";
var cookievalue=grp_no;
document.cookie=cookiename+"="+cookievalue;
}

function menu_expand(submenu_name, micon_name){

blockid=submenu_name+"-"+micon_name;
var cookiename="curr_submenu";
var cookievalue=blockid;
document.cookie=cookiename+"="+cookievalue;
}

function SwitchMenu(obj, obj2){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var el2 = document.getElementById(obj2);
	var ar = document.getElementById("sidebar").getElementsByTagName("div"); //DynamicDrive.com change
	var ir = document.getElementById("sidebar").getElementsByTagName("img"); //switch all img in sidebar as well
		if(el.style.display != "block"){ //DynamicDrive.com change
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu"){ //DynamicDrive.com change
				ar[i].style.display = "none";
				}
			}
			for (var i=0; i<ir.length; i++){
				// since submenu collpase, the icon need to show that it is collapsed as well
				if (ir[i].className=="micon"){ 
				ir[i].src = "file/layout/icon_collapsed.png";
				}
			}
			el.style.display = "block";
			//Change image to indicate that submenu is expanded
			el2.src = "file/layout/icon_expanded.png";
		}else{
			el.style.display = "none";
			//Change image to indicate that submenu is collapsed
			el2.src = "file/layout/icon_collapsed.png";
		}
	}
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { 
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function onloadfunction(){
/*
if (persistmenu=="yes"){
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname;
var cookievalue=get_cookie(cookiename);
if (cookievalue!=""){
//split the cookie string into 2 which is separated by -
var cookie_array = cookievalue.split("-");
if(document.getElementById(cookie_array[0])){
document.getElementById(cookie_array[0]).style.display="block";
if(document.getElementById(cookie_array[1])){
document.getElementById(cookie_array[1]).src ="file/layout/icon_expanded.png";
}
}
}
}
*/
// check if the current page sidebar link is under the submenu. If so, expand it
var cookiename="curr_submenu";
var cookievalue=get_cookie(cookiename);
if (cookievalue!=""){
//split the cookie string into 2 which is separated by -
var cookie_array = cookievalue.split("-");
if(document.getElementById(cookie_array[0])){
document.getElementById(cookie_array[0]).style.display="block";
if(document.getElementById(cookie_array[1])){
document.getElementById(cookie_array[1]).src ="file/layout/icon_expanded.png";
}
}
}


}

function savemenustate(){
var cookiename="menu_grp";
var cookievalue=get_cookie(cookiename);
var inc=1; 
var blockid="";
while (document.getElementById(cookievalue+"sub"+inc)){
if (document.getElementById(cookievalue+"sub"+inc).style.display=="block"){
blockid=cookievalue+"sub"+inc+"-micon"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}

if (window.addEventListener)
window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
window.onload=onloadfunction

if (persistmenu=="yes" && document.getElementById)
window.onunload=savemenustate
