var Notifier = function()
{
	this.LOADING_TRUE = true;
	this.LOADING_FALSE = false;

	this._notification_loading = new Notification();
	this._dom_root = createElement('div','notifier');
	this._dom_root.id = 'notifier';
	this._timeout = 3500;

	this.setTimeout = function(timeout)
	{
		this._timeout = timeout;
	}
	
	this.notify = function(type, message)
	{
		var not = new Notification();
		not.setType(type);
		not.setMessage(message);
		this._dom_root.appendChild(not.draw());
		setTimeout(not.remove, this._timeout);
	}

	/**
	* Status is constant, this.LOADING_TRUE or this.LOADING_FALSE
	* callback should be a function to execute after the loading 
	* message has been displayed
	*/
	this.loading = function(status, callback)
	{
		try
		{
			switch(status)
			{
				case this.LOADING_TRUE:
				{
					this._notification_loading._int_opacity =90;
					this._notification_loading.fade();
					break;
				}
				case this.LOADING_FALSE:
				{
					this._notification_loading.remove();
					break;
				}
			}
			//set callback timeout if supplied
			if(callback && typeof callback == 'function')
			{
				setTimeout(function(){callback();}, 50);
			}
		}
		catch(e)
		{
			// exception thrown if laoding is called (e.g. from ajax object)
			// and the notifier hasn't been drawn to the screen yet
			callback();
		}
	}

	this.draw = function()
	{
		this._notification_loading.setType(Notification.TYPE_LOADING);
		this._notification_loading.setMessage('Loading...');
		this._dom_root.appendChild(this._notification_loading.draw());
		this._int_step = 90;
		this._notification_loading.remove();
		return this._dom_root;
	}
}
var Notification = function()
{
	this.TYPE_ERROR = 'error';
	this.TYPE_INFO = 'info';
	this.TYPE_WARNING = 'warning';
	this.TYPE_STATUS = 'status';
	this.TYPE_LOADING = 'loading';
	
	var ref = this;
	this._str_message;
	this._str_type;
	this._dom_root;
	this._int_opacity = 100;
	this._int_step = 2;
	
	this.setType = function(type){ this._str_type = type;}
	this.getType = function(){ return this._str_type;}
	this.setMessage = function(message){this._str_message = message;}
	this.getMessage = function(){ return this._str_message;}

	this.remove = function()
	{
		var speed = Math.round(1000 / 100);
		var timer = 0;
		for(var opacity = ref._int_opacity; opacity >= 0; opacity-=ref._int_step)
		{
			setTimeout(ref.fade, (timer * speed));
			timer+=ref._int_step;
		}
	}
	this.fade = function()
	{
		try
		{
			var object = ref._dom_root.style;
			object.opacity = (ref._int_opacity / 100);
			// folowing not needed for firefox 2, and causes problems in IE 7?!
			//object.MozOpacity = (ref._int_opacity / 100);
			object.KhtmlOpacity = (ref._int_opacity / 100);
			object.filter = "alpha(opacity=" + ref._int_opacity + ")";
			ref._int_opacity -=ref._int_step;
			if(ref._int_opacity <= 0 )
			{
				object.display = 'none';
			}
			else
			{
				object.display = 'block';
			}
		}
		catch(err)
		{
			// do nothing
		}
	}
	this.draw = function()
	{
		this._dom_root = createElement('div', this.getType());
		this._dom_root.innerHTML = this.getMessage();
		this._dom_root.onclick = this.remove;
		this.fade();
		return this._dom_root;
	}
}
