/**
 * @name Notify
 * @author Rick Hopkins
 * @classDescription
 * A small message notification system
 * MIT-style License.
 */

var Notify = new Class({
	/** set options */
	options: {
		text: '', 
		classes: [], 
		delay: 4000
	}, 
	
	/** ini the Bug */
	initialize: function(options){
		/** set the options */
		if (!options) return false;
		else this.setOptions(options);
		
		/** load the css file */
		if (!$('NOTIFY_CSS')) new Asset.css('/js/widgets/Notify/css/Notify.css', {'id': 'NOTIFY_CSS'});
		
		/** create short delay, incase css file must be loaded */
		(function(){
			/** set the timer var */
			this.timer = false;
			
			/** build the notification window */
			this.build();
		}.bind(this)).delay(100);
	}, 
	
	/** show the report form */
	build: function(){
		/** create the element and insert */
		this.id = 'notify_' + $random(0, 10000);
		this.msg = new Element('div', {'id': this.id, 'class':'Notify', 'styles': {'visibility': 'hidden'}}).adopt(this.smTitle = new Element('div', {'class': 'sm-title'})).adopt(this.smMessage = new Element('div', {'class': 'sm-text'})).inject(document.body);
		
		/** check for the status message timer */
		if (this.timer) $clear(this.timer);
		
		/** format the status message */
		this.msg.setStyle('opacity', 1);
		this.smTitle.removeProperty('class').addClass('sm-title');
		this.smTitle.set('text', '');
		this.smMessage.set('text', '');
		
		/** fill the status message */
		this.text = this.options.text.split('::');
		if (this.text.length > 1){
			this.smTitle.set('text', this.text[0]);
			this.smMessage.set('text', this.text[1]);
		} else this.smTitle.set('text', this.text[0]);
		
		/** add classes if any */
		if (this.options.classes && this.options.classes.length > 0) this.options.classes.each(function(c){ this.smTitle.addClass(c); }.bind(this));
		
		/** get the other notify boxes, and move them up */
		this.height = this.msg.getCoordinates().height;
		$$('.Notify').each(function(n){
			if (n.get('id') != this.id) n.setStyle('bottom', (n.getStyle('bottom').toInt() + this.height + 10));
			// if (n.get('id') != this.id) n.tween('bottom', [n.getStyle('bottom').toInt(), n.getStyle('bottom').toInt() + this.height + 10]);
		}.bind(this));
		this.msg.setStyle('visibility', 'visible');
		
		/** set the message to fade out */
		this.timer = (function(){ this.msg.fade('out'); }.bind(this)).delay(this.options.delay);
	}
});

/** implement the options */
Notify.implement(new Options);