/* Copyright (c) 2008 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.0.3
 * Requires jQuery 1.1.3+
 * Docs: http://docs.jquery.com/Plugins/livequery
 */
(function(a){a.extend(a.fn,{livequery:function(b,c,e){var f=this,d;if(a.isFunction(b))e=c,c=b,b=undefined;a.each(a.livequery.queries,function(g,a){if(f.selector==a.selector&&f.context==a.context&&b==a.type&&(!c||c.$lqguid==a.fn.$lqguid)&&(!e||e.$lqguid==a.fn2.$lqguid))return(d=a)&&false});d=d||new a.livequery(this.selector,this.context,b,c,e);d.stopped=false;d.run();return this},expire:function(b,c,d){var e=this;if(a.isFunction(b))d=c,c=b,b=undefined;a.each(a.livequery.queries,function(g,f){e.selector==f.selector&&e.context==f.context&&(!b||b==f.type)&&(!c||c.$lqguid==f.fn.$lqguid)&&(!d||d.$lqguid==f.fn2.$lqguid)&&!this.stopped&&a.livequery.stop(f.id)});return this}});a.livequery=function(d,e,f,c,b){this.selector=d;this.context=e||document;this.type=f;this.fn=c;this.fn2=b;this.elements=[];this.stopped=false;this.id=a.livequery.queries.push(this)-1;c.$lqguid=c.$lqguid||a.livequery.guid++;if(b)b.$lqguid=b.$lqguid||a.livequery.guid++;return this};a.livequery.prototype={stop:function(){var a=this;if(this.type)this.elements.unbind(this.type,this.fn);else this.fn2&&this.elements.each(function(c,b){a.fn2.apply(b)});this.elements=[];this.stopped=true},run:function(){if(this.stopped)return;var c=this,b=this.elements,d=a(this.selector,this.context),e=d.not(b);this.elements=d;if(this.type){e.bind(this.type,this.fn);b.length>0&&a.each(b,function(e,b){a.inArray(b,d)<0&&a.event.remove(b,c.type,c.fn)})}else{e.each(function(){c.fn.apply(this)});this.fn2&&b.length>0&&a.each(b,function(e,b){a.inArray(b,d)<0&&c.fn2.apply(b)})}}};a.extend(a.livequery,{guid:0,queries:[],queue:[],running:false,timeout:null,checkQueue:function(){if(a.livequery.running&&a.livequery.queue.length){var b=a.livequery.queue.length;while(b--)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(d,b){if(!a.fn[b])return;var c=a.fn[b];a.fn[b]=function(){var b=c.apply(this,arguments);a.livequery.run();return b}})},run:function(b){if(b!=undefined)a.inArray(b,a.livequery.queue)<0&&a.livequery.queue.push(b);else a.each(a.livequery.queries,function(b){a.inArray(b,a.livequery.queue)<0&&a.livequery.queue.push(b)});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(b){a.livequery.queries[b].stop()})}});a.livequery.registerPlugin("append","prepend","after","before","wrap","attr","removeAttr","addClass","removeClass","toggleClass","empty","remove");a(function(){a.livequery.play()});var b=a.prototype.init;a.prototype.init=function(a,d){var c=b.apply(this,arguments);if(a&&a.selector)c.context=a.context,c.selector=a.selector;if(typeof a=="string")c.context=d||document,c.selector=a;return c};a.prototype.init.prototype=a.prototype})(jQuery);