// Open tree
function CreateMenu(currentId){
	window.mnu = new slideMenu('mnu');
	mnu.currentItem = currentId;
	mnu.speed = 22;
	mnu.item_height = 22;
	Populate(mnu);
}

function Populate(myMenu){
	var slideObj = document.getElementById('slideMenu');
	var status = false;
	var lvl1 = 0;
	for(var i=0; i<slideObj.childNodes.length; i++){
		var cChild = slideObj.childNodes[i];
		if(cChild.id){
			if(cChild.id.indexOf("menuItem") != -1){
				lvl1++;
				status = (lvl1 == myMenu.currentItem) ? true : false;
				myMenu.AddItem(cChild.id.substring(8),0, status);
			}
			else if(cChild.id.indexOf("submenu") != -1){
				for(var j=0; j<cChild.childNodes.length; j++){
					ccChild = cChild.childNodes[j];
					if(ccChild.nodeType == 1){
						myMenu.AddItem(ccChild.id.substring(8),cChild.id.substring(7));
					}
				}
			}
		}
	}
}

// Item object
function menuItem(id, pid, status) {
	this.id = id;
	this.pid = pid;
	this.status = false || status;
	this.n_children = 0;
};

// Tree object
function slideMenu(objName) {
	this.currentItem = null;
	this.interval = [];
	this.item_height = null;
	this.items = [];
	this.moving = 0;
	this.obj = objName;
	this.speed = null;
};

// Adds a new item to the item array
slideMenu.prototype.AddItem = function(id, pid, status) {
	this.items[this.items.length] = new menuItem(id, pid, status);
	if(pid){
		this.GetItemById(pid).n_children += 1;
	}
};

// Returns menuItem of specified id
slideMenu.prototype.GetItemById = function(cid){
	for(var i=0; i<this.items.length; i++){
		if(this.items[i].id == cid){
			return this.items[i];
		}
	}
	return false;
};

// Toggles submenu open/closed for specified item
slideMenu.prototype.ToggleSubmenu = function(objId){
	var cid = objId.substr(7);
	var iItem; 
	for(var i=0; i<this.items.length; i++){
		iItem = this.items[i];
		if(iItem.status == true && iItem.id != cid){
			iItem.status = false;
			this.ToggleInterval(eval('"submenu'+iItem.id+'"'),0,iItem.status);
		}
	}
	var cItem = this.GetItemById(cid);
	cItem.status = !cItem.status;
	var max_height = cItem.n_children * this.item_height;
	this.ToggleInterval(objId, max_height,cItem.status);
};

// Toggle interval for open/closing of menuItems
slideMenu.prototype.ToggleInterval = function(objId, max_height, status){
	if(this.interval[objId]){
		clearInterval(this.interval[objId]);
	}
	if(status){
		this.interval[objId] = window.setInterval(this.obj+".OpenItem('"+objId+"','"+max_height+"')",20);
	}
	else{
		this.interval[objId] = window.setInterval(this.obj+".CloseItem('"+objId+"')",20);
	}

};

slideMenu.prototype.OpenItem = function(objId, max_height){
	if(this.moving != objId && this.moving != 0){
		return;
	}
	if(this.moving == 0){
		this.moving = objId;
	}
	var obj = document.getElementById(objId);
	var styleObj = new xbStyle(obj);
	var top = styleObj.getHeight();
	if(top < max_height){
		if(max_height - top >= this.speed){
			obj.style.display = "block";
			styleObj.resizeBy(0, this.speed);
		}
		else{
			styleObj.resizeBy(0, max_height - top);
		}
	}
	else{
		clearInterval(this.interval[objId]);
		this.moving = 0;
	}
};

slideMenu.prototype.CloseItem = function(objId){
	if(this.moving != objId && this.moving != 0){
		return;
	}
	if(this.moving == 0){
		this.moving = objId;
	}
	var obj = document.getElementById(objId);	
	var styleObj = new xbStyle(obj); 
	var top = styleObj.getHeight();
	if (top > 0){
		if(top >= this.speed){
			styleObj.resizeBy(0, -1 * this.speed); 
		}
		else{
			obj.style.display = "none";
			styleObj.resizeBy(0, -1 * top);
		}		
	}
	else{
		obj.style.display = "none";
		clearInterval(this.interval[objId]);
		this.moving = 0;
	 }
};

function xbStyle(obj, win){
	this.styleObj = obj.style;
	this.object = obj;
	this.window = win ? win : window;
};

xbStyle.prototype.styleObj = null;
xbStyle.prototype.object = null;

xbStyle.prototype.getHeight = function(){
	var value = null;
	var propname = 'height';
	if(this.window.document.defaultView && this.window.document.defaultView.getComputedStyle){
		value = this.window.getComputedStyle(this.object, '').getPropertyValue(propname);		// Gecko
	}
	else if(typeof(this.object.currentStyle) != 'undefined'){
		value = this.object.currentStyle[propname];		// IE5+
	}
	if(!value && this.styleObj[propname]){
		value = this.styleObj[propname];		// Older browsers
	}
	var height = value;
	if (height == '' || height == 'auto' || height.indexOf('%') != -1){
		if (typeof(this.object.offsetHeight) == 'number'){
			height = this.object.offsetHeight + 'px';
		}
		else if(typeof(this.object.scrollHeight) == 'number'){
			height = this.object.scrollHeight + 'px';
		}
	}
	return parseInt(height);
};

xbStyle.prototype.resizeBy = function(deltaX, deltaY){
	var height = this.getHeight() + deltaY;
	this.styleObj.height = height + 'px';
};