/*

	2010/09/28	: ver.1.1
		インプットウィンドウ追加
		ボタンコールバックが動かないバグ修正

	2010/08/17	: ver.1.0
		一通り動作

*/

//------------------------------------------------------------------------------
//	インプットウィンドウ表示
/*

	crowm_window_input(
		"タイトル",
		"メッセージ",
		"デフォルト入力文",
		OK押下時のコールバック,
		[, ウィンドウの詳細パラメータ]
	);

	※ 通常のウィンドウの詳細パラメータに、以下を追加。
	1. callback_ok （OK時のコールバック）
		指定がなければ、function(text){return(true);}

	2. callback_cancel（キャンセル時のコールバック）
		指定がなければ、function(text){return(true);}

*/
//------------------------------------------------------------------------------
function crowm_window_input( _title, _msg, _default, _param ) {

	if( ! _param ) _param = {};

	var new_input_id = "crowm_input_id_" + (_crowm_input_id++);
	var _buttons = [
		{
			name : "OK",
			callback : function(){
				if( _param.callback_ok ) return( _param.callback_ok($('#'+new_input_id).val()) );
				return( true );
			}
		},
		{
			name : "Cancel",
			callback : function(){
				if( _param.callback_cancel ) return( _param.callback_cancel($('#'+new_input_id).val()) );
				return( true );
			}
		}
	];

	var param = {
		top: "50%",
		left: "50%",
		width: $(window).width()/3,
		height: 150,
		draggable: false,
		scrollable: true,
		visible: false,
		close: true,
		modal: true,
		buttons: _buttons
	};
	if( _param != null ){
		if( _param.top != null ) param.top = _param.top;
		if( _param.left != null ) param.left = _param.left;
		if( _param.width != null ) param.width = _param.width;
		if( _param.height != null ) param.height = _param.height;
		if( _param.scrollable != null ) param.scrollable = _param.scrollable;
		if( _param.visible != null ) param.visible = _param.visible;
		if( _param.close != null ) param.close = _param.close;
		if( _param.close_callback != null ) param.close_callback = _param.close_callback;
		if( _param.modal != null ) param.modal = _param.modal;
	}
	var body_html = '<div style="padding:16px;font-size:12px;line-height:150%;">'+_msg+'<br><input type="text" class="textbox" id="'+new_input_id+'" style="width:100%" maxlength=1024 value="'+_default+'"></div>';
	crowm_window( _title ? _title : "入力", body_html, param).data("crowm_input_id", new_input_id).fadeIn("fast");
}


//------------------------------------------------------------------------------
//	警告ウィンドウ表示
/*

	crowm_window_alert(
		"タイトル",
		"メッセージ"
		[, ウィンドウの詳細パラメータ]
	);

	※ 通常のウィンドウの詳細パラメータに、以下を追加。
	1. callback_yes （「はい」時のコールバック）
		指定がなければ、function(text){return(true);}

	2. callback_no（「いいえ」時のコールバック）
		指定がなければ、function(text){return(true);}
*/
//------------------------------------------------------------------------------
function crowm_window_alert( _title, _msg, _param ) {

	if( ! _param ) _param = {};

	var _buttons = [
		{
			name : "はい",
			callback : function(){
				if( _param.callback_yes ) return( _param.callback_yes() );
				return( true );
			}
		},
		{
			name : "いいえ",
			callback : function(){
				if( _param.callback_no ) return( _param.callback_no() );
				return( true );
			}
		}
	];

	var param = {
		top: "50%",
		left: "50%",
		width: $(window).width()/3,
		height: 150,
		draggable: false,
		scrollable: true,
		visible: false,
		close: true,
		modal: true,
		buttons: _buttons
	};
	if( _param != null ){
		if( _param.top != null ) param.top = _param.top;
		if( _param.left != null ) param.left = _param.left;
		if( _param.width != null ) param.width = _param.width;
		if( _param.height != null ) param.height = _param.height;
		if( _param.scrollable != null ) param.scrollable = _param.scrollable;
		if( _param.visible != null ) param.visible = _param.visible;
		if( _param.close != null ) param.close = _param.close;
		if( _param.close_callback != null ) param.close_callback = _param.close_callback;
		if( _param.modal != null ) param.modal = _param.modal;
	}
	var body_html = '<table width="100%"><tr><td valign="top" width="1%" nowrap style="padding:4px;"><img src="winimg/alert.png" /></td><td valign="top" class="crowm_window_alert_msg">' + _msg + '</td></tr></table>';
	crowm_window( _title ? _title : "警告", body_html, param).fadeIn("fast");
}


//------------------------------------------------------------------------------
//	crowm_window作成コア
/*
	crowm_window(
		"タイトル文字列",				//	文字列でも、オブジェクトでもよい
		"内容",							//	文字列でも、オブジェクトでもよい
		{
			id: "windowid",				//	windowのID、省略可能
			top: "100%",				//	Y座標（"100%" or "50%" or 座標数値）
			left: 50,					//	X座標（"100%" or "50%" or 座標数値）
			width: 100,					//	横幅
			height: 300,				//	高さ
			draggable: true,			//	移動可能？
			scrollable: true,			//	内容が入りきれない場合、スクロールバーを表示するか？
			visible: true,				//	初回表示するか？
			close: true,				//	閉じるボタンがあるか？
			close_callback: func(){},	//	閉じるボタン押下時のコールバック（false返却で閉じない）
			parent: "親エレメントのID",	//	省略時はbodyの子となる
			modal:true,					//	モーダルウィンドウ？
			small:false					//	小さいタイトルバー？（デフォルトはfalse）
		}
	);
*/
//
//------------------------------------------------------------------------------
function crowm_window( _title_html, _body_html, _param ) {

	if( ! _param ) _param = {};

	//	htmlを作り変える
	var is_title = false;
	if( _title_html != "" ) is_title = true;

	var width = _param.width || _crowm_def_width;
	var height = _param.height || _crowm_def_height;
	if( width < _crowm_min_width ) width = _crowm_min_width;
	if( height < _crowm_min_height ) height = _crowm_min_height;

	var scrollable = false;
	if( _param.scrollable == true ){
		scrollable = true;
	}

	//	ウィンドウの外枠作成
	var new_window_id = "";
	var new_window_table_id = "";
	var new_window_title_id = "";
	var new_window_title_td_id = "";
	var new_window_body_id = "";
	var new_window_close_id = "";
	var new_window_resize_id = "";
	var new_window_gray_id = "";

	if( _param.id != null ){
		new_window_id			= _param.id;
		new_window_table_id		= "crowm_window_table_" + new_window_id;
		new_window_title_id		= "crowm_window_title_" + new_window_id;
		new_window_title_td_id	= "crowm_window_title_td_" + new_window_id;
		new_window_body_id		= "crowm_window_body_" + new_window_id;
		new_window_close_id		= "crowm_window_close_" + new_window_id;
		new_window_resize_id	= "crowm_window_resize_" + new_window_id;
		new_window_gray_id		= "crowm_window_gray_" + new_window_id;
	}else{
		var new_id				= _crowm_window_id++;
		new_window_id			= "crowm_window_" + new_id;
		new_window_table_id		= "crowm_window_table_" + new_id;
		new_window_title_id		= "crowm_window_title_" + new_id;
		new_window_title_td_id	= "crowm_window_title_td_" + new_id;
		new_window_body_id		= "crowm_window_body_" + new_id;
		new_window_close_id		= "crowm_window_close_" + new_id;
		new_window_resize_id	= "crowm_window_resize_" + new_id;
		new_window_gray_id		= "crowm_window_gray_" + new_id;
	}

	//	親の決定
	var parent = _param.parent ? $('#'+_param.parent) : $('body');

	//	モーダルの場合、グレイウィンドウを作成
	if( _param.modal ){
		$('<div class="crowm_window_gray" id="' + new_window_gray_id + '"></div>').appendTo(parent).css( {
			position	: 'absolute',
			top			: 0,
			left		: 0,
			width		: $(document).width(),
			height		: $(document).height(),
			zIndex		: _crowm_zindex++,
			opacity		: 0
		} ).animate( {opacity:0.5}, 800 );
	}

	//	外枠の <div> 作成
	var win = $('<div class="crowm_window_div" style="padding:0px;margin:0px;" id="' + new_window_id + '"></div>').appendTo(parent).css( {
		position	: 'absolute',
		display		: 'none',
		width		: width,
		height		: height,
		zIndex		: _crowm_zindex++
	} )
		.data( "window_id",			new_window_id )
		.data( "window_table_id",	new_window_table_id )
		.data( "window_title_id",	new_window_title_id )
		.data( "window_body_id",	new_window_body_id )
		.data( "window_close_id",	new_window_close_id )
		.data( "window_resize_id",	new_window_resize_id )
		.data( "window_gray_id",	new_window_gray_id )
		.data( "is_modal",			_param.modal ? true : false )
		.data( "close_callback",	_param.close_callback ? _param.close_callback : null );

	if( _param.parent ){
		var _overflow = $('#'+_param.parent).css('overflow');
		if( _overflow != 'scroll' && _overflow != 'hidden' ){
			$('#'+_param.parent).css( {
				overflow : scrollable ? 'auto' : 'hidden',
				position : 'relative'
			} );
		}else{
			$('#'+_param.parent).css( {
				position : 'relative'
			} );
		}
	}

	//	中の <table> 作成
	var win_table = $('<table class="crowm_window_table" cellpadding=0 cellspacing=0 id="' + new_window_table_id + '"></table>').appendTo(win).css( {
		width		: "100%",
		height		: "100%"
	} );


	//	<table> の中身作成
	if( is_title ){
		var client_width = $('#'+new_window_id).width() - 11 - 12 - 8; // 角丸画像のサイズを引く
		var client_height = $('#'+new_window_id).height() - 36 - 15 - 8; // 角丸画像のサイズを引く
		var title_fontstyle = "font-size:14px;font-weight:bold;";
		if( _param.small ){
			 client_height = $('#'+new_window_id).height() - 20 - 15 - 8; // 角丸画像のサイズを引く
			title_fontstyle = "font-size:10px";
		}

		//	タイトルバーのクラス
		var topleft_class = "crowm_win_t_topleft";
		var top_class = "crowm_win_t_top";
		var topright_class = "crowm_win_t_topright";
		if( _param.small ){
			topleft_class = "crowm_win_ts_topleft";
			top_class = "crowm_win_ts_top";
			topright_class = "crowm_win_ts_topright";
		}

		//	titleが素のHTMLの場合と、jQueryオブジェクトの場合がある
		if( typeof _title_html == "object" ||
			typeof _title_html == "Object"
		){
			win_table.append(
				$('<tr></tr>').append(
					$('<td class="' + topleft_class + '"></td>')
				).append(
					$('<td class="' + top_class + '" nowrap></td>').append(
						$('<table width="100%" cellpadding=0 cellspacing=0></table>').append(
							$('<tbody></tbody>').append(
								$('<tr></tr>').append(
									$('<td id="' + new_window_title_td_id + '"></td>').append(
										$('<font color="white" style="' + title_fontstyle + '" id="' + new_window_title_id + '"></font>').append( _title_html )
									)
								)
							)
						)
					)
				).append(
					$('<td class="' + topright_class + '"></td>')
				)
			);
		}else{
			$(	'<tr><td class="' + topleft_class + '"></td>'
				+ '<td class="' + top_class + '" nowrap>'
				+ '<table width="100%" cellpadding=0 cellspacing=0><tr><td id="' + new_window_title_td_id + '"><font color="white" style="' + title_fontstyle + '" id="' + new_window_title_id + '">' + _title_html + '</font></td></tr></table>'
				+ '</td><td class="' + topright_class + '"></td></tr>'
			).appendTo(win_table);
		}

		//	bodyが素のHTMLの場合と、jQueryオブジェクトの場合がある
		if( typeof _body_html == "object" ||
			typeof _body_html == "Object"
		){
			win_table.append(
				$('<tr></tr>').append(
					$('<td class="crowm_win_t_left"></td>')
				).append(
					$('<td class="crowm_win_t_center"></td>').append(
						$('<div id="' +  new_window_body_id + '" style="overflow:' + (scrollable ? 'auto' : 'hidden') + ';width:' + client_width + ';height:' + client_height + ';"></div>')
						.append(_body_html)
					)
				).append(
					$('<td class="crowm_win_t_right"></td>')
				)
			);
		}else{
			$(	'<tr><td class="crowm_win_t_left"></td>'
				+ '<td class="crowm_win_t_center"><div id="' +  new_window_body_id + '" style="overflow:' + (scrollable ? 'auto' : 'hidden') + ';width:' + client_width + ';height:' + client_height + ';">' + _body_html + '</div></td>'
				+ '<td class="crowm_win_t_right"></td></tr>'
			).appendTo(win_table);
		}

		$(	'<tr><td class="crowm_win_t_bottomleft"></td>'
			+ '<td class="crowm_win_t_bottom"></td>'
			+ '<td class="crowm_win_t_bottomright"></td></tr>'
		).appendTo(win_table);

	}else{
		var client_width = width - 13 - 13; // 角丸画像のサイズを引く
		var client_height = height - 15 - 16; // 角丸画像のサイズを引く

		$(	'<tr><td class="crowm_win_topleft"></td>'
			+ '<td class="crowm_win_top" nowrap></td>'
			+ '<td class="crowm_win_topright"></td></tr>'
		).appendTo(win_table);

		$(	'<tr><td class="crowm_win_left"></td>'
			+ '<td class="crowm_win_center"><div id="' + new_window_body_id + '" style="overflow:' + (scrollable ? 'auto' : 'hidden') + ';width:' + client_width + ';height:' + client_height + ';">' + _body_html + '</div></td>'
			+ '<td class="crowm_win_right"></td></tr>'
		).appendTo(win_table);

		$(	'<tr><td class="crowm_win_bottomleft"></td>'
			+ '<td class="crowm_win_bottom"></td>'
			+ '<td class="crowm_win_bottomright"></td></tr>'
		).appendTo(win_table);
	}

	//	Y座標を、中央からの位置で計算する
	if( _param.top == '50%' ){
		win.css( "top", $(document).scrollTop() + ($(window).height() - win.height()) / 2 );
		win.data( 'top_percent', _param.top );
	}else if( _param.top == '100%' ){
		win.css( "top", $(document).scrollTop() + ($(window).height() - win.height()) );
		win.data( 'top_percent', _param.top );
	}else{
		if( _param.top != null ){
			win.css( "top", _param.top );
		}else{
			win.css( "top", _crowm_def_top );
			_crowm_def_top += 32;
			_crowm_def_top %= 200;
		}
	}

	//	X座標を、中央からの位置で計算する
	if( _param.left == '50%' ){
		win.css( "left", $(document).scrollLeft() + ($(window).width() - win.width()) / 2 );
		win.data( 'left_percent', _param.left );
	}else if( _param.left == '100%' ){
		win.css( "left", $(document).scrollLeft() + ($(window).width() - win.width()) );
		win.data( 'left_percent', _param.left );
	}else{
		if( _param.left != null ){
			win.css( "left", _param.left );
		}else{
			win.css( "left", _crowm_def_left );
			_crowm_def_left += 32;
			_crowm_def_left %= 300;
		}
	}

	//	閉じるボタン
	var is_close = true;
	if( _param.close != null ) is_close = _param.close;
	if( is_close ){
		var close_class = _param.small ? "crowm_window_close_small" : "crowm_window_close";
		$('<div class="' + close_class + '" id="' + new_window_close_id + '"></div>').appendTo(win).css( {
			position	: 'absolute',
			cursor		: "pointer",
			zIndex		: _crowm_zindex++
		} ).data( "window_id", new_window_id ).mouseover( function(e) {
			if( $(this).attr("className") == "crowm_window_close" ) {
				$(this).removeClass( "crowm_window_close" );
				$(this).addClass( "crowm_window_close_over" );
			}else if( $(this).attr("className") == "crowm_window_close_small" ) {
				$(this).removeClass( "crowm_window_close_small" );
				$(this).addClass( "crowm_window_close_small_over" );
			}
		} ).mouseout( function(e) {
			if( $(this).attr("className") == "crowm_window_close_over" ) {
				$(this).removeClass( "crowm_window_close_over" );
				$(this).addClass( "crowm_window_close" );
			}else if( $(this).attr("className") == "crowm_window_close_small_over" ) {
				$(this).removeClass( "crowm_window_close_small_over" );
				$(this).addClass( "crowm_window_close_small" );
			}
		} ).click( function(e) {
			var _cancel = true;
			if( $("#" + $(this).data("window_id")).data("close_callback") ){
				var _callback_func = $("#" + $(this).data("window_id")).data("close_callback");
				_cancel = _callback_func();
				if( _cancel == null ) _cancel = true;
			}
			if( _cancel ){
				if( $("#" + $(this).data("window_id")).data("is_modal") == true ){
					var _gray_id = $("#" + $(this).data("window_id")).data("window_gray_id");
					$("#" + _gray_id).fadeOut( "fast", function(){$(this).remove();} );
				}
				$("#" + $(this).data("window_id")).fadeOut("fast", function(){$(this).remove();} );
			}
		} );
	}


	//	ボタンが指定されてる場合は、ボタンエリアを作成
	if( _param.buttons != null ){

		var button_area = $('<div class="crowm_window_button_area" align="center"></div>').appendTo(win);
		if( _param.buttons.length > 0 ){
			var arr = [];
			for( var i=0; i<_param.buttons.length; i++ ){
				var _button_id = "crowm_button_" + (_crowm_button_id++);
				_param.buttons[i].id = _button_id;

				arr[i] = {
					id			: _button_id,
					value		: _param.buttons[i].name,
					callback	: function() {
						var _custom_cb = $(this).data("_crowm_window_button_callback");
						if( _custom_cb ){
							var _ret = _custom_cb();
							if( _ret != null ){
								if( _ret == false ){
									return(this);
								}
							}
						}
						$("#" + new_window_close_id).click();
						return( false );
					}
				};
			}
			$(button_area).crowm_button_array( arr );

			for( var i=0; i<_param.buttons.length; i++ ){
				$("#"+_param.buttons[i].id).data( "_crowm_window_button_callback", _param.buttons[i].callback );
			}
		}
	}


	//	リサイズボタン
	var is_resize = _param.resizable==null ? false : _param.resizable;
	if( is_resize ){
		$('<div class="crowm_window_resize" id="' + new_window_resize_id + '">&nbsp;</div>').appendTo(win).css( {
			position	: 'absolute',
			cursor		: "se-resize",
			zIndex		: _crowm_zindex++
		} ).data( "window_id", new_window_id ).data( "window_body_id", new_window_body_id )
		.mousedown( function(e) {

			var this_obj = $(this);
			$("#"+this_obj.data("window_id")).css( "zIndex", ++_crowm_zindex )

			this_obj.data( "drag_start_y", e.pageY )
				.data( "drag_start_x", e.pageX )
				.data( "drag_start_wid", $("#"+this_obj.data("window_id")).width() )
				.data( "drag_start_hei", $("#"+this_obj.data("window_id")).height() );

			$(document).bind( "mousemove", function(e) {

				var suby = e.pageY - this_obj.data("drag_start_y");
				var subx = e.pageX - this_obj.data("drag_start_x");
				var _window_obj = $("#"+this_obj.data("window_id"));

				if( this_obj.data("drag_start_hei") + suby >= _crowm_min_height ){
					if( is_title ){
						$("#"+this_obj.data("window_body_id")).css( {
							height : this_obj.data("drag_start_hei") + suby - 36 - 15 - 8
						} );
					}else{
						$("#"+this_obj.data("window_body_id")).css( {
							height : this_obj.data("drag_start_hei") + suby - 15 - 16 - 8
						} );
					}
					_window_obj.css( {
						height : this_obj.data("drag_start_hei") + suby + "px"
					} );
					this_obj.data("drag_start_y", e.pageY);
					this_obj.data("drag_start_hei", this_obj.data("drag_start_hei") + suby);
				}
				if( this_obj.data("drag_start_wid") + subx >= _crowm_min_width ){
					if( is_title ){
						$("#"+this_obj.data("window_body_id")).css( {
							width : this_obj.data("drag_start_wid") + subx - 11 - 12 - 8
						} );
					}else{
						$("#"+this_obj.data("window_body_id")).css( {
							width : this_obj.data("drag_start_wid") + subx - 13 - 13 - 8
						} );
					}
					_window_obj.css( {
						width : this_obj.data("drag_start_wid") + subx + "px"
					} );
					this_obj.data("drag_start_x", e.pageX);
					this_obj.data("drag_start_wid", this_obj.data("drag_start_wid") + subx);
				}
			} );

			//	IEの問題対処
			$("body").bind( "selectstart", function() { return(false); } );

		} ).mouseup( function(e) {
			$(document).unbind( "mousemove" );
			$("body").unbind( "selectstart" );
		} );
	}

	//	ドラッグ可能な場合の処理
	var is_draggable = _param.draggable==null ? true : _param.draggable;
	if( is_draggable ){

		$("#"+new_window_title_td_id).css( "cursor", "move" );

		//	イベント登録
		$("#"+new_window_id).mousedown( function(e) {
			$("#"+new_window_id).css( "zIndex", ++_crowm_zindex )
		} );

		$("#"+new_window_title_td_id).mousedown( function(e) {
			$("#"+new_window_id)
				.data( "drag_start_y", e.pageY - $("#"+new_window_id).position().top )
				.data( "drag_start_x", e.pageX - $("#"+new_window_id).position().left )
				.data( "top_percent", "" )
				.data( "left_percent", "" );
			$(document).bind( "mousemove", function(e) {
				$("#"+new_window_id).css( {
					top : (e.pageY - $("#"+new_window_id).data("drag_start_y")),
					left : (e.pageX - $("#"+new_window_id).data("drag_start_x"))
				} );
			} );

			//	IEの問題対処
			$("body").bind( "selectstart", function() { return(false); } );

		} ).mouseup( function(e) {
			$(document).unbind( "mousemove" );
			$("body").unbind( "selectstart" );
		} );
	}

	//	必要なら表示しておく
	if( _param.visible == true ){
		win.css( 'display', 'block' );
	}
	return( win );
};


jQuery.fn.extend( {

//------------------------------------------------------------------------------
//	jQueryオブジェクトからwindowを作成する。
/*
	下記のようにして、<div>要素をwindow化する

	$("#エレメントのID").crowm_window( {
		top: "100%",				//	Y座標（"100%" or "50%" or 座標数値）
		left: 50,					//	X座標（"100%" or "50%" or 座標数値）
		width: 100,					//	横幅
		height: 300,				//	高さ
		draggable: true,			//	移動可能？
		scrollable: true,			//	内容が入りきれない場合、スクロールバーを表示するか？
		visible: true,				//	初回表示するか？
		close: true,				//	閉じるボタンがあるか？
		close_callback: func(){},	//	閉じるボタン押下時のコールバック（false返却で閉じない）
		parent: "親エレメントのID"	//	省略時はbodyの子となる
		modal:true					//	モーダルウィンドウ？
	} );

	上記の、全てのオプションは省略可能。

	１．HTMLに下記のように記述しておく。
	<div class="crowm_window" style="display:none" id="testWindow">
		<div class="crowm_window_title">タイトルです。省略時はタイトルなしになります</div>
		<div class="crowm_window_body">内容です。HTMLコンテンツを記載します。</div>
	</div>

	２．javascriptで、window化する。
	$('#testWindow').crowm_window( {width:300, height:300, visible:true} );

*/
//
//------------------------------------------------------------------------------
	crowm_window : function(_param) {

		if( ! _param ) _param = {};
		var is_title = false;
		var title_html = $(".crowm_window_title", this).html();
		var body_html = $(".crowm_window_body", this).html();
		if( ! title_html ) title_html = "";
		if( ! body_html ) body_html = "&nbsp;";

		return( crowm_window(title_html, body_html, _param) );
	},


//------------------------------------------------------------------------------
//	jQueryオブジェクトからボタンを作成する
/*
	指定された要素にボタンを挿入する。
	HTMLは、
		<div id="custom_button"></div>

	とかしておいて、これを、
		$('#custom_button").crowm_button( {
			value : "ボタンのキャプション",
			callback : function(){
				//	押されたときの処理
				alert( "pushed !!" );
			}
		} );

	とすれば、ボタンが出来上がる。

	複数のボタンを並べて挿入するには、crowm_button_array() を使う。
	_paramsの指定を配列にする。

		$('#custom_button").crowm_button_array(
			[
				{
					id : "idididid"					// ← 省略可能
					value : "ボタンのキャプション",
					callback : function(){
						//	押されたときの処理
						alert( "pushed !!" );
					}
				},
				{
					value : "ボタンのキャプション2",
					callback : function(){
						//	押されたときの処理
						alert( "pushed2 !!" );
					}
				}
			]
		);
*/
//
//------------------------------------------------------------------------------
	crowm_button : function(_param) {

		//	IDを決める
		var new_id = "";
		if( ! _param ) _param = {};
		new_id = _param.id ? _param.id : ("crowm_button_" + (_crowm_button_id++));

		//	HTML作成
		var html = '<table cellpadding="0" cellspacing="0" style="font-size:12px;height:24px;cursor:pointer;" id="' + new_id + '">'
			+ '<tr>'
			+ '<td style="background:url(\'winimg/btn_left.png\') no-repeat 0px 0px;height:24px;padding-left:18px;cursor:pointer;">' + _param.value + '</td>'
			+ '<td style="background:url(\'winimg/btn_right.png\') no-repeat 0px 0px;height:24px;padding-right:18px;cursor:pointer;">&nbsp;</td>'
			+ '</tr>'
			+ '</table>';

		//	追加
		$(html).appendTo(this).mouseover( function(){
			$('td:first-child',this).css( "background-position", "0px -24px" );
			$('td:last-child',this).css( "background-position", "0px -24px" );
		} ).mousedown( function(){
			$('td:first-child',this).css( "background-position", "0px -48px" );
			$('td:last-child',this).css( "background-position", "0px -48px" );
		} ).mouseout( function(){
			$('td:first-child',this).css( "background-position", "0px 0px" );
			$('td:last-child',this).css( "background-position", "0px 0px" );
		} );
		if( _param.callback ){
			$("#"+new_id).bind( "click", _param.callback );
		}
		return( this );
	},
	crowm_button_array : function(_param) {
		if( ! _param ) _param = {};
		var tbl		= $('<table></table>').appendTo(this);
		var tbody	= $('<tbody></tbody>').appendTo(tbl);
		var tr		= $('<tr></tr>').appendTo(tbody);
		for( var i=0; i<_param.length; i++ ){
			$('<td></td>').appendTo(tr).crowm_button( _param[i] );
		}
		return( this );
	}


} );


//------------------------------------------------------------------------------
//	内部変数 / 関数
//------------------------------------------------------------------------------
var _crowm_window_id = 1;
var	_crowm_zindex = 1000;
var _crowm_def_width = 200;
var _crowm_def_height = 120;
var _crowm_min_width = 120;
var _crowm_min_height = 100;
var _crowm_def_left = 0;
var _crowm_def_top = 0;
var _crowm_button_id = 1;
var _crowm_input_id = 1;

$(function(){

	//	座標調整関数
	function _crowm_adjust_position(){
		$(".crowm_window_div").each( function(){
			//	Y座標調節
			if( $(this).data('top_percent') == '50%' ){
				$(this).css( "top", $(document).scrollTop() + ($(window).height() - $(this).height()) / 2 );
			}else if( $(this).data('top_percent') == '100%' ){
				$(this).css( "top", $(document).scrollTop() + ($(window).height() - $(this).height()) );
			}
			//	X座標調節
			if( $(this).data('left_percent') == '50%' ){
				$(this).css( "left", $(document).scrollLeft() + ($(window).width() - $(this).width()) / 2 );
			}else if( $(this).data('left_percent') == '100%' ){
				$(this).css( "left", $(document).scrollLeft() + ($(window).width() - $(this).width()) );
			}
		} );
	}

	//	リサイズイベントの設定
	$(window).bind( "resize", _crowm_adjust_position );

	//	スクロールイベントの設定
	$(window).bind( "scroll", _crowm_adjust_position );

});

