Utils.Message = function() {
	var MSGTIMER = 20;
	var MSGSPEED = 5;
	var MSGOFFSET = 3;
	var MSGHIDE = 3;
	
	return {
		msg: null, msgContent: null,
		
		init: function() {
		 	this.msg = $('<div id="msg"></div>');
		 	this.msgContent = $('<div id="msgContent"></div>');
		 	$(this.msgContent).appendTo(this.msg);
	    
		 	$(this.msg).appendTo(document.body);
		 	this.msg = $(this.msg).get(0);
		 	this.msgContent = $(this.msgContent).get(0);
		 	
		 	this.msg.style.filter = 'alpha(opacity=0)';
		 	this.msg.style.opacity = 0;
		 	this.msg.alpha = 0;
		},
		
		show: function(target, message, autohide) {
		  if(!this.msg) { this.init(); } 
		  
		  $(this.msgContent).text(message);
		  this.msg.style.display = 'block';
		  
		  target = typeof target == 'string' ? $('#'+target).get(0) : target;
			if (!target) {
				alert('Target is undefined.');
				return false;
			}
		  target.focus();
		  
		  this.msg.style.top = ($(target).offset().top - (this.msg.offsetHeight - target.offsetHeight) / 2) + 'px';
		  this.msg.style.left = ($(target).offset().left + target.offsetWidth  + MSGOFFSET) + 'px';
		 
		  clearInterval(this.msg.timer);
		  this.msg.timer = setInterval("Utils.Message.fade(1)", MSGTIMER);
		  if (!autohide) { autohide = MSGHIDE; }
		  window.setTimeout("Utils.Message.hide()", (autohide * 1000));
		},
		
		hide: function(msg) {
		  if(!this.msg.timer) {
		  	this.msg.timer = setInterval("Utils.Message.fade(0)", MSGTIMER);
		  }
		},
		
		fade: function(flag) {
		  if (flag == null) { flag = 1; }
		  var value = flag == 1 ? this.msg.alpha + MSGSPEED : this.msg.alpha - MSGSPEED;
		  
		  this.msg.alpha = value;
		  this.msg.style.opacity = (value / 100);
		  this.msg.style.filter = 'alpha(opacity=' + value + ')';
		  if (value >= 99) {
		    clearInterval(this.msg.timer);
		    this.msg.timer = null;
		  } else if (value <= 1) {
		  	this.msg.style.display = 'none';
		    clearInterval(this.msg.timer);
		  }
		},
		
		_preloadImg: function() {
			if (document.images) {
			  arrow = new Image(7,80); 
			  arrow.src = '/img/adm/msg_arrow.gif'; 
			}
		},
		
		_preloadStyles: function() {
      Utils.Init.loadStyle('/css/message.css');
    }
	}
}();

$(document).ready(function() {
	Utils.Message._preloadImg();
	Utils.Message._preloadStyles();
});

