﻿ /****************************************************************
  *                                                              *
  *  JQuery Publish/Subscribe by Leo K.                          *
  *	 http://www.humancaresystems.com                             *
  *  ------------                                                *
  *  Version 1.0                                                 *
  *                                                              *
  *  Origionaly by: Jamie Thompson.                              *
  *  Website: http://www.curvycorners.net                        *
  *                                                              *
  *  This library is free software; you can redistribute         *
  *  it and/or modify it under the terms of the GNU              *
  *  Lesser General Public License as published by the           *
  *  Free Software Foundation; either version 2.1 of the         *
  *  License, or (at your option) any later version.             *
  *                                                              *
  *  This library is distributed in the hope that it will        *
  *  be useful, but WITHOUT ANY WARRANTY; without even the       *
  *  implied warranty of MERCHANTABILITY or FITNESS FOR A        *
  *  PARTICULAR PURPOSE. See the GNU Lesser General Public       *
  *  License for more details.                                   *
  *                                                              *
  *  You should have received a copy of the GNU Lesser           *
  *  General Public License along with this library;             *
  *  Inc., 59 Temple Place, Suite 330, Boston,                   *
  *  MA 02111-1307 USA                                           *
  *                                                              *
  ****************************************************************/    
(function($) {
    $.subscribe = function (eventName, element, fn) 
    {
        var events = eventName.split(',');
        for(var i = 0; i < events.length; i++)
        {
            var evt = events[i].replace(/^\s+|\s+$/g,"");
            if (evt.length > 0)
            {
                $(element).addClass("psevent_subscriber").bind(evt, function() 
                {
                    fn.apply(element, Array.prototype.slice.call(arguments, 1) );
                });
            }
        }
    };
})(jQuery);

(function($) {
    $.publishInternal = function (eventName, args) 
    {    
        $(".psevent_subscriber").trigger(eventName, args);

        for(var i = 0; i < window.frames.length; i++)
            if (window.frames[i].window && window.frames[i].window.jQuery && window.frames[i].window.jQuery.publishInternal)
                window.frames[i].window.jQuery.publishInternal(eventName, args);
    };
})(jQuery);


(function($) {
    $.publishSelf = function (eventName) 
    {   
        $.publishInternal(eventName, Array.prototype.slice.call(arguments,1));
    };
})(jQuery);

(function($) {
    $.publish = function (eventName) 
    {    
        if (top.jQuery && top.jQuery.publishInternal)
            top.jQuery.publishInternal(eventName, Array.prototype.slice.call(arguments,1));
        else
            $.publishInternal(eventName, Array.prototype.slice.call(arguments,1));
    };
})(jQuery);
