// 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($) {
	
	$.alerts = {
		
		// These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time
		
		verticalOffset: -50,                // 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: .6,                // transparency level of overlay
		overlayColor: '#000',               // base color of overlay
		draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
		okButton: 'OK',         // text for the OK button
		cancelButton: 'Cancel', // text for the Cancel button
		dialogClass: null,                  // if specified, this class will be applied to all dialogs
		
		// Public methods
		
		alert: function(message, title, callback) {
			if(title == null || title == "") title = 'Epiday Alert';
			$.alerts._show(title, message, null, 'alert', function(result) {
				if( callback ) callback(result);
			});
		},
		
		confirm: function(message, title, callback) {
			if(title == null || title == "") title = 'Epiday Confirm';
			$.alerts._show(title, message, null, 'confirm', function(result) {
				if( callback ) callback(result);
			});
		},
			
		prompt: function(message, value, title, callback) {
			if(title == null || title == "") title = 'Epiday Prompt';
			$.alerts._show(title, message, value, 'prompt', function(result) {
				if( callback ) callback(result);
			});
		},
		
		// added by epiday
		loading: function(){
			$.alerts._showLoading();
		},
		
		message: function(message, title, callback){
			if(title == null || title == "") title = 'Epiday Message';
			$.alerts._show(title, message, null, 'message', function(result) {
				if( callback ) callback(result);
			});
		},
		
		dialog: function(width, height, url, params, dialogcallback, callback){
			$.alerts._showDialog(width, height, url, params, dialogcallback, function(result){
				if(callback) callback(result);
			});
		},
		
		// Private methods
		
		_showLoading: function(){
			$.alerts._hide("#loading");
			$.alerts._overlay('show','#loading_overlay');

			var strHTML;

			strHTML = "<div id='loading'>"
					+ "<div class='loading_text'><b>Loading...</b><br/>Please wait a moment.</div>"
					+ "<div class='loading_img'><img src='/images/icon/loader_bar.gif'/></div>"
					+ "</div>"
			
			$("BODY").append(strHTML);
			
			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
			
			$("#loading").css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				margin: 0
			});

			$("#loading").css({
				minWidth: $("#loading").outerWidth(),
				maxWidth: $("#loading").outerWidth()
			});

			$.alerts._reposition("#loading");
			$.alerts._maintainPosition(true);
		},
		
		_showDialog: function(width, height, url, params, dialogcallback, callback){
			$.alerts._hide("#dialog");
			$.alerts._overlay('show','#dialog_overlay');
			
			$("BODY").append(
				"<div id='dialog'>"
				 + "<table class='window'><tr><td class='dialog_tl'></td><td class='dialog_tc'></td><td class='dialog_tr'></td></tr><tr><td class='dialog_ml'></td>"
				 + "<td class='dialog_mc'>"
				 + "<div id='dialog_content'></div>"
				 + "</td><td class='dialog_mr'></td></tr><tr><td class='dialog_bl'></td><td class='dialog_bc'></td><td class='dialog_br'></td></tr></table>"
				 + "</div>"
			);
			
			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 

			$("#dialog").css({
				position: pos,
				zIndex: 99997,
				padding: 0,
				margin: 0
			});
			
			$("#dialog_content").css({
				width: width,
				height: height
			});

			$("#dialog").css({
				minWidth: $("#dialog").outerWidth(),
				maxWidth: $("#dialog").outerWidth()
			});

			$.alerts._reposition("#dialog");
			$.alerts._maintainPosition(true);
			$("#dialog_content").load(url, params, dialogcallback);
		},
		
		
		_show: function(title, msg, value, type, callback) {
			
			$.alerts._hide();
			$.alerts._overlay('show');
			$("#popup_overlay").css("zIndex","99998");
			
			$("BODY").append(
			  '<div id="popup">' +
			    '<h1 id="popup_title"></h1>' +
			    '<div id="popup_content">' +
			      '<div id="popup_message"></div>' +
				'</div>' +
			  '</div>');
			
			if( $.alerts.dialogClass ) $("#popup").addClass($.alerts.dialogClass);
			
			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
			
			$("#popup").css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				margin: 0
			});
			
			$("#popup_title").text(title);
			$("#popup_content").addClass(type);
			$("#popup_message").text(msg);
			$("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') );
			
			$("#popup").css({
				minWidth: $("#popup").outerWidth(),
				maxWidth: $("#popup").outerWidth()
			});
			
			$.alerts._reposition();
			$.alerts._maintainPosition(true);
			
			switch( type ) {
				case 'alert':
					$("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /></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"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
					$("#popup_ok").click( function() {
						$.alerts._hide();
						if( callback ) callback(true);
					});
					$("#popup_cancel").click( function() {
						$.alerts._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="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></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 );
					});
					$("#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( $.alerts.draggable ) {
				try {
					$("#popup").draggable({ handle: $("#popup_title") });
					$("#popup_title").css({ cursor: 'move' });
				} catch(e) { /* requires jQuery UI draggables */ }
			}
			
			if(type == 'message'){
				setTimeout("$.alerts._hide()", 3000);
			}
		},

		_hide: function(id) {
			if(!id) id="#popup";
			$(id).remove();
			$.alerts._overlay('hide',id+"_overlay");
			$.alerts._maintainPosition(false);
		},

		_overlay: function(status,id) {
			if(!id) id="#popup_overlay";
			switch( status ) {
				case 'show':
					$.alerts._overlay('hide',id);
					$("BODY").append('<div id="'+id.substr(1)+'"></div>');
					$(id).css({
						position: 'absolute',
						zIndex: 99996,
						top: '0px',
						left: '0px',
						width: '100%',
						height: $(document).height(),
						background: $.alerts.overlayColor,
						opacity: $.alerts.overlayOpacity
					});
				break;
				case 'hide':
					$(id).remove();
				break;
			}
			if(id != "#popup_overlay") $(id).css({zIndex: 99990});
		},

		_reposition: function(id) {
			if(!id) id = "#popup";
			var top = (($(window).height() / 2) - ($(id).outerHeight() / 2)) + $.alerts.verticalOffset;
			var left = (($(window).width() / 2) - ($(id).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();
			
			$(id).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', $.alerts._reposition);
					break;
					case false:
						$(window).unbind('resize', $.alerts._reposition);
					break;
				}
			}
		}
		
	}
	
	// Shortuct functions
	jAlert = function(message, title, callback) {
		$.alerts.alert(message, title, callback);
	}

	jConfirm = function(message, title, callback) {
		$.alerts.confirm(message, title, callback);
	};

	jPrompt = function(message, value, title, callback) {
		$.alerts.prompt(message, value, title, callback);
	};

	// added by epiday
	jLoading = function(value) {
		if(value == null || value.toLowerCase() == "on"){
			$.alerts.loading();
		} else if(value.toLowerCase() == "off") {
			$.alerts._hide("#loading");
		}
	};

	jMessage = function(message, title, callback){
		$.alerts.message(message, title, callback);
	}
	
	jDialog = function(width, height, url, params, dialogcallback, callback){
		if(width == "close"){
			$.alerts._hide("#dialog");
		} else {
			$.alerts.dialog(width, height, url, params, dialogcallback, callback);
		}
	}
	
})(jQuery);