/*
	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");

org.cote.js.ui.object.resultset={
	newInstance:function(parent_object){
		/*
			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_resultset","0.1b");
		
		if(typeof parent_object!="object") parent_object = document.body;
		n.parent_object = parent_object;
	
		n._init=function(){
			var t=this,v,d,_m=org.cote.js.message.MessageService,_d=org.cote.js.dom.event,c;
			c = t.object_config;
			t.style_manager = org.cote.js.dom.css.StyleManager.newInstance(t);
			v = t._load_style_names(c.style_name_id);
			
			d = document.createElement("div");
			t.parent_object.appendChild(d);
			t.style_manager.init(d,c.style_names["resultContainer"]);
			
			_d.addEventListener(d,"mouseover",t._prehandle_mouseover,0);
			_d.addEventListener(d,"mouseout",t._prehandle_mouseout,0);
			_d.addEventListener(d,"click",t._prehandle_click,0);

			_m.subscribe(t,"destroy","_handle_window_destroy",window);
			_m.subscribe(t,"_buffer_updated","_handle_buffer_updated",t);
			c.pointers.container = d;
			
			/*
			buffer_check is not used with the eventfactory
			but the handler is used to retrieve a pointer from the anonymous call
			of a timeout.
			
			 Yet another case where a probe would be nice.
			*/
			t._create_handler("buffer_check",0,0,1);
			t._create_handler("commit_buffer",0,0,1);			
			t.ready_state = 4;
		};
		
		n._handle_window_destroy=function(){
			this.destroy();
		};
		
		n.setIsBuffered=function(b){
			var t=this,c;
			c=t.object_config;
			c.status.buffered=b;
			c.status.buffer_mark=0;
			c.pointers.buffer=[];
		};
		
		n.setBufferOffset=function(i){
			this.object_config.status.buffer_step=i;
		};
		
		n.setBufferDelay=function(i){
			this.object_config.status.buffer_delay=i;
		};

		n.destroy=function(){
			var t=this,c;
			c=t.object_config;
			if(t.ready_state == 4){
				t.clearItems();
				c.pointers.buffer=[];
				/*			alert(t.parent_object + " / " + c.pointers.container);*/
				t.parent_object.removeChild(c.pointers.container);
				org.cote.js._application_scope.remove_object(t);
			}
			t.ready_state = 0;
		};

		n.addItem=function(s,d){
			var t=this,c;
			c=t.object_config;
			if(t.ready_state != 4) return;
			if(c.status.buffered){
				c.pointers.buffer[c.pointers.buffer.length]={"name":s,"data":d};

				if(c.status.auto_commit && !c.status.is_commit){
				
					/* this should be a probe */
					window.setTimeout(t._prehandle_buffer_check,c.status.buffer_delay);
					/* t.commitBuffer();*/
				}
			}
			else{
				t._addItem(s,d);
			}
		};
		
		n._handle_buffer_check=function(){
			var t=this,c;
			c=t.object_config;
			if(c.status.auto_commit && !c.status.is_commit){
				t._handle_commit_buffer();
			}
		};
		
		n.commitBuffer=function(){
			this._handle_commit_buffer();
		};
		
		n._handle_commit_buffer=function(){
			var t=this,c,l,b,m,i,v;
			c = t.object_config;

			c.status.is_commit=1;
			
			l=c.pointers.buffer.length;
			b = false;
			m = c.status.buffer_mark;
			for(i=m;i< (m + c.status.buffer_step) ;i++){
				if(i >= l){
					b=true;
					break;
				}
				v=c.pointers.buffer[i];
				t._addItem(v.name,v.data);
				c.status.buffer_mark++;
			}
			if(b){
				c.status.buffer_mark=0;
				c.pointers.buffer=[];
				c.status.is_commit=0;
				org.cote.js.message.MessageService.publish("onbuffercommitted",t);
			}
			else{
				window.setTimeout(t._prehandle_commit_buffer,c.status.buffer_delay);
			}
		};
		
		n.getBufferMark=function(){
			return this.object_config.status.buffer_mark;
		};
		
		n.getBufferSize=function(){
			return this.object_config.pointers.buffer.length;
		};
		
		n.getItemSize=function(){
			return this.object_config.pointers.results.length;
		};
		
		n.getItem=function(i){
			return this.object_config.pointers.results[i];
		};
		
		n.getActiveItem=function(){
			var c=this.object_config;
			if(c.status.activeItem){
				return c.pointers.results[c.pointers.resultsHash[c.status.active_item.getAttribute("rid")]]
			}
			return null;
		};
		
		n._addItem=function(s,d){
			var t=this,c,e,i=org.cote.js._get_gunid(),v,l;
			c=t.object_config;
			l=c.pointers.results.length;
			
			if(
				c.status.maximum_items > 0
				&& l >= c.status.maximum_items
			){
				if(c.status.maximum_rollover){
					t.clearItem(0);
					l--;
				}
				else{
					return;
				}
			}
			
			e=document.createElement("div");
		
			c.pointers.container.appendChild(e);
			t.style_manager.init(e,c.style_names["resultItem"]);

/*			e.style.width=c.pointers.container.offsetWidth;*/


/* - (org.cote.js.dom.css._get_offset_client_width(e) + org.cote.js.dom.css._get_offset_client_width(c.pointers.container))*/			

			if(d!="_avoid"){
				e.setAttribute("is-result-item","1");
			}
			e.setAttribute("rid",i);
			e.appendChild(document.createTextNode(s));


			v={"name":s,"data":d,"obj":e,"id":i,"index":l};
			
			c.pointers.results[l]=v;
			c.pointers.resultsHash[i]=l;
			
			if(c.status.auto_select) t.setActiveItem(l);
			if(c.status.auto_scroll) t.scrollToItem(l);
		};

		
		n._handle_mouseover=function(v){
			var t=this,o,c,e;
			c=t.object_config;
			if(typeof v=="number"){
				o=c.pointers.results[v].obj;
			}
			else{
				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("is-result-item") && o != c.status.active_item){
				t.style_manager.applyStyle(o,c.style_names["resultItemHover"]);
			}
		};

		n._handle_mouseout=function(v){
			var t=this,o,c,e;
			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("is-result-item") && o != c.status.active_item){
				t.style_manager.applyStyle(o,c.style_names["resultItem"]);
			}
		};


		n.setActiveItem=function(i){
			this.selectItem(i,1);
		};
		
		n.selectItem=function(i,b){
			var t = this,c;
			c=t.object_config;
			if(c.pointers.results[i]!=null){
				t._handle_mouseover(i);
				t._handle_click(i,b);
				t.scrollToItem(i);
			}
		};
		
		n.scrollToItem=function(i){
			var t = this,c;
			c=t.object_config;
			if(c.pointers.results[i]!=null){
				c.pointers.container.scrollTop=c.pointers.results[i].obj.offsetTop;
			}
		};
		
		n._handle_click=function(v,b){
			var t=this,o,c,e;
			c=t.object_config;

			if(typeof(v)=="number"){
				o=c.pointers.results[v].obj;
			}
			else{
				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("is-result-item")){
				if(o == c.status.active_item){
					c.status.active_item=null;
					t.style_manager.applyStyle(o,c.style_names["resultItemHover"]);
					return;
				}
				if(c.status.active_item!=null){
					t.style_manager.applyStyle(c.status.active_item,c.style_names["resultItem"]);
				}
				c.status.active_item=o;
				t.style_manager.applyStyle(o,c.style_names["resultItemActive"]);
				if(!b) org.cote.js.message.MessageService.publish("onresultclick",t);
			}
		};
		
		n.clearItem=function(i){
			var t=this,o,c,d;
			c=t.object_config;
			o=c.pointers.results[i];
			if(o){
				d=o.id;
				if(o.obj==c.status.active_item) c.status.active_item=null;
				c.pointers.resultsHash[d]=null;

				o=o.obj;
				c.pointers.container.removeChild(o);
				c.pointers.results.splice(i,1);
				/*
				c.pointers.results.pull(i);
				c.pointers.results[i]=null;
				*/
			}

		};

		n.clearItems=function(){
			var t=this,c,a,i;
			c=t.object_config;
			c.status.active_item=null;
			c.pointers.results=[];
			c.pointers.resultsHash=[];
			c.status.buffer_mark=0;
			c.pointers.buffer=[];
			a=c.pointers.container.childNodes;
			for(i=a.length - 1;i >= 0;i--){
				c.pointers.container.removeChild(a[i]);
			}
		};
		
		n._init();

		return n;
	}
}

