/**
 * jGrowl 1.0.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.03.24
 *
 * 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).
 *
 * Changes in 1.0.2
 * - All CSS styling is now external.
 * - Added a theme parameter which specifies a secondary class for styling, such
 *   that notifications can be customized in appearance on a per message basis.
 * - Notification life span is now customizable on a per message basis.
 * - Added the ability to disable the global closer, enabled by default.
 * - Added callbacks for when a notification is opened or closed.
 * - Added callback for the global closer.
 * - Customizable animation speed.
 * - jGrowl now set itself up and tears itself down.
 *
 * Changes in 1.0.1:
 * - Removed dependency on metadata plugin in favor of .data()
 * - Namespaced all events
 *
 * @todo    Group by header.
 <div class="top close"></div>
 var notification = $('<div class="jGrowl"><div class="close">&times;</div><div class="header">' + o.header + '</div><div class="message">' + message + '</div></div>').data("jGrowl", {
 
 */
(function($) {

	/** Raise jGrowl Notifications **/
	$.jGrowl = function( message , o ) {
	    this.defaults = $.jGrowl.defaults; // Load the defaults into this context.

		var o = $.extend($.jGrowl.defaults, o || {});
		
		// jGrowl hasn't been started yet, so start her up.
		if ( $('div#jGrowl').size() == 0 ) $.jGrowl.startup();

		var notification = $('<div class="jGrowl"><div class="close">lll</div><div class="top"><img src="http://ymex.org/files/icons/action_remove.png" style="cursor:pointer; float: right;">' + o.header + '</div><div class="message">' + message + '</div><div class="bottom">&nbsp;</div></div>').data("jGrowl", {
		    sticky:     o.sticky,
		    life:       o.life,
		    speed:      o.speed
		}).addClass( o.theme ).children('div.top').bind("click.jGrowl", function() {
			$(this).unbind('click.jGrowl').parent().fadeOut( o.speed , function() {
				$(this).trigger('jGrowl.close').remove();
			});
		}).parent();

		if ($('div#jGrowl').children('div.jGrowl').size() == 0) {
			$('div#jGrowl').append( notification );
		} else {
			$('div#jGrowl div.jGrowl:last').after( notification );
		}
		
		// If the jQuery Corner's plugin is loaded, use it!
		if ( $.fn.corner != undefined ) $('div#jGrowl').children().corner('10px');

		$('div#jGrowl div.jGrowl:last').fadeIn( o.speed , function() {
		    $(this).data("jGrowl").created = new Date();
		}).bind("mouseover.jGrowl", function() {
			$(this).data("jGrowl").pause = true;
		}).bind("mouseout.jGrowl", function() {
			$(this).data("jGrowl").pause = false;
		}).bind('jGrowl.open', function() {
		    o.open.apply( this , [this,message] );
		}).bind('jGrowl.close', function() {
		    o.close.apply( this , [this,message] );
		}).trigger('jGrowl.open');

		/* If we have more then one notification add a global closer.
		if ( $('div#jGrowl div.jGrowl').size() > 1 && $('div#jGrowl div.closer').size() == 0 && this.defaults.closer != false ) {
            $('<div class="closer">[ close all ]</div>').addClass( this.defaults.theme ).appendTo("div#jGrowl").fadeIn( this.defaults.speed ).bind("click.jGrowl", function() {
				$(this).siblings().andSelf().fadeOut( o.speed , function() {
					$(this).children('div.close').trigger("click.jGrowl");
				});

				if ( $.isFunction( $.jGrowl.defaults.closer ) ) $.jGrowl.defaults.closer.apply( $(this).parent()[0] , [$(this).parent()[0]] );
			});
		};*/
	};

	$.extend( $.jGrowl , {

    	/** Default JGrowl Settings **/
        defaults: {
		    header:     '',
			sticky: 	false,
    		theme:      'default',
    		check: 		1000,
    		life: 		3000,
            speed:      'slow',
    		closer:     true,
            open:       function(e,m) {},
            close:      function(e,m) {}
    	},
	
	    /** Interval Function **/
	    interval:   null,

    	/** Update the jGrowl Container, removing old jGrowl notifications **/
        update:     function() {
            $('div#jGrowl div.jGrowl').each( function( key , value) {
    			if ( $(value).data("jGrowl").created != undefined && ($(value).data("jGrowl").created.getTime() + $(value).data("jGrowl").life)  < (new Date()).getTime() && $(value).data("jGrowl").sticky != true && 
    				 ($(value).data("jGrowl").pause == undefined || $(value).data("jGrowl").pause != true) ) {
    				$(value).fadeOut( $(this).data("jGrowl").speed , function() {
    					$(this).children('div.close').trigger('click.jGrowl');
    				});
    			}
    		});

    		if ( $('div#jGrowl div.jGrowl').size() < 2 ) {
    			$('div#jGrowl div.closer').fadeOut( this.defaults.speed , function() {
    				$(this).remove();
    			});
    		};
    		
    		// If there are no more messages, let's tear down jGrowl.
		    if ( $('div#jGrowl').children().size() == 0 ) this.shutdown();
    	},

	    /** Setup the jGrowl Notification Container **/
        startup:    function() {
            this.interval = setInterval("jQuery.jGrowl.update()", this.defaults.check);
    		$('body').append('<div id="jGrowl"></div>');
    	},

    	/** Shutdown jGrowl, removing it and clearing the interval **/
    	shutdown:   function() {
    	    clearInterval( this.interval );
            $('div#jGrowl').remove();
    	}
    });

})(jQuery);