/**
 * @name GROWL for web
 * @author Simon Fletcher
 * @project Youmeo G4
 */

function Growl(  ) {   
}

Growl.prototype.design = function( id, type, title, msg ) {

	switch( type ) {

		case 'success':

			var class2 = 'green';

			break;

		case 'failure':

			var class2 = 'red';

			break;

		case 'notice':

			var class2 = 'grey';

			break;

	}

	var design = '<li class="' + class2 + '" id="' + id + '" style="display: none;"><div class="top"></div><div class="spacer"><div class="close" id="' + id + '-exit"></div><div class="fyi"><div class="icon"></div><div class="text"><h2>' + title + '</h2><p>' + msg + '</p></div></div></div><div class="bottom"/></li>';

	return ( design );

}

Growl.prototype.add = function( id, type, title, msg ) {

	id = 'growl-' + id;
	var design = this.design( id, type, title, msg );

	this.load( design );
	this.fadeIn( id );
	this.checkDel( id );

}

Growl.prototype.checkDel = function( id ) {

	Event.observe( id + '-exit', 'click', function( e ) {

		$( id ).remove();

	});

}

Growl.prototype.load = function( design ) {

	new Insertion.Bottom( 'growl-notifications', design );

}

Growl.prototype.itsShowing = function( id, thedelay ) {

	Event.stopObserving( id, 'mouseover' );

	var Fade = Effect.Fade( id, {

		delay:thedelay,
		duration:2,
		to:0.2,
		beforeStart:function( e ) {

			Event.observe( id, 'mouseover', function( e ) {

				Event.stopObserving( id, 'mouseout' );
				Fade.cancel();
				$( id ).style.opacity = '1';
				$( id ).style.display = 'block';

				Event.observe( id, 'mouseout', function( e ) {

					GROWL.itsShowing( id, 0 );				

				});

			});

		},

		afterFinish:function( e ) {

			$( id ).remove();

		}

	} );

}

Growl.prototype.fadeIn = function( id ) {

	var Appear = Effect.Appear( id, { afterFinish:function() { GROWL.itsShowing( id, 2 ); } } );

	Event.observe( id, 'mouseover', function( e ) {

		Appear.cancel();
		$( id ).style.opacity = '1';
		$( id ).style.display = 'block';

		GROWL.itsShowing( id, 2 );

	});

}

Growl.prototype.comet = function() {

	var Lol = new Ajax.Request( 'http://zood.com/growl/growl.php', {

		parameters:{},
		onSuccess:function( e ) {

			var data = JSON.decode( e.responseText );
			var x = data.notices;

			for( var i = 0; i < x.length; i++ ) {

				var id		= x[ i ].id;
				var type	= x[ i ].type;
				var name	= x[ i ].name;
				var msg		= x[ i ].msg;

				GROWL.add( id, type, name, msg );

			}

			setTimeout( "GROWL.comet()", 5000 );

		}

	});

}

var GROWL = new Growl();