var remember = true; //Remember menu states, and restore them on next visit.

var menu, titles, submenus, arrows, bypixels, links, cookieLink, heightAdj, expandmenu, collapsemenu, linkFound;
var heights = new Array();
var hidden = new Array();

var n = navigator.userAgent;
if(/Opera/.test(n)) bypixels = 2;
else if(/Firefox/.test(n)) bypixels = 3;
else if(/MSIE/.test(n)) bypixels = 2;

function init(){

    menu = getElementsByClassName("sdmenu", "div", document)[0];
    titles = getElementsByClassName("title", "span", menu);
    submenus = getElementsByClassName("submenu", "div", menu);
    arrows = getElementsByClassName("arrow", "img", menu);

    for(i=0; i<Math.max(titles.length, submenus.length); i++) {
	titles[i].onclick = gomenu;
	titles[i].onmouseover = changeStatus;
	titles[i].onmouseout = changeStatus;
	arrows[i].onclick = gomenu;

	titles[i].href = titles[i].title;
	titles[i].title = "";

	heights[i] = submenus[i].offsetHeight;
	submenus[i].style.height = submenus[i].offsetHeight +"px";
        submenus[i].menuValue = i;
    }
    
    links = menu.getElementsByTagName("a");
    for(i=0; i<(links.length); i++) {
		links[i].hrefTag = i
		links[i].onclick = addLinkToCookie;
	}

	expandmenu = getElementsByClassName("showall", "div", document)[0];
	collapsemenu = getElementsByClassName("hideall", "div", document)[0];
	expandmenu.onclick = showhideButton;
	collapsemenu.onclick = showhideButton;

    if(remember) restore();
}

function restore() {

	linkFound = -1;
	cookieLink = getcookie("link");

	if (window.location.href.lastIndexOf("#") != -1) {
		temphref = window.location.href.substring(0, window.location.href.lastIndexOf("#"))
	} else {
		temphref = window.location.href
	}
	
	for(i=0; i<(links.length); i++) {
		if (links[i].href.lastIndexOf("#") != -1) {
			templinks = links[i].href.substring(0,links[i].href.lastIndexOf("#"))
		} else {
			templinks = links[i].href
		}
		//alert (templinks + "--" + window.location.href)
		if (temphref  ==  templinks) {
			linkFound = links[i].hrefTag;
			heightAdj = links[i].offsetHeight;
			links[i].id = "clicked"
			heightAdj = links[i].offsetHeight - heightAdj;
			if (links[i].parentNode.id == "topmenu" && heightAdj != 0) {
				heights[links[i].parentNode.menuValue] += heightAdj;
				submenus[links[i].parentNode.menuValue].style.height = heights[links[i].parentNode.menuValue] + "px";
			} else if (links[i].parentNode.parentNode.id == "topmenu" && heightAdj != 0) {
				heights[links[i].parentNode.parentNode.menuValue] += heightAdj;
				heights[links[i].parentNode.menuValue] += heightAdj;
				submenus[links[i].parentNode.parentNode.menuValue].style.height = heights[links[i].parentNode.parentNode.menuValue] + "px";
				submenus[links[i].parentNode.menuValue].style.height = heights[links[i].parentNode.menuValue] + "px";
			}
			break;
		}
	}
	
	if(getcookie("menu") != null) {
		var hidden = getcookie("menu").split(",");
	} else if (getcookie("link") == null) {
		var hidden = new Array();
		for(i=0; i<Math.max(titles.length, submenus.length); i++) {
			hidden[i] = i;
		}
	}

    for(var i in hidden) {
		titles[hidden[i]].className = "titlehidden";
	        submenus[hidden[i]].style.height = "0px";
		submenus[hidden[i]].style.display = "none";
		changeArrowIMG(hidden[i], "hide");
		//arrows[hidden[i]].alt = "+"

		if (submenus[hidden[i]].parentNode.id == "topmenu") {
			heights[submenus[hidden[i]].parentNode.menuValue] -= heights[hidden[i]];
			if (submenus[hidden[i]].parentNode.style.height != "0px") {
				submenus[hidden[i]].parentNode.style.height = heights[submenus[hidden[i]].parentNode.menuValue] + "px";
			}
	        }
    }
	
	//*************************************************************************
	//the following checks to see if the menu is open, if not it opens the menu
	//it checks the most outer menu first then then the menu inside the outer
	//*************************************************************************
	if (linkFound >= 0) {
		try {
			for(i=0; i<(links.length); i++) {
				if (links[i].hrefTag == linkFound) {
					if (links[i].parentNode.parentNode.id == "topmenu" && titles[links[i].parentNode.parentNode.menuValue].className == "titlehidden") {
						titles[links[i].parentNode.parentNode.menuValue].className = "title";
						showmenu(links[i].parentNode.parentNode.menuValue);
					}
					if (titles[links[i].parentNode.menuValue].className == "titlehidden") {
						titles[links[i].parentNode.menuValue].className = "title";
						showmenu(links[i].parentNode.menuValue);
					}
				}
			}
		} catch (e) {
			//do nothing
		}
	}

	//*****************************************************
	//the following will open the menu for the current page
	//*****************************************************
	for(i=0; i<(titles.length); i++) {
		if (location.href.indexOf(titles[i].href) > 0) {
			if (submenus[i].id == "topmenu" && titles[i].className == "titlehidden") {
				titles[i].className = "title";
				showmenu(i);
			} else if (submenus[i].parentNode.id == "topmenu") {
				if (titles[submenus[i].parentNode.menuValue].className == "titlehidden") {
					titles[submenus[i].parentNode.menuValue].className = "title";
					showmenu(submenus[i].parentNode.menuValue);
					//submenus[i].parentNode.style.display = "";
					//submenus[i].parentNode.style.height = heights[submenus[i].parentNode.menuValue] + "px";
				}
				if (titles[i].className == "titlehidden") {
					titles[i].className = "title";
					showmenu(i);
				}
			}
		}
	}

}		//ends function

function gomenu(e) {
    if (!e)
        var e = window.event;
    var ce = (e.target) ? e.target : e.srcElement;
    var sm;
    
    for(var i in titles) {
        if(titles[i] == ce || arrows[i] == ce)
            sm = i;
    }

	if (this.tagName == "SPAN") {
		if ((titles[sm].href != undefined)  && (titles[sm].href != "")) {
			try {
				titles[sm].className = "title";
				putcookie("link", "fromnav", null);
				window.location.href = titles[sm].href;
				return;
			} catch (e) {
				//error was found, don't do anything and
				//continue on to opening or closing menu
			}
		}
	}

    //this will only be done if an image is clicked
    //or if the menu title has no working href
	if(parseInt(submenus[sm].style.height) > 0) {
        titles[sm].className = "titlehidden";
        hidemenu(sm);
	} else if(parseInt(submenus[sm].style.height) < 2) {
		titles[sm].className = "title";
        showmenu(sm);
	}
	store();
}

function hidemenu(sm) {
    //var nr = submenus[sm].getElementsByTagName("a").length*bypixels;
    var nr = 10;
    submenus[sm].style.height = (parseInt(submenus[sm].style.height)-nr)+"px";

    if(submenus[sm].parentNode.id == "topmenu") {
		submenus[sm].parentNode.style.height = (parseInt(submenus[sm].parentNode.style.height)-nr)+"px";
    }

    var to = setTimeout("hidemenu("+sm+")", 20);
    if(parseInt(submenus[sm].style.height) <= nr) {
        clearTimeout(to);
        submenus[sm].style.display = "none";

		if(submenus[sm].parentNode.id == "topmenu") {
			submenus[sm].parentNode.style.height = (parseInt(submenus[sm].parentNode.style.height)-parseInt(submenus[sm].style.height))+"px";
			heights[submenus[sm].parentNode.menuValue] -= heights[sm];
		}

		submenus[sm].style.height = 0 +"px";
		changeArrowIMG(sm, "hide");
        //arrows[sm].alt = "+"
        titles[sm].className = "titlehidden";
    }
}

function showmenu(sm) {
    //var nr = submenus[sm].getElementsByTagName("a").length*bypixels;
    var nr = 10;
    submenus[sm].style.display = "block";
    submenus[sm].style.height = (parseInt(submenus[sm].style.height)+nr)+"px";

    if(submenus[sm].parentNode.id == "topmenu") {
		submenus[sm].parentNode.style.height = (parseInt(submenus[sm].parentNode.style.height)+nr)+"px";
    }

    var to = setTimeout("showmenu("+sm+")", 20);

    if(parseInt(submenus[sm].style.height) > (parseInt(heights[sm])-nr)) {
        clearTimeout(to);
        submenus[sm].style.height = heights[sm]+"px";
        changeArrowIMG(sm, "show");
        //arrows[sm].alt = "-"
        if (submenus[sm].parentNode.id == "topmenu") {
        	heights[submenus[sm].parentNode.menuValue] += heights[sm];
        	submenus[sm].parentNode.style.height = heights[submenus[sm].parentNode.menuValue] + "px";
        }
    }
}

function store() {
    var hidden = new Array();
    for(var i in titles) {
        if(titles[i].className == "titlehidden")
            hidden.push(i);
    }
    putcookie("menu", hidden.join(","), null);
}

function getElementsByClassName(strClassName, strTagName, oElm){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function putcookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = c_name + "=" + escape(value) + ";path=/" + ((expiredays==null) ? "" : ";expires="+exdate);
}

function getcookie(c_name) {
    if(document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "=");
        if(c_start != -1) {
            c_start = c_start + c_name.length + 1;
            var c_end = document.cookie.indexOf(";",c_start);
            if(c_end == -1)
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return null;
}

function addLinkToCookie() {
	putcookie("link", this.hrefTag, null);
}

function showhideButton() {
	for (var i=0; i<submenus.length; i++) {
		if (titles[i].className == "titlehidden" && this.className == "showall") {
			if (submenus[i].id == "topmenu") {
				titles[i].className = "title";
				showmenu(i);
			}
		} else if (titles[i].className == "title" && this.className == "hideall") {
			if (submenus[i].id == "topmenu"){
				titles[i].className = "titlehidden";
				hidemenu(i);
			}
			if (submenus[i].parentNode.id == "topmenu") {
				titles[i].className = "titlehidden";
				submenus[i].style.height = "0px";
				submenus[i].style.display = "none";
				changeArrowIMG(i, "hide");
				//arrows[i].alt = "+"
				heights[submenus[i].parentNode.menuValue] -= heights[i];
			}
		}
		putcookie("link", "expandall", null);
	}
		store();
}

function changeArrowIMG(menu_number, hideorshow){
		//check for outside images to go from show to hide
		if (arrows[menu_number].id == "" && hideorshow == "hide") {
			arrows[menu_number].style.backgroundImage = "url(/imoweb/market/mi_images/button_bg_right_e.gif)";
		}
		
		//check for inside images to go from show to hide
		if (arrows[menu_number].id != "" && hideorshow == "hide") {
			arrows[menu_number].style.backgroundImage = "url(/imoweb/market/mi_images/button_bg_inner_right_e.gif)";
		}

		//check for outside images to go from hide to show
		if (arrows[menu_number].id == "" && hideorshow == "show") {
			arrows[menu_number].style.backgroundImage = "url(/imoweb/market/mi_images/button_bg_right.gif)";
		}
		
		//check for inside images to go from hide to show
		if (arrows[menu_number].id != "" && hideorshow == "show") {
			arrows[menu_number].style.backgroundImage = "url(/imoweb/market/mi_images/button_bg_inner_right.gif)";
		}
}

//the following function is used to change the text on the status bar
function changeStatus() {
	if (this.href && window.status == "") {
		window.status = this.href;
	} else {
		window.status = "";
	}
}

$(document).ready(function() {
	init();
});
$(window).unload( function () {
	if (remember == true) {
		store();
	}
});
	//window.onload = init;
	//if(remember) window.onunload = store;
