/*
 * jquery.subscribe.1.1
 * 
 * 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
 *
 *  Release Notes:
 *  
 *  version 1.1:
 *  
 *  Fixed unexpected behavior which can occur when a script in a embedded page (page loaded in div,tab etc.) subscribes a handler for a topic using
 *  the jQuery subscribe ($.subscribe) or a no-id element but this subscribe plugin is not reloaded within that embedded page (for example, when
 *  script is included in containing page) . In this case, if the embedded page is reloaded without reloading the entire page (and plugin), the
 *  subscription could be made multiple times for the topic, which will call the handler multiple times each time the topic is published. 
 *  Code has been added to prevent this when the subscription is made using the non-element subscribe ($.subscribe()), which assures that only one
 *  subscription is made for a topic for a given window/frame. To prevent this from happening for an element subscription ($elem.subscribe()), make
 *  sure that the element has an id attribute.
 */

(function(c){var b={},d={},a=function(e){return e.parentWindow||e.defaultView};c.fn.extend({createTopic:function(e){if(e&&!b[e]){b[e]={};b[e].objects={};b[e].objects.__noId__=[]}return this},destroyTopic:function(f){if(f&&b[f]){for(i in b[f].objects){var e=b[f].objects[i];if(c.isArray(e)){if(e.length>0){for(j in e){e[j].unbind(f)}}}else{e.unbind(f,data)}}}delete b[f];return this},subscribe:function(f,h,m){if(this[0]&&f&&h){this.createTopic(f);if(this.attr("id")){b[f].objects[this.attr("id")]=this}else{var n=b[f].objects.__noId__;if(this[0].nodeType==9){for(var e in n){var l=n[e];if(l[0].nodeType==9&&a(this[0]).frameElement==a(l[0]).frameElement){return this}}}var k=false;for(var g=0;g<n.length;g++){if(n[g]==this){k=true;break}}if(!k){b[f].objects.__noId__.push(this)}}if(typeof(h)=="function"){this.bind(f,m,h)}else{if(typeof(h)=="string"&&typeof(d[h])=="function"){this.bind(f,m,d[h])}}}return this},unsubscribe:function(f){if(f){if(b[f]){if(this.attr("id")){var e=b[f].objects[this.attr("id")];if(e){delete b[f].objects[this.attr("id")]}}else{var h=b[f].objects.__noId__;for(var g=0;g<h.length;g++){if(h[g]==this){subscribe_topics[f].objects.__noId__.splice(index,1);break}}}}this.unbind(f)}return this},publish:function(g,l,e){if(g){this.createTopic(g);var h=function(){this.isImmediatePropagationStopped=function(){return true};this.isPropagationStopped=function(){return true};if(this.originalEvent){this.originalEvent.isImmediatePropagationStopped=function(){return true};this.originalEvent.stopPropagation=h}};var k=jQuery.Event(g);c.extend(k,{originalEvent:e,stopPropagation:h});for(i in b[g].objects){var f=b[g].objects[i];if(c.isArray(f)){if(f.length>0){for(j in f){f[j].trigger(k,l)}}}else{f.trigger(k,l)}}}return this},publishOnEvent:function(f,e,g){if(f&&e){this.createTopic(e);this.bind(f,g,function(h){c(this).publish(e,h.data,h)})}return this}});c.extend({subscribe:function(e,f,g){return c().subscribe(e,f,g)},unsubscribe:function(e,f,g){return c().unsubscribe(e,f,g)},subscribeHandler:function(e,f){if(e&&f&&typeof(f)=="function"){d[e]=f}return c()},publish:function(e,f){return c().publish(e,f)},createTopic:function(e){return c().createTopic(e)},destroyTopic:function(e){return c().destroyTopic(e)}})})(jQuery);