/**
 * jQuery jcAlert. This alert can be: success(green), warning(yellow), error(red), loading(blue) or a personal class
 * @param obj 	type: 'success / warning / error / loading', -> the alert type
 * 				html: 'the content', -> the alert content
 * 				timeout: 4000 (default), -> determines when the alert must hide
 * 				modal: false (default), / true -> add an overlay
 * 				hide: true (default) / false, -> not hides the alert
 * 				out: function() {}, -> a function when timeout occur
 * 				click: function() {}, -> a function when click
 * 				icon: false, -> remove the icon default
 * 				width: 300, -> determines the alert width
 * 				pulsate: true -> add a pulsate effect
 */
(function($) {
	var divOverLay;
	
	$.modal = function(act, obj) {		
		if(act == 'hide') {
			if($(divOverLay).length > 0 && !isUndefined(divOverLay)) {
				$(divOverLay).fadeOut('slow', function() { $j('select').show(); $j(this).remove(); });
			}
			//$(divOverLay).fadeOut('slow', function() { $('select').show(); $(this).remove(); })
		} else {
			var bg='#000', opc=0.4;
			
			if(!isUndefined(obj)) {
				bg = isUndefined(obj.bg) ? '#000' : obj.bg;
				opc = isUndefined(obj.opacity) ? 0.4 : obj.opacity;
			}
			
			var _cssOverlay = { background:bg, opacity:opc, position:'absolute', top:0,left:0, zIndex:9, display:'none' };
			
			$window = {height: $(window).height(),width:$(window).width()};
			$body = {height: $('body').height(),width:$('body').width()};
			
			_cssOverlay.height = ($window.height > $body.height) ? $window.height : $body.height;
			_cssOverlay.width = ($window.width > $body.width) ? $window.width : $body.width;
			
			$('select').hide();
			divOverLay = $('<div />').css(_cssOverlay).attr({id:'overlay'}).prependTo('body');
			
			$(divOverLay).fadeIn();	
		}
		return $;
	}
	
	$.fn.modal = function(act, obj) { return this.each(function() { $.modal(act, obj); }); }
})(jQuery);

(function($){	
	var containers=new Array();
	
	destroy = function() { $(containers).each(function() { $(this).remove(); }); $.modal('hide'); }
	
	$.alert = function(obj) {
		obj.modal ? $.modal('show') : null;
		
		var containerStyle=styleContent='', css={};
		var position = !obj.position ? 'bottom' : obj.position;
		
		containerStyle = 'position:'+($.browser.msie && $.browser.version < 7 ? 'absolute' : 'fixed')+';';
		containerStyle += 'z-index:10000; left:0px; width:100%; '+position+':0px; text-align: center; font-weight:bold; color:#fff; display:none';
		
		if(obj && obj != 'hide') {
			var type = !obj.type ? 'success' : obj.type;
			var html = !obj.html ? '&nbsp;' : obj.html;
			var timeout = !obj.timeout ? 4000 : obj.timeout;
			var hide = isUndefined(obj.hide) ? true : false;
			var icon = isUndefined(obj.icon) ? true : false;
			var width = isUndefined(obj.width) ? 400 : obj.width;
			var loading = !obj.loading ? $('#alert-loading').remove() : '';
			
			var container = $('<div id="alert-'+type+'" style="'+containerStyle+'"></div>').appendTo(document.body);
			
			styleContent = 'width:'+width+'px; padding:12px 0; text-align:left; margin:auto; padding-left:38px; background-repeat:no-repeat; background-position:left center;';
			
			obj.title ? container.attr('title', obj.title) : null;
			
			var pathicon = 'alert/';
			
			if(type == 'success') {
				css = position == 'top' ? {borderBottom:'1px solid #4E640B', background:'#74950F'} : {borderTop:'1px solid #4E640B', background:'#74950F'};
				icon ? styleContent += 'background-image:url('+pathicon+'success.png);' : '';
			} else if(type == 'error') {
				css = position == 'top' ? {borderBottom:'1px solid #74200E', background:'#E64826'} : {borderTop:'1px solid #74200E', background:'#E64826'};
				icon ? styleContent += 'background-image:url('+pathicon+'error.png);' : '';
			} else if(type == 'warning') {
				css = position == 'top' ? {borderBottom:'1px solid #DFA200', background:'#FFCC00', color:'#333333'} : {borderTop:'1px solid #DFA200', background:'#FFCC00', color:'#333333'};
				icon ? styleContent += 'background-image:url('+pathicon+'warning.png);' : '';
			} else if(type == 'loading') {
				css = position == 'top' ? {borderBottom:'1px solid #13439F', background:'#3366CC', color:'#ffffff'} : {borderTop:'1px solid #13439F', background:'#3366CC', color:'#ffffff'};
				icon ? styleContent += 'background-image:url('+pathicon+'loading.gif);' : '';
				html = html=='&nbsp;' ? 'Processando, aguarde...' : html;
			}
			
			if(obj.className) {
				container.html('<div style="'+styleContent+'">'+html+'</div>').addClass(obj.className);
			} else {
				container.html('<div style="'+styleContent+'">'+html+'</div>').css(css);	
			}
			
			if(obj.click) { container.click(obj.click).css({cursor:'pointer'}); }
			
			if(obj.pulsate) {
				container.fadeIn();
				for (var i=0; i < 2; i++) {
					container.animate({opacity: 0}, 800).animate({opacity: 1}, 800);
				};
			} else {
				container.slideDown();
			}
			
			hide ? setTimeout(function() { container.slideUp(); $.modal('hide'); obj.out ? obj.out() : ''; }, timeout) : '';			
			containers.push(container);
		} else if(obj == 'hide') {
			$(containers).each(function() { $(this).remove(); }); $.modal('hide');
		}
		return $;
	}
	
	$.fn.alert = function(obj) { return this.each(function() { $.alert(obj); }); }
})(jQuery);
