var mapImage = new MapImage();									// 地図制御クラス.
var toolTip = new ToolTip();										// ToolTip制御クラス.
var dragLayer = new DragLayer();								// 画面ドラック制御クラス.
var common = new Common();											// 共通関数クラス.
var geoAccess = new GeoAccess();								// GeoAccessクラス.
var keyMap = new KeyMap();											// キーマップ制御クラス.
var viewController = new ViewController();			// 表示制御クラス.
var measureFigure = new MeasureFigure();				// 計測制御クラス.
var memoController = new MemoController();			// メモ制御クラス.
var debug = new Debug();												// デバック制御クラス.
var moveFrame = new MoveFrame();								// フレームスライド制御クラス.
var help = new Help();													// ヘルプクラス.
var mailer = new Mailer();											// メール制御クラス.
var currentDocument = new CurrentDocument();		// 地図ページ制御クラス.

var documentHeader = 140;												// ヘッダーの高さ.

// KMZのURL
var kmzURL = "http://gis.g-uonuma.jp/Citizen15225/GoogleEarth.htm";

// 地図ページ制御クラス.
function CurrentDocument()
{
	//現在のマウス座標
	this.ActiveMouseX = 0;						// x軸(px).
	this.ActiveMouseY = 0;						// y軸(px).
	this.KeycodeCancel = false;				// キーコードキャンセルフラグ.
	
	
	// 初期化.
	this.Initialize = function()
	{
		//イベント関連
		window.document.onmousemove = currentDocument.ScreenMouseMove;
		window.document.onkeydown = currentDocument.ScreenKeyDown;
		onresize = currentDocument.ScreenResize;
		
		geoAccess.Initialize();
		geoAccess.GetContents('');
		help.ClearMessage();
	}
	
	// ScreenScroll
	this.ScreenScroll = function()
	{
		window.scrollTo(0, 0);
		return false;
	}
	
	// ScreenMouseDown
	this.ScreenMouseDown = function()
	{
	}
	
	// ScreenMouseUp
	this.ScreenMouseUp = function()
	{
	}
	
	// ScreenMouseMove
	this.ScreenMouseMove = function(e)
	{
		currentDocument.GetActiveMousePos(e);
		mapImage.GetMapPos();
		dragLayer.Drag();
		return false;
	}
	
	// ScreenKeyDown
	this.ScreenKeyDown = function (e)
	{
	
		var keyCode = common.GetKeyCode(e);
		
		if (currentDocument.KeycodeCancel)
		{
			currentDocument.KeycodeCancel = false;
			return;
		}
		
		switch(keyCode)
		{
			//ENTER
			case 13:
				switch(mapImage.MapMode)
				{
					case common.MODEMEASURE:
						measureFigure.KeyPressEnter();
						break;
					default:
						break;
				}
				break;
			//ESC
			case 27:
				document.getElementById("contextmenu").style.visibility = "hidden";
				switch(mapImage.MapMode)
				{
					case common.MODEMEASURE:
						measureFigure.KeyPressESC();
						break;
					case common.MODEMEMO:
						memoController.End();
					default:
						break;
				}
				break;
			//BACKSPACE,DELETE
			case 46:
				switch(mapImage.MapMode)
				{
					case common.MODEMEASURE:
						measureFigure.KeyPressBack_Del();
						break;
					default:
						break;
				}
				break;
				
			//上　↑8
			case 38:
			case 104:
			case 56:
				viewController.MoveCenter(parseInt(mapImage.MapCenterX), parseInt(mapImage.MapCenterY) + mapImage.mapM / 2);
				break;
				
			//下　↓2
			case 40:
			case 98:
			case 50:
				viewController.MoveCenter(parseInt(mapImage.MapCenterX), parseInt(mapImage.MapCenterY) - mapImage.mapM / 2);
				break;
				
			//左　←4
			case 37:
			case 100:
			case 52:
				viewController.MoveCenter(parseInt(mapImage.MapCenterX)-mapImage.mapM / 2, parseInt(mapImage.MapCenterY));
				break;
				
			//右　→6
			case 39:
			case 102:
			case 54:
				viewController.MoveCenter(parseInt(mapImage.MapCenterX)+mapImage.mapM / 2, parseInt(mapImage.MapCenterY));
				break;
				
			//左上　7
			case 36:
			case 103:
			case 55:
				viewController.MoveCenter(parseInt(mapImage.MapCenterX)-mapImage.mapM / 2, parseInt(mapImage.MapCenterY)+mapImage.mapM / 2);
				break;
				
			//右上　9
			case 33:
			case 105:
			case 57:
				viewController.MoveCenter(parseInt(mapImage.MapCenterX)+mapImage.mapM / 2, parseInt(mapImage.MapCenterY)+mapImage.mapM / 2);
				break;
				
			//左下　1
			case 35:
			case 97:
			case 49:
				viewController.MoveCenter(parseInt(mapImage.MapCenterX)-mapImage.mapM / 2, parseInt(mapImage.MapCenterY)-mapImage.mapM / 2);		
				break;
				
			//右下　3
			case 34:
			case 99:
			case 51:
				viewController.MoveCenter(parseInt(mapImage.MapCenterX)+mapImage.mapM / 2, parseInt(mapImage.MapCenterY)-mapImage.mapM / 2);
				break;
			
			//拡大 +
			case 107:
				viewController.ZoomIn();
				break;
				
			//縮小 -
			case 109:
				viewController.ZoomOut();
				break;
				
			default:
				break;
			
		}
	}
	
	// 現在のマウス座標を取得する.
	this.GetActiveMousePos = function(e)
	{
		if(navigator.userAgent.search("Opera(\ |\/)6") != -1 )   //o6用
		{
			this.ActiveMouseX = e.clientX;
			this.ActiveMouseY = e.clientY;
		}
		else if(document.all)               //e4,e5,e6用
		{
			this.ActiveMouseX = event.x;
			this.ActiveMouseY  = event.y;
		}
		else if(document.layers || document.getElementById)    //n4,n6,n7,m1,o7,s1用
		{
			this.ActiveMouseX = e.pageX;
			this.ActiveMouseY = e.pageY;
		}
	}
	
	// 画面サイズ変更にともなう各コントロールの再配置.
	this.ScreenResize= function()
	{
		var footer = 60;
		var maxBlockSize = 800;
		var addressHeight = 23;
		
		// 自作コンテキストメニューは座標が変わった段階で非表示に
		document.getElementById("contextmenu").style.visibility = "hidden";
		
		if (document.body.clientHeight >= documentHeader + maxBlockSize+footer)
		{
			document.getElementById("footer").style.top = documentHeader + maxBlockSize -addressHeight;
		}
		else
		{
			document.getElementById("footer").style.top = parseInt(document.body.clientHeight) - footer;
		}
		
		if (parseInt(document.getElementById("footer").style.top) - documentHeader + addressHeight > 0)
		{
			document.getElementById("leftLine").style.height = parseInt(document.getElementById("footer").style.top) - documentHeader + addressHeight;
			document.getElementById("rightLine").style.height = parseInt(document.getElementById("leftLine").style.height);
		}
		
		if (document.body.clientHeight > 0)
		{
			document.getElementById("rightBlank").style.height = document.body.clientHeight;
		}
		
		if (document.body.clientHeight - parseInt(document.getElementById("footer").style.top) > 0)
		{
			document.getElementById("footerBlank").style.height = document.body.clientHeight - parseInt(document.getElementById("footer").style.top);
		}

		if (document.body.clientWidth - maxBlockSize > 0)
		{
			document.getElementById("rightBlank").style.width = document.body.clientWidth - maxBlockSize;
		}
		
		if (document.body.clientWidth > 0)
		{
			document.getElementById("footerBlank").style.width = document.body.clientWidth;
		}
		
		if (document.body.clientWidth > 0 && document.body.clientHeight > 0)
		{
			document.getElementById("PrintSettingWin").style.top = (parseInt(document.body.clientHeight) - parseInt(document.getElementById("PrintSettingWin").offsetHeight)) / 2;
			document.getElementById("PrintSettingWin").style.left = (parseInt(maxBlockSize) - parseInt(document.getElementById("PrintSettingWin").offsetWidth)) / 2;
			
			document.getElementById("MemoUpLoadWin").style.top = (parseInt(document.body.clientHeight) - parseInt(document.getElementById("MemoUpLoadWin").offsetHeight)) / 2;
			document.getElementById("MemoUpLoadWin").style.left = (parseInt(maxBlockSize) - parseInt(document.getElementById("MemoUpLoadWin").offsetWidth)) / 2;
			
			document.getElementById("KmlWin").style.top = (parseInt(document.body.clientHeight) - parseInt(document.getElementById("KmlWin").offsetHeight)) / 2;
			document.getElementById("KmlWin").style.left = (parseInt(maxBlockSize) - parseInt(document.getElementById("KmlWin").offsetWidth)) / 2;
		}
		
		document.getElementById("map").style.top = documentHeader - parseInt((maxBlockSize - parseInt(document.getElementById("leftLine").style.height)) / 2);
		document.getElementById("dragDiv").style.top = parseInt(document.getElementById("map").style.top);
		document.getElementById("MemoDrawArea").style.top = parseInt(document.getElementById("map").style.top);
		document.getElementById("MeasureFigureArea").style.top = parseInt(document.getElementById("map").style.top);
		
		if (mapImage.MapMode == common.MODEMEASURE)
		{
			measureFigure.Redraw();
		}
	}
	
	
	// 地図画面表示.
	this.ShowMapForm = function(x, y, scale)
	{
		viewController.MoveCenterScale(x, y , scale);
	}
	
}
