/*
 * Dialog
 *
 * Copyright: (c) 2009 九州旅客鉄道株式会社
 *
 * Depends Script:
 *	js/jQuery/jquery.js (1.3.2)
 *	js/jQuery/ui.core.js (1.6rc6)
 *	js/jQuery/ui.draggable.js (1.6rc6)
 */

var dialog = {
	jQuery: $,

	settings: {
		background_click: false
	},

	selectors: {
		background: 'dialog-background',
		container: 'dialog-container',
		header: 'dialog-header',
		title: 'dialog-title',
		close: 'dialog-close',
		content: 'dialog-content',
		message: 'dialog-message',
		button: 'dialog-button',
		ok: 'dialog-ok',
		cancel: 'dialog-cancel'
	},

/* objects~ */
	_ready: false,
	_msie6: false,
	_unview_selectbox: [],
/* ~objects */

/* action~ */
	init: function() {
		if (this.jQuery.browser.msie && parseInt(this.jQuery.browser.version) <= 6) this._msie6 = true;
		this._ready = true;
	},

	alert: function(title, message, callback) {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		if (dialog._ready) {
			if (!title) title = 'Alert';
			if (!message) title = 'Message';
			dialog.setAlert(title, message, function(r) {
				if ($.isFunction(callback)) callback(r);
			});
		}
	},

	confirm: function(title, message, callback) {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		if (dialog._ready) {
			if (!title) title = 'Confirm';
			if (!message) title = 'Message';
			dialog.setConfirm(title, message, function(r) {
				if ($.isFunction(callback)) callback(r);
			});
		}
	},

	html: function(title, message, callback) {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		if (dialog._ready) {
			if (!title) title = 'Html';
			if (!message) title = 'Message';
			dialog.setHtml(title, message, function(r) {
				if ($.isFunction(callback)) callback(r);
			});
		}
	},

	remove: function(callback) {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		if (dialog._ready) {
			if ($.isFunction(callback)) callback();
			dialog.removeDialog();
		}
	},
/* ~action */

	setAlert: function(title, message, callback) {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		var mode = 'alert';
		dialog.removeContainer();
		dialog.setBackground();
		dialog.setContainer(mode, title, message);
		var $container = $('#' + selectors.container),
			$close = $('#' + selectors.close);
		var $button = $('#' + selectors.button);
		$('<input type="button" value="&nbsp;O&nbsp;K&nbsp;" id="' + selectors.ok + '" />').click(function() {
			dialog.removeDialog();
			callback(true);
		}).appendTo($button);
		var $ok = $('#' + selectors.ok);
		$ok.keypress(function(e) {
			if (e.keyCode == 13 || e.keyCode == 27) $ok.trigger('click');
		});
		$close.mousedown(function(e) {
			e.stopPropagation();
		}).click(function() {
			$ok.trigger('click');
		});
		if (settings.background_click) {
			var $background = $('#' + selectors.background);
			$background.click(function() {
				$ok.trigger('click');
			});
		}
		dialog.positionContainer();
		dialog.setResize();
		$ok.focus();
		dialog.viewContainer();
	},

	setConfirm: function(title, message, callback) {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		var mode = 'confirm';
		dialog.setBackground();
		dialog.setContainer(mode, title, message);
		var $container = $('#' + selectors.container),
			$close = $('#' + selectors.close);
		var $button = $('#' + selectors.button);
		$('<input type="button" value="&nbsp;O&nbsp;K&nbsp;" id="' + selectors.ok + '" />').click(function() {
			dialog.removeDialog();
			callback(true);
		}).appendTo($button);
		$('<input type="button" value="キャンセル" id="' + selectors.cancel + '" />').click(function() {
			dialog.removeDialog();
			callback(false);
		}).appendTo($button);
		var $ok = $('#' + selectors.ok),
			$cancel = $('#' + selectors.cancel);
		$ok.keypress(function(e) {
			if (e.keyCode == 27 ) $cancel.trigger('click');
		});
		$button.keypress(function(e) {
			if (e.keyCode == 27 ) $cancel.trigger('click');
		});
		$close.mousedown(function(e) {
			e.stopPropagation();
		}).click(function() {
			$cancel.trigger('click');
		});
		if (settings.background_click) {
			var $background = $('#' + selectors.background);
			$background.click(function() {
				$cancel.trigger('click');
			});
		}
		dialog.positionContainer();
		dialog.setResize();
		$ok.focus();
		dialog.viewContainer();
	},

	setHtml: function(title, message, callback) {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		var mode = 'html';
		dialog.removeContainer();
		dialog.setBackground();
		dialog.setContainer(mode, title, message);
		var $container = $('#' + selectors.container),
			$close = $('#' + selectors.close);
		$close.mousedown(function(e) {
			e.stopPropagation();
		}).click(function() {
			dialog.removeDialog();
			callback(true);
		});
		if (settings.background_click) {
			var $background = $('#' + selectors.background);
			$background.click(function() {
				$close.trigger('click');
			});
		}
		dialog.positionContainer();
		dialog.setResize();
		dialog.viewContainer();
	},

	setContainer: function(mode, title, message) {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		dialog.removeContainer();
		if (mode != 'html') {
			var container_html = '<div id="' + selectors.container + '" class="' + mode + '">' + 
									'<div id="' + selectors.header + '">' + 
										'<p id="' + selectors.title + '" class="ntxt2j">' + title + '</p>' + 
										'<p id="' + selectors.close + '"><a title="閉じる">&nbsp;</a></p>' + 
									'</div>' + 
									'<div id="' + selectors.content + '">' + 
										'<div id="' + selectors.message + '" class="txt2j">' + message.replace(/\n/g, '<br />') + '</div>' + 
										'<p id="' + selectors.button + '"></p>' + 
									'</div>' + 
								'</div>';
		} else {
			var container_html = '<div id="' + selectors.container + '" class="' + mode + '">' + 
									'<div id="' + selectors.header + '">' + 
										'<p id="' + selectors.title + '" class="ntxt2j">' + title + '</p>' + 
										'<p id="' + selectors.close + '"><a title="閉じる">&nbsp;</a></p>' + 
									'</div>' + 
									'<div id="' + selectors.content + '">' + 
										'<div id="' + selectors.message + '" class="txt2j">' + message.replace(/\n/g, '<br />') + '</div>' + 
									'</div>' + 
								'</div>';
		}
		$('body').append(container_html);
		var $container = $('#' + selectors.container),
			$header = $('#' + selectors.header),
			$title = $('#' + selectors.title),
			$close = $('#' + selectors.close);
		if (dialog._msie6) $container.css({position: 'absolute'});
		var d_width = $container.outerWidth(),
			t_width = $title.width() - $close.outerWidth() - 5,
			t_height = $title.outerHeight(),
			c_height = $close.outerHeight(),
			t_c_padding = (c_height - t_height) / 2;
		$container.css({
			opacity: 0,
			width: d_width,
			minWidth: d_width,
			maxWidth: d_width
		});
		$header.addClass('clearfix');
		$title.css({
			width: t_width,
			float: 'left'
		});
		$close.css({
			cursor: 'pointer',
			float: 'right'
		});
		if (t_c_padding > 0) {
			var $target = $title;
			$target.css({paddingTop: Math.ceil(t_c_padding)});
		} else {
			var $target = $close;
			$target.css({marginTop: Math.ceil(Math.abs(t_c_padding)) - 1});
		}
	},

	viewContainer: function() {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		var $container = $('#' + selectors.container);
		$container.animate(
			{opacity: 1},
			200,
			'swing',
			function() {
				dialog.setDrag();
			}
		);
	},

	removeContainer: function() {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		var $container = $('#' + selectors.container);
		$container.remove();
	},

	positionContainer: function() {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		var $window = $(window),
			$container = $('#' + selectors.container),
			top = (($window.height() / 2) - ($container.outerHeight() / 2)) - 75,
			left = (($window.width() / 2) - ($container.outerWidth() / 2));
		if (top < 0) top = 0;
		if (left < 0) left = 0;
		if (dialog._msie6) {
			top += $window.scrollTop();
			left += $window.scrollLeft();
		}
		$container.css({
			top: top,
			left: left
		});
	},

	setBackground: function() {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		dialog.removeBackground();
		if (dialog._msie6) {
			$('select').each(function() {
				$this = $(this);
				if ($this.is(':visible')) {
					$this.css({visibility: 'hidden'});
					dialog._unview_selectbox.push($this);
				}
			});
		}
		$('body').append('<div id="' + selectors.background + '"></div>');
		dialog.positionBackground();
	},

	removeBackground: function() {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		var $background = $('#' + selectors.background);
		$background.remove();
		if (dialog._msie6) {
			if (dialog._unview_selectbox.length > 0) {
				$.each(dialog._unview_selectbox, function(i) {
					this.css({visibility: 'visible'});
				});
			}
		}
	},

	positionBackground: function() {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		var $document = $(document),
			width = $document.width(),
			height = $document.height();
		if (dialog._msie6) {
			if (width > document.body.clientWidth) width = document.body.clientWidth;
			if (height > document.body.clientHeight) height = document.documentElement.clientHeight;
		}
		var $background = $('#' + selectors.background);
		$background.css({
			width: width,
			height: height
		});
	},

	setResize: function() {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		dialog.unsetResize();
		var $window = $(window);
		$window.bind('resize', function() {
			dialog.positionContainer();
			dialog.positionBackground();
		});
		if (dialog._msie6) {
			$window.bind('scroll', function() {
				$window.trigger('resize');
			});
		}
	},

	unsetResize: function() {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		var $window = $(window);
		$window.unbind('resize');
		if (dialog._msie6) $window.unbind('scroll');
	},

	setDrag: function() {
		var dialog = this,
			$ = this.jQuery,
			settings = this.settings,
			selectors = this.selectors;

		var $container = $('#' + selectors.container),
			$header = $('#' + selectors.header);
		$container.draggable({
			containment: 'body',
			handle: $header.css({cursor: 'move'})
		});
	},

	removeDialog: function() {
		this.removeContainer();
		this.removeBackground();
		this.unsetResize();
	}
}

$(function() {
	dialog.init();
});
