/*
	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");
/*
	package org.cote.js
*/
org.cote.js={
	class_imports:{},
	_guid_counter:0
};

org.cote.js._get_gunid=function(){
	var d=new Date(),t,i=4,r,l,x=0;
	t=new String(d.getTime());
/*				t=t.substring(t.length-7,t.length);*/
	r=new String(parseInt(Math.random() * (1000 * i)));
	l=r.length;
	for(;x <i - l;x++) r="0" + r;
	return ( "swc-" + (++org.cote.js._guid_counter) + "-" + t + "-" + r);
}

/*
	The _application_scope is used as a runtime registry
	of objects.
	
	Effectively it creates a repository of uniquely identified objects.
	
	It is up to a class to register/deregister with the scope.
*/
org.cote.js._application_scope={
	_objects:[],
	_objects_index:[],

	add_object:function(o){
		var t = this,i=o.object_id,m;
		
		if(typeof t._objects_index[i] != "object"){
			m = t._objects.length;
			t._objects[m]=o;
			t._objects_index[i]=m;
			return true;
		}
		return false;
	},


	/*
		this will just null out the value.
		An array element will still exist here
	*/
	remove_object:function(o){
		var t = this,i=o.object_id,m;
		
		if(typeof t._objects_index[i] == "number"){
			m = t._objects_index[i];
			t._objects[m]=null;
			t._objects_index[i]=null;
			return true;
		}
		return false;
	},
	get_object:function(i){
		var t = this;
		
		if(typeof t._objects_index[i] == "number"){
			return t._objects[t._objects_index[i]];
		}
		return null;
	},
	object_exists:function(i){
		var t = this;
		
		if(typeof t._objects_index[i] == "number"){
			return true;
		}
		return false;
	},
	_send_sigterm:function(){
		var t = this,o,i;
		for(i=0;i<t._objects.length;i++){
			o = t._objects[i];
			if(o!=null && o.ready_state == 4 && typeof o.sigterm == "function"){
				o.sigterm();
			}
		}
	}
};



org.cote.js._implements=function(o,s){
	var _js = org.cote.js,v,a=arguments,i;

	/*
		big hack here, but I don't want to include o, I don't want it to be an eval,
		and apply requires the argArray param to be the arguments object
	*/
	
	var n_a = [];
	for(i=1;i<a.length;i++)
		n_a[n_a.length] = a[i];
	
	/*
		According to the references, this should break because n_a is not the arguments object
	*/
	v = _js._forName.apply(this.caller,n_a);
	
	if(
		typeof o == "object"
		&&
		typeof v == "object"
	){
		for(i in v)
			o[i]=v[i];
		
	}
};

org.cote.js._forName=function(s){
	var _js=org.cote.js,v,a=arguments;
	if(typeof _js.class_imports[s]=="string"){
		eval("v=" + _js.class_imports[s]);
		return v;
	}
	switch(s){

		case "bootstrap_object":
		case "task_object":
			return {
				state:0,
				handled:0,
				name:a[1],
				type:a[2],
				src:a[3],
				handler:a[4],
				handler_type:a[5],
				id:a[6],
				index:a[7],
				data:0,
				depends:[],
				executed:0,
				busy:0
			};
			break;
/*
		case "event_object":
			return {
				event_name:a[1],
				target:a[2]
			};
			break;

		case "event_listener":
			return {
				object:a[1],
				event_name:a[2],
				function_pointer:a[3],
				target:a[4]
			};
			break;
*/
		case "base_object":
			return{
				object_id:org.cote.js._get_gunid(),
				object_type:a[1],
				object_version:a[2],
				ready_state:0
			};
			break;

		case "basic_message":
			return {
				entry:a[1],
				index:-1,
				data:a[2],
				time:new Date(),
				id:_js._get_gunid()
			};
			break;
		case "basic_message_subscription":
			return {
				object:a[1],
				subscription_name:a[2],
				handler:a[3],
				target:a[4]
			};
			break;
		case "generic_object":
			return {
				parent_object:0,
				child_objects:[],
				child_objects_index:{},
				ready_state:0,
				top_object:0,
				layout_manager:0,
				style_manager:0,
				object_debug:function(){
					alert(this.object_id);
				},
				object_config:{
					pointers:{},
					style_names:{},
					event_handlers:[],
					internal_event_listeners:[],
					dom_event_listeners:[]
				}
			};
			
			break;
		default:
			alert("skip: " + s);
			break;
/*
		case "basic_message_publication":
			return {
				subscription_name:a[1],
				data:a[2]
			};
			break;
*/
	}
	return null;
}

org.cote.js._implements(org.cote.js._application_scope,"base_object","application_scope","0.1");
org.cote.js._application_scope.add_object(org.cote.js._application_scope);

		
		
			

