//--------------------------------------------------------------------------------------------------
// KsGMap - 移動コンソール　プラグイン　　Ver1.10
// Copyright(c) 2005-2006 Ksc    http://www.ksgmap.jp/
//--------------------------------------------------------------------------------------------------

//プラグインを登録（プラグイン名 , バージョン , 本体動作バージョン）
KsGMapEX.addPlugIn( "MoveConsole" , 1.10 , 1.10 );
//グローバル関数定義
//移動ナビゲーション・プラグイン定義
var KsGMapPlugIn_MoveConsole = new function(){
	this.id					= "";
	this.defaultMessage		= "他のマップ（マピオンやマップファンなど）の“地図のURL”をコピー / 緯度と経度を入力";
	this.moveEndMessage		= "移動が完了しました。";
	this.prevPoint			= {};

	this.init				= KsGMapPlugIn_MoveConsole_init;
	this.submit				= KsGMapPlugIn_MoveConsole_submit;
	this.submit_makeReg		= KsGMapPlugIn_MoveConsole_submit_makeReg;
	this.submit_conv		= KsGMapPlugIn_MoveConsole_submit_conv;
	this.submit_finish		= KsGMapPlugIn_MoveConsole_submit_finish;
	this.back				= KsGMapPlugIn_MoveConsole_back;
	this.console			= {};
	this.console.focus		= KsGMapPlugIn_MoveConsole_console_focus;
	this.console.keydown	= KsGMapPlugIn_MoveConsole_console_keydown;
	this.console.paste		= KsGMapPlugIn_MoveConsole_console_paste;
	this.console.clear		= KsGMapPlugIn_MoveConsole_console_clear;

	this.convCoordinateTokyoToWGS84 = KsGMapPlugIn_MoveConsole_convCoordinateTokyoToWGS84;
}
//コールバックを追加
KsGMapEX.addCallback( KsGMapInit			, KsGMapPlugIn_MoveConsole.init		);		//KsGMap初期化

//□移動コンソール - 初期化
function KsGMapPlugIn_MoveConsole_init(){
	var obj_console = document.getElementById( "ksgmapplugin--MoveConsole-console" );

	//初期メッセージの表示
	if( obj_console && KsGMapPlugIn_MoveConsole.defaultMessage ){
		obj_console.style.color = "#444444";
		obj_console.value		= KsGMapPlugIn_MoveConsole.defaultMessage;
	}
}
//□移動コンソール - 実行
function KsGMapPlugIn_MoveConsole_submit( event , form ){
	var obj_console = document.getElementById( "ksgmapplugin--MoveConsole-console" );
	var ref			= obj_console.value;
	var result = {};  var option = "";

	if( ref.match( /^http/ ) ){	//他のサイトのURL
		if(        ref.match( this.submit_makeReg( "google"		, "[&\?]ll=([0-9.-]+),([0-9.-]+)" ) ) ){					//Googleマップ／ローカル
			result = this.submit_conv( "" , RegExp.$2 , RegExp.$1 );
		} else if( ref.match( this.submit_makeReg( "goo.ne.jp"		, "[&\?]MAP=E([0-9.]+)N([0-9.]+)" ) ) ){					//goo
			result = this.submit_conv( "TOKYO BASE60" , RegExp.$1 , RegExp.$2 );
		} else if( ref.match( this.submit_makeReg( "hatena.ne.jp"	, "[&\?]x=([0-9.-]+)" , "[&\?]y=([0-9.-]+)" ) ) ){			//はてなマップ
			result = this.submit_conv( "" , RegExp.$1 , RegExp.$2 );
		} else if( ref.match( this.submit_makeReg( "its-mo.com"	, "[&\?]m=E([0-9.]+)N([0-9.]+)" ) ) ){						//いつもガイド（ゼンリン）
			result = this.submit_conv( "TOKYO BASE60" , RegExp.$1 , RegExp.$2 );
		} else if( ref.match( this.submit_makeReg( "mapfan.com"	, "[&\?]MAP=E([0-9.]+)N([0-9.]+)" ) ) ){					//マップファン
			result = this.submit_conv( "TOKYO BASE60" , RegExp.$1 , RegExp.$2 );
		} else if( ref.match( this.submit_makeReg( "mapion.co.jp"	, "[&\?]nl=([0-9\/.]+)" , "[&\?]el=([0-9\/.]+)" ) ) ){		//マピオン
			result = this.submit_conv( "TOKYO BASE60" , RegExp.$2 , RegExp.$1 );
		} else if( ref.match( this.submit_makeReg( "multimap.com"	, "[&\?]lat=([0-9.-]+)" , "[&\?]lon=([0-9.-]+)" ) ) ){		//Multimap
			result = this.submit_conv( "" , RegExp.$2 , RegExp.$1 );
		} else if( ref.match( this.submit_makeReg( "msn.com"		, "[&\?]cp=([0-9.-]+)\\|([0-9.-]+)" ) ) ){					//MSN Virtual Earth
			result = this.submit_conv( "" , RegExp.$2 , RegExp.$1 );
		} else if( ref.match( this.submit_makeReg( "yahoo.co.jp"	, "[&\?]nl=([0-9.]+)" , "[&\?]el=([0-9.]+)" ) ) ){			//Yahoo!マップ
			result = this.submit_conv( "TOKYO BASE60" , RegExp.$2 , RegExp.$1 );
		} else if( ref.match( this.submit_makeReg( ""				, "[&\?]lng=([0-9\.-]+)" , "[&\?]lat=([0-9\.-]+)" ) ) ){	//他のKsGMap
			result = this.submit_conv( "" , RegExp.$1 , RegExp.$2 );
		}

	} else {						//座標直接入力
		if( ref.match( /TOKYO/i ) ){ option = "\tTOKYO"; }		//世界計測系

		if(        ref.match( this.submit_makeReg( "" , "([NEWS]?[0-9]+[d.\/\\s][0-9]+['.\/\\s][0-9]+[\.\/\\s]?[0-9]*[\"\\s]?[NEWS]?).+?([NEWS]?[0-9]+[d.\/\\s][0-9]+['.\/\\s][0-9]+[\.\/\\s]?[0-9]*[\"\\s]?[NEWS]?)" ) ) ){	//60進数 符号 ／10進数 NEWS
			result = this.submit_conv( "NEWS BASE60" + option , RegExp.$2 , RegExp.$1 );
		} else if( ref.match( this.submit_makeReg( "" , "((?:北緯|南緯)[0-9]+)度([0-9]*)分?([0-9]*)秒?([0-9]*)" , "((?:東経|西経)[0-9]+)度([0-9]*)分?([0-9]*)秒?([0-9]*)" ) ) ){												//60進数 日本語
			result = this.submit_conv( "NEWS BASE60" , ( RegExp.$5 + "." + RegExp.$6 + "." + RegExp.$7 + "." + RegExp.$8 ) , ( RegExp.$1 + "." + RegExp.$2 + "." + RegExp.$3 + "." + RegExp.$4 ) );
		} else if( ref.match( this.submit_makeReg( "" , "lng=['\"]([0-9.+-]+)['\"]" , "lat=['\"]([0-9.+-]+)['\"]" ) ) ){		//10進数 KsGMap XML
			result = this.submit_conv( "" + option , RegExp.$1 , RegExp.$2 );
		} else if( ref.match( this.submit_makeReg( "" , "([0-9NEWS.+-]+).+?([0-9NEWS.+-]+)" ) ) ){								//10進数 符号 ／ 10進数 NEWS
			result = this.submit_conv( "NEWS" + option , RegExp.$2 , RegExp.$1 );
		}

	}

	//移動処理
	if( result.lng && result.lat ){
		var center = KsGMapMap.gmap.getCenter();	//中央座標取得
		KsGMapPlugIn_MoveConsole.prevPoint = { lng : center.x , lat : center.y }	//一つ前の座標を記録しておく

		KsGMapMap.gmap.panTo( new GLatLng( result.lat , result.lng ) );
		obj_console.style.color = "#444444";
		obj_console.value		= KsGMapPlugIn_MoveConsole.moveEndMessage;
		if( ksgmap__ua.MSIE ){ document.getElementById( ksgmap_html_googleMap ).focus(); }	//IEの場合、マップにフォーカス（それ以外はボタンだからOK）

		if( document.getElementById( "ksgmapplugin--MoveConsole-back" ) ){ document.getElementById( "ksgmapplugin--MoveConsole-back" ).disabled = false; }		//戻るボタンを有効にする

		KsGMapPlugIn_MoveConsole_submit_finish( result.lng , result.lat );	//完了用関数
	}

}
//□移動コンソール - 実行 - 完了
function KsGMapPlugIn_MoveConsole_submit_finish( lng , lat ){
	KsGMapEX.callback( this , arguments ,  lng , lat );					//コールバック
}
//□移動コンソール - 実行 - 正規表現の作成
function KsGMapPlugIn_MoveConsole_submit_makeReg( site , arg1 , arg2 ){
	var reg = "";
	if( site && site != undefined ){ reg += "^http:\/\/[a-z]+\." + site + ".+?"; }				//サイト指定

	if( arg2 != undefined ){ reg += "(?=.*" + arg1 + ")(?=.*" + arg2 + ")"; }			//順不同の場合
	else				   { reg += arg1; }												//順序が決まっている場合

	var result = new RegExp( reg , "i" );
	return result;
}
//□移動コンソール - 実行 - 各種変換
function KsGMapPlugIn_MoveConsole_submit_conv( option ){			//option , lng , lat

	//NEWSオプションで、経度にN/S、緯度にE/W、が付いていたら、入れ替える
	if( ( arguments[ 1 ].match( /[NS]/ ) && arguments[ 2 ].match( /[^NS]/ ) ) || ( arguments[ 1 ].match( /[^EW]/ ) && arguments[ 2 ].match( /[EW]/ ) ) ){ var temp = arguments[ 2 ];  arguments[ 2 ] = arguments[ 1 ];  arguments[ 1 ] = temp; }

	for( var i = 1 ; i < arguments.length ; i++ ){
		//NEWS変換
		if( option.match( /NEWS/ ) ){
			arguments[ i ] = arguments[ i ].replace( /N|E|北緯|東経/gi , ""  );		//NとE、北緯、東経は+で省略
			arguments[ i ] = arguments[ i ].replace( /S|W|南緯|西経/gi , "-" );		//SとWは-
			arguments[ i ] = arguments[ i ].replace( /(.+)-$/ , "-$1" );			//お尻の-は頭に置く
		}

		//60進数を10進数に変換する
		if( option.match( /BASE60/ ) ){
			arguments[ i ] = arguments[ i ].replace( /\"/ , "" );
			var temp = arguments[ i ].split( /[d'\/\.\s]/ );
			if( temp[ 3 ] ){ temp[ 2 ] += "." + temp[ 3 ]; }	//小数点以下
			arguments[ i ] = ( ( ( parseFloat( temp[ 2 ] ) / 60 ) + parseInt( temp[ 1 ] ) ) / 60 ) + parseInt( temp[ 0 ] );
		}

		arguments[ i ] = parseFloat( arguments[ i ] );
	}

	//もし緯度が±90度より大きく、経度が±90度より小さい場合は、入れ替える
	if( ( arguments[ 2 ] < -90 || 90 < arguments[ 2 ] ) && ( -90 <= arguments[ 1 ] && arguments[ 1 ] <= 90 ) ){ var temp = arguments[ 2 ];  arguments[ 2 ] = arguments[ 1 ];  arguments[ 1 ] = temp; }


	//世界計測系から日本計測系もどきに変換（KsGMap Ver1.01以降）
	if( option.match( /TOKYO/ ) ){ 
		var temp = KsGMapPlugIn_MoveConsole.convCoordinateTokyoToWGS84( arguments[ 1 ] , arguments[ 2 ] );
		arguments[ 1 ] = temp.x;  arguments[ 2 ] = temp.y;
	}

	var result = {};
	result.lng = ( -180 <= arguments[ 1 ] && arguments[ 1 ] <= 180 ? arguments[ 1 ] : 0 );
	result.lat = (  -90 <= arguments[ 2 ] && arguments[ 2 ] <=  90 ? arguments[ 2 ] : 0 );
	return result;
}
//□移動コンソール - 戻る
function KsGMapPlugIn_MoveConsole_back( event , form ){
	var center = KsGMapPlugIn_MoveConsole.prevPoint;								//一つ前の座標から取得
	KsGMapMap.gmap.panTo( new GLatLng( center.lat , center.lng ) );	//移動
	document.getElementById( "ksgmapplugin--MoveConsole-back" ).disabled = true;	//戻るボタンを無効にする
}
//□移動コンソール - コンソール - フォーカス
function KsGMapPlugIn_MoveConsole_console_focus(){
	var obj_console = document.getElementById( "ksgmapplugin--MoveConsole-console" );
	var this_plugin = KsGMapPlugIn_MoveConsole;

	//初期メッセージが表示されている場合、クリアする
	if( obj_console.value == this_plugin.defaultMessage || obj_console.value == this_plugin.moveEndMessage ){
		KsGMapPlugIn_MoveConsole.console.clear();
	}

	obj_console.select();	//選択状態にする
}
//□移動コンソール - コンソール - キーダウン
function KsGMapPlugIn_MoveConsole_console_keydown( event , form ){
	if( event.keyCode == 13 ){ KsGMapPlugIn_MoveConsole.submit( event , form ); }
}
//□移動コンソール - コンソール - 貼り付け
function KsGMapPlugIn_MoveConsole_console_paste( event , form ){
	setTimeout( "KsGMapPlugIn_MoveConsole.submit();" , 100 );
}
//□移動コンソール - コンソール - クリア
function KsGMapPlugIn_MoveConsole_console_clear(){
	var obj_console = document.getElementById( "ksgmapplugin--MoveConsole-console" );
	obj_console.style.color	= "#000000";
	obj_console.value		= "";
}
//□移動コンソール - 日本測地系から世界測地系に変換する
function KsGMapPlugIn_MoveConsole_convCoordinateTokyoToWGS84( lng , lat ){
	var result = {};
	result.y = lat - 0.00010695  * lat + 0.000017464 * lng + 0.0046017;
	result.x = lng - 0.000046038 * lat - 0.000083043 * lng + 0.010040;
	return result;

}
// ------------------------------------------------------------------------------------------------------------

