/*
 *	地図表示関連
*/
var netmap = null;
var g_tooltip      = null;		//ツールチップ
var hop_icon;					// はねるアイコン

// 指定位置の地図表示
function display_map(po, fixedmap){

  // スケールの指定が無ければ、現状に同じ
  if(!po.scale) po.scale = netmap.getScaleIndex();
  
  // 地図サイズの設定(マイホーム設定など、サイズ固定地図では実施しない)
  if(!fixedmap) set_size_of_map_div();

  netmap.zoomTo(po);
}

// map divの横幅・縦幅、map_center divの位置を設定
function set_size_of_map_div() {

  var map_frame = el('mapframe')
  var map_div = el('map');
//  var indexmap_div = el('indexmap');
  var center_div = el('map_center');
  
  var inner_width  = get_inner_width() - 10 - 10;
  var inner_height = get_inner_height() - 5;

  var panel_space = el('search_panel');
  var navi_space = el('headerNavi');
  var name_space = el('medicalName');
  var imacoco_space = el('pointInfo');
  var legend_space = el('hanrei');
  var copyright_space = el('copyright');

  // map divの幅 = ウィンドウ幅 - searchPanelの幅
  // 最低サイズはコントロールパネルと縮尺が残る程度。
  var w = inner_width - panel_space.offsetWidth;
  if (w < 186) {
    w = 186;
  }
  var h = inner_height - navi_space.offsetHeight - name_space.offsetHeight
			- imacoco_space.offsetHeight - legend_space.offsetHeight - copyright_space.offsetHeight - 1;
//	alert('INNER:'+inner_height+"\n"+
//			'NAVI:'+navi_space.clientHeight+"\n"+
//			'NAME:'+name_space.clientHeight+"\n"+
//			'IMACOCO:'+imacoco_space.clientHeight+"\n"+
//			'LEGEND:'+legend_space.offsetHeight+"\n"+
//			'COPYRIGHT:'+copyright_space.clientHeight);
  if (h < 316) {
    h = 316;
  }

  map_div.style.width  = (w-16) + 'px';
  map_div.style.height = (h-16) + 'px';
  map_frame.style.width  = (w-16) + 'px';
  map_frame.style.height = (h-16) + 'px';
  
  // indexmap divのTOP、LEFT
  /*
  var left = map_div.offsetWidth - indexmap_div.offsetWidth - 10;
  var top = map_div.offsetHeight - indexmap_div.offsetHeight - 10;

  indexmap_div.style.top = top + 'px';
  indexmap_div.style.left = left + 'px';
  */

  // map_center divのTOP、LEFT
  var left = (map_div.offsetWidth / 2) - (center_div.offsetWidth / 2);
  var top = (map_div.offsetHeight / 2) - (center_div.offsetHeight / 2);

  center_div.style.top = top + 'px';
  center_div.style.left = left + 'px';
}

function get_inner_width() {
    if (window.innerWidth) {
        return window.innerWidth;
    } else if (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) {
        // IE6CSS: measure the html element's clientWidth
        return document.body.parentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        return document.body.clientWidth;
    }
    return 0;
}
function get_inner_height() {
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) {
        // IE6CSS: measure the html element's clientHeight
        return document.body.parentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        return document.body.clientHeight;
    }
    return 0;
}

// リスト上の医療機関のリンク・マウスオーバーでアイコンがはねる
function HopIcon(obj)
{
	this.timer = null;
	this.obj = obj;

	this.start = function()
	{
		if (!this.obj) return;
		var scope = this;
		this.timer = setInterval( function(){ scope.jump()}, 50);
	};
	
	this.stop = function()
	{
		if (!this.timer) return;
		clearInterval(this.timer);
	};
	this.jump = function()
	{
		this.obj.jump();
	}
}
function hopIcon_start(id){
	if(id && netmap){
		hop_icon = new HopIcon( netmap.findFigureById(id));
		hop_icon.start();
	}
}
function hopIcon_stop(){
	if(hop_icon){
		hop_icon.stop();
	}
}


// 地図表示変更（移動・スケール変更・ウィンドウリサイズなど）時処理
function move_center_func(evt,extents){

  // ウィンドウリサイズ対応
  switch(search_type){
    case 'address':	// 住所で検索
      set_search_panel_height('addr_search_space');
      
      if(el('addr_list').innerHTML != ""){
        el('zip_list_tbody').style.height = "auto";		// いったんheightをリセット
        if(search_panel_height() - 360 < el('addr_list').offsetHeight){
        	var tbody_height = search_panel_height() - 360 - 37;
        	if(tbody_height < 74){
        		tbody_height = 74;
        		el('addr_search_space').style.height = "471px";		// リストが表示されている状態での最低Height
        	}
        	el('zip_list_tbody').style.height = tbody_height + "px";
        }
      }

      break;

    case 'station':	// 駅で入力
      set_search_panel_height('st_search_space');
      search_station();			// 近隣駅検索が表示されていれば、駅再検索
      break;

    case 'medical':	// 近くの医療機関リスト
      set_search_panel_height('medical_list_space');
      //alert('[DEBUG2] SPACE(offset):'+el('medical_list_space').offsetHeight+"px");
      search_medical(PAGE_RESET);  // 医療機関近隣検索
      break;
  }
  
  // 現時点表示の保存(次画面から戻る際の表示内容)
  var center = netmap.getCenter();
  el('svlat').value = center['lat'];
  el('svlon').value = center['lon'];
  el('svscale').value = netmap.getScaleIndex();
  
  // 現在地近辺の住所表示
  where_is_here();
  
  // 地図上のアイコン描画
  //netmap.addFigureFromJSON( getMappingDataURL(netmap.getExtents()));
  search_medical_mapping();
  
  // 同一箇所の複数医療機関が表示されていれば位置調整
  if(plural_list) _setObjectPosition(netmap.findFigureById(plural_list.objID),plural_list);
}

// 医療機関アイコン・マウスオーバーで名称表示
function mouse_over_func(obj,enter){
	if(netmap.isLocked()) return;

	if (enter) {
		if(g_tooltip) netmap.removeHtmlElement(g_tooltip);

		
		//アイコンに関連付けられた医療機関名をツールチップっぽく表示
		g_tooltip = netmap.addHtmlElement('div', obj.x, obj.y);
		g_tooltip.style.position = "absolute";
		g_tooltip.style.border = "solid 1px black";
		g_tooltip.style.backgroundColor = "#ffffe0";
		g_tooltip.style.fontSize = "10px";
		g_tooltip.style.lineHeight = "12px";
		g_tooltip.style.textAlign = "left";
		//g_tooltip.style.zIndex = 200;
		g_tooltip.style.padding = "2px";
		g_tooltip.style.whiteSpace = "nowrap";
		g_tooltip.innerHTML = obj.name;
		if(obj.other.length){
			for(var i = 0; i < obj.other.length; i++){
				var other_medical = netmap.findFigureById(obj.other[i]);
				g_tooltip.innerHTML += "<br>" + other_medical.name;
			}
		}

		// 表示位置の調整
		/*
		var mapSize = netmap.getPixelSize();
		var pos     = netmap.getPixelPoint(obj.x, obj.y);
//		var w       = obj.name.length*12;
//		var h       = 15;
		var w       = parseInt(netmap.myKaMap.getObjectWidth(g_tooltip));
		var h       = parseInt(netmap.myKaMap.getObjectHeight(g_tooltip));
		var x       = pos.x;
		var y       = pos.y;

		var lr = x + w < mapSize.width;		//left(0) or right(1)
		var tu = y - h < 0;					//top(0) or under(1)

		var cursorSize = 16;
		ox = lr ? 0 : -w;
		oy = tu ? 0 : -h;
		
		netmap.moveHtmlElement(g_tooltip, obj.x, obj.y, ox, oy);
		*/
		_setObjectPosition(obj,g_tooltip);

	} else {
		//ツールチップ消去
		netmap.removeHtmlElement(g_tooltip);
	}
}

// 地図上に表示するツールチップ等の大きさおよび位置の調整
function _setObjectPosition(obj,elm){
	if(!obj || !elm) return;

	// 表示位置の調整
	var mapSize = netmap.getPixelSize();
	var pos     = netmap.getPixelPoint(obj.x, obj.y);
	var w       = parseInt(netmap.myKaMap.getObjectWidth(elm));
	var h       = parseInt(netmap.myKaMap.getObjectHeight(elm));
	var x       = pos.x;
	var y       = pos.y;

	var lr = x + w < mapSize.width;		//left(0) or right(1)
	var tu = y - h < 0;					//top(0) or under(1)

	var cursorSize = 16;
	ox = lr ? 0 : -w;
	oy = tu ? 0 : -h;
	
	netmap.moveHtmlElement(elm, obj.x, obj.y, ox, oy);
}


// 医療機関周辺地図（要は初期化）
function initmap() {
	//netmap.revertPos();	// たまに書き換わってる時があるため（原因調査中）
	display_map({lat: LAT_DEFAULT, lon: LON_DEFAULT});
}

// 閉じるボタン（汎用）
// func = onclick時のfunction指定。
function build_close_button(func){
  var myButton = "<button ";
  myButton += "type='button' ";
  myButton += "class='close' ";
  myButton += "onClick='" + func + "()'";
  myButton += "onMouseover='this.style.backgroundColor=\"#FF8B7C\";'";
  myButton += "onMouseOut='this.style.backgroundColor=\"#E8765D\";'";
  myButton += ">×</button>";

  return myButton;
}


