var xgAllMenus = new Array();


function xgMenuItem(){
	this.label						="";
	this.key							="";
	this.parent						="";
	this.items						=new Array();
	this.add							=xgm_add;
	this.popup						=null;
	this.parent						=null;
	this.index						= null;
	this.constructPopup		=xgm_constructpopup;
	this.hideAllChilds				=xgm_hideAllChilds;
	this.skin							=null;
    this.check                   =xgm_check;
}





function xgMenu(sk){
	if (!sk) sk = "";
		this.left								=-1;
		this.top								=-1;
		this.popup							=new xgPopup(sk);
        this.popup.parentObject =this;
		this.add								=xgm_add;
		this.items							=new Array();
		this.hide								=xgm_hideAllChilds;
        this.clearSelections            =xgm_clearSelections;
		this.show							=xgm_show;
        this.redraw                         =xgm_redraw;
		this.index							=xgAllMenus.length;
		this.constructPopup		     =xgm_constructpopup;
		this.onclick                        =xgm_onclick;

		this.skin								=sk;

		
		xgAllMenus[xgAllMenus.length]=this;
}


function xgm_onclick(){
    // this should be overwrited
    console.log("Overwrite this");
}

function xgm_add(name,key){
	var mi = new xgMenuItem();
	mi.label = name;
	mi.key = key;
	mi.index	 = this.items.length;
	mi.skin = this.skin;
	this.items[this.items.length] = mi;
	
	return mi;
}


function xgm_redraw(){
    this.popup.setContent(this.constructPopup(this.items,this.index,this.index));
}

function xgm_show(left,top){
		this.left = left;
		this.top = top;
		this.popup.setContent(this.constructPopup(this.items,this.index,this.index));
		this.popup.show(left-10,top-10);
}

function xgm_clearSelections(){
    this._selectedItem = null;
    try{
    this._selectedObj.className = "menu_item"+this.skin;
    }catch(E){}
}

function xgm_hideAllChilds(){
	this.popup.hide();
	for (var i =0;i<this.items.length ;i++){
		if (this.items[i].popup!=null) {
			this.items[i].popup.hide();
			this.items[i].hideAllChilds();
			try{this.items[i]._selectedItem._selectedObj.className = "menu_item";
					this.items[i]._selectedItem._selectedItem = null;}catch(e){}
		}
	}

}

function xgm_m_click(parentIndex,key,obj){
	
	var mnu = xgAllMenus[parentIndex];
	
	mnu.hide();
    
	if(typeof(mnu.onclick) == "function"){
		mnu.onclick(key);
	}

}
function xgm_constructpopup(items,_index,parentIndex){
			var _html = "<div class='menu_container' oncontextmenu='return false'>";
		for (var i = 0;i<items.length ;i++ ){
				if (items[i].key == "$sep"){
					_html +="<div class='menu_separator"+this.skin+"'></div>";
				}else{
					var _style = "";
					if (items[i].items.length > 0){
						var arr = "rightw.png";
						if (this.skin == "white") arr = "right.png";
						_style =  "background-image:url(../xg/imgs/combobox/"+arr+");background-repeat:no-repeat;background-position:90% 50%;background-position-x:90%;background-position-y:50%;";
					}
				_html += "<div id='main_div_"+items[i].key+"'  class='menu_item"+this.skin+"' onmouseover='_showOtherPopups("+items[i].index+","+_index+",this,"+parentIndex+")'";
					_html +=" onclick='xgm_m_click("+parentIndex+",&quot;"+items[i].key+"&quot;,this)' style='"+_style+";z-index:10px'>";
					_html += items[i].label;
					_html +="</div>";
				}
		}
		_html +="</div>";
	return _html;
}

function _showOtherPopups(mi_index,popup_index,obj,parentIndex){
  	var mnu = xgAllMenus[popup_index];
	var item = mnu.items[mi_index];
	if (mnu._selectedItem == item) return;
	obj.className="menu_item_over"+mnu.skin;
	  
	if (mnu._selectedItem){
        mnu._selectedObj.className = "menu_item"+mnu.skin;
        if (mnu._selectedItem.popup!=null){
                    mnu._selectedItem.popup.hide();
                    try{mnu._selectedItem._selectedObj.className = "menu_item"+mnu.skin;}catch(e){}
                    mnu._selectedItem._selectedItem = null;
                    mnu._selectedItem.hideAllChilds();
        }
    }
    
	if (item.items.length > 0){
		if (item.popup == null){
			item.index = xgAllMenus.length;
			xgAllMenus[xgAllMenus.length] = item;
			item.popup = new xgPopup(mnu.skin);
			item.popup.setContent(mnu.constructPopup(item.items,item.index,parentIndex));
			item.parent = mnu;
		}
		var _left = xg.util.getLeft(obj) + obj.offsetWidth-40;
		var _top = xg.util.getTop(obj)-10;
		item.parent._childVisible = true;
		item.popup.show(_left,_top);

		if (_top + item.popup.height >document.body.offsetHeight)	{
			_top = parseInt(document.body.offsetHeight)-item.popup.height;
			item.popup.move(_left,_top);
		}
		item.popup.setOrientation(-1);
	}
	
	
	mnu._selectedItem = item;
	mnu._selectedObj = obj;
	mnu._selectedPIndex = popup_index;
	
}

function xgm_check(){
    var mydiv = xg.obj("main_div_"+this.key);
    var parent = mydiv.parentNode;
    var _top = xg.util.getTop(mydiv);
    var _parentTop = xg.util.getTop(parent);
    _top = _top-_parentTop+7;
    var co = document.createElement("DIV");
    co.style.zIndex = 1;
    co.className = "menu_check"+this.skin;
    co.style.width = "6px";
    co.style.height = "6px";
    co.style.position = "absolute";
    co.style.left = "10px";
    co.style.top = _top+"px";
    parent.appendChild(co);
}

