/*
 * jquery.subscribe.1.2.2
 * 
 * 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($){if(!window._subscribe_topics){_subscribe_topics={};_subscribe_handlers={};}
_subscribe_getDocumentWindow=function(document){return document.parentWindow||document.defaultView;};$.fn.extend({createTopic:function(topic){if(topic&&!_subscribe_topics[topic]){_subscribe_topics[topic]={};_subscribe_topics[topic].objects={};_subscribe_topics[topic].objects['__noId__']=[];}
return this;},destroyTopic:function(topic){if(topic&&_subscribe_topics[topic]){for(i in _subscribe_topics[topic].objects){var object=_subscribe_topics[topic].objects[i];if($.isArray(object)){if(object.length>0){for(j in object){if(typeof(object[j])!="function"){object[j].unbind(topic);}}}}else{object.unbind(topic,data);}}}
delete _subscribe_topics[topic];return this;},subscribe:function(topic,handler,data,multiple){if(this[0]&&topic&&handler){this.createTopic(topic);if(this.attr('id')){_subscribe_topics[topic].objects[this.attr('id')]=this;}else{var noIdObjects=_subscribe_topics[topic].objects['__noId__'];if(this[0].nodeType==9){for(var index in noIdObjects){var noIdObject=noIdObjects[index];if(typeof(noIdObject)!="function"&&noIdObject[0].nodeType==9&&_subscribe_getDocumentWindow(this[0]).frameElement==_subscribe_getDocumentWindow(noIdObject[0]).frameElement){return this;}}}
var exists=false;for(var i=0;i<noIdObjects.length;i++){if(noIdObjects[i]==this){exists=true;break;}}
if(!exists){_subscribe_topics[topic].objects['__noId__'].push(this);}}
if(true==multiple){if(typeof(handler)=='function'){this.bind(topic,data,handler);}else if(typeof(handler)=='string'&&typeof(_subscribe_handlers[handler])=='function'){this.bind(topic,data,_subscribe_handlers[handler]);}}else{var events=this.data('events');if(events){var eventsTopic=events[topic];if(eventsTopic&&eventsTopic.length>0){this.unbind(topic);}}
if(typeof(handler)=='function'){this.bind(topic,data,handler);}else if(typeof(handler)=='string'&&typeof(_subscribe_handlers[handler])=='function'){this.bind(topic,data,_subscribe_handlers[handler]);}}}
return this;},unsubscribe:function(topic){if(topic){if(_subscribe_topics[topic]){if(this.attr('id')){var object=_subscribe_topics[topic].objects[this.attr('id')];if(object){delete _subscribe_topics[topic].objects[this.attr('id')];}}else{var noIdObjects=_subscribe_topics[topic].objects['__noId__'];for(var i=0;i<noIdObjects.length;i++){if(typeof(noIdObject)!="function"&&noIdObjects[i]==this){subscribe_topics[topic].objects['__noId__'].splice(index,1);break;}}}}
this.unbind(topic);}
return this;},isSubscribed:function(topic){if(topic){if(_subscribe_topics[topic]){if(this.attr('id')){var object=_subscribe_topics[topic].objects[this.attr('id')];if(object){return true;}}else{var noIdObjects=_subscribe_topics[topic].objects['__noId__'];for(var i=0;i<noIdObjects.length;i++){if(typeof(noIdObject)!="function"&&noIdObjects[i]==this){return true;}}}}}
return false;},publish:function(topic,data,originalEvent){if(topic){this.createTopic(topic);var subscriberStopPropagation=function(){this.isImmediatePropagationStopped=function(){return true;};this.isPropagationStopped=function(){return true;};if(this.originalEvent){this.originalEvent.isImmediatePropagationStopped=function(){return true;};this.originalEvent.stopPropagation=subscriberStopPropagation;}};var event=jQuery.Event(topic);$.extend(event,{originalEvent:originalEvent,stopPropagation:subscriberStopPropagation});for(i in _subscribe_topics[topic].objects){var object=_subscribe_topics[topic].objects[i];if($.isArray(object)){if(object.length>0){for(j in object){if(typeof(object[j])!="function"){object[j].trigger(event,data);}}}}else{object.trigger(event,data);}}}
return this;},publishOnEvent:function(event,topic,data){if(event&&topic){this.createTopic(topic);this.bind(event,data,function(e){$(this).publish(topic,e.data,e);});}
return this;}});$.extend({subscribe:function(topic,handler,data){return $(document).subscribe(topic,handler,data);},unsubscribe:function(topic,handler,data){return $(document).unsubscribe(topic,handler,data);},subscribeHandler:function(name,handler){if(name&&handler&&typeof(handler)=="function"){_subscribe_handlers[name]=handler;}
return $(document);},publish:function(topic,data){return $(document).publish(topic,data);},createTopic:function(topic){return $(document).createTopic(topic);},destroyTopic:function(topic){return $(document).destroyTopic(topic);}});})(jQuery);
