function SDMenu(id) {
	if (!document.getElementById || !document.getElementsByTagName)
		return false;
	this.menu = document.getElementById(id);
	this.submenus = this.menu.getElementsByTagName("div");
	this.remember = true;
	this.speed = 3;
	this.markCurrent = true;
	this.oneSmOnly = false;
}

SDMenu.prototype.init = function() {
	var mainInstance = this;
	for (var i = 0; i < this.submenus.length; i++)
	   	this.submenus[i].getElementsByTagName("span")[0].onclick = function() {
			mainInstance.toggleMenu(this.parentNode);
		};
	if (this.remember) {
        var re = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "_menus=([^;]*)");
        var match = re.exec(document.cookie);
        
        if (match) {
			var states = new Array();
            states = match[1].split(",");
			for (var i = 0; i < (states.length-2); i++)
				this.submenus[i].className = (states[i] == 0 ? "collapsed" : "");
            
            if (states[i] >= 0)
            {
                // mark current active menu
                markselection(states[i], 1);
                             
                // load query box title
                var re = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "_title=([^;]*)");
                var match = re.exec(document.cookie);
                if (match)
                    updateSearchDD(states[i], 0, match[1], 0);
                
                // load query box dropdown selection
                var re = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "_select=([^;]*)");
                var match = re.exec(document.cookie);
                if (match)
                    document.listmenu.category.selectedIndex = match[1];
                
                // update examples hover
                updateexamp(states[i]);                 
                
                // load search type dropdown selection
                var re = new RegExp("query_type=([^;]*)");
                var match = re.exec(document.cookie);
                if (match)
                    document.listmenu.querytype.selectedIndex = match[1];
                    
                // restore main query text
                var re = new RegExp("query_mainstr=([^;]*)");
                var match = re.exec(document.cookie);
                if (match)
                    document.listmenu.inputbox.value = match[1];
                
                // restore 3 sub-query options
                var re = new RegExp("query_opt0=([^;]*)");
                var match = re.exec(document.cookie);
                if (match)
                {
                    var mySplitResult = new Array();
                    mySplitResult = match[1].split("^");
                    document.listmenu.opt0.selectedIndex = mySplitResult[0];
                    document.listmenu.opttext0.value = mySplitResult[1];
                }
                
                var re = new RegExp("query_opt1=([^;]*)");
                var match = re.exec(document.cookie);
                if (match)
                {
                    var mySplitResult = new Array();
                    mySplitResult = match[1].split("^");
                    document.listmenu.opt1.selectedIndex = mySplitResult[0];
                    document.listmenu.opttext1.value = mySplitResult[1];
                }
                
                var re = new RegExp("query_opt2=([^;]*)");
                var match = re.exec(document.cookie);
                if (match)
                {
                    var mySplitResult = new Array();
                    mySplitResult = match[1].split("^");
                    document.listmenu.opt2.selectedIndex = mySplitResult[0];
                    document.listmenu.opttext2.value = mySplitResult[1];
                }
                
                
            }
		}
	}
    mainInstance.drawcontrols();
};
SDMenu.prototype.toggleMenu = function(submenu) {
    if (submenu.className == "collapsed")
		this.expandMenu(submenu);
	else
		this.collapseMenu(submenu);
};
SDMenu.prototype.expandMenu = function(submenu) {
	var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var links = submenu.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++)
		fullHeight += links[i].offsetHeight;
	var moveBy = Math.round(this.speed * links.length);
	
	var mainInstance = this;
    var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight + moveBy;
		if (newHeight < fullHeight)
		{
        	submenu.style.height = newHeight + "px";
            var menuctrl = document.getElementById("menucontrol");
            var menuheight = parseInt(menuctrl.style.top) + moveBy;
            menuctrl.style.top = menuheight + 'px';
        }
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "";
			mainInstance.memorize();
            mainInstance.drawcontrols();
		}
	}, 30);
	this.collapseOthers(submenu);
};
SDMenu.prototype.collapseMenu = function(submenu) {
	var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var moveBy = Math.round(this.speed * submenu.getElementsByTagName("a").length);
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight - moveBy;
		if (newHeight > minHeight)
		{
        	submenu.style.height = newHeight + "px";
            var menuctrl = document.getElementById("menucontrol");
            var menuheight = parseInt(menuctrl.style.top) - moveBy;
            menuctrl.style.top = menuheight + 'px';
        }
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "collapsed";
			mainInstance.memorize();
            mainInstance.drawcontrols();
		}
	}, 30);
};
SDMenu.prototype.collapseOthers = function(submenu) {
	if (this.oneSmOnly) {
		for (var i = 0; i < this.submenus.length; i++)
			if (this.submenus[i] != submenu && this.submenus[i].className != "collapsed")
				this.collapseMenu(this.submenus[i]);
	}
};
SDMenu.prototype.expandAll = function() {
	var oldOneSmOnly = this.oneSmOnly;
	this.oneSmOnly = false;
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className == "collapsed")
			this.expandMenu(this.submenus[i]);
	this.oneSmOnly = oldOneSmOnly;
};
SDMenu.prototype.collapseAll = function() {
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className != "collapsed")
			this.collapseMenu(this.submenus[i]);
};
SDMenu.prototype.memorize = function() {
	if (this.remember) {
		var states = new Array();
		for (var i = 0; i < this.submenus.length; i++)
			states.push(this.submenus[i].className == "collapsed" ? 0 : 1);
		states.push(highlighted);
        states.push(-1);
        var d = new Date();
		d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
		document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "_menus=" + states.join(",") + 
            "; expires=" + d.toGMTString() + "; path=/";
        document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "_title=" + 
            document.listmenu.srctitle.value + "; expires=" + d.toGMTString() + "; path=/"; 
        document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "_select=" + 
            document.listmenu.category.selectedIndex + "; expires=" + d.toGMTString() + "; path=/"; 
	}
};
SDMenu.prototype.drawcontrols = function() {
    this.menu = document.getElementById("my_menu");
    var newheight = (this.menu.clientHeight + this.menu.offsetTop - 12) + 'px';
    this.ctrlmenu = document.getElementById("menucontrol");
    this.ctrlmenu.style.visibility = 'visible';
    this.ctrlmenu.style.position = 'absolute';
    this.ctrlmenu.style.top = newheight;
    this.ctrlmenu.style.left = 10 + 'px';
};
