﻿var Gui = {
	cMenus:[],
	addControl:function(item){
		this.cMenus[item.id] = item;
	}
}

Gui.CollapseMenu = function(el){
	this._init(el);
}
Gui.CollapseMenu.prototype = {
	id:null,
	idExpanded:null,	//id of expanded element
	idSelected:null,	//id of selected element
	getEl:function(){
		return $(this.id);
	},
	_init:function(el){
		this.id = el;
		var me = this;
		var r = this.getEl();
		
	if(r){
		var getObject = "Gui.cMenus['"+this.id+"']";
		
		var aTags = r.getElementsByTagName('a');
		
		var rChildren = r.childNodes;
		for (var i = 0, l = rChildren.length; i < l; i++) {
			if(rChildren[i].tagName == 'LI'){
				var aTags = rChildren[i].getElementsByTagName('A');
				if(aTags.length > 0){
					var a = aTags[0];
					if(a.rel != 'ignore'){
						var id = System.getNewId("cItem");
						var link = this.processLink(a.href);
						a.id = id;
						a.onclick =　new Function(getObject+".expand('"+id+"','"+link+"')");
						a.href="javascript:void(0)";
					}
					if(rChildren[i].className == "selected")
						this.idExpanded = id;

					for(var j = 1, k = aTags.length ; j < k ; j++){
						var a = aTags[j];
						if(a.rel != 'ignore'){
							var id = System.getNewId("sItem");
							var link = this.processLink(a.href);
							a.id = id;
							a.onclick =　new Function(getObject+".select('"+id+"','"+link+"')");
							a.href="javascript:void(0)";
						}						
						if(a.parentNode.className == "selected")
							this.idSelected = id;
					}
				}
			}
				
		}
	}
	},
	// extend
	processLink:function(link){
		return link;
	},
	expand:function(id,link){
		var ref = $(id);

		var prev = $(this.idExpanded);
		prev.parentNode.className = "";
		
		this.idExpanded = id;
		ref.parentNode.className = "selected";
		
		return this.onExpand(id,link);
	},
	// extend
	onExpand:function(id,link){
		return false;
	},
	select:function(id, link){
		var ref = $(id);
		
		var prev = $(this.idSelected);
		prev.parentNode.className = "";

		this.idSelected = id;		
		ref.parentNode.className = "selected";
		
		return this.onSelect(id, link);
	},
	// extend
	onSelect:function(id, link){
		return false;
	}
}		