// 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. 
//
//
// Modified by PorfyP
//
(function($) {
	
	$.calcunalerts = { 
		
		// These properties can be read/written by accessing $.calcunalerts.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: .7,                // transparency level of overlay
		overlayColor: '#000',               // base color of overlay
		draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
		okButton: '',         // text for the OK button
		cancelButton: '', // text for the Cancel button
		dialogClass: null,                  // if specified, this class will be applied to all dialogs
		checkboxStatus: false,				// current checkbox status
		
		// Public methods
		
		alert: function(message, title, callback,className) {
			if( title == null ) title = 'Alert';
			$.calcunalerts._show(title, message, null, 'alert', function(result) {
				if( callback ) callback(result);
			},className);
		},
		
		confirm: function(message, title, callback, checkbox) {
			if( title == null ) title = 'Confirm';
			
			/**
			* Optional checkbox
			* and the callback extended
			*/
			this.checkboxStatus = false;
			if(checkbox) {
				message += '\n<input type="checkbox" id="calcunalerts_checkbox" onclick="$.calcunalerts.checkboxStatus = $.calcunalerts.checkboxStatus ? false : true;"/>' + checkbox;
			}
			$.calcunalerts._show(title, message, null, 'confirm', function(result) {
				if(!result) {
					$.calcunalerts.checkboxStatus = false;
				}
				if( callback ) {
					callback(result, $.calcunalerts.checkboxStatus);
				}
			},'confirm');
			
		},
			
		prompt: function(message, value, title, callback) {
			if( title == null ) title = 'Prompt';
			$.calcunalerts._show(title, message, value, 'prompt', function(result) {
				if( callback ) callback(result);
			},'prompt');
		},
		
		// Private methods
		
		_show: function(title, msg, value, type, callback,alertclass) {
			
			$.calcunalerts._hide();
			$.calcunalerts._overlay('show');
			
			$("BODY").append(
			  '<div id="flashMessage">' +
			    '<a class="close" href="#">X</a>' +
			    '<div id="popup_message" class="calcun-info">' +
				'</div>' +
				'<div class="info-foot"></div>' +
			  '</div>');
			
			if( $.calcunalerts.dialogClass ) $("#popup_container").addClass($.calcunalerts.dialogClass);
			$('#flashMessage .close').click(function(){
				$.calcunalerts._hide();	
				if( callback ) callback();
				return false;
			});
			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
			
			$("#flashMessage").css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				margin: 0
			});
			
			$("#popup_title").text(title);
			$("#popup_content").addClass('calcun-' + alertclass);
			$("#popup_message").text(msg);
			$("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') );
			
			$("#flashMessage").css({
				minWidth: $("#flashMessage").outerWidth(),
				maxWidth: $("#flashMessage").outerWidth()
			});
			
			$.calcunalerts._reposition();
			$.calcunalerts._maintainPosition(true);
			
			switch( type ) {
				case 'alert':
					$("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.calcunalerts.okButton + '" id="popup_ok"  class="inputButton"/></div>');
					$("#popup_ok").click( function() {
						$.calcunalerts._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"><input type="button" value="' + $.calcunalerts.okButton + '" id="popup_ok" class="inputButton" /> <input type="button" value="' + $.calcunalerts.cancelButton + '" id="popup_cancel" class="inputButton"/></div>');
					$("#popup_ok").click( function() {
						$.calcunalerts._hide();
						if( callback ) callback(true);
					});
					$("#popup_cancel").click( function() {
						$.calcunalerts._hide();
						if( callback ) callback(false);
					});
					$("#popup_ok").focus();
					$("#popup_ok, #popup_cancel").keypress( function(e) {
						if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
						if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
					});
				break;
				case 'prompt':
					$("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + $.calcunalerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.calcunalerts.cancelButton + '" id="popup_cancel" /></div>');
					$("#popup_prompt").width( $("#popup_message").width() );
					$("#popup_ok").click( function() {
						var val = $("#popup_prompt").val();
						$.calcunalerts._hide();
						if( callback ) callback( val );
					});
					$("#popup_cancel").click( function() {
						$.calcunalerts._hide();
						if( callback ) callback( null );
					});
					$("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) {
						if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
						if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
					});
					if( value ) $("#popup_prompt").val(value);
					$("#popup_prompt").focus().select();
				break;
			}
			
			// Make draggable
			if( $.calcunalerts.draggable ) {
				try {
					$("#flashMessage").draggable({ handle: $("#popup_title") });
					$("#popup_title").css({ cursor: 'move' });
				} catch(e) { /* requires jQuery UI draggables */ }
			}
		},
		
		_hide: function() {
			$("#flashMessage").remove();
			$.calcunalerts._overlay('hide');
			$.calcunalerts._maintainPosition(false);
			$('#flashMessageShadow').fadeOut(function(){
				$(this).remove();	
			});
		},
		
		_overlay: function(status) {
			switch( status ) {
				case 'show':
					$.calcunalerts._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: $.calcunalerts.overlayColor,
						opacity: $.calcunalerts.overlayOpacity
					});
				break;
				case 'hide':
					$("#popup_overlay").remove();
				break;
			}
		},
		
		_reposition: function() {
			var top = (($(window).height() / 2) - ($("#flashMessage").outerHeight() / 2)) + $.calcunalerts.verticalOffset;
			var left = (($(window).width() / 2) - ($("#flashMessage").outerWidth() / 2)) + $.calcunalerts.horizontalOffset;
			if( top < 0 ) top = 0;
			if( left < 0 ) left = 0;
			
			// IE6 fix
			if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
			
			$("#flashMessage").css({
				top: top + 'px',
				left: left + 'px'
			});
			$("#popup_overlay").height( $(document).height() );
		},
		
		_maintainPosition: function(status) {
			if( $.calcunalerts.repositionOnResize ) {
				switch(status) {
					case true:
						$(window).bind('resize', $.calcunalerts._reposition);
					break;
					case false:
						$(window).unbind('resize', $.calcunalerts._reposition);
					break;
				}
			}
		},
		
		/**
		* Translation method
		* @author PorfyP
		*/
		translate: function(message, title, extra, callback) {
			extra = extra ? extra : '';
			var labels = '["'+message+'","'+title+'","'+extra+'"'; //json format, later should be via a json plugin
			
			/**
			* Button translate with cache
			*/
			if(!$.calcunalerts.okButton || !$.calcunalerts.cancelButton) {
				labels += ',"ok","cancel"';
			}
			
			labels += ']';
			
			$.ajax({url: CalcunConfig.basePath + 'Api/Lang/Translate.php', data: {
				labels: labels
			}, 
			dataType: 'json',
			success: function(data) {
				if(!$.calcunalerts.okButton || !$.calcunalerts.cancelButton) {
					$.calcunalerts.okButton = data['ok'];
					$.calcunalerts.cancelButton = data['cancel'];
				}
				callback(data[message], data[title], data[extra]);
			}});
		}
		
	}
	
	// Shortuct functions
	jcError = function(message, title, callback) {
		$.calcunalerts.translate(message, title, '', function(message, title, extra) {
			$.calcunalerts.alert(message, title, callback, 'error');	
		});
	}
	jcWarn = function(message, title, callback) {
		$.calcunalerts.translate(message, title, '', function(message, title, extra) {
			$.calcunalerts.alert(message, title, callback, 'warn');
		});
	}
	jcInfo = function(message, title, callback) {
		$.calcunalerts.translate(message, title, '', function(message, title, extra) {
			$.calcunalerts.alert(message, title, callback,'info');
		});
	}
	jcConfirm = function(message, title, callback, checkbox) {
		$.calcunalerts.translate(message, title, checkbox, function(message, title, extra) {
			$.calcunalerts.confirm(message, title, callback, extra);
		});
	};
		
	jcPrompt = function(message, title, value, callback) {
		$.calcunalerts.translate(message, title, value, function(message, title, extra) {
			$.calcunalerts.prompt(message, extra, title, callback);
		});
	};
	
})(jQuery);
