// jQuery Alert Dialogs Plugin
//
// Version 1.1
//
// 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] )
// 
// 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
//
// Edited:
//
//      Michal Matuška

(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        
		dialogClass: null,                  // if specified, this class will be applied to all dialogs
		
		// Public methods
		
		alert: function(message, title, btn1, callback) {
			$.alerts._show(title, message, btn1, null, null, 'alert', function(result) {
				if( callback ) callback(result);
			});
		},
		
		confirm: function(message, title, btn1, btn2, callback) {
			//if( title == null ) title = 'Confirm';
			$.alerts._show(title, message, btn1, btn2, null, 'confirm', function(result) {
				if( callback ) callback(result);
			});
		},
		
		// Private methods
		
		_show: function(title, msg, btn1, btn2, value, type, callback) {
			
			$.alerts._hide();
			
			$("BODY").append(
			  '<div id="fbBox-overlay"></div>' +
              '<div id="fbBox-window"><div class="top">&nbsp;</div><div class="left"><div class="right">'+
              '<div id="fbBox-main"><h2 id="fbBox-title"></h2><p class="big"></p></div>'+
              '</div></div><div class="bottom">&nbsp;</div><span class="cor cor-l"></span><span class="cor cor-r"></span><span class="cor cor-bl"></span><span class="cor cor-br"></span>'+
              '</div>');
			
			if( $.alerts.dialogClass ) $("#fbBox-window").addClass($.alerts.dialogClass);
			
			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
			
			$("#fbBox-window").css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				margin: 0
			});
			if(title==null){
                $("#fbBox-title").remove()
            }
            else{
			     $("#fbBox-title").text(title);
			}
			$("fbBox-main").addClass(type);
			$("#fbBox-main .big").text(msg);
			$("#fbBox-main .big").html( $("#fbBox-main .big").text().replace(/\n/g, '<br />') );
			
			$("#fbBox-window").css({
				minWidth: $("#fbBox-window").outerWidth(),
				maxWidth: 400
			});
			
			$.alerts._reposition();
			$.alerts._maintainPosition(true);
			
			switch( type ) {
				case 'alert':
					$("#fbBox-main .big").after('<p class="buttons center"><a class="inline-btn save-btn" href="#"><span></span></a></p>');
					$("#fbBox-window .buttons .save-btn span").text(btn1==null?'Ok':btn1);
                    $("#fbBox-window .buttons .save-btn").click( function() {
						$.alerts._hide();
						callback(true);
						return false;
					});
					$("#fbBox-window .buttons .save-btn").focus().keypress( function(e) {
						if( e.keyCode == 13 || e.keyCode == 27 ) $("#fbBox-window .buttons .save-btn").trigger('click');
					});
				break;
				case 'confirm':
					$("#fbBox-main .big").after('<p class="buttons center"><a class="inline-btn save-btn" href="#"><span></span></a><a href="#" class="inline-btn delete-btn"><span></span></a></p>');
					$("#fbBox-window .buttons .save-btn span").text(btn1==null?'Ok':btn1);
					$("#fbBox-window .buttons .delete-btn span").text(btn2==null?'Zrušit':btn2);
					$("#fbBox-window .buttons .save-btn").click( function() {
						$.alerts._hide();
						if( callback ) callback(true);
						return false;
					});
					$("#fbBox-window .buttons .delete-btn").click( function() {
						$.alerts._hide();
						if( callback ) callback(false);
						return false;
					});
					$("#fbBox-window .buttons .save-btn").focus();
					$("#fbBox-window .buttons .save-btn, #fbBox-window .buttons .delete-btn").keypress( function(e) {
						if( e.keyCode == 13 ) $("#fbBox-window .buttons .save-btn").trigger('click');
						if( e.keyCode == 27 ) $("#fbBox-window .buttons .delete-btn").trigger('click');
					});
				break;
			}
			
		  
		},
		
		_hide: function() {
			$("#fbBox-overlay").remove();
			$("#fbBox-window").remove();
			$.alerts._maintainPosition(false);
		},
		
		_reposition: function() {
			var top = (($(window).height() / 2) - ($("#fbBox-window").outerHeight() / 2)) + $.alerts.verticalOffset;
			var left = (($(window).width() / 2) - ($("#fbBox-window").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();
			
			$("#fbBox-window").css({
				top: top + 'px',
				left: left + 'px'
			});
			$("#fbBox-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, btn1, callback) {
		$.alerts.alert(message, title, btn1, callback);
	}
	
	jConfirm = function(message, title, btn1, btn2, callback) {
		$.alerts.confirm(message, title, btn1, btn2, callback);
	};
	
})(jQuery);

