/*! Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Version: 1.1.0-pre
* Requires jQuery 1.3+
* Docs: http://docs.jquery.com/Plugins/livequery
*/

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