// JavaScript Document
// jQuery Alert Dialogs Plugin
//
// Version 1.1
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 14 May 2009
//
// 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)
//
//		1.01 - Fixed bug where unbinding would destroy all resize events
//
// License:
// 
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC. 
//
(function($) {
	
	$.dialogo = {
		// These properties can be read/written by accessing $.dialogo.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: 0.45,                // transparency level of overlay
		overlayColor: '#43939e',               // base color of overlay
		draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
		okButton: '&nbsp;Ok&nbsp;',         // text for the OK button
		yesButton: 'Sim',
		closeButton: 'Fechar',
		cancelButton: '&nbsp;Não&nbsp;', // text for the Cancel button
		dialogClass: null,                  // if specified, this class will be applied to all dialogs
		
		// Public methods
		_show: function(type, title, msg, width, height, value, callback){
			
			$.dialogo._hide();
			$.dialogo._overlay('show');

			if(window.parent.topo) {
				window.parent.topo.$.dialogo._overlay('show');
				window.parent.menu.$.dialogo._overlay('show');
				window.parent.rodape.$.dialogo._overlay('show');
			}

			$("body").append(
				  ' <div id="dialogo_container">'
				+ '   <div id="dialogo_title">Mensagem do Sistema</div>'
				+ '  <div id="dialogo_content">'
				+ ' 	<div id="dialogo_msg"></div>'
				+ '   </div>'
				+ '  <div id="dialogo_footer"></div>'
				+ ' </div>'
			);
			
			//IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
			
			$("#dialogo_container").css({
				minWidth: width + 15 + 'px',
				position:pos,
				zIndex:99999,
				padding:'2px',
				margin:0,
				background:'#fff'				
				
			});

			$("#dialogo_title").text(title);
			
			$("#dialogo_msg").css({
				minHeight: height + 'px',
			}).html(msg).text().replace(/\n/g, '<br />');
			
			$("#dialogo_container").css({
				minWidth:$("#dialogo_container").outerWidth(),
				maxWidth:$("#dialogo_container").outerWidth(),
				padding:'2px' 
			});
			
			$("#dialogo_content").css({
				background: 'url(img/dialog/'+type+'.png) 5px 0',
				'background-repeat':'no-repeat'
			});
			
			$.dialogo._reposition();
			$.dialogo._maintainPosition(true);

			switch(type){
				
				case 'alert':
					
					$("#dialogo_footer").append('<input type="button" value="' + $.dialogo.closeButton + '" id="dialogo_ok" />');
					$("#dialogo_ok").click(function(){
						$.dialogo._hide();
						callback(true);
					});
					$("#dialogo_ok").focus().keypress( function(e) {
						if(e.keyCode == 13 || e.keyCode == 27) $("#dialogo_ok").trigger('click');
					});
					
				break;
				
				case 'confirm':
					
					$("#dialogo_footer").append('<input type="button" value="' + $.dialogo.yesButton + '" id="dialogo_ok" />&nbsp;<input type="button" value="' + $.dialogo.cancelButton + '" id="dialogo_cancel" />');
					
					$("#dialogo_ok").click( function() {
						$.dialogo._hide();
						if( callback ) callback(true);
					});
					
					$("#dialogo_cancel").click( function() {
						$.dialogo._hide();
						if(callback) callback(false);
					});
					
					$("#dialogo_cancel").focus();
					
					$("#dialogo_ok, #dialogo_cancel").keypress( function(e) {
						if( e.keyCode == 13 ) $("#dialogo_ok").trigger('click');
						if( e.keyCode == 27 ) $("#dialogo_cancel").trigger('click');
					});
					
				break;
				
				case 'prompt':
					
					$("#dialogo_msg").append('<br /><input type="text" name="dialogo_input" id="dialogo_input" value="" />');
					$("#dialogo_footer").append('<input type="button" value="' + $.dialogo.okButton + '" id="dialogo_ok" />&nbsp;<input type="button" value="' + $.dialogo.closeButton + '" id="dialogo_cancel" />');
					$("#dialogo_input").css({
						minWidth: $("#dialogo_msg").outerWidth()-10,
						maxWidth: $("#dialogo_msg").outerWidth()-10
					});
					
					$("#dialogo_ok").click( function() {
						var val = $("#dialogo_input").val();
						$.dialogo._hide();
						if(callback) callback(val);
					});
					
					$("#dialogo_cancel").click( function() {
						$.dialogo._hide();
						if(callback) callback(null);
					});
					
					$("#dialogo_ok").focus();
					$("#dialogo_ok, #dialogo_cancel").keypress( function(e) {
						if( e.keyCode == 13 ) $("#dialogo_ok").trigger('click');
						if( e.keyCode == 27 ) $("#dialogo_cancel").trigger('click');
					});
					
				break;
				
				case 'pop':
					
					$("#dialogo_msg").html(value).css({
						margin: 5 + 'px',	
						height: height + 'px',
						overflow: 'auto'
					});
					
					$("#dialogo_footer").append('<input type="button" value="' + $.dialogo.closeButton + '" id="dialogo_cancel" />');
					
					$("#dialogo_input").css({
						minWidth: $("#dialogo_msg").outerWidth()-10,
						maxWidth: $("#dialogo_msg").outerWidth()-10
					});
					
					$("#dialogo_ok").click( function() {
						var val = $("#dialogo_input").val();
						$.dialogo._hide();
						if(callback) callback(val);
					});
					
					$("#dialogo_cancel").click( function() {
						$.dialogo._hide();
						if(callback) callback(null);
					});
					
					$("#dialogo_ok").focus();
					$("#dialogo_ok, #dialogo_cancel").keypress( function(e) {
						if( e.keyCode == 13 ) $("#dialogo_ok").trigger('click');
						if( e.keyCode == 27 ) $("#dialogo_cancel").trigger('click');
					});
					
				break;
				
			}
			
			/* Make draggable
			if( $.dialogo.draggable ) {
				try {
					$("#dialogo_container").draggable({ handle: $("#dialogo_title") });
					$("#dialogo_title").css({ cursor: 'move' });
				} catch(e) { /* requires jQuery UI draggables  }
			}*/
			
		},
		
		_hide: function() {
			$("#dialogo_container").remove();
			$.dialogo._overlay('hide');

			if(window.parent.topo) {
				window.parent.topo.$.dialogo._overlay('hide');
				window.parent.menu.$.dialogo._overlay('hide');
				window.parent.rodape.$.dialogo._overlay('hide');
			}

			$.dialogo._maintainPosition(false);
		},
		
		_overlay: function(status) {
			
			switch(status) {
				
				case 'show':
				
				$.dialogo._overlay('hide');
					$("body").append('<div id="dialogo_overlay"></div>');
					$("#dialogo_overlay").css({
						position: 'absolute',
						zIndex: 99998,
						top: '0px',
						left: '0px',
						width: '100%',
						height: $(document).height(),
						background: $.dialogo.overlayColor,
						opacity: $.dialogo.overlayOpacity
					});
				break;
				
				case 'hide':
					$("#dialogo_overlay").remove();
				break;
				
			}
		},
		
		_reposition: function() {
			var top = (($(window).height() / 2) - ($("#dialogo_container").outerHeight() / 2)) + $.dialogo.verticalOffset;
			var left = (($(window).width() / 2) - ($("#dialogo_container").outerWidth() / 2)) + $.dialogo.horizontalOffset;
			
			if(window.parent.rodape) left = left - 80
			
			if( top < 0 ) top = 0;
			if( left < 0 ) left = 0;
			
			// IE6 fix
			if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
			
			$("#dialogo_container").css({
				top: top + 'px',
				left: left + 'px'
			});
			$("#dialogo_overlay").height( $(document).height() );
		},
		
		_maintainPosition: function(status) {
			if( $.dialogo.repositionOnResize ) {
				switch(status) {
					case true:
						$(window).bind('resize', $.dialogo._reposition);
					break;
					case false:
						$(window).unbind('resize', $.dialogo._reposition);
					break;
				}
			}
		}
	}
	
	dialogo = function(type, title, msg, width, height, value, callback){
		$.dialogo._show(type, title, msg, width, height, value, callback)
	}
	
})(jQuery);
