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]._x = i;

		this.submenus[i].getElementsByTagName("span")[0].onclick = function() {	
			if(mainInstance._lock != true){
				mainInstance._lock = true;
				for (var j = 0; j < mainInstance.submenus.length; j++){
					if(j == this._x){
						//alert('ok');
						mainInstance.expandMenu(mainInstance.submenus[j].getElementsByTagName("span")[0].parentNode);
					}
					else{					
						//alert('no');
						mainInstance.collapseMenu(mainInstance.submenus[j].getElementsByTagName("span")[0].parentNode);
					}					
				}
				mainInstance.expandMenu(this.parentNode);
			}
			mainInstance._curr = this._x;
		};		
		/*
		this.submenus[i].getElementsByTagName("span")[0].onmouseout = function() {
			//mainInstance.toggleMenu(this.parentNode);
			for (var j = 0; j < mainInstance.submenus.length; j++){
				mainInstance.collapseMenu(mainInstance.submenus[j].getElementsByTagName("span")[0].parentNode);
			}
			//mainInstance.collapseMenu(this.parentNode);
		};
		*/
	}
	if (this.markCurrent) {
		var links = this.menu.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++)
			if (links[i].href == document.location.href) {
				links[i].className = "current";
				break;
			}
	}
	if (this.remember) {
		var regex = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "=([01]+)");
		var match = regex.exec(document.cookie);
		if (match) {
			var states = match[1].split("");
			for (var i = 0; i < states.length; i++)
				this.submenus[i].className = (states[i] == 0 ? "collapsed" : "");
		}
	}
	
	
		// Detect if the browser is IE or not.
		// If it is not IE, we assume that the browser is NS.
		var IE = document.all?true:false;
		if (!IE) document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = getMouseXY;		
		function getMouseXY(e) {
				var tempX = 0;
				var tempY = 0;
				if (IE) { // grab the x-y pos.s if browser is IE
					tempX = event.clientX + document.body.scrollLeft;
					tempY = event.clientY + document.body.scrollTop;
					//document.getElementById('ttt').innerHTML = event.clientY+','+document.body.scrollTop;
				} else {  // grab the x-y pos.s if browser is NS
					tempX = e.pageX;
					tempY = e.pageY;
				}  
				// catch possible negative values in NS4
				if (tempX < 0)
					{tempX = 0}
				if (tempY < 0)
					{tempY = 0}  
				// show the position values in the form named Show
				// in the text fields named MouseX and MouseY
				//document.Show.MouseX.value = tempX;
				//document.Show.MouseY.value = tempY;
				
				obj = document.getElementById("my_menu");
				var x1 = y1 = 0;
				if (obj.offsetParent) {
					do {
						x1 += obj.offsetLeft;
						y1 += obj.offsetTop;
					} while (obj = obj.offsetParent);
				}
				obj = document.getElementById("my_menu");
				x2 = x1 +obj.clientWidth;
				y2 = y1 + obj.clientHeight;
				
				
			///if(!(tempX > x1 && tempX < x2 && tempY > y1 && tempY < y2)){
				if(!(tempX > x1 && tempX < x2)){
					
					//document.getElementById('ttt').innerHTML = tempX+','+tempY;
					
					if(mainInstance._curr != null && !mainInstance._lock){
						
					//document.getElementById('ttt').innerHTML = mainInstance._lock+'-'+mainInstance._curr;
						mainInstance.collapseMenu(mainInstance.submenus[mainInstance._curr].getElementsByTagName("span")[0].parentNode);
						mainInstance._curr = null;
					}
					for (var j = 0; j < mainInstance.submenus.length; j++){
						//mainInstance.collapseMenu(mainInstance.submenus[mainInstance._curr].getElementsByTagName("span")[0].parentNode);
					}
				}
				return true;
			}
	
	
	
	
	
};
SDMenu.prototype.toggleMenu = function(submenu) {
	if (submenu.className == "collapsed")
		this.expandMenu(submenu);
	else
		this.collapseMenu(submenu);
};
SDMenu.prototype.expandMenu = function(submenu) {
	this._lock = true;
	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";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "";
			mainInstance.memorize();
			mainInstance._lock = false;
		}
	}, 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";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "collapsed";
			mainInstance.memorize();
		}
	}, 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);
		var d = new Date();
		d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
		document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
	}
};

			
		


