////////////////////////////////////////////////////////////
//
//ファイル名:   common.js
//機能		:	アプリケーション共通変数定義等
//依存		:	-
//バージョン:　 2.0.0.1
//更新日時	:　 2006.08.01
//更新者	:	kawame
//
////////////////////////////////////////////////////////////


//
/* 座標計算に必要な変数＆定数 */
var DISPLAY_PPC = (DISPLAY_DPI)/2.54;//画面上のpixel/cm(変更不可)
var standard_length = 100000;//(変更不可)
var	standard_scale = 100000;//座標計算の際に基準とする縮尺分母(変更不可)
var standard_map_dpi = standard_length/standard_scale / DISPLAY_PPC*1000;//(変更不可)
var map_mpp = null;//地図のm/pixel
var map_magnification = 1.0;//地図画像の画面に対する倍数（現在未使用）

var search_mode = null;

//モード


var cur_mode = "none";

var cur_sizebtn = null;
var cur_scalebtn = null;

var style_path = null;

var mapClip_minx = null;
var mapClip_maxx = null;
var mapClip_miny = null;
var mapClip_maxy = null;
var mapLayer_left = null;
var mapLayer_top  = null;

var mapExt_minx = null;
var mapExt_maxx = null;
var mapExt_miny = null;
var mapExt_maxy = null;

//Viewの変更の可否	
var flg_viewLock ;//= false;
	
//地図
var mapLayer		= null;
var moveLayer       = null;

//地図上で行う（マウス）イベントをキャッチさせるレイヤ
var eventLayer		= null;//マウスイベントを取得する画面上の場所

//地図読み込み中に表示するメッセージレイヤ
var MAP_LOAD_LAYER	= "map_loading";
var loadLayer		= null;
var LOADING_IMG		= "styleset/themeD/images/loadingmsg.gif";

//地図表示切替時に画面レイアウトが崩れないようにするレイヤ
var MAP_DUMMY_LAYER="map_dummy";
var dummyLayer		= null;


//現在の地図設定



var map_minX=null;
var map_minY=null;
var map_maxX=null;
var map_maxY=null;
var map_centerX=null;
var map_centerY=null;
var map_width=null;
var map_height=null;
var map_scale=null;
var map_scaleMin = null;
var map_scaleMax = null;

//Ajaxに引き渡す座標・半径



var con_coord  = null;//座標



var con_coords = null;//座標配列(x1 y1,x2 y2,...の文字列)
var con_word   = null;//フリーワード



var con_sid	   = null;//AigsDefMngSearchのID
var con_mapurl = null;//地図URL
var con_radius = null;//属性検索範囲
var con_attrcount = null;//属性表示件数
var con_attrview = null;//属性表示方法 0：個票 1:一覧
var con_layerno = null;//レイヤ番号
var con_neighbor_search_dist = null;//近隣施設検索の半径



var con_measuretype = null;//計測タイプ length:距離 area:面積


var con_searchtype = "point";//属性検索タイプ point  or  circle
var con_skey = null;//近隣施設表示で選択されたアイテムのキー
var con_removekey = null;//一時図形削除キー
var con_post= null;//
var con_emphasisid = null;//強調図形ID
var con_routerad = null;//ルート検索のノード検索範囲

//印刷
var print_maptitle = null;//現在表示している地図のLevel0名称
var print_url = null;//印刷のためのURL
var print_date = null;//印刷時刻
var print_scale = null;//印刷縮尺
var pdf_id = null;
var lv1ID = null;
//マウスダウン・アウトイベント実行の可否フラグ
//(矩形拡大・つかみ移動で使用)
var mouse_do_flg = false;//down & up

var click_flg = false;//click

var move_flg = false;//move

var dbl_flg = false;//doubleclick
//ダブルクリックイベント使用時に
//クリックイベントとの調整を行うために
//座標を格納しておくために準備
var point_click = null;

var cur_cursor = "auto";

// 何もしない


var NONE	 			= "none";	


//マウスイベント用の透明レイヤを用意


function setEventLayer()
{
	//if(!eventLayer)
	//{
		eventLayer=document.getElementById("map_event");
	//}
	eventLayer.height=map_height;	
	eventLayer.width=map_width;	
	
	eventLayer.style.display="block";
	
	if(gis.modeManager.getCurrentMode() == 'none')
	{
		modeChange(DEF_MODE)
	}
}


//ダブルクリックイベントを利用する際に使用	
var click_count = 0;

function setClick_count(evt)
{//logger.debug("click" + click_count);
	click_count += 1;
}
	
function setDblClickFlg(evt)
{//logger.debug("dbl");
	dbl_flg = true;	
}


function checkDblClickEvent()
{
	if(dbl_flg) 
	{
		//ダブルクリックで行いたいメソッド
		setMC(point_click,true);
		point_click = null;
	}
	else 
	{	
		if(click_count == 1)
		{
			//クリックで行いたいメソッド
			//setAttrSearchPoint(point_click,true);
			onClickEvent(point_click,'coord');
		}
	}
	click_count = 0;
	dbl_flg = false;
	window.clearInterval(dblClickInterval);
	dblClickInterval = null;
}
//--------------------------------------------

function onClickEvent(pos,mode)
{
	if(cur_mode == 'move_drag' || cur_mode == 'zoom_rect')
	{
		setAttrSearchPoint(pos,mode);
	}
	else if(cur_mode == 'search_route')
	{
		setRouteGoal(pos,mode);
	}
	else if(cur_mode == 'start_route')
	{
		setRouteStart(pos,mode);
	}
}


//
//実座標からマップ上の座標を計算



//
function getmapCoords(px,py)
{
	var x=(px-map_minX)/(map_maxX-map_minX)*map_width;
	var y=(map_height-(py-map_minY)/(map_maxY-map_minY)*map_height);
	
	var c=new InputPoint(x,y);
	
	return c;
}

//
//	イベントから座標を取得する



//
function getCoords(evt)
{
	var coords = {x:0, y:0};
	if (evt.pageX)
	{
		coords.x = evt.pageX;
		coords.y = evt.pageY;
	}
	else if (evt.clientX)
	{
		coords.x = evt.clientX + document.body.scrollLeft - document.body.clientLeft;
		coords.y = evt.clientY + document.body.scrollTop - document.body.clientTop;
		if (document.body.parentElement && document.body.parentElement.clientLeft)
		{
			var bodyParent = document.body.parentElement;
			coords.x += bodyParent.scrollLeft - bodyParent.clientLeft;
			coords.y += bodyParent.scrollTop - bodyParent.clientTop;
		}
	}
	return coords;
}

//
//	イベントから座標を取得する（サブ）



//
function getPositionedEventCoords(evt)
{
	var coords = new Coords(0, 0);
	
	if (evt.layerX)
	{
		var borders = {
				x:parseInt(getElementStyle("progressBar", "borderLeftWidth", "border-left-width")),
				y:parseInt(getElementStyle("progressBar", "borderTopWidth", "border-top-width"))
				};
		coords.x = evt.layerX - borders.x;
		coords.y = evt.layerY - borders.y;
	}
	else if (evt.offsetX)
	{
		coords.x = evt.offsetX;
		coords.y = evt.offsetY;
	}
	evt.cancelBubble = true;
	return coords;
}

//
//	指定要素のスタイルを取得する



//
function getElementStyle(elmID, IEStyleAttr, CSSStyleAttr)
{
	var elm = document.getElementById(elmID);
	if (elm)
	{
		if (elm.currentStyle)
		{
			return elm.currentStyle[IEStyleAttr];
		}
		else if (window.getComputedStyle)
		{
			var compStyle = window.getComputedStyle(elm, "");
			return compStyle.getPropertyValue(CSSStyleAttr);
		}
	}
	else
	{
		return 0;
	}
}

//
//	位置指定要素の座標を取得する




//
function getElementPosition(elem)
{
	var l = null;
	var t = null;

    if(document.layers )
	{
		l = 0;
		t = 0;
    }
	else if(document.all || document.getElementById)
	{
		var offsetTrail = elem;//(typeof elem == 'string') ? document.getElementById(elem) : elem;
		var offsetLeft = 0;
		var offsetTop = 0;
		while (offsetTrail)
		{
			offsetLeft += offsetTrail.offsetLeft;
			offsetTop += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined")
		{
		   offsetLeft += document.body.leftMargin;
		   offsetTop += document.body.topMargin;
		}
		l = offsetLeft;
		t = offsetTop;
    }
     return {left:l, top:t};
}


//
//	位置指定要素の親要素から見た座標を取得する



//
function getElementPositionFromParent(elem)
{
	var l = null;
	var t = null;

    if(document.layers )
	{
		l = 0;
		t = 0;
    }
	else if(document.all || document.getElementById)
	{
		var offsetTrail = elem;//(typeof elem == 'string') ? document.getElementById(elem) : elem;
		l = offsetTrail.offsetLeft;
		t = offsetTrail.offsetTop;
		
		if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined")
		{
		   offsetLeft += document.body.leftMargin;
		   offsetTop += document.body.topMargin;
		}
    }
     return {left:l, top:t};
}

//
//	座標




//
function Coords(x, y)
{
	this.x = x;
	this.y = y;
	
	this.toString = function()
	{
		return this.x + "," + this.y;
	}
	
	this.equals = function(that)
	{
		if (that)
		{
			return (this.x == that.x) && (this.y == that.y);
		}
		else
		{
			return false;
		}
	}

	this.isNeighbor = function(that)
	{
		if (that)
		{
			return (Math.abs(this.x - that.x) < 2) && (Math.abs(this.y - that.y) < 2);
		}
		else
		{
			return false;
		}
	}
}

//
//	点
//
function InputPoint(x, y)
{
	var _x = parseFloat(x);
	var _y = parseFloat(y);
	
	// X座標（緯度経度の場合は経度）




	this.x = _x;
	
	// Y座標（緯度経度の場合は緯度）




	this.y = _y;
	
	this.getX = function()
	{
		return _x;
	}
	
	this.getReal = function(xmin, xmax, width, ymin, ymax, height)
	{
		var rx = Math.round((xmin + _x * (xmax - xmin) / width)*1000)/1000;
		var ry = Math.round((ymin + (height - _y) * (ymax - ymin) / height)*1000)/1000;
		var rp = new InputPoint(rx,ry);
		
		return rp;	
	}
	
	this.getXReal = function(xmin, xmax, width)
	{
		return  Math.round((xmin + _x * (xmax - xmin) / width)*1000)/1000;
	}
	
	this.getXRealCM = function(xmin, xmax, width)
	{
		return  Math.round((xmin + _x * (xmax - xmin) / width)*100000)/1000;
	}
	
	this.getY = function()
	{
		return _y;
	}
	
	this.getYReal = function(ymin, ymax, height)
	{
		return Math.round((ymin + (height - _y) * (ymax - ymin) / height)*1000)/1000;
	}

	this.getYRealCM = function(ymin, ymax, height)
	{
		return Math.round((ymin + (height - _y) * (ymax - ymin) / height)*100000)/1000;
	}
	
	this.setX = function(x)
	{
		_x = parseInt(x);
	}

	this.setY = function(y)
	{
		_y = parseInt(y);
	}
	
	this.toString = function()
	{
		return _x + " " + _y;
	}

	this.toStringReal = function(xmin, xmax, width, ymin, ymax, height)
	{
		return this.getXReal(xmin, xmax, width) + " " + this.getYReal(ymin, ymax, height);
	}
	
	this.toStringRealCM = function(xmin, xmax, width, ymin, ymax, height)
	{
		return this.getXRealCM(xmin, xmax, width) + " " + this.getYRealCM(ymin, ymax, height);
	}
	
	this.equals = function(p)
	{
		return (_x == p.getX()) && (_y == p.getY());
	}

	this.isNeighbor = function(p)
	{
		if (p)
		{
			return (Math.abs(_x - p.getX()) < 2) && (Math.abs(_y - p.getY()) < 2);
		}
		else
		{
			return false;
		}
	}
}


/*画像の入れ替えメソッド*/
//ノーマル
function setImgN()
{	
	var s = this.src;
	var i = s.lastIndexOf(".",s.length)-1;
	var txt = "_"+s.substr(i,1)+".";
	this.src=s.replace(txt,"_n.");
	//this.src=s.replace(s.substr(s.length-6,6),"_n.gif");
}
//カレント




function setImgC()
{
	var s = this.src;

	var i = s.lastIndexOf(".",s.length)-1;
	var txt = "_"+s.substr(i,1)+".";
	this.src=s.replace(txt,"_c.");
	//this.src=s.replace(s.substr(s.length-6,6),"_c.gif");
}
//使用不可
function setImgD()
{
	var s = this.src;
	var i = s.lastIndexOf(".",s.length)-1;
	var txt = "_"+s.substr(i,1)+".";
	this.src=s.replace(txt,"_d.");
	//this.src=s.replace(s.substr(s.length-6,6),"_d.gif");
}
//フォーカス中
function setImgO()
{
	var s = this.src;
	var i = s.lastIndexOf(".",s.length)-1;
	var txt = "_"+s.substr(i,1)+".";
	this.src=s.replace(txt,"_o.");
	//this.src=s.replace(s.substr(s.length-6,6),"_o.gif");
	return false;
}

/*ラジオボタンのクリックイベントを行う*/
//node　=　ラジオボタンのid
function radioClick(node)
{

	var _node = win.document.getElementById(node);
	if(_node)
	{
		_node.checked = true;
		_node.onclick();
	}

}
//
//	ログ
//
function Logger(nodeId)
{
	this.nodeId = nodeId;
	this.debug = function(message)
	{
		var node = document.getElementById(nodeId);
		if (node)
		{
			node.value = node.value + "\n" + message;
		}
	}
}

//コンテンツのdisplay切替メソッド
function displaychange(obj,container)
{
	var node =document.getElementById(container);
	if(node)
	{
		if((obj.type == "image") || (obj.tagName == "IMG"))
		{
			var src = obj.src;
			if(node.style.display == "block")
			{	
				node.style.display = "none";
				obj.src=src.replace("close.gif","open.gif");
			}
			else
			{
				node.style.display = "block";
				obj.src=src.replace("open.gif","close.gif");
			}
		}
		else if(obj.type == "button")
		{
			if(node.style.display == "block")
			{	
				node.style.display = "none";
				obj.value="open▽";
			}
			else
			{
				node.style.display = "block";
				obj.value="close△";
			}
		}
	}
}

var removeItemMsg = "";

///図形を全て削除
function removeAll()
{
	if(figure_flg)
	{
		var msg = removeItemMsg + "を削除します。\n\nよろしいですか？";
		//var f = confirm("【メモ】・【計測結果】・【選択した情報の強調/ピン】を削除します。\n\nよろしいですか？");
		var f = confirm(msg);
		if(f)
		{	
			setMapLoadingLayer(); 
			GetRequestItem('reqItemRemove');
			var node = document.getElementById("clear_temp");
			if(node)
			{
				node.style.display = "none";
			}
			figure_flg =false;
			removeItemMsg = "";
		}
	}
}

	
//コンテンツの表示初期設定offのものを非表示にする
function setControlDisp()
{
	var nodes = document.getElementsByName("displayAlter");
	if(nodes && controlNoneArray.length>0)
	{
		for(var i = 0;i<nodes.length;i++)
		{
			for(var j = 0;j<controlNoneArray.length;j++)
			{
				if(nodes[i].id == controlNoneArray[j])
				{
					nodes[i].onclick();
					break;
				}
			}
		}
	}
}
//コントロールコンテナーのサイズ変更（ウィンドウサイズに合わせる）


function setsize()
{
	var node = document.getElementById("contents_container");
	var parent = node.parentNode;
	if(node)
	{
		if(document.all)
		{
			if(map_height+map_around_height-50 > document.body.clientHeight-frame_height-50)
			{
				node.style.height = map_height+map_around_height-50;
			}
			else
			{
				node.style.height = document.body.clientHeight-frame_height-50;
			}
		}
		else
		{
			if(map_height+map_around_height > window.innerHeight-frame_height-50)
			{
				node.style.height = map_height+map_around_height;
			}
			else
			{
				node.style.height = window.innerHeight-frame_height-50;
			}
		}
		node.style.width = parent.width; 
	}
}


