/**
 * jGrowl 1.1.2
 *
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Written by Stan Lemon <stanlemon@mac.com>
 * Last updated: 2008.08.17
 *
 * jGrowl is a jQuery plugin implementing unobtrusive userland notifications.  These 
 * notifications function similarly to the Growl Framework available for
 * Mac OS X (http://growl.info).
 *
 */
(function($) {

	/** jGrowl Wrapper - Establish a base jGrowl Container for compatibility with older releases. **/
   //kevinly: m=message body, o=object: the properties of the popup window.
	$.jGrowl = function( m , o ) {
		if ( $('#jGrowl').size() == 0 ) $('<div id="jGrowl"></div>').addClass($.jGrowl.defaults.position).appendTo('body');     // create a container, if there is not one.
		$('#jGrowl').jGrowl(m, o);       // Create notifications on the container.
	};

	/** Raise jGrowl Notification on a jGrowl Container **/
	$.fn.jGrowl = function( m , o ) {
      //alert(m);   return;
      
		if ( $.isFunction(this.each) ) {
//         arrayShow(new $.fn.jGrowl());
//         alert(this.each);
//         return;
			var args = arguments;
			return this.each(function() {
				var self = this;

				/** Create a jGrowl Instance on the Container if it does not exist **/
				if ( $(this).data('jGrowl.instance') == undefined ) {
					$(this).data('jGrowl.instance', new $.fn.jGrowl());
					$(this).data('jGrowl.instance').startup( this );
				}

				/** Optionally call jGrowl instance methods, or just raise a normal notification **/
				if ( $.isFunction($(this).data('jGrowl.instance')[m]) ) {
					$(this).data('jGrowl.instance')[m].apply( $(this).data('jGrowl.instance') , $.makeArray(args).slice(1) );
				} 
            else {
					$(this).data('jGrowl.instance').notification( m , o );
				}
			});
		};
	};

	$.extend( $.fn.jGrowl.prototype , {

		/** Default JGrowl Settings **/
		defaults: {
			         header: 		      '',   //Extra heading: "Pop up windows...."
			         sticky: 		      true,
			         position: 		   'top-right', // Is this still needed?
			         glue: 			   'after',
			         theme: 			   'default',
			         corners: 		   '10px',
			         check: 			   500,
			         life: 			   3000,
			         speed: 			   'normal',
			         easing: 		      'swing',
			         closer: 		      false,
			         closeTemplate:    'x',
			         closerTemplate:   '<div>- Hide all -</div>',
                  
			         log: 			      function(e,m,o) {},  //kevinly: m=message body, o=object: the properties of the popup window.
			         beforeOpen: 	   function(e,m,o) {},
			         open: 			   function(e,m,o) {},
			         beforeClose: 	   function(e,m,o) { 
                                       if(o.msg_id){
                                          $.get("msg_process.php",   {op:"close",msg_id:o.msg_id},   function(){$.jGrowl("Message has been <b>closed</b>!", {sticky: false})});
                                       }
                                    },
			         close: 			   function(e,m,o) {},
			         animateOpen: 	   {opacity: 	'show'},
			         animateClose: 	   {opacity: 	'hide'},
                  msg_id:           null,
                  msg_from:         null
		},
		
		/** jGrowl Container Node **/
		element: 	null,
	
		/** Interval Function **/
		interval:   null,
		
		/** Create a Notification **/
		notification: 	function( message , o ) {
			var self = this;
			var o = $.extend({}, this.defaults, o);

			o.log.apply( this.element , [this.element,message,o] );

         
         var ttl = o.msg_id ? ' title="Close without reply." ' : ''; //kevinly: 2009-03-12
			var notification = $('<div class="jGrowl-notification"><div class="close" ' + ttl + '>' + o.closeTemplate + '</div><div class="header">' + o.header + '</div><div class="message">' + message + '</div></div>')
				.data("jGrowl", o)
            .addClass(o.theme)
            .children('div.close')
            .bind("click.jGrowl", function() {
					                     $(this).unbind('click.jGrowl').parent().trigger('jGrowl.beforeClose').animate(o.animateClose, o.speed, o.easing, function() {
						                     $(this).trigger('jGrowl.close').remove();
					                     });
				                     })
            .parent();
				
			( o.glue == 'after' ) ? $('div.jGrowl-notification:last', this.element).after(notification) : $('div.jGrowl-notification:first', this.element).before(notification);

			/** Notification Actions **/
			$(notification)
         .bind("mouseover.jGrowl",  function() {$(this).data("jGrowl").pause = true;})
         .bind("mouseout.jGrowl",   function() {$(this).data("jGrowl").pause = false;})
         .bind('jGrowl.beforeOpen', function() {o.beforeOpen.  apply( self.element , [self.element,message,o] );})
         .bind('jGrowl.beforeClose',function() {o.beforeClose. apply( self.element , [self.element,message,o] );})
         .bind('jGrowl.open',       function() {o.open.        apply( self.element , [self.element,message,o] );})
         .bind('jGrowl.close',      function() {o.close.       apply( self.element , [self.element,message,o] );})
         .trigger('jGrowl.beforeOpen')
         .animate(o.animateOpen, o.speed, o.easing, function() {$(this).data("jGrowl").created = new Date();})
         .trigger('jGrowl.open');
		
			/** Optional Corners Plugin **/
			if ( $.fn.corner != undefined ) $(notification).corner( o.corners );

			/** Add a Global Closer if more than one notification exists **/
			if ( $('div.jGrowl-notification:parent', this.element).size() > 1 && $('div.jGrowl-closer', this.element).size() == 0 && this.defaults.closer != false ) {
				$(this.defaults.closerTemplate)
            .addClass('jGrowl-closer')
            .addClass(this.defaults.theme)
            .appendTo(this.element)
            .animate(this.defaults.animateOpen, this.defaults.speed, this.defaults.easing)
            .bind("click.jGrowl", function() {
					$(this).siblings().children('div.close').trigger("click.jGrowl");
					if ( $.isFunction( self.defaults.closer ) ) self.defaults.closer.apply( $(this).parent()[0] , [$(this).parent()[0]] );
				});
			};
		},

		/** Update the jGrowl Container, removing old jGrowl notifications **/
		update:	 function() {
			$(this.element).find('div.jGrowl-notification:parent').each( function() {
				if ( $(this).data("jGrowl") != undefined && $(this).data("jGrowl").created != undefined && ($(this).data("jGrowl").created.getTime() + $(this).data("jGrowl").life)  < (new Date()).getTime() && $(this).data("jGrowl").sticky != true && 
					 ($(this).data("jGrowl").pause == undefined || $(this).data("jGrowl").pause != true) ) {
					$(this).children('div.close').trigger('click.jGrowl');
				}
			});

			if ( $(this.element).find('div.jGrowl-notification:parent').size() < 2 ) {
				$(this.element).find('div.jGrowl-closer').animate(this.defaults.animateClose, this.defaults.speed, this.defaults.easing, function() {
					$(this).remove();
				});
			};
		},

		/** Setup the jGrowl Notification Container **/
		startup:	function(e) {
			this.element = $(e).addClass('jGrowl').append('<div class="jGrowl-notification"></div>');
			this.interval = setInterval( function() { jQuery(e).data('jGrowl.instance').update(); }, this.defaults.check);
			if ($.browser.msie && parseInt($.browser.version) < 7 && !window["XMLHttpRequest"]) $(this.element).addClass('ie6');
		},

		/** Shutdown jGrowl, removing it and clearing the interval **/
		shutdown:   function() {
			$(this.element).removeClass('jGrowl').find('div.jGrowl-notification').remove();
			clearInterval( this.interval );
		}
	});
	        
	/** Reference the Defaults Object for compatibility with older versions of jGrowl **/
	$.jGrowl.defaults = $.fn.jGrowl.prototype.defaults;

})(jQuery);
