// jQuery Alert Dialogs Plugin
//
// Version 1.0
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 29 December 2008
//
// Visit http://abeautifulsite.net/notebook/87 for more information
//
// Usage:
//		jAlert( message, [title, callback] )
//		jConfirm( message, [title, callback] )
//		jPrompt( message, [value, title, callback] )
// 
// History:
//
//		1.00 - Released (29 December 2008)
//
// License:
// 
//		This plugin is licensed under the GNU General Public License: http://www.gnu.org/licenses/gpl.html
//
(function($) {
	
	$.alerts ={
		
		// These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time
		
		verticalOffset: -75,                // vertical offset of the dialog from center screen, in pixels
		horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
		repositionOnResize: true,           // re-centers the dialog on window resize
		overlayOpacity: .09,                // transparency level of overlay
		overlayColor: '#000',               // base color of overlay
		draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
		okButton: '&nbsp;Aceptar&nbsp;',         // text for the OK button
		cancelButton: '&nbsp;Cancelar&nbsp;', // text for the Cancel button
		dialogClass: null,                  // if specified, this class will be applied to all dialogs
		
		// Public methods
		
		alert: function(message, title, imgE, callback) {
			if( title == null ) title = 'Alert';
			$.alerts._show(title, message, imgE, 'alert', function(result) {
				if( callback ) callback(result);
			});
		},
		
		confirm: function(message, title, callback) {
			if( title == null ) title = 'Confirm';
			$.alerts._show(title, message, null, 'confirm', function(result) {
				if( callback ) callback(result);
			});
		},
			
		prompt: function(message, value, title, callback) {
			if( title == null ) title = 'Prompt';
			$.alerts._show(title, message, value, 'prompt', function(result) {
				if( callback ) callback(result);
			});
		},
		
		progresbar: function(message, title, callback) {
			if( title == null ) title = 'Prompt';
			$.alerts._show(title, message,'','progresbar',null);
		},
		
		// Private methods
		
		_show: function(title, msg, value, type, callback) {
			
			$.alerts._hide();
			$.alerts._overlay('show');
			
			$("BODY").append(
			  '<div id="popup_container">' +
			    '<div id="popup_title"><table border="0" cellpadding="0" cellspacing="0" align="center" width="100%"><tr><td id="img1" width="56"></td><td id="tit" align="center"></td><td id="img2" align="right" width="56"></td></tr></table></div>' +
			    '<div id="popup_content">' +
			      '<div id="popup_message"></div>' +
				'</div>' +
			  '</div>');
			
			if(value!='' && type!='progresbar'){
				el=$("#popup_content");
				if(value=="error")
					el.css({background: '16px 16px no-repeat url(css/images/error.png)'});
				else if(value=="adv")
					el.css({background: '16px 16px no-repeat url(css/images/important.png)'});
			}
			if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass);
			
			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
			$("#img1").css({background:'url(images/prointel_pq.png) center no-repeat'})
			$("#img2").css({background:'url(images/eep.png) center no-repeat'})
			$("#popup_container").css({
				position: pos,
				zIndex: 99999,
				padding: '0em 0em 1.75em',
				margin: 0,
			});
			
			$("#tit").html(title)
			
			$("#popup_content").addClass(type);
			$("#popup_message").html(msg);
			$("#popup_message").html( $("#popup_message").html().replace(/\n/g, '<br />') ); 
			
			$("#popup_container").css({
				minWidth: $("#popup_container").outerWidth(),
				maxWidth: $("#popup_container").outerWidth()
			}).on('keypress',function(e){
				if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
			});
			
			$.alerts._reposition();
			$.alerts._maintainPosition(true);
			
			switch(type) {
				case 'alert':
					$("#popup_message").after('<div id="popup_panel"><button type="button" id="popup_ok" class="btn btn-small btn-info"><span>'+$.alerts.okButton+'</span></button></div>');
					$("#popup_ok").click( function() {
						$.alerts._hide();
						callback(true);
					});
					$("#popup_ok").focus().keypress( function(e) {
						if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click');
					});
				break;
				case 'confirm':
					$("#popup_message").after('<div id="popup_panel"><button type="button" id="popup_ok" class="btn btn-small btn-info"><span>'+$.alerts.okButton+'</span></button>&nbsp;&nbsp;<button type="button" id="popup_cancel" class="btn btn-small btn-warning"><span>'+$.alerts.cancelButton+'</span></button></div>');
					$("#popup_ok").on('click',function() {
						$.alerts._hide();
						if( callback ) callback(true);
					});
					$("#popup_cancel").on('click',function() {
						$.alerts._hide();
						if( callback ) callback(false);
					});
					
					$("#popup_ok").focus();//"#popup_cancel"
					
				break;
				case 'prompt':
					$("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><button type="button" id="popup_ok" class="btn btn-small btn-info"><span>'+$.alerts.okButton+'</span></button>&nbsp;&nbsp;<button type="button" id="popup_cancel" class="btn btn-small btn-warning"><span>'+$.alerts.cancelButton+'</span></button></div>');
					$("#popup_prompt").width( $("#popup_message").width() );
					$("#popup_ok").click( function() {
						var val = $("#popup_prompt").val();
						$.alerts._hide();
						if( callback ) callback( val );
					});
					$("#popup_cancel").click( function() {
						$.alerts._hide();
						if( callback ) callback( null );
					});
					
					if( value ) $("#popup_prompt").val(value);
					$("#popup_prompt").focus().select();
				break;
				case 'progresbar':
					$("#popup_message").css({
						'padding-left':'0px',
						'margin-bottom':'8px'
					}).after('<div id="" class="pbar"></div>');
					$("#popup_content").css('background-image','none');
					$("#popup_container").off('keypress');
				break;
			}
			
			// Make draggable
			if( $.alerts.draggable ) {
				try {
					$("#popup_container").draggable({ handle: $("#popup_title") });
					$("#popup_title").css({ cursor: 'move' });
				} catch(e) { /* requires jQuery UI draggables */ }
			}
		},
		
		_hide: function() {
			$("#popup_container").remove();
			$.alerts._overlay('hide');
			$.alerts._maintainPosition(false);
		},
		
		_overlay: function(status) {
			switch( status ) {
				case 'show':
					$.alerts._overlay('hide');
					$("BODY").append('<div id="popup_overlay"></div>');
					$("#popup_overlay").css({
						position: 'absolute',
						zIndex: 99998,
						top: '0px',
						left: '0px',
						width: '100%',
						height: $(document).height(),
						background: $.alerts.overlayColor,
						opacity: $.alerts.overlayOpacity
					});
				break;
				case 'hide':
					$("#popup_overlay").remove();
				break;
			}
		},
		
		_reposition: function() {
			var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
			var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset;
			if( top < 0 ) top = 0;
			if( left < 0 ) left = 0;
			
			// IE6 fix
			if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
			
			$("#popup_container").css({
				top: top + 'px',
				left: left + 'px'
			});
			$("#popup_overlay").height( $(document).height() );
		},
		
		_maintainPosition: function(status) {
			if( $.alerts.repositionOnResize ) {
				switch(status) {
					case true:
						$(window).bind('resize', function() {
							$.alerts._reposition();
						});
					break;
					case false:
						$(window).unbind('resize');
					break;
				}
			}
		}
		
	}
	
	// Shortuct functions
	jAlert = function(message, title, imgE, callback) {
		if($.isFunction(imgE)){
			$.alerts.alert(message, title, '', callback);	
		}else{
			$.alerts.alert(message, title, imgE, callback);
		}
	}
	
	jConfirm = function(message, title, callback) {
		$.alerts.confirm(message, title, callback);
	};
		
	jPrompt = function(message, value, title, callback) {
		$.alerts.prompt(message, value, title, callback);
	};
	
	jProgresbar = function(message, value, title, callback) {
		$.alerts.progresbar(message, value, title, callback);
	}; 
	jPbarClose = function() {
		$.alerts._hide();
	};
	
})(jQuery);