(function($)
{	
	// event manager
	$.extend({
		eventManager:{
			// variables
			bound:{},
			
			// methods			
			set:function(config)
			{
				config = !config.length ? Array(config) : config;
				for(var n = 0; n < config.length; n++)
				{
					var current	= config[n];
					current.obj.each(
						function()
						{
							var seed 	= $.eventManager.seed();			
							current.obj	= $(this);
							current.obj.attr({seed:seed});							
							$.eventManager.bound[seed] = current;
							
							// distribute seed to children
							if(current.dive)
							{
								$(this).children().attr({seed:seed})
							}							
						}
					)			
				}
			},
			
			remove:function(obj, type)
			{
				obj.unbind(type);
			},	
			
			seed:function()
			{
				return Math.floor(Math.random()*9999);
			},	
			
			trigger:function(e)
			{
				var target = $(e.target);
				if(target.attr("seed"))
				{
					var seed	= target.attr("seed");
					var obj		= this.bound[seed];
					if(e.type == obj.type)
					{
						switch(obj.type)
						{
							case "click":
								return this.clicked(seed, obj);
							break;
							case "change":
								return this.changed(seed, obj);						
							break;
						}						
					}
				}
				return true;
			},
			
			clicked:function(seed, obj)
			{		
				// check if there is a 'this' reference in the callback
				var regex	= /\b(this)\b/g
				if(obj.callback.match(regex))
				{
					var selector	= "$('[seed=" + seed + "]');";
					obj.callback = obj.callback.replace(regex, "\"" + selector + "\"");
				}
				if(obj.callback.match(/\d+/g) && obj.callback.match(/[$]/g)){obj.callback = obj.callback.replace(/\d+/g, seed);}
				!obj.data ? eval(obj.callback) : eval(obj.callback).apply(obj.callback, obj.data);	
				return false;
			},
			
			changed:function(seed, obj)
			{
				var regex	= /\b(this)\b/g
				if(obj.callback.match(regex))
				{
					var selector	= "$('[seed=" + seed + "]');";
					obj.callback = obj.callback.replace(regex, "\"" + selector + "\"");
				}
				if(obj.callback.match(/\d+/g) && obj.callback.match(/[$]/g)){obj.callback = obj.callback.replace(/\d+/g, seed);}
				!obj.data ? eval(obj.callback) : eval(obj.callback).apply(obj.callback, obj.data);	
				return false;		
			}		
		}
	})
	
	// run it and pass it in!
	$(document).bind(
		'click change',
		function(e)
		{
			return $.eventManager.trigger(e);
		}
	)	
})(jQuery)
