(function( $ ) {
/*
	var settings = {
		displayTime 	: 5000
	}	
*/	
	var doBasicAlert = false;
	
	$.DWPAlert = function() {
		// needed as a parent class
	};	
	
	$.DWPAlert.init = function() {
		
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
			var ieversion=new Number(RegExp.$1);
			if (ieversion <= 6) {
				doBasicAlert = true;
			}
		}
		
		$('body').append("<div id='DWPAlertContainer'></div>");
	};
	
	$.DWPAlert.alert = function( message, color ) {
	
		if (doBasicAlert == true) {			
 			alert( message.replace(/<\/?[^>]+(>|$)/g, "") );
			return true;
		}
		
		var alertCount = $('#DWPAlertContainer').children().length;
		//console.log(alertCount);

		if (color != "") {
			colorClass = " "+color;
		} else {
			colorClass = "";
		}
		
		var DWPAlertWrapper = jQuery('<div/>', {
			'class'	: 'DWPAlertWrapper',
			'html'	: DWPAlertInner
		});
		
		closelink = "<a href='#' class='DWPAlertClose'>x</a>";
		var DWPAlertInner = jQuery('<div/>', {
			'class'	: 'DWPAlert'+colorClass,
			'html'	: closelink + "<p>" + message + "</p>"
		}).css('opacity', 0).appendTo(DWPAlertWrapper);
		
		DWPAlertWrapper.appendTo('#DWPAlertContainer');
		
		alertHeight = DWPAlertWrapper.outerHeight()+ 10; // Margin Add 5 for safety to make sure not visible.		
		
		if (alertCount > 0) {
			// other alerts, slide
			DWPAlertWrapper.css("height", 0).animate({
				height	: alertHeight
			}, 500, function() {
				$(this).children('.DWPAlert').fadeTo(500, 1).delay(6*1000).fadeOut('slow', function() {
					$(this).closest('.DWPAlertWrapper').detach();
				});
			});		
		} else {
			// only alert, no need to slide
			DWPAlertWrapper.children('.DWPAlert').fadeTo(500, 1).delay(6*1000).fadeOut('slow', function() {
					$(this).closest('.DWPAlertWrapper').detach();
			});
		}
	};	
}) (jQuery);

$(document).ready(function() {
	
	$.DWPAlert.init();
	
	$('#DWPAlertContainer').delegate('.DWPAlertClose', 'click', function(event) {
		event.preventDefault();
		
		var AlertDiv   = $(this).closest('.DWPAlert');
		var WrapperDiv = $(this).closest('.DWPAlertWrapper');
		
		AlertDiv.clearQueue();
		WrapperDiv.clearQueue();
		
		AlertDiv.animate( { opacity:0 }, 400, function() {
			WrapperDiv.animate( { height:0 }, 400, function() {
				WrapperDiv.detach();
			});
		});	
	});	

	
});


