(function($) {
$.extend($.fn, {
	live: function(type, fn, fn2) {
		var self = this, q;
		if ($.isFunction(type))
			fn2 = fn, fn = type, type = undefined;
		$.each( $.live.queries, function(i, query) {
			if ( self.selector == query.selector && self.context == query.context &&
				type == query.type && (!fn || fn.$lqguid == query.fn.$lqguid) && (!fn2 || fn2.$lqguid == query.fn2.$lqguid) )
					return (q = query) && false;
		});
		q = q || new $.live(this.selector, this.context, type, fn, fn2);
		q.stopped = false;
		q.run();
		return this;
	},
	expire: function(type, fn, fn2) {
		var self = this;
		if ($.isFunction(type))
			fn2 = fn, fn = type, type = undefined;
		$.each( $.live.queries, function(i, query) {
			if ( self.selector == query.selector && self.context == query.context && 
				(!type || type == query.type) && (!fn || fn.$lqguid == query.fn.$lqguid) && (!fn2 || fn2.$lqguid == query.fn2.$lqguid) && !this.stopped )
					$.live.stop(query.id);
		});
		return this;
	}
});
$.live = function(selector, context, type, fn, fn2) {
	this.selector = selector;
	this.context  = context || document;
	this.type     = type;
	this.fn       = fn;
	this.fn2      = fn2;
	this.elements = [];
	this.stopped  = false;
	this.id = $.live.queries.push(this)-1;
	fn.$lqguid = fn.$lqguid || $.live.guid++;
	if (fn2) fn2.$lqguid = fn2.$lqguid || $.live.guid++;
	return this;
};
$.live.prototype = {
	stop: function() {
		var query = this;
		if ( this.type )
			this.elements.unbind(this.type, this.fn);
		else if (this.fn2)
			this.elements.each(function(i, el) {
				query.fn2.apply(el);
			});
		this.elements = [];
		this.stopped = true;
	},
	run: function() {
		if ( this.stopped ) return;
		var query = this;
		var oEls = this.elements,
			els  = $(this.selector, this.context),
			nEls = els.not(oEls);
		this.elements = els;
		if (this.type) {
			nEls.bind(this.type, this.fn);
			if (oEls.length > 0)
				$.each(oEls, function(i, el) {
					if ( $.inArray(el, els) < 0 )
						$.event.remove(el, query.type, query.fn);
				});
		} else {
			nEls.each(function() {
				query.fn.apply(this);
			});
			if ( this.fn2 && oEls.length > 0 )
				$.each(oEls, function(i, el) {
					if ( $.inArray(el, els) < 0 )
						query.fn2.apply(el);
				});
		}
	}
};
$.extend($.live, {
	guid: 0,
	queries: [],
	queue: [],
	running: false,
	timeout: null,
	checkQueue: function() {
		if ( $.live.running && $.live.queue.length ) {
			var length = $.live.queue.length;
			while ( length-- )
				$.live.queries[ $.live.queue.shift() ].run();
		}
	},
	pause: function() {
		$.live.running = false;
	},
	play: function() {
		$.live.running = true;
		$.live.run();
	},
	registerPlugin: function() {
		$.each( arguments, function(i,n) {
			if (!$.fn[n]) return;
			var old = $.fn[n];
			$.fn[n] = function() {
				var r = old.apply(this, arguments);
				$.live.run();
				return r;
			}
		});
	},
	run: function(id) {
		if (id != undefined) {
			if ( $.inArray(id, $.live.queue) < 0 )
				$.live.queue.push( id );
		}
		else
			$.each( $.live.queries, function(id) {
				if ( $.inArray(id, $.live.queue) < 0 )
					$.live.queue.push( id );
			});
		if ($.live.timeout) clearTimeout($.live.timeout);
		$.live.timeout = setTimeout($.live.checkQueue, 20);
	},
	stop: function(id) {
		if (id != undefined)
			$.live.queries[ id ].stop();
		else
			$.each( $.live.queries, function(id) {
				$.live.queries[ id ].stop();
			});
	}
});
$.live.registerPlugin('append', 'prepend', 'after', 'before', 'wrap', 'attr', 'removeAttr', 'addClass', 'removeClass', 'toggleClass', 'empty', 'remove');
$(function() { $.live.play(); });
var init = $.prototype.init;
$.prototype.init = function(a,c) {
	var r = init.apply(this, arguments);
	if (a && a.selector)
		r.context = a.context, r.selector = a.selector;
	if ( typeof a == 'string' )
		r.context = c || document, r.selector = a;
	return r;
};
$.prototype.init.prototype = $.prototype;
})(jQuery);
