// メモ情報クラス.
// key							キー.
// name							名前.
// description			詳細.
// x								x座標.
// y								y座標.
function MemoInfo(key, name, description, x, y)
{
	this.Key = key;
	this.Name =name.substr(0, 10);
	this.Description = description.substr(0, 25);
	this.X = parseInt(x);
	this.Y = parseInt(y);
	this.Lat = 0;
	this.Lon = 0;
}

// メモ制御クラス.
function MemoController()
{
	this.memoList = new Array();

	
	var cookieController = new CookieController();
	
	// 処理開始.
	this.Start = function()
	{
		document.getElementById(common.MODELINE + "Button").src = "MapForm/img/" + common.MODELINE + ".gif";
		document.getElementById(common.MODEAREA + "Button").src = "MapForm/img/" + common.MODEAREA + ".gif";
//		document.getElementById(modeMemo + "Button").src = "MapForm/img/" + modeMemo + ".gif";

/*		
		switch(mapImage.MapMode)
		{
			case "Memo":
				this.End();
				return;
				break;
			case "Measure":
				measureFigure.End();
				break;
			default:
				break;
		}
*/
		
		if (mapImage.MapMode == common.MODEMEASURE)
		{
			measureFigure.End();
		}
		
		
		mapImage.MapMode = common.MODEMEMO;
//		document.getElementById(modeMemo + "Button").src = "MapForm/img/" + modeMemo + "_select.gif";
		document.getElementById("memoVisible").checked = true;
		cookieController.SaveVisible(document.getElementById("memoVisible").checked);
		document.getElementById("MemoArea").style.visibility = "visible";
//		this.ShowMemo();
		//document.body.style.cursor ="url('MapForm/img/PEN.cur')";
		return;
	}
	
	
	// 終了.
	this.End = function()
	{
		mapImage.MapMode = "";
		//document.getElementById(modeMemo + "Button").src = "MapForm/img/" + modeMemo + ".gif";
		//document.body.style.cursor = "default";
		return false;
	}
	
	// 読み込み.
	this.Load = function()
	{
		this.memoList = cookieController.Load();
		this.ShowList();
		
		document.getElementById("memoVisible").checked = cookieController.LoadVisible();
	}
	
	// 保存.
	this.Save = function()
	{
		cookieController.Save(this.memoList);
	}
	
	// 追加.
	// name			名前.
	// x				X座標.
	// y				Y座標.
	this.Add = function(name, x, y)
	{
		exp=new Date();
		this.memoList[this.memoList.length] = new MemoInfo(exp.getTime(), name, "", x, y);
	}
	
	// 削除.
	// Key					キー.
	this.Delete = function(key)
	{
		/*
		for(var i=0; i<this.memoList.length; i++)
		{
			memoinfo = this.memoList[i];
			if (memoinfo.Key == key)
			{
				cookieController.Delete(this.memoList[i].Key);
				this.memoList.splice(i, 1);
				break;
			}
		}
		
		document.getElementById(key + 'Area').innerHTML = "";
		document.getElementById(key + 'Area').style.width = "0px";
		document.getElementById(key + 'Area').style.height = "0px";
		document.getElementById(key + 'Area').style.visibility = "hidden";
		
		this.ShowList(); 
		*/
			
		for(var i=0; i<this.memoList.length; i++)
		{
			memoinfo = this.memoList[i];
			if (memoinfo.Key == key)
			{
				this.memoList.splice(i, 1);
				cookieController.Save(this.memoList);
				break;
			}
		}
		
		document.getElementById(key + 'Area').innerHTML = "";
		document.getElementById(key + 'Area').style.width = "0px";
		document.getElementById(key + 'Area').style.height = "0px";
		document.getElementById(key + 'Area').style.visibility = "hidden";
		
		// リストの描画.
		this.ShowList(); 
	}
	
	// メモ全削除.
	this.DeleteAll = function()
	{
/*		
		if (confirm("全てのメモを削除してよろしいですか？"))
		{
			for(var i=0; i<this.memoList.length; i++)
			{
				memoinfo = this.memoList[i];
				cookieController.Delete(this.memoList[i].Key);
			}
			
			this.memoList = new Array();
			
			document.getElementById("memoList").innerHTML = "";
			document.getElementById("MemoArea").innerHTML ="";
		}
*/
		if (confirm("全てのメモを削除してよろしいですか？"))
		{
			this.memoList = new Array();
			
			//cookieの保存.
			cookieController.Save(this.memoList);
			
			document.getElementById("memoList").innerHTML = "";
			document.getElementById("MemoArea").innerHTML ="";
		}		
	}
	
	// 更新.
	// key					キー.
	this.Update = function(key)
	{
		/*
		for(var i=0; i<this.memoList.length; i++)
		{
			memoinfo = this.memoList[i];
			if (memoinfo.Key == key)
			{
				cookieController.Delete(this.memoList[i].Key);
				memoinfo.Name = document.getElementById(key + "Text").value;
				memoinfo.Description = document.getElementById(key + "Detail").value;
				
				cookieController.Add(memoinfo);
				break;
			}
		}
		
		this.ShowList();
		
		return;
		*/
			
		for(var i=0; i<this.memoList.length; i++)
		{
			memoinfo = this.memoList[i];
			if (memoinfo.Key == key)
			{
				memoinfo.Name = document.getElementById(key + "Text").value;
				memoinfo.Description = document.getElementById(key + "Detail").value;
				break;
			}
		}
		cookieController.Save(this.memoList);
		this.ShowList();
		
		return;
	}
	

	// メモ画面表示.
	// x					x座標(m).
	// y					y座標(m).
	this.ShowWindow = function(x, y)
	{
		if (memoController.memoList.length > 15)
		{
			alert("保持できるメモの件数は１５件までです。");
			return;
		}
		
		this.Start();
		
		this.Add("", x, y);
		
		memoinfo = this.memoList[this.memoList.length - 1];
		
//		dispX = parseInt(currentDocument.ActiveMouseX) - parseInt(document.getElementById("MemoArea").style.left);
//		dispY = parseInt(currentDocument.ActiveMouseY) - parseInt(document.getElementById("MemoArea").style.top) - parseInt(document.getElementById("map").style.top);

		dispX = parseInt(mapImage.GetMapPosXToPixcelPosX(x)) - parseInt(document.getElementById("MemoArea").style.left);
		dispY = parseInt(mapImage.GetMapPosYToPixcelPosY(y)) - parseInt(document.getElementById("MemoArea").style.top);
		
		html ="";
		html +="<div id ='" + memoinfo.Key + "Area' style = 'Z-INDEX: 16;position:absolute;top:" + (dispY-20)  + "px;left:" + dispX + "px;'>"
		html +="<img src='MapForm/img/pin_red.gif' style = 'Z-INDEX: 16;position:absolute;top:0px;left:0px;'>"
		html +="<div id ='" + memoinfo.Key + "' style = 'Z-INDEX: 16;position:absolute;top:0px;left:20px;BACKGROUND-COLOR: #DCDCDC;width:240px;height:137px;border-top: 1px solid #D4D0C8;border-left: 1px solid #D4D0C8;border-right: 2px solid #404040;border-bottom: 2px solid #404040;visibility:;' onmousedown='memoController.EditStart(" + memoinfo.Key + ")' onmousemove='mapImage.MouseMove(event);' onmouseup='mapImage.MouseUp(event);'>";
		html +="	<div id ='" + memoinfo.Key + "Input' style = 'position:absolute;top:2px;left:2px;width:240px;height:135px;visibility:;' onmouseover = \"help.ShowMessage(help.memoDeteilmsg);\" onmouseout = \"help.ClearMessage();\">";
		html +="		<table>";
		html +="			<tr><td style='font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;'>見出し</td><td><input id='" + memoinfo.Key +"Text' type='text' size='25' maxlength='10' value = '" + memoinfo.Name + "' style='border: 1px solid gray;ime-mode: active;font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;width:183px' onkeydown ='javascript:common.ClearKeyCode(event);'></td></tr>";
		if(document.all)
		{
			html +="			<tr><td style='font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;'>詳細</td><td><textarea id='" + memoinfo.Key +"Detail' cols=20 rows=4 onkeydown ='javascript:common.ClearKeyCode(event);' style='border: 1px solid gray;ime-mode: active;font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;width:167px;height:65px;'>" + memoinfo.Description + "</textarea></td></tr>";
		}
		else
		{
			html +="			<tr><td style='font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;'>詳細</td><td><textarea id='" + memoinfo.Key +"Detail' cols=20 rows=3 onkeydown ='javascript:common.ClearKeyCode(event);' style='border: 1px solid gray;ime-mode: active;font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;width:167px;height:65px;'>" + memoinfo.Description + "</textarea></td></tr>";
		}
		html +="			<tr><td colspan='2'>";
		html +="				<div align='right'>"
		html +="					<button id='memoDelete' title='削除する' Align='right' style='cursor:pointer;BORDER-RIGHT: #DCDCDC 0px solid; PADDING-RIGHT: 0px; BORDER-TOP: #DCDCDC 0px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; BORDER-LEFT: #DCDCDC 0px solid; WIDTH: 64px; PADDING-TOP: 0px; BORDER-BOTTOM: #DCDCDC 0px solid; HEIGHT: 23px; BACKGROUND-COLOR: #DCDCDC' onClick ='memoController.Delete(" +  memoinfo.Key +")' type='button'><IMG style='cursor:pointer;BORDER-RIGHT: #DCDCDC 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #DCDCDC 1px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; BORDER-LEFT: #DCDCDC 1px solid; WIDTH: 56px; PADDING-TOP: 0px; BORDER-BOTTOM: #DCDCDC 1px solid; HEIGHT: 18px; BACKGROUND-COLOR: #DCDCDC' height='18' src='MapForm/img/delete_mini.gif' width='56' border='0' ></button>";
		html +="					&nbsp;<button id='memoClose' title='閉じる' Align='right' style='cursor:pointer;BORDER-RIGHT: #DCDCDC 0px solid; PADDING-RIGHT: 0px; BORDER-TOP: #DCDCDC 0px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; BORDER-LEFT: #DCDCDC 0px solid; WIDTH: 54px; PADDING-TOP: 0px; BORDER-BOTTOM: #DCDCDC 0px solid; HEIGHT: 23px; BACKGROUND-COLOR: #DCDCDC' onClick ='memoController.EditEnd(" +  memoinfo.Key +")' type='button'><IMG style='cursor:pointer;BORDER-RIGHT: #DCDCDC 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #DCDCDC 1px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; BORDER-LEFT: #DCDCDC 1px solid; WIDTH: 46px; PADDING-TOP: 0px; BORDER-BOTTOM: #DCDCDC 1px solid; HEIGHT: 18px; BACKGROUND-COLOR: #DCDCDC' height='18' src='MapForm/img/close_mini.gif' width='46' border='0' ></button>";
		html +="				</div>"
		html +="			</td></tr>";
		html +="		</table>";
		html +="	</div>";
		html +="	<div id ='" + memoinfo.Key + "Label' style = 'CURSOR:pointer;position:absolute;top:2px;left:2px;width:230px;height:25px;visibility:hidden;font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;' onmouseover = \"help.ShowMessage(help.memomsg);\" onmouseout = \"help.ClearMessage();\">";
		html +="	</div>";
		html +=" <table id ='" + memoinfo.Key + "Table' border =0 style='position:absolute;top:0px;left:0px;visibility:hidden;font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;'>";
		html +=" <tr><td id ='" + memoinfo.Key + "td'></td></tr>";
		html +="</table>";
		html +="</div>";
		html +="</div>";
		
		
		document.getElementById("MemoArea").innerHTML += html;
		
		if(!document.all)
		{
			for(var i=0; i<this.memoList.length; i++)
			{
				memoinfo = this.memoList[i];
				if(memoinfo.Name.length > 0)
				{
					// 『'』, 『"』が消えてしまう為、別のところから取得する
					document.getElementById(memoinfo.Key +"Text").value = memoinfo.Name;
				}
			}
		}
		
		this.End();
	}
	
	// リスト表示.
	this.ShowList = function()
	{
		document.getElementById("memoList").innerHTML = "";
		
		html = "<table style='WIDTH: 260px;'>";
		for(var i=0; i<this.memoList.length; i++)
		{
			memoinfo = this.memoList[i];
			
			html += "<tr><td>";
			html += "<a id = \"" + memoinfo.Key +  "Link\" href='javascript:memoController.SetCenter(" +  memoinfo.X + "," + memoinfo.Y + ")' onmouseover='hide(); return true;' onfocus='hide(); return true;'>";
			html += "</a>";
			html += "</td></tr>";
		}
		html += "</table>";
		document.getElementById("memoList").innerHTML += html;
		
		for(var i=0; i<this.memoList.length; i++)
		{
			memoinfo = this.memoList[i];
			if (document.all)
			{
				document.getElementById(memoinfo.Key +  "Link").innerText = memoinfo.Name;
			}
			else
			{
				document.getElementById(memoinfo.Key +  "Link").textContent = memoinfo.Name;
			}
		}
		
	}
	
	// 編集開始.
	// key					キー.
	this.EditStart = function(key)
	{
		// 計測モードなら処理は行わない.
		if (mapImage.MapMode == common.MODEMEASURE)
		{
			return;
		}
	
		document.getElementById(key+ "Area").style.zIndex = 17;
		document.getElementById(key).style.height = "137px";
		
		document.getElementById(key).style.width = "240px";
	  document.getElementById(key + "Input").style.visibility = "";
		document.getElementById(key + "Label").style.visibility = "hidden";
		
	}
	
	// 編集終了.
	// key				キー.
	this.EditEnd = function(key)
	{
		if (document.getElementById(key + "Detail").value.length > 25)
		{
			alert("詳細に登録可能な文字数は２５文字までです。");
			return;
		}
		
		document.getElementById(key + "Input").style.visibility = "hidden";
		document.getElementById(key + "Label").style.visibility = "";
		
		if (document.all)
		{
			document.getElementById(key + "Label").innerText = this.GetLabelText(document.getElementById(key + "Text").value);
			document.getElementById(key + "td").innerText = document.getElementById(key + "Label").innerText;
		}
		else
		{
			document.getElementById(key + "Label").textContent = this.GetLabelText(document.getElementById(key + "Text").value);
			document.getElementById(key + "td").textContent = document.getElementById(key + "Label").textContent;
		}
		
		
		document.getElementById(key).style.width = 2 + document.getElementById(key + "Table").offsetWidth;
		document.getElementById(key + "Label").style.width =document.getElementById(key).style.width;
		document.getElementById(key).style.height = "25px";
		document.getElementById(key+ "Area").style.zIndex = 16;
		
		
		if (document.getElementById(key + "Text").value.length > 0)
		{
			memoController.Update(key);
		}
		else
		{
			memoController.Delete(key);
		}
	}
	
	// メモの表示.
	// 表示されていない場合はinnerHTMLに追加.
	// 表示されている場合は座標をずらす.
	this.ShowMemo = function()
	{
		// 既存のメモは全て破棄.
		//document.getElementById("MemoArea").innerHTML ="";
		
		cookieController.SaveVisible(document.getElementById("memoVisible").checked);

		if (document.getElementById("memoVisible").checked)
		{
			document.getElementById("MemoArea").style.visibility = "visible";
		}
		else
		{
			document.getElementById("MemoArea").style.visibility = "hidden";
		}

		if (document.getElementById("memoVisible").checked && this.memoList != null && this.memoList.length > 0)
		{
			for(var i=0; i<this.memoList.length; i++)
			{
				memoinfo = this.memoList[i];
				x = (parseInt(mapImage.GetMapPosXToPixcelPosX(memoinfo.X)) - parseInt(document.getElementById("MemoArea").style.left));
				y = (parseInt(mapImage.GetMapPosYToPixcelPosY(memoinfo.Y)) - parseInt(document.getElementById("MemoArea").style.top));
				
				if (document.getElementById(memoinfo.Key + "Area") != null)
				{
					document.getElementById(memoinfo.Key + "Area").style.top = (y - 20);
					document.getElementById(memoinfo.Key + "Area").style.left = x;
				}
				else if(memoinfo.Name.length > 0)
				{
					html ="";
					html +="<div id ='" + memoinfo.Key + "Area' style = 'Z-INDEX: 16;position:absolute;top:" + (y - 20)  + "px;left:" + x + "px;'>"
					html +="<img src='MapForm/img/pin_red.gif' style = 'Z-INDEX: 16;position:absolute;top:0px;left:0px;'>"
					html +="<div id ='" + memoinfo.Key + "' style = 'Z-INDEX: 16;position:absolute;top:0px;left:20px;BACKGROUND-COLOR: #DCDCDC;width:190px;height:25px;border-top: 1px solid #D4D0C8;border-left: 1px solid #D4D0C8;border-right: 2px solid #404040;border-bottom: 2px solid #404040;visibility:;'  onmousedown='memoController.EditStart(" + memoinfo.Key + ")' onmousemove='mapImage.MouseMove(event);' onmouseup='mapImage.MouseUp(event);'>";
					html +="	<div id ='" + memoinfo.Key + "Input' style = 'position:absolute;top:2px;left:2px;width:240px;height:137px;visibility:hidden;' onmouseover = \"help.ShowMessage(help.memoDeteilmsg);\" onmouseout = \"help.ClearMessage();\">";
					html +="		<table>";
					html +="			<tr><td style='font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;'>見出し</td><td><input id='" + memoinfo.Key +"Text' type='text' size='25' maxlength='10' value = \"\" style='border: 1px solid gray;ime-mode: active;font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;width:183px' onkeydown ='javascript:common.ClearKeyCode(event);'></td></tr>";
					if(document.all)
					{
						html +="			<tr><td style='font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;'>詳細</td><td><textarea id='" + memoinfo.Key +"Detail'  cols=20 rows=4 onkeydown ='javascript:common.ClearKeyCode(event);' style='border: 1px solid gray;ime-mode: active;font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;width:167px;height:65px;'>" + memoinfo.Description + "</textarea></td></tr>";
					}
					else
					{
						html +="			<tr><td style='font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;'>詳細</td><td><textarea id='" + memoinfo.Key +"Detail'  cols=20 rows=3 onkeydown ='javascript:common.ClearKeyCode(event);' style='border: 1px solid gray;ime-mode: active;font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;width:167px;height:65px;'>" + memoinfo.Description + "</textarea></td></tr>";
					}
					html +="			<tr><td colspan='2'>";
					html +="				<div align='right'>"
					html +="					<button id='memoDelete' title='削除する' Align='right' style='cursor:pointer;BORDER-RIGHT: #DCDCDC 0px solid; PADDING-RIGHT: 0px; BORDER-TOP: #DCDCDC 0px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; BORDER-LEFT: #DCDCDC 0px solid; WIDTH: 64px; PADDING-TOP: 0px; BORDER-BOTTOM: #DCDCDC 0px solid; HEIGHT: 23px; BACKGROUND-COLOR: #DCDCDC' onClick ='memoController.Delete(" +  memoinfo.Key +")' type='button'><IMG style='cursor:pointer;BORDER-RIGHT: #DCDCDC 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #DCDCDC 1px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; BORDER-LEFT: #DCDCDC 1px solid; WIDTH: 56px; PADDING-TOP: 0px; BORDER-BOTTOM: #DCDCDC 1px solid; HEIGHT: 18px; BACKGROUND-COLOR: #DCDCDC' height='18' src='MapForm/img/delete_mini.gif' width='56' border='0' ></button>";
					html +="					&nbsp;<button id='memoClose' title='閉じる' Align='right' style='cursor:pointer;BORDER-RIGHT: #DCDCDC 0px solid; PADDING-RIGHT: 0px; BORDER-TOP: #DCDCDC 0px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; BORDER-LEFT: #DCDCDC 0px solid; WIDTH: 54px; PADDING-TOP: 0px; BORDER-BOTTOM: #DCDCDC 0px solid; HEIGHT: 23px; BACKGROUND-COLOR: #DCDCDC' onClick ='memoController.EditEnd(" +  memoinfo.Key +")' type='button'><IMG style='cursor:pointer;BORDER-RIGHT: #DCDCDC 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #DCDCDC 1px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; BORDER-LEFT: #DCDCDC 1px solid; WIDTH: 46px; PADDING-TOP: 0px; BORDER-BOTTOM: #DCDCDC 1px solid; HEIGHT: 18px; BACKGROUND-COLOR: #DCDCDC' height='18' src='MapForm/img/close_mini.gif' width='46' border='0' ></button>";
					html +="				</div>"
					html +="			</td></tr>";
					html +="		</table>";
					html +="	</div>";
					html +="	<div id ='" + memoinfo.Key + "Label' style = 'CURSOR:pointer;position:absolute;top:2px;left:2px;width:230px;height:25px;visibility:;font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;' onmouseover = \"help.ShowMessage(help.memomsg);\" onmouseout = \"help.ClearMessage();\">";
				//	html += this.GetLabelText(memoinfo.Name);
					html +="	</div>";
					html +=" <table id ='" + memoinfo.Key + "Table' border =0 style='position:absolute;top:0px;left:0px;visibility:hidden;font-size: 10pt;font-family: 'ＭＳ ゴシック', 'ＭＳ Ｐゴシック', Osaka-等幅, Osaka, 平成角ゴシック;'>";
					html +=" <tr><td id ='" + memoinfo.Key + "td'></td></tr>";
					html +=" </table>";
					html +="</div>";
					html +="</div>";
					
					document.getElementById("MemoArea").innerHTML += html;
					
					document.getElementById(memoinfo.Key +"Text").value = memoinfo.Name;
					
					
					//  タグが悪さをしないようにinnerText
					if (document.all)
					{
						document.getElementById(memoinfo.Key +"Label").innerText = this.GetLabelText(memoinfo.Name);
						document.getElementById(memoinfo.Key +"td").innerText = this.GetLabelText(memoinfo.Name);
					}
					else
					{
						document.getElementById(memoinfo.Key +"Label").textContent = this.GetLabelText(memoinfo.Name);
						document.getElementById(memoinfo.Key +"td").textContent = this.GetLabelText(memoinfo.Name);
					}
					
					document.getElementById(memoinfo.Key).style.width = 2 + document.getElementById(memoinfo.Key + "Table").offsetWidth;
					document.getElementById(memoinfo.Key + "Label").style.width =document.getElementById(memoinfo.Key).style.width;
				}
			}
			
			if(!document.all)
			{
				for(var i=0; i<this.memoList.length; i++)
				{
					memoinfo = this.memoList[i];
					if(memoinfo.Name.length > 0)
					{
						// 『'』, 『"』が消えてしまう為、別のところから取得する
						document.getElementById(memoinfo.Key +"Text").value = memoinfo.Name;
					}
				}
			}
			
			/*
			for(var i=0; i<this.memoList.length; i++)
			{
				memoinfo = this.memoList[i];
				
				if (memoinfo.Name.length == 0)
				{
					this.Delete(memoinfo.Key);
				}
			}
			*/
		}
	}
	
	// 中心移動.
	// x				x座標
	// y				y座標
	this.SetCenter = function(x, y)
	{
	//	document.getElementById("MemoArea").innerHTML = "";
		document.getElementById("MemoArea").style.top = "0px";
		document.getElementById("MemoArea").style.left = "0px";
		viewController.MoveCenter(x, y);
	}
	
	// Kmlの作成.
	this.SaveKml = function()
	{
		if (this.memoList.length > 0)
		{
			window.location.href='KmlCreator.aspx';
		}
	}
	
	// Kmlの読み込み画面表示.
	this.LoadKml = function()
	{
		document.getElementById("MemoUpLoadWin").style.visibility = "visible";
	}
	
	// KML読み込み.
	this.GetMemoInfo = function()
	{
		Nais.Gis2006.SubSystems.Citizen15225.MemoLoadForm.GetMemoInfo(memoController.GetMemoInfo_Callback);
	}
	
	// KML読み込みコールバック.
	// response				結果.
	this.GetMemoInfo_Callback = function(response)
	{
/*		
		var memoInfo;
		
		if (response.value != null)
		{
			if (typeof(response.value[0]) == "undefined")
			{
				alert("選択されたファイルは対応している形式と異なる為、取り込めません。");
			}
			else
			{
				if (memoController.memoList.length > 100)
				{
					alert("保持できるメモの件数は１００件までです。");
				}
				
				for(var i=0; i<response.value.length; i++)
				{
					memoinfo = response.value[i];
					memoinfo.Key = exp.getTime();
					memoinfo.Key += i.toString(10);
					memoController.memoList[memoController.memoList.length] = response.value[i];
					
					cookieController.Add(memoinfo);
					
					if (memoController.memoList.length > 100)
					{
						alert("メモの件数が１００件を超えた為、全て取り込むことができませんでした。");
						break;
					}
				}
				
				memoController.ShowList();
				memoController.ShowMemo();
				dragLayer.Close('MemoUpLoadWin');
			}
		}
*/		
		var memoInfo;
		
		if (response.value != null)
		{
			if (typeof(response.value[0]) == "undefined")
			{
				alert("選択されたファイルは対応している形式と異なるため、取り込めません。");
			}
			else
			{
				if (memoController.memoList.length > 15)
				{
					alert("保持できるメモの件数は１５件までです。");
					return;
				}
				
				for(var i=0; i<response.value.length; i++)
				{
					if (memoController.memoList.length > 15)
					{
						alert("メモの件数が１５件を超えた為、全て取り込むことができませんでした。" );
						break;
					}
					
					memoinfo = response.value[i];
					memoinfo.Key = exp.getTime();
					memoinfo.Key += i.toString(10);
					memoinfo.Name = memoinfo.Name.substr(0, 10);
					memoinfo.Description = memoinfo.Description.substr(0, 25);
					memoinfo.X = parseInt(memoinfo.X);
					memoinfo.Y = parseInt(memoinfo.Y);
					memoController.memoList[memoController.memoList.length] = response.value[i];
					
				}
				
				cookieController.Save(memoController.memoList);
				
				memoController.ShowList();
				memoController.ShowMemo();
				dragLayer.Close('MemoUpLoadWin');
			}
		}
	}
	
	// ラベルの作成.
	// text					対象文字列.
	this.GetLabelText = function(text)
	{
		var label;
		
		if (text.length > 15)
		{
			label = text.substr(0,12) + "...";
		}
		else
		{
			label = text.substr(0,15);
		}
		
		return label;
	}
}

// Cookie制御クラス.
function CookieController()
{
	
	// メモをCookieに保存する.
	// memoList				メモリスト.
	this.Save = function(memoList)
	{
/*
		exp=new Date();
		exp.setTime(exp.getTime()+1000*60*60*24*7300);
		
		for(var i=0; i<memoList.length; i++)
		{
			if (memoList[i] != null)
			{
				memoinfo = memoList[i];
				attribute = escape(memoinfo.Name) + "%00" + escape(memoinfo.Description)  + "%00" + escape(memoinfo.X)  + "%00" + escape(memoinfo.Y);
				document.cookie = "Memo_" + memoinfo.Key + "=" + attribute + "; expires=" + exp.toGMTString();
			}
		}
*/		
	
		exp=new Date();
		exp.setTime(exp.getTime()+1000*60*60*24*7300);
		
		attribute =  "";
		for(var i=0; i<memoList.length; i++)
		{
			if (memoList[i] != null)
			{
				memoinfo = memoList[i];
				
				if (attribute.length > 0)
				{
					attribute += "%00%00%00";
				}
				
				attribute += escape(memoinfo.Name) + "%00" + escape(memoinfo.Description)  + "%00" + escape(memoinfo.X)  + "%00" + escape(memoinfo.Y)  + "%00" + escape(memoinfo.Key);
			}
		}
		
		document.cookie = "UserMemo=" + attribute + "; expires=" + exp.toGMTString();
	}
	
	
	// 保存.
	// memoList				メモリスト.
	this.Add = function(memoinfo)
	{
		exp=new Date();
		exp.setTime(exp.getTime()+1000*60*60*24*7300);
		attribute = escape(memoinfo.Name) + "%00" + escape(memoinfo.Description)  + "%00" + escape(memoinfo.X)  + "%00" + escape(memoinfo.Y);
		document.cookie = "Memo_" + memoinfo.Key + "=" + attribute + "; expires=" + exp.toGMTString();
	}
	
	// 読み込み.
	this.Load = function()
	{
		var memoList = new Array();
		memocount = 0;
		
		cklng = document.cookie.length;
		ckary = document.cookie.split("; ");
/*
		for(var i=0; i<ckary.length; i++)
		{
			// Memo_XXX （旧形式）
			if (ckary[i].substr(0, ckary[i].indexOf("=") + 1).indexOf("Memo_") >= 0)
			{
				attribute = ckary[i].substr(ckary[i].indexOf("=") + 1, ckary[i].length);
				memoary = attribute.split("%00");
				key = ckary[i].substr(5, ckary[i].indexOf("=") - 5);

				memoList[memocount] = new MemoInfo(key, unescape(memoary[0]), unescape(memoary[1]), unescape(memoary[2]), unescape(memoary[3]));
				
				memocount++;
			}
		}
		
		return memoList;
*/
		
		for(var i=0; i<ckary.length; i++)
		{
			if (ckary[i].substr(0, ckary[i].indexOf("=") + 1).indexOf("UserMemo") >= 0)
			{
				// UserMemo
				usermemos = ckary[i].substr(ckary[i].indexOf("=") + 1, ckary[i].length);
				usermemoList = usermemos.split("%00%00%00");
				
				for(var j=0; j < usermemoList.length; j++)
				{
					attribute = usermemoList[j];
					memoary = attribute.split("%00");
					
					if (typeof(memoary[4]) != "undefined" && memoary[4] != "undefined" && memoary[4] != "")
					{
						memoList[memocount] = new MemoInfo(unescape(memoary[4]), unescape(memoary[0]), unescape(memoary[1]), unescape(memoary[2]), unescape(memoary[3]));
						memocount++;
					}
				}
			}
			else if (ckary[i].substr(0, ckary[i].indexOf("=") + 1).indexOf("Memo_") >= 0)
			{
				// Memo_XXX （旧形式）
				attribute = ckary[i].substr(ckary[i].indexOf("=") + 1, ckary[i].length);
				memoary = attribute.split("%00");
				key = ckary[i].substr(5, ckary[i].indexOf("=") - 5);

				memoList[memocount] = new MemoInfo(key, unescape(memoary[0]), unescape(memoary[1]), unescape(memoary[2]), unescape(memoary[3]));
				
				memocount++;
				
				// 前の形式は破棄
				exp=new Date();
				exp.setYear(exp.getYear() - 1);
				document.cookie = "Memo_" + key + "=; expires=" + exp.toGMTString();
			}
		}
		
		// 形式変更の可能性があるので再度保存
		this.Save(memoList);
		
		return memoList;
	}

	// 削除.
	// key			キー.
	this.Delete = function(key)
	{
		exp=new Date();
		exp.setYear(exp.getYear() - 1);
		document.cookie = "Memo_" + key + "=; expires=" + exp.toGMTString();
	}
	
	// 表示状態の保存.
	// visible			表示状態.
	this.SaveVisible = function(visible)
	{
		exp=new Date();
		exp.setTime(exp.getTime()+1000*60*60*24*7300);
		attribute = escape(visible);
		document.cookie = "MemoVisible"  + "=" + attribute + "; expires=" + exp.toGMTString();
	}
	
	// 表示状態の読み込み.
	this.LoadVisible = function()
	{
		var visible = false;
		memocount = 0;

		cklng = document.cookie.length;
		ckary = document.cookie.split("; ");
		
		for(var i=0; i<ckary.length; i++)
		{
			
			if (ckary[i].substr(0, ckary[i].indexOf("=") + 1).indexOf("MemoVisible") >= 0)
			{
				attribute = ckary[i].substr(ckary[i].indexOf("=") + 1, ckary[i].length);
				memoary = attribute.split("%00");
				if (unescape(memoary[0]) == "true")
				{
					visible = true;
				}
				break;
			}
		}
		
		return visible;
	}
}

