(function( $ ){
	$.fn.displayMessage = function(options) {
	
		// Default configuration properties.
		var defaults = {
			message		: 'Welcome to PHM China',
			background	: '#3F6',
			color		: '#FFFFFF',
			speed		: 'fast',
			delay		: 5000,
			position 	: 'relative', // relative, absolute, fixed
			autohide	: true
		}
		
		var options = $.extend( defaults, options );
		$(this).slideUp('fast');
		$(this).removeClass().empty();
		return this.each(function() {
			var sticky = (options.sticky == false) ? 'relative' : 'absolute';
			$(this).addClass('messagebar').css('position',options.position).css('background-color',options.background);
			
			if(options.autohide == true){
				$(this).append('<div class="messagebar_inner"><span class="messagebar_text"></span></div>').css('color',options.color);
			}else{
				$(this).append('<div class="messagebar_inner"><span class="messagebar_text"></span><a href="#" id="close" class="messagebar_close">[x]</a></div>').css('color',options.color);
			}
			
			$(this).find('span').html(options.message);
			
			$(this).slideDown(options.speed ,function(){
				
				var parent = ($(this).attr('id')) ? "#"+$(this).attr('id') : "."+$(this).attr('class');
				var close_button = ($(this).find('a:last').attr('id')) ? "#"+$(this).find('a:last').attr('id') : "."+$(this).find('a:last').attr('class');
				
				if(options.autohide == true)
				{
					$(this).delay(options.delay).slideUp('slow');
				}
				
				$(parent+">div>"+close_button).bind("click", function (event) {
					event.preventDefault();
					$(parent+">div>"+close_button).animate({"opacity": "hide"}, function(){
						$(parent+">div>span").fadeOut("slow").html("");
						$(parent+">div>"+close_button).parent().parent().slideUp(options.speed);
					});
				});
			});
		});
	};
})( jQuery );

