var NotificationsManager = new Class({
        options: {
        	'my_id':'notification_box',
        	'my_class': 'notification',
        	'styles': { 'width':250 },
        	'timeOut': 0,
        	'notify_up':'',
        	'lblClose':'close',
			'onover': $empty,
			'onout': $empty,
			'onclos': $empty
        },
        
        initialize: function(options) {
                this.setOptions(options);
                this.numMessages = 0;
                this.sliding = false; // not used yet.
                this.showed = false;
				this.notificationBox = new Element('div', {
					'id': this.options.my_id,
					'class': this.options.my_class,
					'events': {
						'mouseenter' : this.options.onover,
						'mouseleave' : this.options.onout
					}
				}).setStyles($extend({
						'left': (($(document.body).getSize().x - this.options.styles.width) >> 1),
						'overflow': 'auto',
						'position': 'fixed'
					},this.options.styles)).setHTML(this.options.notify_up);
				/** FIX FOR IE6 */
				if (Browser.Engine.trident4) {
					this.notificationBox.setStyles({
						'position':'absolute',
						'top':($(document).getScroll().y - 500)
					});
					window.addEvent('scroll', (function() {
						if (this.numMessages == 0) return 1;
						this.notificationBox.setStyle('top', $(document).getScroll().y);
					}).bindWithEvent(this));
				}
				/** END FIX **/
				$(document.body).grab(this.notificationBox, 'top');
				closeBtn = new Element('div', {
					'id': 'close_messages_btn_'+this.options.my_id,
					'styles': {
						'float': 'right',
						'cursor': 'pointer'
					},
					'events': {
						'click': this.hide.bindWithEvent(this)
					}
				}).setHTML(this.options.lblClose);
				this.notificationBox.grab(closeBtn);
        },
        
        addEntry: function(msg) {
        	this.showed = true;
        	var my_id = (msg.id?msg.id:this.numMessages);
        	if ($('notification_id_'+this.options.my_id+my_id)) return 0;

				if (this.options.timeOut != 0) {
					this.mTimer = this.hide.bind(this).delay(this.options.timeOut);
					this.notificationBox.addEvent('mouseenter', (function() {
						$clear(this.mTimer);
					}).bindWithEvent(this));
					this.notificationBox.addEvent('mouseleave', (function() {
						this.mTimer = this.hide.bind(this).delay(this.options.timeOut);
					}).bindWithEvent(this));
				}

        	var cont = new Element('div', {
        		'id':'notification_id_'+this.options.my_id+my_id,
        		'class':'forremove'+this.options.my_id
        	});
        	
			if (msg.title) {
				var msgTitle = new Element('div', {
					'id': 'notification_title_' + my_id,
					'class': 'notification-title'+(msg.type?' '+msg.type:''),
					'events' : {
						'click': (msg.clickTitle?msg.clickTitle:this.removeMessage.bindWithEvent(this, cont))
					}
				}).setHTML(msg.title).inject(cont);
			}
			if (msg.description) {
				var msgDescript = new Element('div', {
					'id': 'notification_description_' + my_id,
					'class': 'notification-description'+(msg.type?' '+msg.type:'')
					}).setHTML(msg.description).inject(cont);
				if (msg.clickDesc) {
					msgDescript.addEvent('click', msg.clickDesc);
				}
			}
						
			$('close_messages_btn_'+this.options.my_id).grab(cont, 'before');
			if (this.notificationBox.getPosition().y != 0) {
				if (msgTitle == undefined) alert(msg.toSource());
				var newTop = this.notificationBox.getPosition().y - msgTitle.getSize().y - msgTitle.getStyle('margin-top').toInt() - msgTitle.getStyle('margin-bottom').toInt();
				newTop -= (msgDescript) ? msgDescript.getSize().y - msgDescript.getStyle('margin-top').toInt() - msgDescript.getStyle('margin-bottom').toInt() : 0.0;

				this.notificationBox.setStyle('top', newTop);			
			}
			if (this.numMessages == 0) {
				this.notificationBox.tween('top', -this.notificationBox.getSize().y, (Browser.Engine.trident4?$(document).getScroll().y:'0px'));
			}			
			this.numMessages++;
        },
        addError: function(msg) {
			this.addEntry($merge(msg, {'type':'error'}));
		},
	
		addMessage: function(msg) {
			this.addEntry($merge(msg, {'type':'message'}));
		},
		
		removeMessage: function(ev, el) {
			el.destroy();
			this.numMessages--;
			if (this.numMessages == 0) {
				this.hide();
			}
		},
		
		hide:function() {
				if (!this.showed) return true;
				this.showed = false;
				var minusTop =  -this.notificationBox.getSize().y;
				var myFx = new Fx.Tween(this.notificationBox, {
					onComplete: (function() {
						$$('.forremove'+this.options.my_id).each(function(el){ el.destroy()});
					}).bind(this) 
				});
				//Transitions the background color of the Element from black to red:

				myFx.start('top', minusTop);
				this.numMessages = 0;
				this.options.onclos();
		},
		
		processRes:function(res) {
			if (!$chk(res)) return 0;
			if (res.errors !== undefined) {
				for(i=0; i<res.errors.length; i++) {
					this.addError({'title':res.errors[i]});
				}
			}
			if (res.messages != undefined) {
				for(i=0; i<res.messages.length; i++) {
					this.addMessage({'title':res.messages[i]});
				}
			}
		}
			
});

NotificationsManager.implement(new Options, new Events);