/*
	Author: Stephen W. Cote
	Email: wranlon@hotmail.com
	
	Copyright 2002, All Rights Reserved.
	
	Do not copy, archive, or distribute without prior written consent of the author.
*/

_js_package("org.cote.js.ui.object");

/*
	Bug: stuck menu items are being cleared.
		A partial fix is in, but the event target causes the menu to be cleared
		when the mouse moves off the label.
		
		Current status is that the label is cleared when moving from the menu container
		to a menu item that has a sub menu.
	
	
	Menu items contain these attributes:
		mrid = menu id
		srid = sub menu id
		irid = item index
		
		Note the mrid and srid are ids, and irid is an index.


*/

org.cote.js.ui.object.menu={
	newInstance:function(parent_object,menu_type){
		/*
			ui.object.newInstance draws on js.object.newInstance.
			j.o.n implements the event and style code, and draws on the base
			generic object class definition.
		*/
		var n = org.cote.js.ui.object.newInstance("spec_menu","0.1b");
		
		if(typeof menu_type=="undefined") menu_type=1;
		
		n.object_config.status.menu_type = menu_type;
		
		if(typeof parent_object!="object") parent_object = document.body;
		n.parent_object = parent_object;
	
		n._init=function(){
			var t=this,v,d=0,_m=org.cote.js.message.MessageService,_d=org.cote.js.dom.event,c;
			c = t.object_config;
			t.ms = org.cote.js.message.MessageService;

			t.style_manager = org.cote.js.dom.css.StyleManager.newInstance(t);

			v = t._load_style_names(c.style_name_id);
			if(c.status.menu_type == 1){
				d = document.createElement("div");
				t.parent_object.appendChild(d);
				t.style_manager.init(d,c.style_names["menucont"]);
	
				_m.subscribe(t,"destroy","_handle_window_destroy",window);
				
				c.pointers.container = d;
			}

			if(c.status.menu_type == 2 && n.parent_object!=null){
				d = n.parent_object;
				_d.addEventListener(d,"contextmenu",t._prehandle_contextmenu);
			}
			
			if(d){
				_d.addEventListener(d,"mouseover",t._prehandle_mouseover,0);
				_d.addEventListener(d,"mouseout",t._prehandle_mouseout,0);
				_d.addEventListener(d,"click",t._prehandle_click,0);
				_d.addEventListener(d,"losecapture",t._prehandle_click,0);
				_d.addEventListener(d,"selectstart",t._handle_selectstart,0);
			}

			t.ready_state = 4;
			
		};
		n._handle_selectstart=function(){
			return false;
		};
		n._handle_window_destroy=function(){
			this.destroy();
		};
		n.destroy=function(){
			var t=this,c;
			c=t.object_config;
			if(t.ready_state == 4){
				t.parent_object.removeChild(c.pointers.container);
				org.cote.js._application_scope.remove_object(t);
			}
			t.ready_state = 0;
		};

		n.bindElement=function(o){
			var t=this,i,c;
			c=t.object_config;
			i=org.cote.js._get_gunid();
			o.setAttribute("lid",i);
			o.setAttribute("isMenuContext","1");
			c.pointers.menus[i]={"obj":o,"items":[]};
			c.pointers.menuIndex[c.pointers.menuIndex.length]=i;
			c.pointers.container = t.parent_object;
			return i;
		};
		
		n.addMenuLabel=function(s){
			var t=this,o,c,i;
			c=t.object_config;
			o=document.createElement("span");
			t.style_manager.init(o,c.style_names["menulabel"]);
			o.style.left=c.status.labelWidth + "px";
			/*
org.cote.js.dom.css._gabs_left(c.pointers.container) 			
			*/
/*
					x=org.cote.js.dom.css._gabs_left(m) + (m.offsetWidth - org.cote.js.dom.css._get_offset_client_width(m));
					y=org.cote.js.dom.css._gabs_top(m);
*/
			c.pointers.container.appendChild(o);
			org.cote.js.xml.setInnerXHTML(o,s);
/*
			if(o.offsetWidth < 40) o.style.width="40px";
*/
			c.status.labelWidth+=o.offsetWidth + c.status.labelOffset;
			o.setAttribute("isMenuLabel","1");
			i=org.cote.js._get_gunid();
			o.setAttribute("lid",i);

/*			o.appendChild(document.createTextNode(s));*/
			c.pointers.menus[i]={"obj":o,"items":[]};
			c.pointers.menuIndex[c.pointers.menuIndex.length]=i;

			return i;
		};
		n.addMenuItem=function(i,l,f,b,w,y){
			/*
				i = id
				l = label
				f = function pointer
				b = bool
				w = menu-type
				y = option-value
			*/
			var t=this,c,m,z,o;
			c=t.object_config;
			o=c.pointers.menus[i];
			m=o.items.length;
			if(typeof w!="number") w = 0;
			if(typeof y=="undefined") y = 0;
			o.items[m]={"mid":m,"pid":i,"type":w,"opt":y,"lobj":null,"pobj":0,"sobj":null,"obj":null,"sid":null,"label":l,"fp":f,"issub":(b)?1:0,"items":null,"id":null};
			if(b){
				z=o.items[m];
				z.sid=i;
				z.items=[];
				i=org.cote.js._get_gunid();
				z.id=i;
				c.pointers.menus[i]=z;
				c.pointers.menuIndex[c.pointers.menuIndex.length]=i;

				return i;
			}
			return null;
		};

		n._rebuildMenu=function(i,b){
			/*
				i = id
				b = bool
			*/
			var t=this,c,m,z,a,l,x=0,o;
			c=t.object_config;
			o=c.pointers.menus[i];
			

			a=o.items;
			l=a.length;
			for(;x<l;x++){
				t._createMenuItem(i,x,b);
				/* a[x]  */
			}
		};
		n._createMenuItem=function(i,x,b){
			/*
				i = id
				XXX o = item
				x = item index
				b = bool
			*/
			var t=this,c,m,z,w,s,l,o;
			c=t.object_config;
			o=c.pointers.menus[i].items[x];
/*			
			w=document.createElement("nobr");
*/
			s=document.createElement("div");
			s.setAttribute("isMenuItem","1");
			s.setAttribute("mrid",i);
			s.setAttribute("irid",x);

/*
			l=document.createElement("span");
			org.cote.js.xml.setInnerXHTML(l,o.label);
*/
			org.cote.js.xml.setInnerXHTML(s,o.label);
/*			l.appendChild(document.createTextNode(o.label));*/
/*
			l.setAttribute("isItemHelper","1");
*/
/*
			l.style.border="1px solid #00FF00";
			l.style.width=(z - 15) + "px";
*/
/*
			s.appendChild(l);
*/
/*
			w.appendChild(s);
*/
			if(o.fp!=null) s.setAttribute("fp",o.fp);
			t.style_manager.applyStyle(s,c.style_names["menuitem"]);
/*
			l.style.width=org.cote.js.dom.css._get_offset_client_width(s);
*/
			if(!b){
/*
				l.style.width=org.cote.js.dom.css._get_offset_client_width(c.pointers.activeSub);
*/
				c.pointers.activeSub.appendChild(s);

				/*		this.objects.activeSub.appendChild(_gdocument._createElement("BR"));*/
			}
			else{
/*
				l.style.width=org.cote.js.dom.css._get_offset_client_width(c.pointers.menus[i].sobj);
*/
				c.pointers.menus[i].sobj.appendChild(s);
				/*		this.objects.menus[sID].sobj.appendChild(_gdocument._createElement("BR"));*/
			}


			if(o.issub){
				z=document.createElement("span");
				z.setAttribute("isItemHelper","1");
/*
				z.setAttribute("width","5");
				z.setAttribute("height","9");
*/
				z.setAttribute("border","0");
				s.appendChild(z);
/*
				z.src=t.style_manager.getStyle("backgroundImage",c.style_names["subarrow"]);
*/
				s.setAttribute("srid",o.id);
				s.setAttribute("hasSub","1");
				t.style_manager.applyStyle(z,c.style_names["subarrow"]);

				o.pobj=z;
				o.obj=s;
			}
		};
		n._buildSub=function(i){
			this._activateMenu(i,1);
		};

		n._activateMenu=function(i,b){
			var t=this,c,o,k,l,m,x=0,y=0,p=org.cote.js.dom.browser.client.bswitch;
			c = t.object_config;
			if(c.pointers.menus[i]){
				if(!b && c.pointers.activeSub!=null){
					c.pointers.container.removeChild(c.pointers.activeSub);
				}
				o=document.createElement("span");
				t.style_manager.applyStyle(o,c.style_names["submenu"]);
				o.setAttribute("sid",i);
				if(!b){
					c.pointers.activeSub=o;
					c.status.activeId=i;
				}
				else{

					l=c.pointers.openSubs.length;
					/* Close any subs up to the menu parent */

					t._closeSubs(c.pointers.menus[i].sid,0);

					c.pointers.openSubs[l]={"id":i,"obj":o};
					c.pointers.menus[i].sobj=o;
				}

	
				m=c.pointers.menus[i].obj;
				if(!b){
					if(c.status.menu_type==1){
						y=m.offsetTop + m.offsetHeight;
						x=m.offsetLeft;
					}
					else if(c.status.menu_type==2){
						y=c.status.client_y + (p==1?document.documentElement.scrollTop:document.body.scrollTop);
						x=c.status.client_x + (p==1?document.documentElement.scrollLeft:document.body.scrollLeft);
					}
					t._rebuildMenu(i);
				}
				else{
					if(c.status.menu_type==1){
						y=org.cote.js.dom.css._gabs_top(m) - org.cote.js.dom.css._gabs_top(c.pointers.container);
						x = (org.cote.js.dom.css._gabs_left(m) + m.offsetWidth) - org.cote.js.dom.css._gabs_left(c.pointers.container);
					}
					else if(c.status.menu_type==2){
						y=org.cote.js.dom.css._gabs_top(m);
						x=m.offsetWidth + org.cote.js.dom.css._gabs_left(m);
					}

					t._rebuildMenu(i,1);
				}
				o.style.left=x + "px";
				o.style.top=y + "px";
				c.pointers.container.appendChild(o);
			}
			else{
				alert("BAD BAD BAD");
			}
		};

		
		n._closeMenu=function(){
			var t=this,c;
			c = t.object_config;
			
			if(c.pointers.container != null){
				org.cote.js.dom.event.disableMotionCapture(c.pointers.container);
			}
			c.status.isActive=false;
			c.status.activeId=null;
			if(c.pointers.activeSub!=null){
				c.pointers.container.removeChild(c.pointers.activeSub);
				c.pointers.activeSub=null;
			}
			if(c.pointers.activeLabel!=null){
				t.style_manager.applyStyle(c.pointers.activeLabel,c.style_names["menulabel"]);
				c.pointers.activeLabel=null;
			}
			t._closeSubs();
		};

		n._closeSubs=function(i,s){
			var t=this,c,a,l,r,b=0,o,z,y;
			c = t.object_config;
			a = c.pointers.openSubs;
			l = a.length;
			r = [];

			for(x=l-1;x>=0;x--){
				if(a[x]!=null){
					if(a[x].id==i){
						b=1;
						break;
					}
					o=a[x].obj;

					if(!s && c.pointers.menus[a[x].id].obj.getAttribute("isStuck")){

						z = c.pointers.menus[a[x].id].obj;
						t.style_manager.applyStyle(z,c.style_names["menuitem"]);
						y = c.pointers.menus[z.getAttribute("mrid")].items[z.getAttribute("irid")];
						if(typeof y!="undefined" && y.pobj){

							t.style_manager.applyStyle(y.pobj,c.style_names["subarrow"]);
						}
					}
					if(o && o.parentNode) o.parentNode.removeChild(o);
					a[x]=null;
				}
			}
	
			for(x=0;x<l;x++){
				if(a[x]!=null){
					r[x]=a[x];
				}
			}

			c.pointers.openSubs=r;
		};
		
		
		
		n._handle_mouseover=function(v){
			var t=this,o,c,e,s,i,z,y;
			c=t.object_config;

			e=org.cote.js.dom.event._gevt(v);
			o=(typeof v!="undefined" && v.nodeType)?v:org.cote.js.dom.event._gevt_src(v);

			if(o && o.nodeType==3) o=o.parentNode;

			if(o==null) return;
			
			z=org.cote.js.dom.event._gevt_org(e);
			if(typeof z!="undefined" && z.nodeType==3) z = z.parentNode;
			if(o.getAttribute("isItemHelper")) o=o.parentNode;
			if(z && z.getAttribute("isItemHelper")) z=z.parentNode;
			
			if(o.getAttribute("isMenuContext")=="1"){
/*
				alert('cm');
*/
			}
			if(o.getAttribute("isMenuItem")=="1"){
				if(z==o) return false;	
				if(
					c.pointers.activeItem!=null
					&& c.pointers.activeItem!=o
					&& c.pointers.activeItem.getAttribute("isStuck") != "1"
				){
					t.style_manager.applyStyle(c.pointers.activeItem,c.style_names["menuitem"]);
/*
					y = c.pointers.menus[c.pointers.activeItem.getAttribute("mrid")].items[c.pointers.activeItem.getAttribute("irid")];
					if(typeof y!="undefined" && y.pobj){
						t.logger.log("Reset sub arrow","200.1");
						t.style_manager.applyStyle(y.pobj,c.style_names["subarrow"]);
					}
*/
				}
				s="menuitemhover";
				t.style_manager.applyStyle(o,c.style_names[s]);

				y = c.pointers.menus[o.getAttribute("mrid")].items[o.getAttribute("irid")];
				if(typeof y!="undefined" && y.pobj){
					t.style_manager.applyStyle(y.pobj,c.style_names["subarrowhover"]);
				}

				if(o.getAttribute("hasSub")=="1"){
					o.setAttribute("isStuck","1");
					t._buildSub(o.getAttribute("srid"));
				}
				else{

					t._closeSubs(o.getAttribute("mrid"));
				}
				c.pointers.activeItem=o;
			}
			if(o.getAttribute("isMenuLabel")=="1"){
				if(c.pointers.activeLabel!=null && c.pointers.activeLabel!=o){
					t.style_manager.applyStyle(c.pointers.activeLabel,c.style_names["menulabel"]);
				}
				c.pointers.activeLabel=o;
				s=(c.status.isActive)?"menulabelactive":"menulabelhover";
				t.style_manager.applyStyle(o,c.style_names[s]);
				i=o.getAttribute("lid");
				if(c.status.isActive && c.status.activeId!=i){

					t._closeSubs();
					this._activateMenu(i);
				}
			}
			org.cote.js.dom.event._gevt_cancel(e);
			return false;
			
		};

		n._handle_mouseout=function(v){
			var t=this,o,c,e,z,y;
			c=t.object_config;
			e=org.cote.js.dom.event._gevt(v);
			o=(typeof v!="undefined" && v.nodeType)?v:org.cote.js.dom.event._gevt_src(v);
			
			z=org.cote.js.dom.event._gevt_dest(e);
			
			if(o && o.nodeType==3) o=o.parentNode;
			if(z && z.nodeType==3) z = z.parentNode;

			if(o.getAttribute("isItemHelper")) o=o.parentNode;
			if(z && z.getAttribute("isItemHelper")) z=z.parentNode;

			if(o.getAttribute("isMenuLabel")=="1" && !c.status.isActive){
				c.pointers.activeLabel=null;
				t.style_manager.applyStyle(o,c.style_names["menulabel"]);
			}
			if(
				o.getAttribute("isMenuItem")=="1"
				&& o.getAttribute("isStuck")!="1"
			){
				if(z==o) return;
				c.pointers.activeItem=null;
				t.style_manager.applyStyle(o,c.style_names["menuitem"]);
/*
				y = c.pointers.menus[o.getAttribute("mrid")].items[o.getAttribute("irid")];
				if(typeof y!="undefined" && y.pobj){

					t.style_manager.applyStyle(y.pobj,c.style_names["subarrow"]);
				}
*/
			}
			org.cote.js.dom.event._gevt_cancel(e);
			return false;
		};

		n._handle_contextmenu=function(v,b){
			n._handle_click(v,b);
			org.cote.js.dom.event._gevt_cancel(v);
		};
		n._handle_click=function(v,b){
			var t=this,o,c,e,f;
			c=t.object_config;


			e=org.cote.js.dom.event._gevt(v);
			o=(typeof v!="undefined" && v.nodeType)?v:org.cote.js.dom.event._gevt_src(v);

			if(o && o.nodeType==3) o=o.parentNode;
			if(o.getAttribute("isItemHelper")) o=o.parentNode;


		
			if(o.getAttribute("isMenuItem")=="1"){
				f=o.getAttribute("fp");
				t._closeMenu();
				if(f!=null){
					try{
						if(typeof f=="string") eval(f);
						if(typeof f=="function") f();
					}
					catch(e){
						t.ms.sendMessage(e.toString(),"515.4");
					}

				}
				org.cote.js.message.MessageService.publish("onmenuitemclick",{fp:f,id:o.getAttribute("mrid") + ":" + o.getAttribute("irid"),label:org.cote.js.xml.getInnerText(o)});
				
			}
			else if(o.getAttribute("isMenuLabel")=="1" || o.getAttribute("isMenuContext")=="1"){
				if(!c.status.isActive){
					c.status.isActive=1;
					if(o.getAttribute("isMenuLabel")=="1"){
						t.style_manager.applyStyle(o,c.style_names["menulabelactive"]);
					}
					if(o.getAttribute("isMenuContext")=="1"){
						c.status.client_x = e.clientX;
						c.status.client_y = e.clientY;
					}
					t._activateMenu(o.getAttribute("lid"));
					org.cote.js.dom.event.enableMotionCapture(c.pointers.container);
				}
				else{
					c.status.isActive=false;
					c.status.activeId=null;
					if(c.pointers.activeSub!=null){
						c.pointers.container.removeChild(c.pointers.activeSub);
						c.pointers.activeSub=null;
						t._closeSubs();
					}
					if(o.getAttribute("isMenuLabel")=="1") t.style_manager.applyStyle(o,c.style_names["menulabelhover"]);
				}
			}
			else{
				t._closeMenu();
			}
		};
		
		n.loadMenu=function(p,i){
			var t=this,a,m,_t = org.cote.js.task.TaskService,_a = org.cote.js.util.driver.ApplicationDriver;
			if(typeof _a.application_config._menu_load_count != "number") _a.application_config._menu_load_count = 0;

			a = "menu_" + i + "_" + (++_a.application_config._menu_load_count);
			t.object_config.status.src_path = p;
			t.object_config.status.src_request_id = a;
			t.object_config.status.src_menu_id = i;
			m=_t.execTask(
				a,
				"xml",
				p,
				t._prehandle_load_menu,
				"function"
			);
			
					
		};
		n._handle_load_menu=function(s,v){
			var t=this,m,a,o,n,_x = org.cote.js.xml,c,h,l;
			c = t.object_config;
			if(typeof v.obj == "object" && v.obj.documentElement != null){
				m = _x.queryNode(v.obj,"menu-config","menus","id",c.status.src_menu_id);
				if(m!=null){
					a = _x.queryNodes(m,"menu");
					for(h=0;h<a.length;h++){
						o = a[h];

						n = o.getAttribute("label");
						if(typeof n != "string" || n.length == 0) n = "[ undefined ]";
						l = t.addMenuLabel(n);
						t._parse_menu_config(o,l);

					}
				}
			}
		};
		n._parse_menu_config=function(o,l){
			var t=this,_x = org.cote.js.xml,c,a,i,n,b,y,z,d;
			c = t.object_config;
			
			a = o.childNodes;
			for(i=0;i<a.length;i++){
				n = a[i];
				if(n.nodeType == 1 && n.nodeName == "menu-item"){
					b = (_x.queryNode(n,"sub-menu") != null);
					y = n.getAttribute("label");
					z = n.getAttribute("action");
					if(typeof y != "string" || y.length == 0) y = "[ undefined ]";
					if(typeof z != "string" || z.length == 0) z = null;
					
					d=t.addMenuItem(l,y,z,b);
					if(b) t._parse_menu_config(_x.queryNode(n,"sub-menu"),d);
				}
			}
			
		};		
		n._init();


		return n;
	}
}


