/**
 * jQuery Alert Dialogs Plugin
 * @author:jianjun.wang@jrj.com.cn
 */
(function($) {
	$.alerts = {
		verticalOffset: -75,               
		horizontalOffset: 0,               
		repositionOnResize: true,
		overlayOpacity: 0.5,               
		overlayColor: '#CCC',              
		draggable: true,                  
		okButton: '确认',
		cancelButton: '取消',              
		dialogClass: null,
		hideSelects:[],
		//Public methods
		alert: function(parameters) {
			args = {
				message:"",
				title:"Alert",
				hasCloseBtn:true,
				callback:null
			};
			jQuery.extend(args,parameters);

			$.alerts._show(args.title, args.message, null, 'alert', function(result) {
				if( args.callback ) args.callback(result);
			},args.hasCloseBtn);
		},
		
		confirm: function(parameters) {
			args = {
				message:"",
				title:"Confirm",
				hasCloseBtn:true,
				callback:null
			};
			jQuery.extend(args,parameters);
			$.alerts._show(args.title, args.message, null, 'confirm', function(result) {
				if( args.callback ) args.callback(result);
			},args.hasCloseBtn);
		},
			
		prompt: function(parameters) {
			args = {
				message:"",
				value:"",				
				title:"Prompt",
				hasCloseBtn:true,
				callback:null
			};
			jQuery.extend(args,parameters);
			$.alerts._show(args.title, args.message, args.value, 'prompt', function(result) {
				if( args.callback ) args.callback(result);
			},args.hasCloseBtn);
		},
		
		// Private methods		
		_show: function(title, msg, value, type, callback ,hasCloseBtn) {
			$.alerts._overlay('show');
			$("#popup_container").remove();
			var html = '<div id="popup_container">' +'<h1 id="popup_title"><span id="popup_title_text"></span></h1>' 
			if(hasCloseBtn){
			    html+='<img id="popup_close" alt="关闭" src="http://i0.jrj.com.cn/myjrj/shut.gif" unselectable="on" style="-moz-user-select:none;"/>'
			}
			html+= '<div id="popup_content">' ;
			html+= '<div id="popup_message"></div>';
			html+= '</div>';
			html+= '</div>';
			$("BODY").append(html);
			
			if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass);
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
			
			$("#popup_container").css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				margin: 0
			});

			$("#popup_title_text").html( title );
			$("#popup_title_text")[0].unselectable="on";
      $("#popup_content").addClass(type);
      $("#popup_message").addClass("popup_message_"+type);
			$("#popup_message").text(msg);
			$("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br/>') );
		  
			$("#popup_close").click( function() {
				 $.alerts._hide();
			});
			
			$.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();
					});
					$("#popup_ok").focus();
					$("#popup_ok").keypress( function(e) {
						if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
					});
					$("#popup_cancel").keypress( function(e) {
						if( e.keyCode == 13 || e.keyCode == 27 )
						  $("#popup_cancel").trigger('click');
					});
				break;
				case 'prompt':
					$("#popup_message").append('<br/><input type="text" size="25" 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( 250 );
					$("#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").keypress( function(e) {
						if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
					});
					$("#popup_cancel").keypress( function(e) {
						if( e.keyCode == 13||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_container").draggable({ handle: $("#popup_title") });
					$("#popup_title").css({ cursor: 'move' });
				} catch(e) { /* requires jQuery UI draggables */ }
			}
		},
		
		_hide: function() {
		  $.alerts.okButton = '确认';
		  $.alerts.cancelButton = '取消';
			$("#popup_container").remove();
			$.alerts._overlay('hide');
			$.alerts._maintainPosition(false);
		},
		
		_overlay: function(status) {
			switch( status ) {
				case 'show':
					$.alerts._overlay('hide');
					$("BODY").append('<div id="popup_overlay"></div>');
					$("#popup_overlay").css({
						position: 'absolute',
						zIndex: 99998,
						top: '0px',
						left: '0px',
						width: $(window).width(),
						height: $(document).height(),
						background: $.alerts.overlayColor,
						opacity: $.alerts.overlayOpacity
					});
					if(navigator.appVersion.indexOf("MSIE 6.0") != -1){
						$("#popup_overlay").bgiframe();
					}
				break;
				case 'hide':
					$("#popup_overlay").remove();

				break;
			}
		},
		//重定位
		_reposition: function() {
			var winHeight = $(window).height();
			var winWidth = $(window).width();
			if(jQuery.browser.opera){
				winHeight = document.documentElement.clientHeight ;
				winWidth = document.documentElement.clientWidth; 
			}
			var top = ((winHeight / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
			var left = ((winWidth / 2) - ($("#popup_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();
			
			$("#popup_container").css({
				top: top + 'px',
				left: left + 'px'
			});
			
			$("#popup_overlay").css({
				width: $(window).width(),
			  height: $(document).height()
			})
		},
		//保持位置
		_maintainPosition: function(status) {
			if( $.alerts.repositionOnResize ) {
				switch(status) {
					case true:
						$(window).bind('resize', function() {
							$.alerts._reposition();
						});
						$(window).bind('scroll', function() {
							$.alerts._reposition();
						});
					break;
					case false:
						$(window).unbind('resize',$.alerts._reposition);
						$(window).unbind('scroll',$.alerts._reposition);
					break;
				}
			}
		}		
	}
})(jQuery);

/**
 * jQuery Dialogs Plugin
 */
(function($) {
	$.dialog = {			
		verticalOffset: -75,                // 基于居中的垂直偏移量, in pixels
		horizontalOffset: 0,                // 水平偏移量, in pixels/
		repositionOnResize: true,           // 重定位窗口
		overlayOpacity: 0.5,                // overlay透明度
		overlayColor: '#CCC',               // overlay color
		draggable: false,                    // 拖拽标志
		okButton: '确认',                   // OK button文本
		cancelButton: '取消',               // Cancel button文本
		dialogClass: null,                  // dialogs css
		hasCancelBtn:true,                  // 是否要Cancel按钮
		hasContentBtmLine:false,            // 是否要content底线
		hideSelects:[],
				
		modeDialog:function(parameters) {
			args = {
				content:"",
				title:"",
				callback:null
			};
			jQuery.extend(args,parameters);
			$.dialog._show(args.title, args.content, "modeDialog", function(result) {
				if( args.callback ) return args.callback(result);
			});
		},
		
		redraw:function(parameters){
			$("#dialog_container").remove();
			args = {
				content:"",
				title:"",
				callback:null
			};
			jQuery.extend(args,parameters);
			$.dialog._show(args.title, args.content, "modeDialog", function(result) {
				if( args.callback ) return args.callback(result);
			});
		},
		
		// Private methods
		_show: function(title, content , type, callback ){
			$.dialog._overlay('show');
			$("#dialog_container").remove();
			var dialogHtml = '<div id="dialog_container">' +
			    '<div id="dialog_title"></div>' +
			    '<img id="dialog_close" alt="关闭" src="http://i0.jrj.com.cn/myjrj/shut.gif" unselectable="on" style="-moz-user-select:none;"/>'+
			    '<div id="dialog_body">' +
			      '<div id="dialog_content"></div>' +
				  '</div>' +
			  '</div>'
			
			$("BODY").append(dialogHtml);
						
			if( $.dialog.dialogClass ) $("#dialog_container").addClass($.dialog.dialogClass);
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed';
			$("#dialog_container").css({
				position:  pos,
				zIndex: 99999,
				padding: 0,
				margin: 0
			});
			      
			$("#dialog_title").html( title );
			$("#dialog_title")[0].unselectable="on";

			$("#dialog_content").addClass(type);
			if(!$.dialog.hasContentBtmLine)  $("#dialog_content").addClass("none_bottom");
			$("#dialog_content").text(content);
			$("#dialog_content").html( $("#dialog_content").text().replace(/\n/g, '<br/>'));
			
			$("#dialog_container").css({
				minWidth: $("#dialog_container").outerWidth(),
				maxWidth: $("#dialog_container").outerWidth()
			});
			
			$("#dialog_close").click( function() {
					$.dialog._hide();
			});
			
			$.dialog._reposition();
			$.dialog._maintainPosition(true);
			
			switch( type ) {
				case 'modeDialog':
				  if($.dialog.hasCancelBtn)
					  $("#dialog_content").after('<div id="dialog_panel"><input type="button" value="' + $.dialog.okButton + '" id="dialog_ok" /><input type="button" value="' + $.dialog.cancelButton + '" id="dialog_cancel"/></div>');
					else
						$("#dialog_content").after('<div id="dialog_panel"><input type="button" value="' + $.dialog.okButton + '" id="dialog_ok" /></div>');
					$("#dialog_ok").click( function() {
						var notClose=false;
						if( callback ) {
							//1.返回true不关闭对话框，2.无返回或返回false则关闭
							notClose = callback(true);
							if(!notClose)
							  $.dialog._hide();
						}
						else
						   $.dialog._hide();
					});
					$("#dialog_cancel").click( function() {
						$.dialog._hide();
						//if( callback ) callback(false);
					});
					$("#dialog_ok").focus();
					$("#dialog_ok").keypress( function(e) {						
						if( e.keyCode == 27 ) $("#dialog_cancel").trigger('click');
					});
					$("#dialog_cancel").keypress( function(e) {
						if( e.keyCode == 13||e.keyCode == 27 )
						  $("#dialog_cancel").trigger('click');
					});
				break;
			}
			
			// Make draggable
			if( $.dialog.draggable ) {
				try {
					$("#dialog_container").draggable({ handle: $("#dialog_title") });
					$("#dialog_title").css({ cursor: 'move' });
				} catch(e) {  }
			}
		},
		
		_hide: function() {
		  $.dialog.okButton = '确认';
		  $.dialog.cancelButton = '取消';
		  $.dialog.hasCancelBtn = true;
		  $.dialog.hasContentBtmLine = false;
			$("#dialog_container").remove();
			$.dialog._overlay('hide');
			$.dialog._maintainPosition(false);
		},
		
		_overlay: function(status) {
			switch( status ) {
				case 'show':
					$.dialog._overlay('hide');
					$("BODY").append('<div id="dialog_overlay"></div>');
					$("#dialog_overlay").css({
						position: 'absolute',
						zIndex: 99998,
						top: '0px',
						left: '0px',
						width: $(window).width(),
						height: $(document).height(),
						background: $.dialog.overlayColor,
						opacity: $.dialog.overlayOpacity
					});

				  if(navigator.appVersion.indexOf("MSIE 6.0") != -1){
						$("#popup_overlay").bgiframe();
					}

				break;
				case 'hide':
					$("#dialog_overlay").remove();
				break;
			}
		},
		//重定位
		_reposition: function() {
			var winHeight = $(window).height();
			var winWidth = $(window).width();
			if(jQuery.browser.opera){
				winHeight = document.documentElement.clientHeight ;
				winWidth = document.documentElement.clientWidth; 
			}
			var top = ((winHeight / 2) - ($("#dialog_container").outerHeight() / 2)) + $.dialog.verticalOffset;
			var left = ((winWidth / 2) - ($("#dialog_container").outerWidth() / 2)) + $.dialog.horizontalOffset;
			if( top < 0 ) top = 0;
			if( left < 0 ) left = 0;
			
			// IE6 fix
			if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
			
			$("#dialog_container").css({
				top: top + 'px',
				left: left + 'px'
			});
			
			$("#dialog_overlay").css({
				width: $(window).width(),
			  height: $(document).height()
			})
		},
		//window resize或scroll时保持位置
		_maintainPosition: function(status) {
			if( $.dialog.repositionOnResize ) {
				switch(status) {
					case true:
						$(window).bind('resize', function() {
							$.dialog._reposition();
						});
						$(window).bind('scroll', function() {
							$.dialog._reposition();
						});
					break;
					case false:
						$(window).unbind('resize',$.dialog._reposition);
						$(window).unbind('scroll',$.dialog._reposition);
					break;
				}
			}
		}
	}	
})(jQuery);
