/*
 * jquery.subscribe.1.2.min
 * 
 * Implementation of publish/subcription framework for jQuery
 * Requires use of jQuery. Tested with jQuery 1.3 and above
 *
 *
 * Copyright (c) 2008 Eric Chijioke (obinna a-t g mail dot c o m)
 *
 * 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 */
(function(a){_subscribe_topics={};_subscribe_handlers={};_subscribe_getDocumentWindow=function(b){return b.parentWindow||b.defaultView};a.fn.extend({createTopic:function(b){if(b&&!_subscribe_topics[b]){_subscribe_topics[b]={};_subscribe_topics[b].objects={};_subscribe_topics[b].objects.__noId__=[]}return this},destroyTopic:function(c){if(c&&_subscribe_topics[c]){for(i in _subscribe_topics[c].objects){var b=_subscribe_topics[c].objects[i];if(a.isArray(b)){if(b.length>0){for(j in b){if(typeof(b[j])!="function"){b[j].unbind(c)}}}}else{b.unbind(c,data)}}}delete _subscribe_topics[c];return this},subscribe:function(c,e,h){if(this[0]&&c&&e){this.createTopic(c);if(this.attr("id")){_subscribe_topics[c].objects[this.attr("id")]=this}else{var k=_subscribe_topics[c].objects.__noId__;if(this[0].nodeType==9){for(var b in k){var g=k[b];if(typeof(g)!="function"&&g[0].nodeType==9&&_subscribe_getDocumentWindow(this[0]).frameElement==_subscribe_getDocumentWindow(g[0]).frameElement){return this}}}var f=false;for(var d=0;d<k.length;d++){if(k[d]==this){f=true;break}}if(!f){_subscribe_topics[c].objects.__noId__.push(this)}}if(typeof(e)=="function"){this.bind(c,h,e)}else{if(typeof(e)=="string"&&typeof(_subscribe_handlers[e])=="function"){this.bind(c,h,_subscribe_handlers[e])}}}return this},unsubscribe:function(c){if(c){if(_subscribe_topics[c]){if(this.attr("id")){var b=_subscribe_topics[c].objects[this.attr("id")];if(b){delete _subscribe_topics[c].objects[this.attr("id")]}}else{var e=_subscribe_topics[c].objects.__noId__;for(var d=0;d<e.length;d++){if(typeof(noIdObject)!="function"&&e[d]==this){subscribe_topics[c].objects.__noId__.splice(index,1);break}}}}this.unbind(c)}return this},isSubscribed:function(c){if(c){if(_subscribe_topics[c]){if(this.attr("id")){var b=_subscribe_topics[c].objects[this.attr("id")];if(b){return true}}else{var e=_subscribe_topics[c].objects.__noId__;for(var d=0;d<e.length;d++){if(typeof(noIdObject)!="function"&&e[d]==this){return true}}}}}return false},publish:function(d,g,b){if(d){this.createTopic(d);var e=function(){this.isImmediatePropagationStopped=function(){return true};this.isPropagationStopped=function(){return true};if(this.originalEvent){this.originalEvent.isImmediatePropagationStopped=function(){return true};this.originalEvent.stopPropagation=e}};var f=jQuery.Event(d);a.extend(f,{originalEvent:b,stopPropagation:e});for(i in _subscribe_topics[d].objects){var c=_subscribe_topics[d].objects[i];if(a.isArray(c)){if(c.length>0){for(j in c){if(typeof(c[j])!="function"){c[j].trigger(f,g)}}}}else{c.trigger(f,g)}}}return this},publishOnEvent:function(c,b,d){if(c&&b){this.createTopic(b);this.bind(c,d,function(f){a(this).publish(b,f.data,f)})}return this}});a.extend({subscribe:function(b,c,d){return a(document).subscribe(b,c,d)},unsubscribe:function(b,c,d){return a(document).unsubscribe(b,c,d)},subscribeHandler:function(b,c){if(b&&c&&typeof(c)=="function"){_subscribe_handlers[b]=c}return a(document)},publish:function(b,c){return a(document).publish(b,c)},createTopic:function(b){return a(document).createTopic(b)},destroyTopic:function(b){return a(document).destroyTopic(b)}})})(jQuery);