/*
jAlert( message, [params, callback] )
jConfirm( message, [params, callback] )
jPrompt( message, [params, callback] )
params={
	title
	button_ok
	button_no
	buttons
	value
	length_value
	url
	lifetime
	width
}
*/

(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: .6,                // transparency level of overlay
		overlayColor: '#ffffff',               // base color of overlay
		draggable: false,                    // make the dialogs draggable (requires UI Draggables plugin)
		buttons: true,         // display buttons
		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
		speedOpen: 300,
		speedClose: 200,
		lifeTime:0,                         // set a time before remove dialog in seconds
		closeImg:true,                     // show a to close image in title
		closeImgPath:'/images/alerts-close.gif', // path to close image in title

		// Public methods

		alert: function(message, params, callback) {
			$.alerts._show(params, message, 'alert', function(result) {
				if( callback ) callback(result);
			});
		},

		confirm: function(message, params, callback) {
			$.alerts._show(params, message, 'confirm', function(result) {
				if( callback ) callback(result);
			});
		},

		prompt: function(message, params, callback) {
			$.alerts._show(params, message, 'prompt', function(result) {
				if( callback ) callback(result);
			});
		},

		// Private methods

		_show: function(params, msg, type, callback) {

			if(!params) params=new Array();
			$.alerts._overlay('show');

			tmp='<table id="popup_container" style="display:none" cellspacing="0" cellpadding="0"><tr>';
			tmp+='<td class="corner1"><img src="/images/spacer.gif" /></td>';
			tmp+='<td class="title"><a href="javascript://" onclick="jClose()" class="textclose">Закрыть Х</a><h1 id="popup_title"></h1></td>';
			tmp+='<td class="corner2"><img src="/images/spacer.gif" /></td></tr>';
			tmp+='<tr><td class="left"><img src="/images/spacer.gif" /></td>';
			tmp+='<td class="popup_center"><div id="popup_content"><div id="popup_message"></div></div></td>';
			tmp+='<td class="right"><img src="/images/spacer.gif" /></td></tr>';
			tmp+='<tr><td class="corner3"><img src="/images/spacer.gif" /></td>';
			tmp+='<td class="bottom"><img src="/images/spacer.gif" /></td>';
			tmp+='<td class="corner4"><img src="/images/spacer.gif" /></td></tr></table>';
			$("BODY").append(tmp);

			//if($.alerts.closeImg) $("#popup_container .title").prepend('<img scr="'+$.alerts.closeImgPath+'" id="popup_imgclose" alt="close" onclick="$.alerts._hide()" />').children().eq(0).attr('src', $.alerts.closeImgPath);
			if($.alerts.dialogClass) $("#popup_container").addClass($.alerts.dialogClass);
			$("#popup_container").animate({opacity:'show'}, $.alerts.speedOpen);

			// IE6 Fix
			var pos = ($.browser.msie /*&& (parseInt($.browser.version) <= 6 || parseInt($.browser.version)==8)*/) ? 'absolute' : 'fixed';

			$("#popup_container").css({
				position: pos,
				zIndex: 999,
				padding: 0,
				margin: 0
			});

			if(params['title']==null) params['title']='&nbsp;';
			$("#popup_title").html(params['title']);
			//$("#popup_content").addClass(type);
			if(params['url']) $.get(params['url'], function(s){$("#popup_message").html(s);}); else $("#popup_message").html(msg);

			$.alerts._reposition();
			$.alerts._maintainPosition(true);

			if(params['button_ok']) okButton=params['button_ok']; else okButton=$.alerts.okButton;
			if(params['button_no']) cancelButton=params['button_no']; else cancelButton=$.alerts.cancelButton;
			if(params['width']) $("#popup_container").css({				'width':params['width']+'px',
				'left':Math.round(($(window).width()-params['width'])/2)+'px'
			}); else $("#popup_container").css({
				minWidth: $("#popup_container").outerWidth(),
				maxWidth: $("#popup_container").outerWidth()
			});

			switch( type ) {
				case 'alert':
					$("#popup_message").after('<div id="popup_panel"><input type="button" value="' + okButton + '" id="popup_ok" class="submit" /></div>');
					if(params['buttons']==false) $('#popup_panel').hide();
					$("#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="' + okButton + '" id="popup_ok" class="submit" /> <input type="button" value="' + cancelButton + '" id="popup_cancel" class="submit" /></div>');
					if(params['buttons']==false) $('#popup_panel').hide();
					$("#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" maxlength="'+params['length_value']+'" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + okButton + '" id="popup_ok" class="submit" /> <input type="button" value="' + cancelButton + '" id="popup_cancel" class="submit" /></div>');
					if(params['buttons']==false) $('#popup_panel').hide();
					$("#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(params['value']) $("#popup_prompt").val(params['value']);
					$("#popup_prompt").focus().select();
				break;
			}
			if(params['lifetime']) setTimeout(function(){ $.alerts._hide(); }, params['lifetime']*1000);
			else if($.alerts.lifeTime) setTimeout(function(){ $.alerts._hide(); }, $.alerts.lifeTime*1000);
		},

		_hide: function(callback) {
			$("#popup_container").animate({opacity:'hide'}, $.alerts.speedClose, function(){
				$(this).remove();
				$.alerts._maintainPosition(false);
				if(callback) callback();
			});
			$.alerts._overlay('hide');
		},

		_overlay: function(status) {
			switch( status ) {
				case 'show':
					$.alerts._overlay('hide');
					$("BODY").append('<div id="popup_overlay"></div>');
					$("#popup_overlay").css({
						position: 'absolute',
						zIndex: 990,
						top: '0px',
						left: '0px',
						width: '100%',
						height: $(document).height(),
						background: $.alerts.overlayColor,
						opacity: $.alerts.overlayOpacity,
						display:'none'
					}).bind('click', function(){$.alerts._hide()}).animate({opacity:'show'}, $.alerts.speedOpen);
				break;
				case 'hide':
					$("#popup_overlay").animate({opacity:'hide'}, $.alerts.speedClose, function(){ $(this).remove(); });
				break;
			}
		},

		_reposition: function() {
			var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
			var left = Math.round((($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset);
			if($("#popup_container").css('position')=='absolute') top+=$('body').scrollTop();
			if( top < 10 ) top = 10;
			if( left < 0 ) left = 0;

			// IE6 fix
			if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();

			$("#popup_container").css({
				'top': top + 'px',
				'left': left + 'px'
			});
			$("#popup_overlay").height( $(document).height() );
		},

		_maintainPosition: function(status) {
			if( $.alerts.repositionOnResize ) {
				if($("#popup_container").css('position')=='absolute') $(window).bind('scroll', function(){ $.alerts._reposition()});
				switch(status) {
					case true:
						$(window).bind('resize', $.alerts._reposition);
					break;
					case false:
						$(window).unbind('resize', $.alerts._reposition);
					break;
				}
			}
		}

	}

	// Shortuct functions
	jAlert = function(message, params, callback) {
		$('#popup_container').remove();
		$('#popup_overlay').remove();
		$.alerts.alert(message, params, callback);
	}

	jConfirm = function(message, params, callback) {
		$('#popup_container').remove();
		$('#popup_overlay').remove();
		$.alerts.confirm(message, params, callback);
	};

	jPrompt = function(message, params, callback) {
		$('#popup_container').remove();
		$('#popup_overlay').remove();
		$.alerts.prompt(message, params, callback);
	};

	jClose = function() {
		$('#popup_container').remove();
		$('#popup_overlay').remove();
	};

})(jQuery);