(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: .01,                // transparency level of overlay
		overlayColor: '#FFF',               // base color of overlay
		draggable: false,                    // make the dialogs draggable (requires UI Draggables plugin)
		okButton: '&nbsp;OK&nbsp;',         // text for the OK button
		cancelButton: '&nbsp;Cancel&nbsp;', // 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 = 'Alert';
			$.alerts._show(title, message, null, '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);
			});
		},

        prompt2: function(message, value, title, callback) {
            if( title == null ) title = 'Prompt2';
            $.alerts._show(title, message, value, 'prompt2', function(result) {
                if( callback ) callback(result);
            });
        },
		
		// Private methods
		
		_show: function(title, msg, value, type, callback) {
			
			$.alerts._hide();
			$.alerts._overlay('show');
			
			$("BODY").append(
			  '<div id="jpopup_container">' +
			    '<h1 id="jpopup_title"></h1>' +
			    '<div id="jpopup_content">' +
			      '<div id="jpopup_message"></div>' +
				'</div>' +
			  '</div>');

			$.blockUI();
			
			if( $.alerts.dialogClass ) $("#jpopup_container").addClass($.alerts.dialogClass);
			
			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
			
			$("#jpopup_container").css({
				position: pos,
				zIndex: 1006,
				padding: 0,
				margin: 0
			});
			
			$("#jpopup_title").text(title);
			$("#jpopup_content").addClass(type);
			
			if (type == 'prompt') {
			    $("#jpopup_container").css({minWidth: 400, maxWidth: 680, padding: '0px'});
			    $("#jpopup_message").css({padding: '0px'});
			    $.alerts.verticalOffset = -200;
			    $.alerts.horizontalOffset = -140;
			    $.alerts.okButton = '&nbsp;Fermer&nbsp;';
			}
            else if (type == 'prompt2') {
                $("#jpopup_container").css({minWidth: 800, maxWidth: 1000, width: '950px', padding: '0px', overflow: 'auto', height: '700px'});
                $("#jpopup_message").css({padding: '0px'});
				$.alerts.verticalOffset = 0;
                $.alerts.horizontalOffset = 0;
                $.alerts.okButton = '&nbsp;Fermer&nbsp;';
            }
			else {
			    $("#jpopup_container").css({
				minWidth: $("#jpopup_container").outerWidth(),
				maxWidth: $("#jpopup_container").outerWidth()
			    });
			    $("#jpopup_message").text(msg);
			    $("#jpopup_message").html( $("#jpopup_message").text().replace(/\n/g, '<br />') );
			}

			$.alerts._reposition();
			$.alerts._maintainPosition(true);
			
			switch( type ) {
				case 'alert':
					$("#jpopup_message").after('<div id="jpopup_panel"><input type="button" value="' + $.alerts.okButton + '" id="jpopup_ok" /></div>');
					$("#jpopup_ok").click( function() {
						$.alerts._hide();
						callback(true);
					});
//					$("#jpopup_ok").focus().keypress( function(e) {
//						if( e.keyCode == 13 || e.keyCode == 27 ) $("#jpopup_ok").trigger('click');
//					});
				break;
				case 'confirm':
					$("#jpopup_message").after('<div id="jpopup_panel"><input type="button" value="' + $.alerts.okButton + '" id="jpopup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="jpopup_cancel" /></div>');
					$("#jpopup_ok").click( function() {
						$.alerts._hide();
						if( callback ) callback(true);
					});
					$("#jpopup_cancel").click( function() {
						$.alerts._hide();
						if( callback ) callback(false);
					});
					$("#jpopup_ok").focus();
					$("#jpopup_ok, #jpopup_cancel").keypress( function(e) {
						if( e.keyCode == 13 ) $("#jpopup_ok").trigger('click');
						if( e.keyCode == 27 ) $("#jpopup_cancel").trigger('click');
					});
				break;
				case 'prompt':
					switch(value) {
					case '':
					$("#jpopup_message").html('<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/'+msg+'?fs=1&amp;hl=ru_RU&amp;color1=0x006699&amp;color2=0x54abd6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+msg+'?fs=1&amp;hl=ru_RU&amp;color1=0xa8a8a8&amp;color2=0xd6d6d6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>');
					break;
					case '1':
                    $("#jpopup_message").html('<object width="640" height="332"><param name="movie" value="/references/'+msg+'"><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><EMBED src="/references/'+msg+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="332" style="border:1px solid #CCC;" ></EMBED></object>');
					break;
					case '2':
                    $("#jpopup_message").html('<img src="/references/'+msg+'" border="0">');
					break;
					}

					$("#jpopup_message").append('<br /><div id="jpopup_panel"><input type="button" value="' + $.alerts.okButton + '" id="jpopup_ok" /></div>');
					$("#jpopup_prompt").width( $("#jpopup_message").width() );
					$("#jpopup_ok").click( function() {
						var val = $("#jpopup_prompt").val();
						$.alerts._hide();
						if( callback ) callback( val );
					});
					$("#jpopup_cancel").click( function() {
						$.alerts._hide();
						if( callback ) callback( null );
					});
/*
					$("#jpopup_prompt, #jpopup_ok, #jpopup_cancel").keypress( function(e) {
						if( e.keyCode == 13 ) $("#jpopup_ok").trigger('click');
						if( e.keyCode == 27 ) $("#jpopup_cancel").trigger('click');
					});
					if( value ) $("#jpopup_prompt").val(value);
					$("#jpopup_prompt").focus().select();
*/
				break;
                case 'prompt2':			
				switch(value) {
				case '':
                    $("#jpopup_message").html(msg);
				break;
				case '1':
					$("#jpopup_message").html('<img src="/references/'+msg+'" border="0" style="border:1px solid #333;">');
				break;
				}

                    $("#jpopup_message").append('<br /><div id="jpopup_panel"><input type="button" value="' + $.alerts.okButton + '" id="jpopup_ok" /></div>');
                    $("#jpopup_prompt2").width( $("#jpopup_message").width() );
                    $("#jpopup_ok").click( function() {
                        var val = $("#jpopup_prompt2").val();
                        $.alerts._hide();
                        if( callback ) callback( val );
                    });
                    $("#jpopup_cancel").click( function() {
                        $.alerts._hide();
                        if( callback ) callback( null );
                    });
                break;
			}
			
			// Make draggable
			if( $.alerts.draggable ) {
				try {
					$("#jpopup_container").draggable({ handle: $("#jpopup_title") });
					$("#jpopup_title").css({ cursor: 'move' });
				} catch(e) { /* requires jQuery UI draggables */ }
			}
		},
		
		_hide: function() {
			$("#jpopup_container").remove();
			$.alerts._overlay('hide');
			$.alerts._maintainPosition(false);
			$.unblockUI();
		},
		
		_overlay: function(status) {
			switch( status ) {
				case 'show':
					$.alerts._overlay('hide');
					$("BODY").append('<div id="jpopup_overlay"></div>');
					$("#jpopup_overlay").css({
						position: 'absolute',
						zIndex: 999,
						top: '0px',
						left: '0px',
						width: '100%',
						height: $(document).height(),
						background: $.alerts.overlayColor,
						opacity: $.alerts.overlayOpacity
					});
				break;
				case 'hide':
					$("#jpopup_overlay").remove();
				break;
			}
		},
		
		_reposition: function(type) {
			var top = (($(window).height() / 2) - ($("#jpopup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
			var left = (($(window).width() / 2) - ($("#jpopup_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();
			
			$("#jpopup_container").css({
				top: top + 'px',
				left: left + 'px'
			});
			$("#jpopup_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);
	};

    jPrompt2 = function(message, value, title, callback) {
        $.alerts.prompt2(message, value, title, callback);
    };
	
})(jQuery);

