/**
 * Messagebar.js - Browser message bar
 * 
 * @author  Webstores <info at webstores dot nl>
 *          Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */
var MessageBar = function() {
	
	var msgBar = null,
		durationTimeout = null;
	
	/**
	 * Public API
	 */
	return {
		/**
		 * Initialize
		 */
		init: function(options) {
			var self = this;
			msgBar = $('#message-wrapper');
			
			if(msgBar.length) {
				this.setBodyOffset(msgBar.height());
			}
			else {
				msgBar = $('<div id="message-wrapper" style="display: none;"><div id="message"></div><a id="message-close" title="Sluit bericht">Sluit bericht</a></div>');
				$(document.body).prepend(msgBar);
			}
			
			$('#message-close').click(function() {
				self.clearDurationTimeout();
				self.hide();
			});
			
			msgBar.mouseenter(function() {
				self.clearDurationTimeout();
			});
		},
		
		/**
		 * Show the message bar
		 *
		 * @param {String} type The type (class name) of information bar to show (success, info, warning or error)
		 * @param {String} msg The message to show
		 * @param {Number} duration The duration in milliseconds to display the message bar
		 */
		show: function(type, msg, duration) {
			var self = this;
			
			this.setType(type);
			this.setMessage(msg);
			this.setBodyOffset(msgBar.height());
			msgBar.fadeIn('fast');
			
			if(duration) {
				durationTimeout = setTimeout(function() {
					self.hide();
				}, duration);
			}
		},
		
		/**
		 * Hide the message bar
		 */
		hide: function() {
			this.setBodyOffset(0);
			msgBar.fadeOut('fast');
		},
		
		/**
		 * Set the message bar type
		 *
		 * @param {String} type The the to set
		 */
		setType: function(type) {
			msgBar.attr('class', type);
		},
		
		/**
		 * Set the message bar message
		 *
		 * @param {String} msg The message to set
		 */
		setMessage: function(msg) {
			if(msgBar.length) {
				$('#message', msgBar).html(msg);
			}
		},
		
		/**
		 * Sets the HTMLBodyElement top offset
		 * 
		 * @param {Number} offset The offset in pixels
		 */
		setBodyOffset: function(offset) {
			$(document.body).animate({
				paddingTop: offset
			}, 'fast');
		},
		
		/**
		 * Clears the duration timeout
		 */
		clearDurationTimeout: function() {
			clearTimeout(durationTimeout);
			durationTimeout = null;
		}
	}
}();

