// class 
// MapDragging Class
var MapDragging = Class.create();

MapDragging.prototype = {

  initialize: function(header, map, mapView, map_mask_for_ie) {
    this.header = $(header);
    this.map = $(map);
    this.mapView = $(mapView);
    this.ie_flg = /MSIE/.test(navigator.userAgent);
    this.map_mask = this.ie_flg ? $(map_mask_for_ie) : null;
    this.x = null;
    this.y = null;
    this.header_height = this.header.offsetHeight;

    Element.observe(this.header, 'mousedown',
      this.capture.bindAsEventListener(this));
    Element.observe(document, 'mouseup',
      this.uncapture.bindAsEventListener(this));
    Element.observe(document, 'mousemove',
      this.moveByMouse.bindAsEventListener(this));
    Element.observe(this.header, 'dblclick',
      this.moveHomePosition.bindAsEventListener(this));
  },

  capture: function(e) {
    this.captured = true;

    this.mapView.style.position = "relative";
    this.map.style.position = "absolute";

    if(this.ie_flg) this.removeExpression();

    this.mouseX = Event.pointerX(e);
    this.mouseY = Event.pointerY(e);

    this.x = this.mouseX - this.pointerOffsetX(e);
    this.y = this.mouseY - this.pointerOffsetY(e);

    this.map.style.left = this.x + "px";
    this.map.style.top  = this.y + "px";
  },

  removeExpression: function() {
    this.map.style.removeExpression("top");
    this.map.style.removeExpression("left");
    if(this.map_mask != null) {
      this.map_mask.style.removeExpression("top");
      this.map_mask.style.removeExpression("left");
      this.map_mask.style.display = 'none';
    }
  },

  moveHomePosition: function() {
    this.captured = false;
    if(this.ie_flg) this.removeExpression();
    this.map.style.position = "static";
    this.mapView.style.position = "static";
    this.x = null;
    this.y = null;
    this.map.style.left = "50px"; // position:staticなので無視される
    this.map.style.top = "50px";  // position:staticなので無視される
  },

  uncapture: function(e) {
    if(!this.captured) return;

    this.map.style.position = this.ie_flg ? "absolute" : "fixed";

    this.x -= getScrollLeft();
    this.y -= getScrollTop();

    if(this.ie_flg) {
      this.map.style.setExpression("left",
        this.x + "+(documentElement.scrollLeft||body.scrollLeft)+'px'");
      this.map.style.setExpression("top",
        this.y + "+(documentElement.scrollTop||body.scrollTop)+'px'");
      if(this.map_mask != null) {
        this.map_mask.style.setExpression("left",
          this.x + "+(documentElement.scrollLeft||body.scrollLeft)+'px'");
        this.map_mask.style.setExpression("top",
          this.y + "+(documentElement.scrollTop||body.scrollTop)+'px'");
      }
    } else {
      this.move(this.x, this.y);
    }

    this.captured = false;
  },

  fixed_mask: function(src,trg) {
    trg.style.left = src.style.left;
    trg.style.top = src.style.top;
    trg.style.width = src.offsetWidth + 'px';
    trg.style.height = src.offsetHeight + 'px';
    trg.style.display = 'block';
  },

  move: function(x, y) {
    this.x = x;
    this.y = y;
    this.map.style.left = x + 'px';
    this.map.style.top  = y + 'px';
    if(this.map_mask != null) this.fixed_mask(this.map, this.map_mask);
  },

  moveBy: function(x, y) {
    this.move(this.x + x, this.y + y);
  },

  moveByMouse: function(e) {
    if (this.captured) {
      var oldX = this.mouseX, oldY = this.mouseY;
      this.mouseX = Event.pointerX(e);
      this.mouseY = Event.pointerY(e);
      this.moveBy(this.mouseX - oldX, this.mouseY - oldY);
    }
  },

  pointerOffsetX: function(event) {
    return event.layerX || event.offsetX;
  },

  pointerOffsetY: function(event) {
    return event.layerY || event.offsetY;
  }
};

Object.extend(MapDragging, {
   create: function(header, map, mapView, map_mask_for_ie) {
      return new MapDragging(header, map, mapView, map_mask_for_ie);
   }
});

// constants

// map (500 * 500)
/*
var VIEWPORTWIDTH = 500;
var VIEWPORTHEIGHT = 500;
var TILESIZE = 500;
var ZOOMSIZES = [ [ "500px", "500px" ], [ "1000px", "1000px" ], [ "2000px", "2000px" ], [ "4000px", "4000px" ] ];
var ZOOMSIZESVALUE = [ 500, 1000, 2000, 4000 ];
*/

// map (400 * 400)
var VIEWPORTWIDTH = 400;
var VIEWPORTHEIGHT = 400;
var TILESIZE = 400;
// var ZOOMSIZES = [ [ "400px", "400px" ], [ "800px", "800px" ], [ "1600px", "1600px" ], [ "3200px", "3200px" ] ];
var ZOOMSIZESVALUE = [ 400, 800, 1600, 3200 ];
/*
*/

// map (250 * 250)
/*
var ZOOMSIZESVALUE = [ 250, 500, 1000, 2000 ];
var ZOOMSIZES = [ [ "250px", "250px" ], [ "500px", "500px" ], [ "1000px", "1000px" ], [ "2000px", "2000px" ] ];
var TILESIZE = 250;
var VIEWPORTHEIGHT = 250;
var VIEWPORTWIDTH = 250;
*/

var TSV_COLUMN_NAME = [
						["プロット番号", "MSG004A"],
						["x軸座標",      "MSG004B"],
						["y軸座標",      "MSG004C"],
						["プロット半径", "MSG004D"],
						["線色",         "MSG004E"],
						["面色",         "MSG004F"],
						["公開番号",     "MSG004G"],
						["名称",         "MSG004H"],
						["概要",         "MSG004I"],
						["出願人",       "MSG004J"],
						["発明者",       "MSG004K"],
						["明細書URL",    "MSG004L"],
						["issue date",   "MSG004M"]
					];


var ZOOMVALUE = [ 1, 2, 4, 8 ];
var ZOOMMAXPLUS = [ 406, 562, 875, 1501 ];
var ZOOMMAXMINUS = [ -330, -910, -2070, -4390 ];


// サムネイル 横幅
var VIEWPORTTHUMBNAILWIDTH = 100;
// サムネイル 縦幅
var VIEWPORTTHUMBNAILHEIGHT = 100;

// 吹き出し 横幅
var BALLOONWIDTH = 170;
// 吹き出し 縦幅
var BALLOONHEIGHT = 100;
// 吹き出し 最大表示個数 0以下ならば制限なし
var BALLOONSHOWCNT = 0;
// 吹き出し 移動量
var BALLOONMOVEVALUE = 15;
// 吹き出し 移動時間
var BALLOONMOVETIME = 90;
// 吹き出し 拡大・縮小時　調整量
var BALLOON_ADJUSTMENT_X = [ 0, 2, 4, 4 ];
var BALLOON_ADJUSTMENT_Y = [ 0, 2, 2, 2 ];

var ZOOMMAXMINUS = [ -330, -910, -2070, -4390 ];


// 矩形選択された際のメニュー
// 1. 表示順
// 2. 表示内容
// 3. メソッド名
// 4. ID名
var RANGE_MENU_LIST = [
		[ 0, "MSG000A", close_range_menu, "MSG000A" ],
		[ 1, "MSG000B", show_char_words, "MSG000B" ],
		[ 2, "MSG000C", replace_search_term, "MSG000C" ],
		[ 3, "MSG000D", add_search_term, "MSG000D" ],
		[ 4, "MSG000E", show_range_patent, "MSG000E" ]
	];

var MOVERVALUE = 100;
var MARKERWIDTH = [ 9, 13, 19, 23 ];
var MARKERHEIGHT = [ 9, 13, 19, 23 ];

var SLIDERTOPVALUE = [ 133, 120, 107, 95 ];

var WAIT_TIME_ON_PLOT = 600; // msec

var MAX_FOLDER_CNT = 100;

var SERVICE_PREFIX = ""; // make_mapにて設定

var BASE_URL_MAP = "/map/";
var BASE_URL_SEARCH = "/search/";

// 以下のURL変数値の初期化はinit_service_prefix関数で行う
var MAP_URL;
var THUMBNAIL_URL;
var READ_LABEL_URL;
var PLOT_URL;
var PLOT_INFO_URL;
var SEARCH_PATENT_URL;
var SEARCH_PATENTS_URL;
var RANGE_PATENT_URL;
var SAVE_FOLDER_URL;
var SHOW_FOLDER_URL;
var SHOW_HISTORY_URL;
var DELETE_FOLDER_URL;
var GET_PATENT_CNT_URL;
var GET_CHAR_WORDS_URL; // 特徴語取得のURL

// URL追加パラメータ
var APPEND_URL_PARAMS = "";

// 矩形選択メニューID
var RANGEMENU_ID = "rangemenu";

// ラベル
var PLOTLABEL_ID = "plotLabel";

// リンク
var PLOTLINK_ID = "";

// 矩形選択メニュー幅
// var RANGEMENU_WIDTH = 140;

// START:globalstate
var zoom = -1;// 注意:この値(-1)を変更しないこと。

// 吹き出し用配列
var balloon = new Array();

// 選択marker用配列
// var selmarker = new Array();

// 選択
var SELECTION_LIMIT = 6; // 発明者,出願人の選択上限数
var selected_selection_type = 0; // 0:未設定 1:発明者 2:出願人
var selected_id = new Array(); // 選択中のid, 要素数=選択数
var selected_idx = new Array(); // 選択中のidx, 要素数=選択数
var selected_plotno = new Array(); // 選択中のplotno, 要素数=選択数
var selected_itemno = new Array(); // 選択中のitemno, 要素数=選択数
var selected_color = new Array(); // 選択色
var patentitem_selection_canceled = false; // 特許リストアイテムの選択キャンセル
var PATENT_MARKER_TYPE = 3;// 1:balloon, 2:marker, 3:balloon+marker

// plotno <-> itemno 変換テーブル
var plotno2itemno = new Array();
var itemno2plotno = new Array();

// 検索カウンター
var search_count = 0;

var mapDragging = null;

var tsv_url = "csv.100.csv";

var time_flg = false;
var now_mouse_x = 0;
var now_mouse_y = 0;

// used to control moving the map div
var dragging = false;
var top;
var left;
var dragStartTop;
var dragStartLeft;

var draggingSlider = false;
var topSlider;
var dragStartTopSlider;

var useRangeFlg = true;// 矩形選択機能の有効/無効
var shiftRangeId = "shiftRangeDiv";
var shiftRangeFlg = false;
var shiftStartTop;
var shiftStartLeft;
var shiftRangeWidth=0;
var shiftRangeHeight=0;


var balloonFlg = false;

var innerLeft = 0;
var innerTop = 0;

var showLabel = false;
var search_term = "";

var rangePatentList = new Array();

// END:globalstate

function make_map(url, init_x, init_y, init_s, markers, range, balloons, selmarkers, link_history, service_prefix, service_locale) {
//	today = new Date();
//	tsv_url = "?tsv_url=" + url + "&time=" + today.getTime();
	if (url.length == 0) {
		return;
	}

	if (!document.getElementById("outerDiv") || !document.getElementById("innerDiv")) {
		setTimeout("make_map('" + url + "','" + init_x + "','" + init_y + "','" + init_s + "','" + markers + "','" + range +"','"+ balloons + "','" + selmarkers + "','" + link_history + "','" + service_prefix + "')", 1000);
		return;
	} else {
		clearTimeout();
	}
	
	tsv_url = "?tsv_url=" + url;
        init_locale(service_locale);
	init(service_prefix);

	if (init_s.length < 1) {
		init_s = get_cookie('zoom');
		if (init_s != null) {
			init_s = parseInt(init_s);
		} else {
			init_s = 0;
		}
		init_x =  0 - (ZOOMSIZESVALUE[init_s]/2) + (TILESIZE/2);	
		init_y =  init_x;
	} else {
		init_x = parseInt(init_x);
		init_y = parseInt(init_y);
		init_s = parseInt(init_s);
	}

	if (init_s == 1){
		toggleZoom("up");
	}else if (init_s == 2){
		toggleZoom("up");
		toggleZoom("up");
	}else if (init_s == 3){
		toggleZoom("up");
		toggleZoom("up");
		toggleZoom("up");
	} else {
	}

	setInnerDivPosition(init_x, init_y);

	checkTiles();

	if (markers.length > 0 && selected_selection_type >= 0) {
		var items = markers.split(":");
		for (var i = 0; i < items.length; i++) {
			selected_plotno.push(items[i]);
			selected_color.push(i+1);
			var marker = items[i].split("|");
			for (var j = 0; j < marker.length; j++) {
				show_marker_on_plot(marker[j], selected_color[i]);
			}
		}
	}

  range_init(range);
  link_history_init(link_history);
  add_balloon_by_plotno(balloons, false,
    ((PATENT_MARKER_TYPE & 1) == 1), (PATENT_MARKER_TYPE == 3));
  add_selmarker_by_plotno(selmarkers, false,
    ((PATENT_MARKER_TYPE & 2) == 2));
}

function load_thumbnail(url, init_x, init_y, init_s, thumbnail_index, map_id) {
  var innerDiv = document.getElementById("innerDiv" + thumbnail_index); 
  var tileName = "imgthumbnail" + thumbnail_index;

  img = document.createElement("img");
  img.src = THUMBNAIL_URL + "?tsv_url=" + (url) + "&map_id=" + (map_id) ;
  img.style.position = "absolute";

  img.style.left = "0px";
  img.style.top = "0px";

  img.style.width = VIEWPORTTHUMBNAILWIDTH + "px";
  img.style.height = VIEWPORTTHUMBNAILHEIGHT + "px";

  img.style.zIndex = 0;
  img.setAttribute("id", tileName);
  innerDiv.appendChild(img);
}

function make_thumbnail(url, init_x, init_y, init_s, index, service_prefix, use_thumbnail, map_id) {
	if (url.length == 0) {
		return;
	}
	zoom = 0;
	innerLeft = 0;
	innerTop = 0;
	shiftRangeFlg = false;
	tsv_url = "?tsv_url=" + url;
        map_id = parseInt(map_id);

        init_service_prefix(service_prefix);

        if(parseInt(use_thumbnail) == 1) {
          load_thumbnail(url, init_x, init_y, init_s, index, map_id);
          return;
        }

	checkThumbnail(index);

	init_x = parseInt(init_x);
	init_y = parseInt(init_y);
	init_s = parseInt(init_s);

	if (init_s == 1) {
		toggleThumbnailZoom("up", index);
	} else if (init_s == 2) {
		toggleThumbnailZoom("up", index);
		toggleThumbnailZoom("up", index);
	} else if (init_s == 3) {
		toggleThumbnailZoom("up", index);
		toggleThumbnailZoom("up", index);
		toggleThumbnailZoom("up", index);
	} else {
	}
//alert(Math.floor(init_x) + "::::" + Math.floor(init_y));
	setInnerDivThumbnailPosition(Math.floor(init_x) , Math.floor(init_y), index);
	checkThumbnail(index);

//	setInnerDivThumbnailPosition(Math.floor(init_x / 5) , Math.floor(init_y / 5), index);
	setInnerDivThumbnailPosition(Math.floor(init_x / (VIEWPORTWIDTH / VIEWPORTTHUMBNAILWIDTH)) , Math.floor(init_y / (VIEWPORTHEIGHT / VIEWPORTTHUMBNAILHEIGHT)), index);
}

function init_service_prefix(service_prefix) {
    SERVICE_PREFIX = service_prefix;
    MAP_URL = SERVICE_PREFIX + BASE_URL_MAP + "make_png";
    THUMBNAIL_URL = SERVICE_PREFIX + BASE_URL_MAP + "get_thumbnail";
    READ_LABEL_URL = SERVICE_PREFIX + BASE_URL_MAP + "read_label";
    PLOT_URL = SERVICE_PREFIX + BASE_URL_MAP + "get_mouseover_plot";
    PLOT_INFO_URL = SERVICE_PREFIX + BASE_URL_MAP + "get_plot_info";
    SEARCH_PATENT_URL = SERVICE_PREFIX + BASE_URL_MAP + "search_patent";
    SEARCH_PATENTS_URL = SERVICE_PREFIX + BASE_URL_MAP + "search_patents";
    RANGE_PATENT_URL = SERVICE_PREFIX + BASE_URL_MAP + "range_patent";
    SAVE_FOLDER_URL = SERVICE_PREFIX + BASE_URL_MAP + "save_folder";
    SHOW_FOLDER_URL = SERVICE_PREFIX + BASE_URL_MAP + "show_folder";
    SHOW_HISTORY_URL = SERVICE_PREFIX + BASE_URL_SEARCH + "show_history";
    DELETE_FOLDER_URL = SERVICE_PREFIX + BASE_URL_MAP + "delete_folder";
    GET_PATENT_CNT_URL = SERVICE_PREFIX + BASE_URL_MAP + "get_patent_cnt";
    GET_CHAR_WORDS_URL = SERVICE_PREFIX + BASE_URL_SEARCH + "get_char_words";
}

function init(service_prefix) {
	// 変数の初期化
	zoom = 0;
	innerLeft = 0;
	innerTop = 0;
	search_term = "";
	thumbnail_index = "";
	delete_range_menu();
	shiftRangeFlg = false;
	balloon = new Array();
	selmarker = new Array();

        init_service_prefix(service_prefix);
    // make inner div big enough to display the map
    // START:innerDivSize
    setInnerDivSize(ZOOMSIZESVALUE[zoom], ZOOMSIZESVALUE[zoom]);
    // END:innerDivSize


	document.body.onmousemove = check_mouse_on_outerdiv;

    // wire up the mouse listeners to do dragging
    var outerDiv = document.getElementById("outerDiv");
    outerDiv.onmousedown = startMove;
    outerDiv.onmousemove = processMove;
    outerDiv.onmouseup = stopMove;
    outerDiv.ondblclick = show_balloon_click_on_plot;

    // necessary to enable dragging on IE
    outerDiv.ondragstart = function() { return false; }



	if (outerDiv.attachEvent) {
		outerDiv.attachEvent("onmousewheel", wheel);
	}

	if (outerDiv.addEventListener) {
		outerDiv.addEventListener("DOMMouseScroll", wheel, false);
	}


    // wire up the mouse listeners to do dragging
    var slider = document.getElementById("slider");
    if (slider != null) {
      slider.onmousedown = startMoveSlider;
      slider.onmousemove = processMoveSlider;
      slider.onmouseup = stopMoveSlider;
    }

    // necessary to enable dragging on IE
    if (slider != null) {
      slider.ondragstart = function() { return false; }
      slider.onmouseout = stopMoveSlider;
    }

    // wire up the mouse listeners to do dragging
	if (document.getElementById("show_label_button")) {
	    var show_label_button = document.getElementById("show_label_button");
	    show_label_button.onclick = show_label;
	}

  // マップ移動クラスのインスタンス作成
  if(document.getElementById("mapHeader")) {
    mapDragging = MapDragging.create("mapHeader", "map", "mapView", "map_mask_for_ie" );
  }

    checkTiles();
}

//-----------------------------------------------
//Slider
function startMoveSlider(event) {
    dragging = false;
    // necessary for IE
    if (!event) event = window.event;
    dragStartTopSlider = event.clientY;
    var slider = document.getElementById("slider");
    topSlider = stripPx(slider.style.top);
    draggingSlider = true;
    return false;
}

//-----------------------------------------------
//Slider
function processMoveSlider(event) {
    dragging = false;
    if (!event) event = window.event;  // for IE
    if (draggingSlider) {
	    var slider = document.getElementById("slider");
		var bar_min_y = SLIDERTOPVALUE[SLIDERTOPVALUE.length - 1] - Math.floor(stripPx(slider.style.height) / 2);
		var bar_max_y = SLIDERTOPVALUE[0] - Math.floor(stripPx(slider.style.height) / 2);

        slider.style.top = (topSlider + (event.clientY - dragStartTopSlider)) + "px";
/*
		if(stripPx(slider.style.top) < bar_min_y){
			 slider.style.top = bar_min_y + "px";
		}
		if(stripPx(slider.style.top) > bar_max_y){
			 slider.style.top = bar_max_y + "px";
		}
*/
    }
}

//-----------------------------------------------
//Slider
function stopMoveSlider() {
    dragging = false;
	if (draggingSlider) {
	    var slider = document.getElementById("slider");
//				alert("zoom;" + zoom + " \ntopSlider:" + topSlider + " \nslider.offsetTop" + slider.offsetTop);
		moveSlider(topSlider - slider.offsetTop);
	    draggingSlider = false;
	}
}

function startMove(event) {
    // necessary for IE
    if (!event) event = window.event;

    shiftStartLeft = (mapDragging != null && mapDragging.x != null) ? get_now_mouseX(event) : event.clientX;
    shiftStartTop  = (mapDragging != null && mapDragging.x != null) ? get_now_mouseY(event) : event.clientY;

    dragStartLeft = event.clientX;
    dragStartTop = event.clientY;

    var innerDiv = document.getElementById("innerDiv");
    //innerDiv.style.cursor = "-moz-grab";

    top = stripPx(innerDiv.style.top);
    left = stripPx(innerDiv.style.left);

	if (balloonFlg) {
		return true;
	}

	if (useRangeFlg && event.shiftKey) {
		shiftRangeFlg = true;
		search_term = "";
		return false;
	}
    dragging = true;
    return false;
}

function get_now_mouseX(e) {
  now_x = 0;
  if (document.getElementById("outerDiv")) {
    var outerDiv = document.getElementById("outerDiv");
    var scrollLeft = (mapDragging == null || mapDragging.x == null) ? getScrollLeft() : 0;
    var offsetLeft = (mapDragging == null || mapDragging.x == null) ? outerDiv.offsetLeft : mapDragging.x;
    var now_x = e.clientX - offsetLeft + scrollLeft;
    if (typeof ScriptEngine == "function") {
      now_x = now_x - 2;
    }
  }
  return now_x;
}

function get_now_mouseY(e) {
  now_y = 0;
  if (document.getElementById("outerDiv")) {
    var outerDiv = document.getElementById("outerDiv");
    var scrollTop = (mapDragging == null || mapDragging.y == null) ? getScrollTop() : 0;
    var offsetTop = (mapDragging == null || mapDragging.y == null) ? outerDiv.offsetTop : mapDragging.y + mapDragging.header_height;
    var now_y = e.clientY - offsetTop + scrollTop;
    if (typeof ScriptEngine == "function") {
      now_y = now_y - 2;
    }
  }
  return now_y;
}


function processMove(event) {
    if (!event) event = window.event;  // for IE

	now_mouse_x = get_now_mouseX(event);
	now_mouse_y = get_now_mouseY(event);

	if (!time_flg) {
		time_flg = true;
		setTimeout("show_label_mouseover_on_plot('" + now_mouse_x + "','" + now_mouse_y + "')", WAIT_TIME_ON_PLOT);
	}

	if (shiftRangeFlg) {
	    var outerDiv = document.getElementById("outerDiv");
        var innerDiv = document.getElementById("innerDiv");
		// if (document.getElementById(shiftRangeId)) {
            // innerDiv.removeChild(document.getElementById(shiftRangeId));
		// }
		// var move_x = event.clientX - shiftStartLeft;
		var tmp_mouse_x = (mapDragging != null && mapDragging.x != null) ? now_mouse_x : event.clientX;
		var tmp_mouse_y = (mapDragging != null && mapDragging.x != null) ? now_mouse_y : event.clientY;
		var move_x = tmp_mouse_x - shiftStartLeft;
		// var move_y = event.clientY - shiftStartTop;
		var move_y = tmp_mouse_y - shiftStartTop;

        var div = document.getElementById(shiftRangeId);
        var scrollLeft = (mapDragging != null && mapDragging.x != null) ? 0 : getScrollLeft();
        var scrollTop = (mapDragging != null && mapDragging.x != null) ? 0 : getScrollTop();

        // div.style.position = "absolute";

		if (move_x > 0) {
             div.style.left = scrollLeft + shiftStartLeft - left - outerDiv.offsetLeft + "px";
  		} else {
             div.style.left = scrollLeft + tmp_mouse_x - left - outerDiv.offsetLeft + "px";
		}
		shiftRangeWidth = Math.ceil(Math.abs(move_x) / ZOOMVALUE[zoom]);
        div.style.width = Math.abs(move_x)  + "px";

		if (move_y > 0) {
            div.style.top = scrollTop + shiftStartTop - top - outerDiv.offsetTop + "px";
		} else {
            div.style.top = scrollTop + tmp_mouse_y - top - outerDiv.offsetTop + "px";
		}

		shiftRangeHeight = Math.ceil(Math.abs(move_y) / ZOOMVALUE[zoom]);
        div.style.height = Math.abs(move_y)  + "px";

		// set_filter(div);

	    // var ua = navigator.userAgent;

	    // if(ua.indexOf("MSIE") >= 0) {
	        // div.style.backgroundColor = "#000000";
	        // div.style.borderStyle = "solid";
		// }
        // div.style.zIndex = 3;
        // div.setAttribute("id", shiftRangeId);
        // innerDiv.appendChild(div);
				div.style.display = "block";
		return;
	}

    var innerDiv = document.getElementById("innerDiv");
    if (dragging) {
		balloon_over_hidden();

		if ((top + (event.clientY - dragStartTop)) > ZOOMMAXPLUS[zoom]) {
	        innerDiv.style.top = ZOOMMAXPLUS[zoom] + "px";
		} else if ((top + (event.clientY - dragStartTop)) < ZOOMMAXMINUS[zoom]) {
	        innerDiv.style.top = ZOOMMAXMINUS[zoom] + "px";
		} else {
	        innerDiv.style.top = (top + (event.clientY - dragStartTop)) + "px";
		}
		if ((left + (event.clientX - dragStartLeft)) > ZOOMMAXPLUS[zoom]) {
	        innerDiv.style.left = ZOOMMAXPLUS[zoom] + "px";
		} else if ((left + (event.clientX - dragStartLeft)) < ZOOMMAXMINUS[zoom]) {
	        innerDiv.style.left = ZOOMMAXMINUS[zoom] + "px";
		} else {
	        innerDiv.style.left = (left + (event.clientX - dragStartLeft)) + "px";
		}
    }

    checkTiles();
	return true;
}

function set_filter() {

}

function check_mouse_on_outerdiv(event) {
    if (!event) event = window.event;  // for IE

	var mouse_x = get_now_mouseX(event);
	var mouse_y = get_now_mouseY(event);

	if (!is_mouse_on_outerdiv(mouse_x, mouse_y)) {
		stopMove(event);
	}
}

function is_mouse_on_outerdiv(x, y) {
	if (document.getElementById("outerDiv")) {
	    var outerDiv = document.getElementById("outerDiv");
		
		var outerMaxTop = outerDiv.offsetHeight;
		var outerMaxLeft = outerDiv.offsetWidth;

		if (0 < x && x < outerMaxLeft && 0 < y && y < outerMaxTop) {
			return true;
		}
	}
	return false;
}

function stopMove(event) {
    if (document.getElementById("outerDiv")) {
        var innerDiv = document.getElementById("innerDiv");
        innerDiv.style.cursor = "";
        dragging = false;

        if (shiftRangeFlg) {
            if (document.getElementById(shiftRangeId)) {
                // var range = document.getElementById(shiftRangeId);
                // range.onclick = click_range;
                get_range_patent();
            }
            shiftRangeFlg = false;
            click_range(event);
        }
		balloon_over_auto();
    }
}

function rangeMenuSelect (event) {
    if (!event) event = window.event;  // for IE
	var select_menu = Event.element(event);
	select_menu.className = "rangeMenuSelect";
}

function rangeMenuUnSelect (event) {
    if (!event) event = window.event;  // for IE
	var select_menu = Event.element(event);
	select_menu.className = "rangeMenuInner";
}

function addRangeMenuDiv() {
  if (!useRangeFlg) return;
  var rangeMenuDiv = document.createElement("div");
  // 選択領域の生成
  for (var i = 0; i < RANGE_MENU_LIST.length; i++) {
    var rangeMenuInnerDiv = document.createElement("div");

    rangeMenuInnerDiv.className = "rangeMenuInner";
    set_text(rangeMenuInnerDiv, RANGE_MENU_LIST[i][1]);
    rangeMenuInnerDiv.onclick=RANGE_MENU_LIST[i][2];

    rangeMenuInnerDiv.onmouseover=rangeMenuSelect
    rangeMenuInnerDiv.onmouseout=rangeMenuUnSelect

    rangeMenuDiv.appendChild(rangeMenuInnerDiv);
    rangeMenuInnerDiv = null;
  }

  rangeMenuDiv.style.top = "0px";
  rangeMenuDiv.style.left = "0px";
  rangeMenuDiv.style.width = RANGEMENU_WIDTH + "px";
  rangeMenuDiv.style.zIndex = 5;
  rangeMenuDiv.style.display = "none";
  rangeMenuDiv.setAttribute("id", RANGEMENU_ID);
			
  var outerDiv = document.getElementById("outerDiv");
  outerDiv.appendChild(rangeMenuDiv);
  // document.body.appendChild(rangeMenuDiv);
}

function click_range(event) {
  if (!useRangeFlg) return;
  if (!event) event = window.event;
  if (!shiftRangeFlg) {
    if (document.getElementById(shiftRangeId)) {
      var rangeMenuDiv = document.getElementById(RANGEMENU_ID);
      var rangeMenuXorg = get_now_mouseX(event);
      var rangeMenuY = get_now_mouseY(event);
      var outerDiv = document.getElementById("outerDiv");
      var outerHeight = outerDiv.offsetHeight;
      var outerWidth = outerDiv.offsetWidth;
      var rangeMenuX = rangeMenuXorg;

      Element.show(RANGEMENU_ID);
      if ((rangeMenuX + rangeMenuDiv.offsetWidth) > outerWidth) {
        rangeMenuX -= rangeMenuDiv.offsetWidth;
        rangeMenuX -= 2;
        if (rangeMenuX < 1) {
          rangeMenuX = 2;
        }
      } else {
        rangeMenuX += 2;
      }
      if ((rangeMenuY + rangeMenuDiv.offsetHeight) > outerHeight) {
        rangeMenuY -= rangeMenuDiv.offsetHeight;
        rangeMenuY -= 2;
      } else {
        rangeMenuY += 2;
      }

      rangeMenuDiv.style.top = rangeMenuY + "px";
      rangeMenuDiv.style.left = rangeMenuX + "px";

      // Element.hide(RANGEMENU_ID);
      // Effect.Appear(RANGEMENU_ID);
    }
  }
}

// 矩形選択後のメニューを閉じる (HTMLタグは削除)
function close_range_menu() {
  if (document.getElementById(RANGEMENU_ID)) {
    Element.hide(RANGEMENU_ID);
    // Effect.Appear(RANGEMENU_ID);
    // Effect.SwitchOff(RANGEMENU_ID);
  }
}

// 矩形選択後のメニューを削除 (HTMLタグ)
function delete_range_menu() {
	if (document.getElementById(RANGEMENU_ID)) {
		rangeMenuDiv = document.getElementById(RANGEMENU_ID);
        document.body.removeChild(rangeMenuDiv);
	}
}

// 選択範囲を検索条件に設定する
function set_search_term_by_range(append_flg) {
  if(rangePatentList.length < 1) {
    close_range_menu();
    return;
  }

  var search_term_org = get_search_term();
  var tmp_terms = append_flg ? search_term_org.split(/\s+/) : new Array();
  var search_terms = new Array();
  var search_term = "";

  for (var i = 0; i < tmp_terms.length; i++) {
    if (tmp_terms[i].length > 0) search_terms.push(tmp_terms[i]);
  }

  for (var i = 0; i < rangePatentList.length; i++) {
    var rangePatentInfo = rangePatentList[i];
    if (search_terms.indexOf(rangePatentInfo[6]) < 0)
      search_terms.push(rangePatentInfo[6]);
  }

  set_search_term(search_terms.join(" "));

  Effect.Appear('patentform');
  Effect.SwitchOff('textform');
  close_range_menu();
}

// 検索条件に新規設定
function replace_search_term() {
  set_search_term_by_range(false);
}

// 検索条件に追記
function add_search_term() {
  set_search_term_by_range(true);
}

// 特徴語の表示
function show_char_words() {
    if(rangePatentList.length < 1) {
      close_range_menu();
      return;
    }

    var xmlhttp = createXmlHttp();

    var patent_no_list = new Array();
    var plotLabel = document.getElementById(PLOTLABEL_ID);
    plotLabel.value = MSG002A;

    // 特許番号の取得
    for (i = 0; i < rangePatentList.length; i++) {
        var rangePatentInfo = rangePatentList[i];
        patent_no_list.push(rangePatentInfo[6]);
    }

    if (xmlhttp) {
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                var plotLabel = document.getElementById(PLOTLABEL_ID);
                plotLabel.value = "";
                if (xmlhttp.status == 200) {
                    // 特徴語の取得
                    var char_words = xmlhttp.responseText;

                    if (char_words.length > 0) {
                        plotLabel.value = char_words;
                    }

                }
                time_flg = false;
                clearTimeout();
            }
        }
        //HTTPリクエスト
        xmlhttp.open("POST", GET_CHAR_WORDS_URL, true);
        xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
        xmlhttp.send("scnt=" + search_count + "&patent_no=" + patent_no_list.join("|") + APPEND_URL_PARAMS);
    }
    close_range_menu();
}

//ダブルクリックで当該特許の明細書を表示

// 範囲内特許の表示
function show_range_patent() {
	if(rangePatentList.length < 1) {
		close_range_menu();
		return;
	}
	var xmlhttp = createXmlHttp();

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

				var patent_cnt = xmlhttp.responseText;

				var range_plot_no = new Array();

				for (i = 0; i < rangePatentList.length; i++) {
					var rangePatentInfo = rangePatentList[i];

					var colon_index = rangePatentInfo[7].indexOf(":");
					var list_index = rangePatentInfo[7].substr(0, colon_index);

					range_plot_no.push(list_index - 1);
				}
				show_item_only(range_plot_no.join("|"), patent_cnt);
			}
		}
		//HTTPリクエスト
		xmlhttp.open("GET", GET_PATENT_CNT_URL + tsv_url + APPEND_URL_PARAMS);
		xmlhttp.send(null);
	}
	close_range_menu();
        new Effect.Appear('showallbutton');
        new Effect.SwitchOff('makemap');
}

function get_range_patent() {
	var shiftRangeDiv = document.getElementById(shiftRangeId);

	s_x = shiftRangeDiv.offsetLeft;
	s_y = shiftRangeDiv.offsetTop;
	e_x = shiftRangeDiv.offsetLeft + stripPx(shiftRangeDiv.style.width);
	e_y = shiftRangeDiv.offsetTop + stripPx(shiftRangeDiv.style.height);

	rangePatentList = new Array();

	var xmlhttp = createXmlHttp();

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {

			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

				var patent_list = xmlhttp.responseText;
				var patent_rows = patent_list.split("\n");

				for (i = 0; i < patent_rows.length; i++) {
					var patent_row = patent_rows[i];

					if (patent_row.length > 0) {
						var patent_cols = patent_row.split("\t");

//						var plot_no	 = patent_cols[0];
//						var label	 = patent_cols[6];

						rangePatentList[i] = patent_cols;
//						rangePatentList[i]['label'] = label;

//						search_term = search_term + " " + label;
						
					}
				}
//				set_search_term(search_term);
			}
//                        Effect.Appear('patentform');
//                        Effect.SwitchOff('textform');
		}
		//HTTPリクエスト
		xmlhttp.open("GET", RANGE_PATENT_URL + tsv_url + "&s_x=" + s_x + "&s_y=" + s_y + "&e_x=" + e_x + "&e_y=" + e_y + "&s=" + ZOOMVALUE[zoom] + APPEND_URL_PARAMS);
		xmlhttp.send(null);
	}
}

function set_search_term(term) {
	var search_term = document.getElementById("formdatum_publication_number");
	search_term.value = term;
}

function get_search_term() {
	var term = "";

	if (document.getElementById("formdatum_publication_number")) {
		term = document.getElementById("formdatum_publication_number").value;
	}

	return term;
}


// START:checktiles
function checkTiles() {
    // check which tiles should be visible in the inner div
    var visibleTiles = getVisibleTiles();
    // add each tile to the inner div, checking first to see
    // if it has already been added
    var innerDiv = document.getElementById("innerDiv");
    var visibleTilesMap = {};

	setzoom = ZOOMVALUE[zoom];
    for (i = 0; i < visibleTiles.length; i++) {
        var tileArray = visibleTiles[i];
        // START:imgZoomLevel
        var tileName = "x" + tileArray[0] + "y" + tileArray[1] + "z" + (setzoom);
        // END:imgZoomLevel
        visibleTilesMap[tileName] = true;
        var img = document.getElementById(tileName);
        if (!img) {
            img = document.createElement("img");
//            img.src = MAP_URL + "?x=" + (tileArray[0] * TILESIZE) + "&y=" + (tileArray[1] * TILESIZE) + "&s=" + (setzoom) + "&time=" + today.getTime() + "&tsv_url=" + tsv_url;

            img.src = MAP_URL + tsv_url + "&x=" + (tileArray[0] * TILESIZE) + "&y=" + (tileArray[1] * TILESIZE) + "&s=" + (setzoom) + APPEND_URL_PARAMS;
            img.style.position = "absolute";
            img.style.left = (tileArray[0] * TILESIZE) + "px";
            img.style.top = (tileArray[1] * TILESIZE) + "px";

            img.style.zIndex = 0;
            img.setAttribute("id", tileName);
            innerDiv.appendChild(img);
        }
    }

    var imgs = innerDiv.getElementsByTagName("img");
    for (i = 0; i < imgs.length; i++) {
        var id = imgs[i].getAttribute("id");
        if (!visibleTilesMap[id]) {
            innerDiv.removeChild(imgs[i]);
            i--;  // compensate for live nodelist
        }
    }
}

// START:checkThumbnail
function checkThumbnail(thumbnail_index) {
    // check which tiles should be visible in the inner div
    var visibleTiles = getVisibleThumbnail(thumbnail_index);
    // add each tile to the inner div, checking first to see
    // if it has already been added
    var innerDiv = document.getElementById("innerDiv" + thumbnail_index);
    var visibleTilesMap = {};
	setzoom = ZOOMVALUE[zoom];
    for (i = 0; i < visibleTiles.length; i++) {
        var tileArray = visibleTiles[i];
        // START:imgZoomLevel
        var tileName = "x" + tileArray[0] + "y" + tileArray[1] + "z" + (setzoom) + "index" + thumbnail_index;
        // END:imgZoomLevel
        visibleTilesMap[tileName] = true;
        var img = document.getElementById(tileName);
        if (!img) {
            img = document.createElement("img");
//            img.src = MAP_URL + "?x=" + (tileArray[0] * TILESIZE) + "&y=" + (tileArray[1] * TILESIZE) + "&s=" + (setzoom) + "&time=" + today.getTime() + "&tsv_url=" + tsv_url;
            img.src = MAP_URL + tsv_url + "&x=" + (tileArray[0] * TILESIZE) + "&y=" + (tileArray[1] * TILESIZE) + "&s=" + (setzoom) + APPEND_URL_PARAMS;
            img.style.position = "absolute";

            img.style.left = (tileArray[0] * VIEWPORTTHUMBNAILWIDTH) + "px";
            img.style.top = (tileArray[1] * VIEWPORTTHUMBNAILHEIGHT) + "px";

            img.style.width = VIEWPORTTHUMBNAILWIDTH + "px";
            img.style.height = VIEWPORTTHUMBNAILHEIGHT + "px";

            img.style.zIndex = 0;
            img.setAttribute("id", tileName);
            innerDiv.appendChild(img);
        }
    }

    var imgs = innerDiv.getElementsByTagName("img");
    for (i = 0; i < imgs.length; i++) {
        var id = imgs[i].getAttribute("id");
        if (!visibleTilesMap[id]) {
            innerDiv.removeChild(imgs[i]);
            i--;  // compensate for live nodelist
        }
    }
}


// END:checktiles

function getVisibleTiles() {
    var innerDiv = document.getElementById("innerDiv");
    var mapX = stripPx(innerDiv.style.left);
    var mapY = stripPx(innerDiv.style.top);

    var startX = Math.abs(Math.floor(mapX / TILESIZE)) - 1;
    var startY = Math.abs(Math.floor(mapY / TILESIZE)) - 1;

    var tilesX = Math.ceil(VIEWPORTWIDTH / TILESIZE) + 1;
    var tilesY = Math.ceil(VIEWPORTHEIGHT / TILESIZE) + 1;

    var visibleTileArray = [];
    var counter = 0;
    for (x = startX; x < (tilesX + startX); x++) {
        for (y = startY; y < (tilesY + startY); y++) {
            visibleTileArray[counter++] = [x, y];
        }
    }
    return visibleTileArray;
}

// END:checktiles

function getVisibleThumbnail(thumbnail_index) {
    var innerDiv = document.getElementById("innerDiv" + thumbnail_index);
    var mapX = stripPx(innerDiv.style.left);
    var mapY = stripPx(innerDiv.style.top);

    var startX = Math.abs(Math.floor(mapX / TILESIZE)) - 1;
    var startY = Math.abs(Math.floor(mapY / TILESIZE)) - 1;

    var tilesX = Math.ceil(VIEWPORTWIDTH / TILESIZE) + 1;
    var tilesY = Math.ceil(VIEWPORTWIDTH / TILESIZE) + 1;

    var visibleTileArray = [];
    var counter = 0;
    for (x = startX; x < (tilesX + startX); x++) {
        for (y = startY; y < (tilesY + startY); y++) {
            visibleTileArray[counter++] = [x, y];
        }
    }
    return visibleTileArray;
}

function getScrollLeft() {
  Position.prepare();
  return Position.deltaX;
}

function getScrollTop() {
  Position.prepare();
  return Position.deltaY;
}

function stripPx(value) {
    if (value == "") return 0;
    return parseFloat(value.substring(0, value.length - 2));
}

function setInnerDivSize(width, height) {
	if (document.getElementById("innerDiv")) {
	    var innerDiv = document.getElementById("innerDiv");
	    innerDiv.style.width = width + "px";
	    innerDiv.style.height = height + "px";
	}
}

function setInnerDivPosition(left, top) {
	if (document.getElementById("innerDiv")) {
	    var innerDiv = document.getElementById("innerDiv");
	    innerDiv.style.left = left + "px";
	    innerDiv.style.top = top + "px";
	}
}


function setInnerDivThumbnailPosition(left, top, thumbnail_index) {
	if (document.getElementById("innerDiv"+ thumbnail_index)) {
	    var innerDiv = document.getElementById("innerDiv" + thumbnail_index);
	    innerDiv.style.left = Math.floor(left) + "px";
	    innerDiv.style.top = Math.floor(top) + "px";
	}
}


// START:toggleZoom
function toggleZoom(type,saveflag) {
    var innerDiv = document.getElementById("innerDiv");
    var slider = document.getElementById("slider");
    if(typeof saveflag == "undefined") {
      saveflag = false;
    }
	var zoomOrg = zoom;
	var move_x_value_org = 0;
	var move_y_value_org = 0;

	balloon_over_hidden();

	if(type == "up"){

		if (zoom == "0") {
			move_x_value_org = stripPx(innerDiv.style.left);
			move_y_value_org = stripPx(innerDiv.style.top);

			innerLeft += (ZOOMSIZESVALUE[zoomOrg] / 2);
			innerTop += (ZOOMSIZESVALUE[zoomOrg] / 2);
			zoom = 1;
			innerDiv.style.left = (stripPx(innerDiv.style.left) + move_x_value_org  - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) + move_y_value_org  - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
		    var scale = document.getElementById("scale" + ZOOMVALUE[zoom]);
			if (slider != null) {
				slider.style.top    = SLIDERTOPVALUE[zoom] + Math.floor(stripPx(scale.style.height) / 2) - Math.floor(stripPx(slider.style.height) / 2) + "px";
			}
		} else if (zoom == "1") {
			move_x_value_org = stripPx(innerDiv.style.left) + innerLeft;
			move_y_value_org = stripPx(innerDiv.style.top) + innerTop;

			innerLeft += (ZOOMSIZESVALUE[zoomOrg] / 2);
			innerTop += (ZOOMSIZESVALUE[zoomOrg] / 2);

			zoom = 2;
			innerDiv.style.left = (stripPx(innerDiv.style.left) + move_x_value_org - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) + move_y_value_org - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
		    var scale = document.getElementById("scale" + ZOOMVALUE[zoom]);
			if (slider != null) {
				slider.style.top    = SLIDERTOPVALUE[zoom] + Math.floor(stripPx(scale.style.height) / 2) - Math.floor(stripPx(slider.style.height) / 2) + "px";
			}
		} else if (zoom == "2") {
			move_x_value_org = stripPx(innerDiv.style.left) + innerLeft;
			move_y_value_org = stripPx(innerDiv.style.top) + innerTop;

			innerLeft += (ZOOMSIZESVALUE[zoomOrg] / 2);
			innerTop += (ZOOMSIZESVALUE[zoomOrg] / 2);
			zoom = 3;
			innerDiv.style.left = (stripPx(innerDiv.style.left) + move_x_value_org - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) + move_y_value_org - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
		    var scale = document.getElementById("scale" + ZOOMVALUE[zoom]);
			if (slider != null) {
				slider.style.top    = SLIDERTOPVALUE[zoom] + Math.floor(stripPx(scale.style.height) / 2) - Math.floor(stripPx(slider.style.height) / 2) + "px";
			}
		} else if (zoom == "3") {    
			zoom = 3;
		    var scale = document.getElementById("scale" + ZOOMVALUE[zoom]);
			if (slider != null) {
				slider.style.top    = SLIDERTOPVALUE[zoom] + Math.floor(stripPx(scale.style.height) / 2) - Math.floor(stripPx(slider.style.height) / 2) + "px";
			}
		}
	}else{
		if (zoom == "0") {
			zoom = 0;
		    var scale = document.getElementById("scale" + ZOOMVALUE[zoom]);
			if (slider != null) {
				slider.style.top    = SLIDERTOPVALUE[zoom] + Math.floor(stripPx(scale.style.height) / 2) - Math.floor(stripPx(slider.style.height) / 2) + "px";
			}
		} else if (zoom == "1") {
			move_x_value_org = (stripPx(innerDiv.style.left) + innerLeft) / 2;
			move_y_value_org = (stripPx(innerDiv.style.top) + innerTop) / 2;

			zoom = 0;
			innerLeft -= (ZOOMSIZESVALUE[zoom] / 2);
			innerTop -= (ZOOMSIZESVALUE[zoom] / 2);
			innerDiv.style.left = (stripPx(innerDiv.style.left) - move_x_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) - move_y_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
		    var scale = document.getElementById("scale" + ZOOMVALUE[zoom]);
			if (slider != null) {
				slider.style.top    = SLIDERTOPVALUE[zoom] + Math.floor(stripPx(scale.style.height) / 2) - Math.floor(stripPx(slider.style.height) / 2) + "px";
			}
		} else if (zoom == "2") {
			move_x_value_org = (stripPx(innerDiv.style.left) + innerLeft) / 2;
			move_y_value_org = (stripPx(innerDiv.style.top) + innerTop) / 2;
			zoom = 1;
			innerLeft -= (ZOOMSIZESVALUE[zoom] / 2);
			innerTop -= (ZOOMSIZESVALUE[zoom] / 2);
			innerDiv.style.left = (stripPx(innerDiv.style.left) - move_x_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) - move_y_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
		    var scale = document.getElementById("scale" + ZOOMVALUE[zoom]);
			if (slider != null) {
				slider.style.top    = SLIDERTOPVALUE[zoom] + Math.floor(stripPx(scale.style.height) / 2) - Math.floor(stripPx(slider.style.height) / 2) + "px";
			}
		} else if (zoom == "3") {
			move_x_value_org = (stripPx(innerDiv.style.left) + innerLeft) / 2;
			move_y_value_org = (stripPx(innerDiv.style.top) + innerTop) / 2;
			zoom = 2;
			innerLeft -= (ZOOMSIZESVALUE[zoom] / 2);
			innerTop -= (ZOOMSIZESVALUE[zoom] / 2);
			innerDiv.style.left = (stripPx(innerDiv.style.left) - move_x_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) - move_y_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
		    var scale = document.getElementById("scale" + ZOOMVALUE[zoom]);
			if (slider != null) {
				slider.style.top    = SLIDERTOPVALUE[zoom] + Math.floor(stripPx(scale.style.height) / 2) - Math.floor(stripPx(slider.style.height) / 2) + "px";
			}
		}
	}

    var imgs = innerDiv.getElementsByTagName("img");
    while (imgs.length > 0) innerDiv.removeChild(imgs[0]);

    setInnerDivSize(ZOOMSIZESVALUE[zoom], ZOOMSIZESVALUE[zoom]);

    checkTiles();

	label_zoom();
	marker_zoom(zoomOrg, zoom);
	range_zoom(zoomOrg, zoom);
	balloon_zoom(zoomOrg, zoom);
	selmarker_zoom(zoomOrg, zoom);
    if(saveflag) {
        set_cookie('zoom', zoom);
    }

	setTimeout("balloon_over_auto()", 300);
	clearTimeout();
}
// END:toggleZoom

function toggleThumbnailZoom(type, thumbnail_index) {
    var innerDiv = document.getElementById("innerDiv" + thumbnail_index);
	var zoomOrg = zoom;
	var move_x_value_org = 0;
	var move_y_value_org = 0;

	if(type == "up"){
		if (zoom == "0") {
			move_x_value_org = stripPx(innerDiv.style.left);
			move_y_value_org = stripPx(innerDiv.style.top);

			innerLeft += (ZOOMSIZESVALUE[zoomOrg] / 2);
			innerTop += (ZOOMSIZESVALUE[zoomOrg] / 2);
			zoom = 1;
			innerDiv.style.left = (stripPx(innerDiv.style.left) + move_x_value_org  - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) + move_y_value_org  - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
		} else if (zoom == "1") {
			move_x_value_org = stripPx(innerDiv.style.left) + innerLeft;
			move_y_value_org = stripPx(innerDiv.style.top) + innerTop;

			innerLeft += (ZOOMSIZESVALUE[zoomOrg] / 2);
			innerTop += (ZOOMSIZESVALUE[zoomOrg] / 2);

			zoom = 2;
			innerDiv.style.left = (stripPx(innerDiv.style.left) + move_x_value_org - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) + move_y_value_org - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
		} else if (zoom == "2") {
			move_x_value_org = stripPx(innerDiv.style.left) + innerLeft;
			move_y_value_org = stripPx(innerDiv.style.top) + innerTop;

			innerLeft += (ZOOMSIZESVALUE[zoomOrg] / 2);
			innerTop += (ZOOMSIZESVALUE[zoomOrg] / 2);
			zoom = 3;
			innerDiv.style.left = (stripPx(innerDiv.style.left) + move_x_value_org - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) + move_y_value_org - (ZOOMSIZESVALUE[zoomOrg] / 2)) + "px";
		} else if (zoom == "3") {    
			zoom = 3;
		}
	}else{
		if (zoom == "0") {
			zoom = 0;
		} else if (zoom == "1") {
			move_x_value_org = (stripPx(innerDiv.style.left) + innerLeft) / 2;
			move_y_value_org = (stripPx(innerDiv.style.top) + innerTop) / 2;

			zoom = 0;
			innerLeft -= (ZOOMSIZESVALUE[zoom] / 2);
			innerTop -= (ZOOMSIZESVALUE[zoom] / 2);
			innerDiv.style.left = (stripPx(innerDiv.style.left) - move_x_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) - move_y_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
		} else if (zoom == "2") {
			move_x_value_org = (stripPx(innerDiv.style.left) + innerLeft) / 2;
			move_y_value_org = (stripPx(innerDiv.style.top) + innerTop) / 2;
			zoom = 1;
			innerLeft -= (ZOOMSIZESVALUE[zoom] / 2);
			innerTop -= (ZOOMSIZESVALUE[zoom] / 2);
			innerDiv.style.left = (stripPx(innerDiv.style.left) - move_x_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) - move_y_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
		} else if (zoom == "3") {
			move_x_value_org = (stripPx(innerDiv.style.left) + innerLeft) / 2;
			move_y_value_org = (stripPx(innerDiv.style.top) + innerTop) / 2;
			zoom = 2;
			innerLeft -= (ZOOMSIZESVALUE[zoom] / 2);
			innerTop -= (ZOOMSIZESVALUE[zoom] / 2);
			innerDiv.style.left = (stripPx(innerDiv.style.left) - move_x_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top) - move_y_value_org + (ZOOMSIZESVALUE[zoom] / 2)) + "px";
		}
	}

    var imgs = innerDiv.getElementsByTagName("img");
    while (imgs.length > 0) innerDiv.removeChild(imgs[0]);

    setInnerDivThumbnailPosition(ZOOMSIZESVALUE[zoom], ZOOMSIZESVALUE[zoom]);

    checkThumbnail(thumbnail_index);

	marker_zoom(zoomOrg, zoom);
}

// 吹き出しの拡大・縮小
function balloon_zoom(zoomOrg, zoom) {
  if (zoomOrg == zoom) return;
  for (var i = 0; i < balloon.length; i++) {
    if (!document.getElementById(balloon[i]["id"])) continue;
    balloon_setup(zoom,balloon[i],null);
  }
}

// balloonの属性設定
function balloon_setup(zoom,info,balloonDiv) {
  if (balloonDiv == null) {
    var balloonDiv = document.getElementById(info["id"]);
    if (!balloonDiv) return;
  }
  var x = info["x"].split("|");
  var y = info["y"].split("|");
  var s = info["s"].split("|");
  var range_x = parseInt(x[zoom]) * (ZOOMVALUE[zoom] / parseInt(s[zoom]));
  var range_y = parseInt(y[zoom]) * (ZOOMVALUE[zoom] / parseInt(s[zoom]));
  balloonDiv.style.left = range_x - (BALLOONWIDTH / 2) + 15 + "px";
  balloonDiv.style.top  = range_y - BALLOONHEIGHT + "px";
}

// 選択markerの拡大・縮小
function selmarker_zoom(zoomOrg, zoom) {
  if (zoomOrg == zoom) return;
  for (var i = 0; i < selmarker.length; i++) {
    if (!document.getElementById(selmarker[i]["id"])) continue;
    selmarker_setup(zoom,selmarker[i],null);
  }
}

// 選択markerの属性設定
function selmarker_setup(zoom,info,markerDiv) {
  if (markerDiv == null) {
    markerDiv = document.getElementById(info["id"]);
    if (!markerDiv) return;
  }
  var x = info["x"].split("|");
  var y = info["y"].split("|");
  var s = info["s"].split("|");
  markerDiv.style.backgroundImage = "url(" + SERVICE_PREFIX + "/images/circle_" + ZOOMVALUE[zoom] + "_selected.gif)";
  markerDiv.style.width = MARKERWIDTH[zoom] + "px";
  markerDiv.style.height = MARKERHEIGHT[zoom] + "px";
  markerDiv.style.backgroundRepeat = "no-repeat";
  var range_x = parseInt(x[zoom]) * (ZOOMVALUE[zoom] / parseInt(s[zoom]));
  var range_y = parseInt(y[zoom]) * (ZOOMVALUE[zoom] / parseInt(s[zoom]));
  markerDiv.style.left = range_x - Math.ceil(MARKERWIDTH[zoom] / 2) + 1 + "px";
  markerDiv.style.top = range_y - Math.ceil(MARKERHEIGHT[zoom] / 2) + 1 + "px";
}

// 吹き出しの追加表示
//    plotno : plotnoを|で区切った文字列
function add_balloon_by_plotno(plotno, move_flag, sellistitem_flag, selmarker_flag) {
  if (plotno.length < 1) return;

  var xmlhttp = createXmlHttp();

  if (xmlhttp) {
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var patent_list = xmlhttp.responseText;
        var items = patent_list.split("\n");
        for(var i=0; i<items.length; i++) {
          add_balloon(items[i], move_flag, sellistitem_flag, selmarker_flag);
        }
      }
    }
    xmlhttp.open("POST", PLOT_INFO_URL, true);
    xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
    xmlhttp.send(
      tsv_url.substr(1, tsv_url.length) + 
      "&s=" + encodeURI(ZOOMVALUE.join("|")) +
      "&plotno=" + encodeURI(plotno) + 
      APPEND_URL_PARAMS);
  }
}

// 選択markerの追加表示
//    plotno : plotnoを|で区切った文字列
function add_selmarker_by_plotno(plotno, move_flag, sellistitem_flag) {
  if (plotno.length < 1) return;

  var xmlhttp = createXmlHttp();

  if (xmlhttp) {
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var patent_list = xmlhttp.responseText;
        var items = patent_list.split("\n");
        for(var i=0; i<items.length; i++) {
          add_selmarker(items[i], move_flag, sellistitem_flag);
        }
      }
    }
    xmlhttp.open("POST", PLOT_INFO_URL, true);
    xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
    xmlhttp.send(
      tsv_url.substr(1, tsv_url.length) + 
      "&s=" + encodeURI(ZOOMVALUE.join("|")) +
      "&plotno=" + encodeURI(plotno) + 
      APPEND_URL_PARAMS);
  }
}

// 特許番号リンク参照履歴の初期表示
function link_history_init(link_history) {
  if (link_history.length < 1) return; 
  var items = link_history.split("|");
  for(var i=0; i<items.length; i++) {
    patentitem_url_click(items[i], false);
  }
}

// 矩形領域の初期表示
function range_init(range) {
  var rangeDiv = document.getElementById(shiftRangeId);

  if(!rangeDiv) return;

  if (range.length > 0) {
    var items = range.split(":");

    rangeDiv.style.left = items[0];
    rangeDiv.style.top  = items[1];
    rangeDiv.style.width = items[2];
    rangeDiv.style.height = items[3];

    shiftRangeWidth = Math.ceil(stripPx(rangeDiv.style.width) / ZOOMVALUE[zoom]);
    shiftRangeHeight = Math.ceil(stripPx(rangeDiv.style.height) / ZOOMVALUE[zoom]);
  } else {
    shiftRangeWidth = 0;
    shiftRangeHeight = 0;
  }

  var ua = navigator.userAgent;
  if(ua.indexOf("MSIE") >= 0) {
    rangeDiv.style.backgroundColor = "#000000";
    rangeDiv.style.borderStyle = "solid";
  }

  rangeDiv.style.zIndex = 3;
  rangeDiv.style.display = (shiftRangeWidth>0 || shiftRangeHeight>0) ? "block" : "none";

  if(useRangeFlg) {
    addRangeMenuDiv();
    rangeDiv.onclick = click_range;
    get_range_patent();
  }
}

// 矩形選択領域の拡大・縮小
function range_zoom(zoomOrg, zoom) {

	if (document.getElementById(shiftRangeId)) {
		var rangeDiv = document.getElementById(shiftRangeId);
		if(shiftRangeWidth < 1 || shiftRangeHeight < 1) return;
		if (zoomOrg != zoom) {
		    var innerDiv = document.getElementById("innerDiv");
			
	        // innerDiv.removeChild(rangeDivOrg);

	        // var rangeDiv = document.createElement("div");

		//     rangeDiv.onclick = click_range;

			if (zoomOrg < zoom) {
				var range_x = (stripPx(rangeDiv.style.left) - (ZOOMSIZESVALUE[zoomOrg] / 2)) * 2;
				var range_y = (stripPx(rangeDiv.style.top)  - (ZOOMSIZESVALUE[zoomOrg] / 2)) * 2;
			} else {
				var range_x = Math.ceil((stripPx(rangeDiv.style.left) - (ZOOMSIZESVALUE[zoomOrg] / 2)) / 2);
				var range_y = Math.ceil((stripPx(rangeDiv.style.top)  - (ZOOMSIZESVALUE[zoomOrg] / 2)) / 2);
			}

			rangeDiv.style.left = range_x + (ZOOMSIZESVALUE[zoom] / 2) + "px";
			rangeDiv.style.top  = range_y + (ZOOMSIZESVALUE[zoom] / 2) + "px";
			rangeDiv.style.width = (shiftRangeWidth * ZOOMVALUE[zoom]) + "px";
			rangeDiv.style.height = (shiftRangeHeight * ZOOMVALUE[zoom]) + "px";

		    // var ua = navigator.userAgent;

		    // if(ua.indexOf("MSIE") >= 0) {
		        // rangeDiv.style.backgroundColor = "#000000";
			// }
	        // rangeDiv.style.zIndex = 3;
	        // rangeDiv.setAttribute("id", shiftRangeId);
	        // innerDiv.appendChild(rangeDiv);
		}
	}
}

// START:getPos
function getPos(from,to) {
  	var innerDiv = document.getElementById("innerDiv");
	//zoom up
	if((to - from) == 1){
 		toggleZoom("up",true);
/*
		if(from == "0"){
			innerDiv.style.left = (stripPx(innerDiv.style.left) - (ZOOMSIZESVALUE[from] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  - (ZOOMSIZESVALUE[from] / 2)) + "px";
		}
		if(from == "1"){
			innerDiv.style.left = (stripPx(innerDiv.style.left) - (ZOOMSIZESVALUE[from] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  - (ZOOMSIZESVALUE[from] / 2)) + "px";
		}
		if(from == "2"){
			innerDiv.style.left = (stripPx(innerDiv.style.left) - (ZOOMSIZESVALUE[from] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  - (ZOOMSIZESVALUE[from] / 2)) + "px";
		}
*/
	}	
	if((to - from) == 2){
 		toggleZoom("up");
 		toggleZoom("up",true);
/*
		if(from == "0"){
			innerDiv.style.left = (stripPx(innerDiv.style.left) - (ZOOMSIZESVALUE[from] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  - (ZOOMSIZESVALUE[from] / 2)) + "px";
			from++;
			innerDiv.style.left = (stripPx(innerDiv.style.left) - (ZOOMSIZESVALUE[from] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  - (ZOOMSIZESVALUE[from] / 2)) + "px";
		}
		if(from == "1"){
			innerDiv.style.left = (stripPx(innerDiv.style.left) - (ZOOMSIZESVALUE[from] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  - (ZOOMSIZESVALUE[from] / 2)) + "px";
			from++
			innerDiv.style.left = (stripPx(innerDiv.style.left) - (ZOOMSIZESVALUE[from] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  - (ZOOMSIZESVALUE[from] / 2)) + "px";
		}
*/
	}	

	if((to - from) == 3){
 		toggleZoom("up");
 		toggleZoom("up");
 		toggleZoom("up",true);
/*
		if(from == "0"){
			innerDiv.style.left = (stripPx(innerDiv.style.left) - (ZOOMSIZESVALUE[from] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  - (ZOOMSIZESVALUE[from] / 2)) + "px";
			from++;
			innerDiv.style.left = (stripPx(innerDiv.style.left) - (ZOOMSIZESVALUE[from] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  - (ZOOMSIZESVALUE[from] / 2)) + "px";
			from++;
			innerDiv.style.left = (stripPx(innerDiv.style.left) - (ZOOMSIZESVALUE[from] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  - (ZOOMSIZESVALUE[from] / 2)) + "px";
		}
*/
	}	
	//zoom down
	if((to - from) == -1){
 		toggleZoom("down",true);
/*
		if(from == "3"){
			from_temp = from - 1
			innerDiv.style.left = (stripPx(innerDiv.style.left) + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
		}
		if(from == "2"){
			from_temp = from - 1
			innerDiv.style.left = (stripPx(innerDiv.style.left) + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
		}
		if(from == "1"){
			from_temp = from - 1
			innerDiv.style.left = (stripPx(innerDiv.style.left) + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
		}
*/
	}
	if((to - from) == -2){
 		toggleZoom("down");
 		toggleZoom("down",true);
/*
		if(from == "3"){
			from_temp = from - 1
			innerDiv.style.left = (stripPx(innerDiv.style.left) + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			from_temp--;
			innerDiv.style.left = (stripPx(innerDiv.style.left) + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
		}
		if(from == "2"){
			from_temp = from - 1
			innerDiv.style.left = (stripPx(innerDiv.style.left) + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			from_temp--;
			innerDiv.style.left = (stripPx(innerDiv.style.left) + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
		}
*/
	}

	if((to - from) == -3){
 		toggleZoom("down");
 		toggleZoom("down");
 		toggleZoom("down",true);
/*
		if(from == "3"){
			from_temp = from - 1
			innerDiv.style.left = (stripPx(innerDiv.style.left) + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			from_temp--;
			innerDiv.style.left = (stripPx(innerDiv.style.left) + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			from_temp--;
			innerDiv.style.left = (stripPx(innerDiv.style.left) + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
			innerDiv.style.top  = (stripPx(innerDiv.style.top)  + (ZOOMSIZESVALUE[from_temp] / 2)) + "px";
		}
*/
	}
}
// END:getPos

// START:moveSlider
function moveSlider(sliderY) {
  	var innerDiv = document.getElementById("innerDiv");
  	var slider = document.getElementById("slider");
	barWidth = sliderY;
//alert(barWidth);
	level    = Math.round(barWidth / 12);
	var zoomOrg = zoom;
	var zoom_temp = zoom + level;
	if (zoom_temp > 3) {
		zoom_temp = 3;
	} else if (zoom < 0) {
		zoom_temp = 0;
	}

  	var scale = document.getElementById("scale" + ZOOMVALUE[zoom_temp]);
	slider.style.top    = SLIDERTOPVALUE[zoom_temp] + Math.floor(stripPx(scale.style.height) / 2) - Math.floor(stripPx(slider.style.height) / 2) + "px";
	getPos(zoomOrg,zoom_temp);
	checkTiles()
}
// END:moveSlider


// START:checkPos
function checkPos(sliderY) {
  	var outerDiv = document.getElementById("outerDiv");
  	var innerDiv = document.getElementById("innerDiv");
  	var slider = document.getElementById("slider");

	var scale_slider_y = slider.offsetTop + outerDiv.offsetTop;
	var scale_mouse_y = dragStartTop + getScrollTop;

//alert(slider.offsetTop + "::::" + outerDiv.offsetTop + "::::" + scale_mouse_y);
	if(sliderY == 0){
		barWidth = scale_slider_y - scale_mouse_y;
	}else{
		barWidth = sliderY;
		level    = Math.round(barWidth/9);
		zoom = zoom + level -1;
//alert("zoom:" + zoom);
//alert("level:" + level);
	}
//alert(barWidth);
	if (1 <= barWidth && barWidth < 12){
 		toggleZoom("up", true);
	}else if (13 <= barWidth && barWidth < 25){
		toggleZoom("up");
		toggleZoom("up", true);
	}else if (25 <= barWidth ){
		toggleZoom("up");
		toggleZoom("up");
		toggleZoom("up", true);
	}else if (-25 < barWidth && barWidth <= -13){
		toggleZoom("down", true);
	}else if (-38 < barWidth && barWidth <= -25){
		toggleZoom("down");
		toggleZoom("down", true);
	}else if (barWidth <= -39){
		toggleZoom("down");
		toggleZoom("down");
		toggleZoom("down", true);
	}
}
// END:checkPos

// START:mover
function mover(type) {

	var innerDiv = document.getElementById("innerDiv");

	if(type == "1"){
		innerDiv.style.left = (stripPx(innerDiv.style.left) + MOVERVALUE) + "px";
		innerDiv.style.top  = (stripPx(innerDiv.style.top)  + MOVERVALUE) + "px";
    }else if(type == "2"){
		innerDiv.style.top  = (stripPx(innerDiv.style.top)  + MOVERVALUE) + "px";
    }else if(type == "3"){
		innerDiv.style.left = (stripPx(innerDiv.style.left) - MOVERVALUE) + "px";
		innerDiv.style.top  = (stripPx(innerDiv.style.top)  + MOVERVALUE) + "px";
    }else if(type == "4"){
		innerDiv.style.left = (stripPx(innerDiv.style.left) + MOVERVALUE) + "px";
    }else if(type == "5"){
		innerDiv.style.left = (ZOOMSIZESVALUE[0] / 2) - (ZOOMSIZESVALUE[zoom] / 2) + "px";
		innerDiv.style.top  = (ZOOMSIZESVALUE[0] / 2) - (ZOOMSIZESVALUE[zoom] / 2) + "px";
    }else if(type == "6"){
		innerDiv.style.left = (stripPx(innerDiv.style.left) - MOVERVALUE) + "px";
    }else if(type == "7"){
		innerDiv.style.left = (stripPx(innerDiv.style.left) + MOVERVALUE) + "px";
		innerDiv.style.top  = (stripPx(innerDiv.style.top)  - MOVERVALUE) + "px";
    }else if(type == "8"){
		innerDiv.style.top  = (stripPx(innerDiv.style.top)  - MOVERVALUE) + "px";
    }else if(type == "9"){
		innerDiv.style.left = (stripPx(innerDiv.style.left) - MOVERVALUE) + "px";
		innerDiv.style.top  = (stripPx(innerDiv.style.top)  - MOVERVALUE) + "px";
	}
    checkTiles();
}
// END:mover

// START:wheel
function wheel(event) {
	var delta = 0;

	//IE
	if (event.wheelDelta) {
		delta =  event.wheelDelta / -120;
	}

	//Firefox
	if (event.detail) {
		delta = event.detail / 3;
	}

	if (delta < 0){
		toggleZoom("up", true);
	}else if (delta > 0){
		toggleZoom("down", true);
	}
}
// END:wheel

/*
// START:setEvent
function setEvent() {
	if (!document.getElementById("outerDiv")) {
		setTimeout("setEvent()", 1000);
		return;
	} else {
		clearTimeout();
	}

	element = document.getElementById("outerDiv");

	if (element.attachEvent) {
		element.attachEvent("onmousewheel", wheel);
		return;
	}

	if (element.addEventListener) {
		element.addEventListener("DOMMouseScroll", wheel, false);
		return;
	}
}
// START:wheel
*/

//var label_info = ""

// textの取得 (ブラウザによって動作が異なるため)
function get_text(element) {
	if (typeof element.textContent != "undefined") {
		return element.textContent;
	} else {
		return element.innerText;
	}
}

// textの設定 (ブラウザによって動作が異なるため)
function set_text(element, value) {
	if (typeof element.textContent != "undefined") {
		element.textContent = value;
	} else {
		element.innerText = value;
	}
}


// ラベルを表示
function show_label() {
	showLabel = true;
	show_label_on_map();
}

// ラベルを表示
function show_label_on_map() {
	if (showLabel && !document.getElementById("labelDiv")) {
		create_label_on_map();
	}
}

// ラベルを生成
function create_label_on_map() {
	var innerDiv = document.getElementById("innerDiv");
	var labelDiv = document.createElement("div");
    labelDiv.style.zIndex = 1;
    labelDiv.setAttribute("id", "labelDiv");

	var xmlhttp = createXmlHttp();

	var result = ""

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {

			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var innerDiv = document.getElementById("innerDiv");
				var labelDiv = document.createElement("div");
			    labelDiv.style.zIndex = 1;
			    labelDiv.setAttribute("id", "labelDiv");

				var label_info = xmlhttp.responseText;
				var label_rows = label_info.split("\n");

				for (i = 0; i < label_rows.length; i++) {
					var label_row = label_rows[i];

					var label_cols = label_row.split("\t");
					var label_x	 = label_cols[0] * ZOOMVALUE[zoom];
					var label_y	 = label_cols[1] * ZOOMVALUE[zoom];
					var label	 = label_cols[2];

					var labelInnerDiv = document.createElement("div");
					set_text(labelInnerDiv, label);
					labelInnerDiv.style.position = "absolute";
					labelInnerDiv.style.left = label_x + "px";
					labelInnerDiv.style.top = label_y + "px";
				    labelInnerDiv.style.zIndex = 2;

				    labelDiv.appendChild(labelInnerDiv);
					innerDiv.appendChild(labelDiv);
				}
			}
		}
		//HTTPリクエスト
		xmlhttp.open("GET", READ_LABEL_URL + tsv_url);
		xmlhttp.send(null);
	}
}

// 表示されているラベルを拡大
function label_zoom() {
	delete_label_on_map();
	show_label_on_map();
}

// 表示されているラベルを削除
function delete_label_on_map() {
	if (document.getElementById("labelDiv")) {
		var innerDiv = document.getElementById("innerDiv");
		var labelDiv = document.getElementById("labelDiv");
		innerDiv.removeChild(labelDiv);
	}
}

// 表示されている「+」を拡大
function marker_zoom(zoomOrg, zoom) {

	if (zoomOrg == zoom) {
		return false;
	}

	if (document.getElementById("markerDiv")) {
		var innerDiv = document.getElementById("innerDiv");
		var markerDiv = document.getElementById("markerDiv");
		innerDiv.removeChild(markerDiv);

		for(var i=0; i<selected_plotno.length; i++) {
			var marker = selected_plotno[i].split("|");
            show_markers_on_map(selected_plotno[i], selected_color[i]);
//			for (var j = 0; j < marker.length; j++) {
//				show_marker_on_plot(marker[j], selected_color[i]);
//			}
		}
	}
}

// 表示されている「+」を表示リセット
function reset_marker() {
    // marker = new Array();
    var innerDiv = document.getElementById("innerDiv");
    var markerDiv;
    if (markerDiv = document.getElementById("markerDiv")) {
        innerDiv.removeChild(markerDiv);
    }
    reset_item_selection();
}

// 該当する特許に対するプロットに「+」画像表示
function show_markers_on_map(plotNos, colorIdx) {
    var xmlhttp = createXmlHttp();

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {

			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var patents_info = xmlhttp.responseText;
                var patent_info = patents_info.split("\n");
	            for (i = 0; i < patents_info.length; i++) {
                  var patent = patent_info[i].split("\t");
                  var plotNo = patent[0];

                  if (!document.getElementById("innerDiv") || document.getElementById("marker" + plotNo + "_" + colorIdx)) {
                    return;
                  }

                  var innerDiv = document.getElementById("innerDiv");
                  var markerDiv;
                  if (document.getElementById("markerDiv")) {
                    markerDiv = document.getElementById("markerDiv");
                  } else {
                    markerDiv = document.createElement("div");
                    markerDiv.style.zIndex = 2;
                    markerDiv.setAttribute("id", "markerDiv");
                  }
                  var markerInnerDiv = document.createElement("div");
                  markerInnerDiv.setAttribute("id", "marker" + plotNo + "_" + colorIdx);

                  markerDiv.appendChild(markerInnerDiv);
                  innerDiv.appendChild(markerDiv);


                  var result = ""

                    if (markerInnerDiv = document.getElementById("marker" + plotNo + "_" + colorIdx)) {

                      patent_x = patent[1];
                      patent_y = patent[2];
                      patent_r = patent[3];

                      markerInnerDiv.style.backgroundImage = "url(" + SERVICE_PREFIX + "/images/circle_" + ZOOMVALUE[zoom] + "_" + colorIdx + ".gif)";
                      markerInnerDiv.style.position = "absolute";
                      markerInnerDiv.style.width = MARKERWIDTH[zoom] + "px";
                      markerInnerDiv.style.height = MARKERHEIGHT[zoom] + "px";
                      markerInnerDiv.style.backgroundRepeat = "no-repeat";
                      markerInnerDiv.style.left = patent_x - Math.ceil(MARKERWIDTH[zoom] / 2) + 1 + "px";
                      markerInnerDiv.style.top = patent_y - Math.ceil(MARKERHEIGHT[zoom] / 2) + 1 + "px";

                      markerInnerDiv.style.zIndex = 2;
                    }
                }
			}
		}
        xmlhttp.open("GET", SEARCH_PATENTS_URL + tsv_url + "&plot_nos=" + plotNos + "&s=" + ZOOMVALUE[zoom] + APPEND_URL_PARAMS);
        xmlhttp.send(null);
	}
}

// 該当する特許に対するプロットに「+」画像表示
function show_marker_on_plot(plotNo, colorIdx) {
	if (!document.getElementById("innerDiv") || document.getElementById("marker" + plotNo + "_" + colorIdx)) {
		return;
	}

    var innerDiv = document.getElementById("innerDiv");
	var markerDiv;
	if (document.getElementById("markerDiv")) {
		markerDiv = document.getElementById("markerDiv");
	} else {
		markerDiv = document.createElement("div");
		markerDiv.style.zIndex = 2;
		markerDiv.setAttribute("id", "markerDiv");
	}
	var markerInnerDiv = document.createElement("div");
    markerInnerDiv.setAttribute("id", "marker" + plotNo + "_" + colorIdx);

    markerDiv.appendChild(markerInnerDiv);
    innerDiv.appendChild(markerDiv);

	var xmlhttp = createXmlHttp();

	var result = ""

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {

			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

				if (markerInnerDiv = document.getElementById("marker" + plotNo + "_" + colorIdx)) {
					var patent_info = xmlhttp.responseText;

					var patent = patent_info.split("\t");

					patent_x = patent[1];
					patent_y = patent[2];
					patent_r = patent[3];

					markerInnerDiv.style.backgroundImage = "url(" + SERVICE_PREFIX + "/images/circle_" + ZOOMVALUE[zoom] + "_" + colorIdx + ".gif)";
					markerInnerDiv.style.position = "absolute";
				    markerInnerDiv.style.width = MARKERWIDTH[zoom] + "px";
				    markerInnerDiv.style.height = MARKERHEIGHT[zoom] + "px";
                    markerInnerDiv.style.backgroundRepeat = "no-repeat";
                    markerInnerDiv.style.left = patent_x - Math.ceil(MARKERWIDTH[zoom] / 2) + 1 + "px";
                    markerInnerDiv.style.top = patent_y - Math.ceil(MARKERHEIGHT[zoom] / 2) + 1 + "px";

				    markerInnerDiv.style.zIndex = 2;
				}
			}
		}
		//HTTPリクエストを送ったりするとこここから
		xmlhttp.open("GET", SEARCH_PATENT_URL + tsv_url + "&plot_no=" + plotNo + "&s=" + ZOOMVALUE[zoom] + APPEND_URL_PARAMS);
		xmlhttp.send(null);
	}
}

// プロット上でマウスオーバされた際、該当プロットの情報を特定の領域に表示
function show_label_mouseover_on_plot(x, y) {

	if (document.getElementById(PLOTLABEL_ID)) {
        var innerDiv = document.getElementById("innerDiv");

		var move_x = innerDiv.offsetLeft;
		var move_y = innerDiv.offsetTop;

		var xmlhttp = createXmlHttp();

		var result = ""
	
		if (xmlhttp) {
			xmlhttp.onreadystatechange = function() {
	
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

					var patent_info = xmlhttp.responseText;

					var plotLabel = document.getElementById(PLOTLABEL_ID);

					//plotLabel.value = "";

					if (patent_info.length > 0) {
						var patent = patent_info.split("\t");
						var label = patent[7];

						plotLabel.value = label;

						if (PLOTLINK_ID.length > 0) {
							Element.show(PLOTLABEL_ID);
							Element.hide(PLOTLINK_ID);
						}
					} else {
						if (PLOTLINK_ID.length > 0) {
							Element.hide(PLOTLABEL_ID);
							Element.show(PLOTLINK_ID);
						}
					}
					time_flg = false
					clearTimeout();
				}
			}
			//HTTPリクエストを送ったりするとこここから
			xmlhttp.open("GET", PLOT_URL + tsv_url + "&now_x=" + now_mouse_x + "&now_y=" + now_mouse_y + "&m_x=" + move_x + "&m_y=" + move_y + "&zoom=" + zoom + "&s=" + ZOOMVALUE[zoom] + APPEND_URL_PARAMS);
			xmlhttp.send(null);
		}
	}
}

// 特許リストアイテムのクラス名
function get_classname_patentitem(rank, selected) {
  if(selected) return "ListLine" + (rank % 2) + "On"; 
  return "ListLine" + (rank % 2) + "Off";
}

// 特許リストアイテムの選択キャンセル
function patentitem_selection_cancel() {
  patentitem_selection_canceled = true;
}

// 特許リストアイテムURLクリック
function patentitem_url_click(rank, selection_cancel_flg) {
  var obj;
  if(obj= document.getElementById("itemchk"+rank)) {
    obj.checked = true;
  }
  if(selection_cancel_flg) patentitem_selection_cancel();
}

// 特許リストアイテム選択
function patentitem_selection(rank) {
  if(patentitem_selection_canceled) {
    patentitem_selection_canceled = false;
    return;
  }

  if(!document.getElementById("mapView")) return;

  if(itemno2plotno.length < 1) return;

  var obj = document.getElementById("item"+rank);
  if(!obj) return;

  if(obj.className == get_classname_patentitem(rank, true)) {
    obj.className = get_classname_patentitem(rank, false);
    if((PATENT_MARKER_TYPE & 1) == 1)
      close_balloon_by_plotno(itemno2plotno[rank], false, (PATENT_MARKER_TYPE == 3));
    if(PATENT_MARKER_TYPE == 2)
      remove_selmarker_by_plotno(itemno2plotno[rank], false);
  } else {
    if((PATENT_MARKER_TYPE & 1) == 1)
      add_balloon_by_plotno(itemno2plotno[rank], true, true, (PATENT_MARKER_TYPE == 3));
    if(PATENT_MARKER_TYPE == 2)
      add_selmarker_by_plotno(itemno2plotno[rank], true, true);
  }
}

// 選択markerの追加
function add_selmarker(patent_list, move_flag, sellistitem_flag) {
  if(patent_list.length < 1) return;

  var patent_info = patent_list.split("\t");
  var markerId = "markerSelected" + patent_info[0];

  if(document.getElementById(markerId)) return;// 表示中

  // 選択markerのtest
  var innerDiv = document.getElementById("innerDiv");
  var markerSelectedDiv = document.getElementById("markerSelectedDiv");
  if (!markerSelectedDiv) {
    markerSelectedDiv = document.createElement("div");
    markerSelectedDiv.style.zIndex = 1;
    markerSelectedDiv.setAttribute("id", "markerSelectedDiv");
    innerDiv.appendChild(markerSelectedDiv); 
  }

  var marker_column = new Object();
  marker_column["id"] = markerId;
  marker_column["x"] = patent_info[1];
  marker_column["y"] = patent_info[2];
  marker_column["s"] = ZOOMVALUE.join("|");
  marker_column["no"] = patent_info[0];// plotno

  selmarker.push(marker_column);

  var markerInnerDiv = document.createElement("div");
  markerInnerDiv.setAttribute("id", markerId);
  markerInnerDiv.style.zIndex = 1;
  markerInnerDiv.style.position = "absolute";
  selmarker_setup(zoom, marker_column, markerInnerDiv);
  markerSelectedDiv.appendChild(markerInnerDiv);

  if (sellistitem_flag) {
    var obj;
    var plotno = parseInt(patent_info[0]);
    var rank = plotno2itemno[plotno];

    if(obj = document.getElementById("item" + rank)) {
      obj.className = get_classname_patentitem(rank, true);
    }
  }

  if(move_flag) move_center(markerId, true);
}

// 吹き出しの追加
function add_balloon(patent_list, move_flag, sellistitem_flag, selmarker_flag) {
  if(patent_list.length < 1) return;
  if(plotno2itemno.length < 1) sellistitem_flag = false;

  if(selmarker_flag) add_selmarker(patent_list, false, ((PATENT_MARKER_TYPE & 2) == 2));

  var patent_info = patent_list.split("\t");
  var balloonId = "balloon_" + patent_info[0];

  if(document.getElementById(balloonId)) return;// balloon表示中

  var balloonDiv = document.getElementById("balloon");

  var balloon_column = new Object();

  balloon_column["id"] = balloonId;
  balloon_column["x"] = patent_info[1];
  balloon_column["y"] = patent_info[2];
  balloon_column["s"] = ZOOMVALUE.join("|");
  balloon_column["no"] = patent_info[0];// plotno

  var balloonOuterDiv = document.createElement("div");

  balloonOuterDiv.className ="balloon";
  balloonOuterDiv.style.backgroundImage = "url(" + SERVICE_PREFIX + "/images/balloon.gif)";
  balloonOuterDiv.style.width = BALLOONWIDTH + "px";
  balloonOuterDiv.style.height = BALLOONHEIGHT + "px";

  balloonOuterDiv.onmouseover=function(){balloonFlg = true; return true;}
  balloonOuterDiv.onmouseout=function(){balloonFlg = false; return true;}
  balloonOuterDiv.setAttribute("id", balloonId);
  balloon_setup(zoom,balloon_column,balloonOuterDiv);

  var balloonInnerDiv = document.createElement("div");

  balloonInnerDiv.className ="balloonInner";
  balloonInnerDiv.style.left = 2 + "px";
  balloonInnerDiv.style.top = 3 + "px";

  var balloonValueDiv = create_patent_info_in_balloon(patent_info);
  balloonInnerDiv.appendChild(balloonValueDiv);

  var balloonCloseDiv = document.createElement("div");

  balloonCloseDiv.className ="balloonClose";
  balloonCloseDiv.style.backgroundImage = "url(" + SERVICE_PREFIX + "/images/close.gif)";
  balloonCloseDiv.style.left = BALLOONWIDTH - 12 + "px";
  balloonCloseDiv.style.top = 2 + "px";

  balloonCloseDiv.onclick=function(){close_balloon(balloonId,null,sellistitem_flag,selmarker_flag);}

  balloon.push(balloon_column);

  balloonOuterDiv.appendChild(balloonInnerDiv);
  balloonOuterDiv.appendChild(balloonCloseDiv);

  balloonDiv.appendChild(balloonOuterDiv);

  if (sellistitem_flag) {
    var obj;
    var plotno = parseInt(patent_info[0]);
    var rank = plotno2itemno[plotno];

    if(obj = document.getElementById("item" + rank)) {
      obj.className = get_classname_patentitem(rank, true);
    }
  }

  // 定数 BALLOONSHOWCNT 以上の吹き出しを作成しようとした場合、過去の吹き出しを削除
  if (BALLOONSHOWCNT > 0 && balloon.length > BALLOONSHOWCNT) {
    close_balloon(balloon[0]["id"],0,sellistitem_flag,selmarker_flag);
  }

  if(move_flag) move_map_for_balloon(balloonId);
}

// プロット上でマウスクリックされた際、該当プロットの情報を吹き出しとして表示
function show_balloon_click_on_plot(event) {
    if (!event) event = window.event;  // for IE
	var click_mouse_x = get_now_mouseX(event);
	var click_mouse_y = get_now_mouseY(event);

    var innerDiv = document.getElementById("innerDiv");
    // var balloonDiv = document.getElementById("balloon");

	var move_x = innerDiv.offsetLeft;
	var move_y = innerDiv.offsetTop;

	var xmlhttp = createXmlHttp();

	var result = ""

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var patent_list = xmlhttp.responseText;
        add_balloon(patent_list, true,
          ((PATENT_MARKER_TYPE & 1) == 1), (PATENT_MARKER_TYPE == 3));
			}
		}
		//HTTPリクエストを送ったりするとこここから
		xmlhttp.open("GET", PLOT_URL + tsv_url + "&now_x=" + now_mouse_x + "&now_y=" + now_mouse_y + "&m_x=" + move_x + "&m_y=" + move_y + "&zoom=" + zoom + "&s=" + encodeURI(ZOOMVALUE.join("|")) + "&mkurl=1" + APPEND_URL_PARAMS);
		xmlhttp.send(null);
	}
}

function create_patent_info_in_balloon(patent_info) {
	var innerBaseDiv = document.createElement("div");

	for (i = 6; i < 11; i++) {
		if(patent_info[i].length < 1) continue;
		var innerLineDiv = document.createElement("div");

		var titleDiv = document.createElement("span");
		titleDiv.className = "balloonTitle";
		set_text(titleDiv, TSV_COLUMN_NAME[i][0] + ":");

		var contentDiv = document.createElement("span");

		// 公開番号の場合リンクを作成
		if (i == 6) {
			contentDiv.className = "balloonContent";

			var patent_link = document.createElement("a");

			patent_link.className = "patentLink";
			patent_link.href = patent_info[11];
			patent_link.target = "_blank";

			if(plotno2itemno.length > 0) {
				var rank = plotno2itemno[parseInt(patent_info[0])];
				patent_link.onclick = function(){patentitem_url_click(rank);}
			}

			set_text(patent_link, patent_info[i]);

			contentDiv.appendChild(patent_link);
		} else {
			contentDiv.className = "balloonContent";
			set_text(contentDiv, patent_info[i]);
		}

		innerLineDiv.appendChild(titleDiv);
		innerLineDiv.appendChild(contentDiv);

                // issuedate
		if (i == 6 && patent_info[12].length > 0) {
		  var issuedateDiv = document.createElement("span");
                  issuedateDiv.className = "balloonContent";
                  set_text(issuedateDiv, "(" + patent_info[12] + ")");
		  innerLineDiv.appendChild(issuedateDiv);
                }

		innerBaseDiv.appendChild(innerLineDiv);
	}
	return innerBaseDiv;
}

// 吹き出しを閉じる
function close_balloon(balloonId,idx,sellistitem_flag,selmarker_flag) {
  if (!document.getElementById(balloonId)) return;
  var innerDiv = document.getElementById("balloon");
  var balloonDiv = document.getElementById(balloonId);
  innerDiv.removeChild(balloonDiv);
  if(idx == null) {
    for (idx = 0; idx < balloon.length; idx++) {
      if (balloonId == balloon[idx]["id"]) break;
    }
  }
  
  if(idx >= balloon.length) return;

  if(sellistitem_flag) {
    if(plotno2itemno.length > 0) {
      var plotno = parseInt(balloon[idx]["no"]);
      var rank = plotno2itemno[plotno];
      var obj;
      if(obj = document.getElementById("item" + rank)) {
        obj.className = get_classname_patentitem(rank, false);
      }
    }
  }

  if(selmarker_flag) remove_selmarker_by_plotno(balloon[idx]["no"], ((PATENT_MARKER_TYPE & 2) == 2));

  balloon.splice(idx,1);
  balloonFlg = false;
}

// 選択markerを削除
function remove_selmarker(markerId,idx,sellistitem_flag) {
  if (!document.getElementById(markerId)) return;
  var markerSelectedDiv = document.getElementById("markerSelectedDiv");
  var markerDiv = document.getElementById(markerId);
  markerSelectedDiv.removeChild(markerDiv);
  if(idx == null) {
    for (idx = 0; idx < selmarker.length; idx++) {
      if (markerId == selmarker[idx]["id"]) break;
    }
  }

  if(idx >= selmarker.length) return;

  if(sellistitem_flag) {
    if (plotno2itemno.length > 0) {
      var plotno = parseInt(selmarker[idx]["no"]);
      var rank = plotno2itemno[plotno];
      var obj;
      if(obj = document.getElementById("item" + rank)) {
        obj.className = get_classname_patentitem(rank, false);
      }
    }
  }

  selmarker.splice(idx,1);
}

// プロット番号を指定して選択markerを削除
function remove_selmarker_by_plotno(plotno, sellistitem_flag) {
  for (var i = 0; i < selmarker.length; i++) {
    if (plotno == parseInt(selmarker[i]["no"])) {
      remove_selmarker(selmarker[i]["id"], i, sellistitem_flag);
      break;
    }
  }
}

// プロット番号を指定して吹き出しを閉じる
function close_balloon_by_plotno(plotno, sellistitem_flag, selmarker_flag) {
  for (var i = 0; i < balloon.length; i++) {
    if (plotno == parseInt(balloon[i]["no"])) {
      close_balloon(balloon[i]["id"],i,sellistitem_flag,selmarker_flag);
      break;
    }
  }
}

// 吹き出しを閉じる
function balloon_over_hidden() {
	for (i = 0; i < balloon.length; i++) {
		if (!document.getElementById(balloon[i]["id"])) {
			continue;
		}

		var balloonDiv = document.getElementById(balloon[i]["id"]);

		if (!balloonDiv.getElementsByTagName("div")) {
			continue;
		}

	    var divs = balloonDiv.getElementsByTagName("div");

	    for (j = 0; j < divs.length; j++) {
			if (divs[j] && divs[j].className == "balloonInner") {
				divs[j].style.overflowX = "hidden";
				divs[j].style.overflowY = "hidden";
			}
		}
	}
}

// 吹き出しを閉じる
function balloon_over_auto() {
	for (i = 0; i < balloon.length; i++) {
		if (!document.getElementById(balloon[i]["id"])) {
			continue;
		}

		var balloonDiv = document.getElementById(balloon[i]["id"]);

		if (!balloonDiv.getElementsByTagName("div")) {
			continue;
		}

	    var divs = balloonDiv.getElementsByTagName("div");

	    for (j = 0; j < divs.length; j++) {
			if (divs[j] && divs[j].className == "balloonInner") {
				divs[j].style.overflowX = "auto";
				divs[j].style.overflowY = "scroll";
			}
		}
	}
}

// マップ中央にオブジェクトが表示されるように、マップを移動する
function move_center(id, effect_flg) {
  var obj = document.getElementById(id);
  if (!obj) return;
  var innerDiv = document.getElementById("innerDiv");
  var mx = Math.ceil((VIEWPORTWIDTH/2) - (obj.offsetWidth/2));
  var my = Math.ceil((VIEWPORTHEIGHT/2) - (obj.offsetHeight/2));
  var current_left = obj.offsetLeft + innerDiv.offsetLeft;
  var current_top = obj.offsetTop + innerDiv.offsetTop;
  var left;
  var top;
  var ox = Math.abs(mx - current_left);
  var oy = Math.abs(my - current_top);

  if (effect_flg) {
    if (ox > BALLOONMOVEVALUE) ox =  BALLOONMOVEVALUE;
    if (oy > BALLOONMOVEVALUE) oy =  BALLOONMOVEVALUE;
  }

  left = (current_left < mx) ? innerDiv.offsetLeft + ox : innerDiv.offsetLeft - ox;
  top  = (current_top  < my) ? innerDiv.offsetTop  + oy : innerDiv.offsetTop  - oy;

  innerDiv.style.left = left + "px";
  innerDiv.style.top  = top + "px";
		
  if (effect_flg) {
    if (mx != (obj.offsetLeft + left) || my != (obj.offsetTop + top)) {
      var effect = effect_flg ? "true" : "false";
      setTimeout("move_center('" + id + "'," + effect + ")", BALLOONMOVETIME);
      return;
    }
  }

  checkTiles();
  clearTimeout();
}

// 吹き出しがマップ外に表示される場合マップを移動
function move_map_for_balloon(balloonId) {

	if (document.getElementById(balloonId)) {
		var innerDiv = document.getElementById("innerDiv");
		var balloonDiv = document.getElementById(balloonId);

		var balloon_left = balloonDiv.offsetLeft;
		var balloon_top = balloonDiv.offsetTop;

		var current_balloon_left = balloon_left + innerDiv.offsetLeft;
		var current_balloon_top = balloon_top + innerDiv.offsetTop;

		if (current_balloon_left < 0) {
			innerDiv.style.left = (innerDiv.offsetLeft + BALLOONMOVEVALUE) + "px";
		}
		if (current_balloon_top < 0) {
			innerDiv.style.top = (innerDiv.offsetTop + BALLOONMOVEVALUE) + "px";
		}

		if ((current_balloon_left + stripPx(balloonDiv.style.width)) > VIEWPORTWIDTH) {
			innerDiv.style.left = (innerDiv.offsetLeft - BALLOONMOVEVALUE) + "px";
		}

		if ((current_balloon_top + stripPx(balloonDiv.style.height)) > VIEWPORTHEIGHT) {
			innerDiv.style.top = (innerDiv.offsetTop - BALLOONMOVEVALUE) + "px";
		}
		
		if (current_balloon_left < 0 || current_balloon_top < 0 ||
                     (current_balloon_left + stripPx(balloonDiv.style.width)) > VIEWPORTWIDTH ||
                     (current_balloon_top  + stripPx(balloonDiv.style.height)) > VIEWPORTHEIGHT) {
			setTimeout("move_map_for_balloon('" + balloonId + "')", BALLOONMOVETIME);
			return;
		} else {
			checkTiles();
			clearTimeout();
		}

	}
}

// マップ履歴の表示 (同画面内)
function show_folder() {

	var xmlhttp = createXmlHttp();

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {

			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

				var folders_html = xmlhttp.responseText;
			    var folderlist = document.getElementById("folderlist");

				Element.update(folderlist, folders_html);
				Element.hide('folderlist');
				Effect.Appear("folderlist");
			}
		}
		//HTTPリクエストを送ったりするとこここから
		xmlhttp.open("GET", SHOW_FOLDER_URL);
		xmlhttp.send(null);
	}
}

// 検索履歴の表示 (同画面内)
function show_history() {

	var xmlhttp = createXmlHttp();

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {

			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

				var folders_html = xmlhttp.responseText;
			    var historylist = document.getElementById("historylist");

				Element.update(historylist, folders_html);
				Element.hide('historylist');
				Effect.Appear("historylist");
			}
		}
		//HTTPリクエストを送ったりするとこここから
		xmlhttp.open("GET", SHOW_HISTORY_URL);
		xmlhttp.send(null);
	}
}

// 検索結果及びマップの保存
function save_folder() {

    var innerDiv = document.getElementById("innerDiv");
    var range = new Array();

	var move_x = innerDiv.offsetLeft;
	var move_y = innerDiv.offsetTop;

    var patentform = document.getElementById("patentform");
    var textform = document.getElementById("textform");

  if (rangeDiv = document.getElementById(shiftRangeId)) {
    range.push(rangeDiv.style.left);
    range.push(rangeDiv.style.top);
    range.push(rangeDiv.style.width);
    range.push(rangeDiv.style.height);
  }

	if (patentform.style.display.indexOf("none") >= 0) {
	    var commit_text = document.getElementById("text");
		commit = commit_text.value;
	} else {
	    var commit_patent = document.getElementById("patent");
		commit = commit_patent.value;
	}

    var formdatum_qtext = document.getElementById("formdatum_qtext");
	qtext = formdatum_qtext.value;

    var formdatum_publication_number = document.getElementById("formdatum_publication_number");
	publication_number = formdatum_publication_number.value;

    var formdatum_stype = document.getElementById("formdatum_stype");

  var balloonValue = new Array();
  for (var i=0; i < balloon.length; i++) {
    balloonValue.push(balloon[i]["no"]);
  }
  var selmarkerValue = new Array();
  for (var i=0; i < selmarker.length; i++) {
    selmarkerValue.push(selmarker[i]["no"]);
  }

  var link_history = new Array();
  var i=0;
  var obj;
  while(true) {
    if(obj = document.getElementById("itemchk"+i)) {
      if(obj.checked) link_history.push(i);
    } else {
      break;
    }
    i++;
  }

	var xmlhttp = createXmlHttp();

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {

			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				alert(MSG003A);
			}
		}
		//HTTPリクエストを送ったりするとこここから
		xmlhttp.open("POST", SAVE_FOLDER_URL, true);
		xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
		xmlhttp.send(
						tsv_url.substr(1, tsv_url.length) + 
						"&m_x=" + encodeURI(move_x) + 
						"&m_y=" + encodeURI(move_y) + 
						"&s=" + encodeURI(zoom) + 
						"&scnt=" + encodeURI(search_count) + 
						"&qtext=" + encodeURIComponent(qtext) + 
						"&publication_number=" + encodeURIComponent(publication_number) + 
						"&commit=" + encodeURI(commit) + 
						"&stype=" + encodeURI(formdatum_stype.checked + 1) + 
						"&marker=" + encodeURI(selected_plotno.join(":")) +
						"&range=" + encodeURI(range.join(":")) +
						"&selected_idx=" + encodeURI(selected_idx.join("|")) +
						"&selected_selection_type=" + encodeURI(selected_selection_type) +
						"&link_history=" + encodeURI(link_history.join("|")) +
						"&balloon=" + encodeURI(balloonValue.join("|")) +
						"&selmarker=" + encodeURI(selmarkerValue.join("|"))
					);
	}
}

// 検索結果及びマップの削除
function delete_folder() {
	var checked_folder = new Array();
	var delete_value = "";
	for (i =1; i <= MAX_FOLDER_CNT; i++) {
		if (!document.getElementById("folder" + i)) {
			break;
		}
	    var folder = document.getElementById("folder" + i);

		if (folder.checked) {
			delete_value = delete_value + folder.value + "|";
		}
	}
	var xmlhttp = createXmlHttp();

	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var folders_html = xmlhttp.responseText;
			    var folders = document.getElementById("folders");
				Element.update(folders, folders_html);
			}
		}
		//HTTPリクエストを送ったりするとこここから
		xmlhttp.open("POST", DELETE_FOLDER_URL, true);
		xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
		xmlhttp.send("delete=" + delete_value);
	}
}

// 全てをチェック
function all_checked() {
	for (i =1; i <= MAX_FOLDER_CNT; i++) {
		if (!document.getElementById("folder" + i)) {
			break;
		}
	    var folder = document.getElementById("folder" + i);
		folder.checked = true;
	}
}


// 検索結果及びマップの削除
function form_submit(form_id) {
	var submit_form = document.getElementById(form_id);
	submit_form.submit();
}


// 検索履歴からの検索
function search_from_history(formtype, stype, scnt) {
	var hidden_from_name_text	 = document.getElementById("from_name_text");
	var hidden_from_name_patent	 = document.getElementById("from_name_patent");

        search_count = scnt

	if (hidden_from_name_text.value == "history" || hidden_from_name_patent.value == "history") {

		if (formtype == "text") {
			new Effect.Appear('textform')
			new Effect.SwitchOff('patentform')
		} else if (formtype == "patent") {
			new Effect.Appear('patentform')
			new Effect.SwitchOff('textform')
		}

		// 検索実行
		var button_type = document.getElementById(formtype);
		button_type.click();

		hidden_from_name_text.value = "";
		hidden_from_name_patent.value = "";
	}
}

// フォルダからの検索
function search_from_folder(formtype, stype, scnt) {
	var hidden_from_name_text	 = document.getElementById("from_name_text");
	var hidden_from_name_patent	 = document.getElementById("from_name_patent");

        search_count = scnt

	if (hidden_from_name_text.value == "folder" || hidden_from_name_patent.value == "folder") {

		if (formtype == "text") {
			new Effect.Appear('textform')
			new Effect.SwitchOff('patentform')
		} else if (formtype == "patent") {
			new Effect.Appear('patentform')
			new Effect.SwitchOff('textform')
		}

		// 検索実行
		var search_button = document.getElementById(formtype);
		search_button.click();

		// xidの開放
		if (document.getElementById("xid_patent")) {
			document.getElementById("xid_patent").value = "";
		}
		if (document.getElementById("xid_text")) {
			document.getElementById("xid_text").value = "";
		}
		
		hidden_from_name_text.value = "";
		hidden_from_name_patent.value = "";
	}
}

// XMLHttpRequestの作成
function createXmlHttp(){
	// Mozilla, Firefox, Safari, IE7
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
       // IE5, IE6
        try {
		    // MSXML3
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
			// MSXML2まで
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    } else {
        return null;
    }
}

// 選択リセット
function reset_item_selection(){
  var obj;
  for (var i = 0; i < selected_idx.length; i++) {
    if(obj = document.getElementById(selected_id[i])) {
      obj.className = "selectedOff";
      obj.title = MSG001A; 
    }
  }
  selected_selection_type = 0;
  selected_id.clear();
  selected_idx.clear();
  selected_plotno.clear();
  selected_color.clear();
  selected_itemno.clear();
}

// title属性の設定
function set_selection_title(result_n,service_locale){
  var obj;
  init_locale(service_locale); 
  for (var i = 0; i < result_n; i++) {
    if(obj = document.getElementById('inventor' + i)) {
      obj.title = (obj.className == "selectedOff") ? MSG001A : MSG001B;
    }
    if(obj = document.getElementById('applicant' + i)) {
      obj.title = (obj.className == "selectedOff") ? MSG001A : MSG001B;
    }
  }
}

// item選択
function select_item(obj, idx, selection_type, result_n, itemno, plotno){
  var add_flag;
  var rmcolor = null;

  if(selected_selection_type < 1) { 
    reset_marker();
    selected_selection_type = selection_type;
  }
  
  if(selection_type != selected_selection_type) {
    alert(MSG001C);
    return false;
  }

  if(obj.className == "selectedOff") {
    if(selected_idx.length >= SELECTION_LIMIT) {
      alert(MSG001D);
      return false;
    }
    selected_idx.push(idx);
    selected_id.push(obj.id);
    selected_itemno.push(itemno);
    if(plotno != null) {
      var cidx;
      var chit;
      selected_plotno.push(plotno);
      for(var i=1; i<=SELECTION_LIMIT; i++) {
        chit = false;
        for(var j=0; j<selected_color.length; j++) {
          if(selected_color[j] == i){
            chit = true;
            break;
          }
        }
        if(!chit) {
          selected_color.push(i);
        }
      }
    }
    obj.className = "selectedOn";
    obj.title = MSG001B; 
    add_flag = true;
  } else {
    var rmidx = selected_idx.indexOf(idx);
    selected_idx = selected_idx.without(idx);
    selected_id.splice(rmidx,1);
    selected_itemno.splice(rmidx,1);
    if(plotno != null) {
      rmcolor = selected_color[rmidx];
      selected_color.splice(rmidx,1);
      selected_plotno.splice(rmidx,1);
    }
    obj.className = "selectedOff";
    obj.title = MSG001A; 
    add_flag = false;
  }

  // update marker
  if(plotno != null) {
    var marker = plotno.split("|");
    var markerDiv;
    var plotitemDiv;
    for(var i=0; i<marker.length; i++) {
      if(add_flag) {
        show_markers_on_map(plotno, selected_color[selected_idx.length - 1]);
        break;
//        show_marker_on_plot(marker[i], selected_color[selected_idx.length - 1]);
      } else {
        if(markerDiv = document.getElementById("markerDiv")){
          if(plotitemDiv = document.getElementById("marker" + marker[i] + "_" + rmcolor)){
            markerDiv.removeChild(plotitemDiv);
          }
        }
      }
    }
  }

  // update patentlist
  if(selected_idx.length > 0) {
    var items = selected_itemno.join("|");
    show_item_only_ex(items, result_n, false);
  } else {
    selected_selection_type = 0;
    show_all_ex(result_n, (zoom < 0) ? true : false, false);
  }

  return true;
}

function init_locale(service_locale){
  set_locale_val("MSG000A", service_locale, false);
  set_locale_val("MSG000B", service_locale, false);
  set_locale_val("MSG000C", service_locale, false);
  set_locale_val("MSG000D", service_locale, false);
  set_locale_val("MSG000E", service_locale, false);
  set_locale_val("MSG001A", service_locale, false);
  set_locale_val("MSG001B", service_locale, false);
  set_locale_val("MSG001C", service_locale, false);
  set_locale_val("MSG001D", service_locale, false);
  set_locale_val("MSG002A", service_locale, false);
  set_locale_val("MSG003A", service_locale, false);
  for(var i=0;i<RANGE_MENU_LIST.length;i++){
    RANGE_MENU_LIST[i][1] = set_locale_val(RANGE_MENU_LIST[i][3], service_locale, false);
  }
  for(var i=0;i<TSV_COLUMN_NAME.length;i++){
    TSV_COLUMN_NAME[i][0] = set_locale_val(TSV_COLUMN_NAME[i][1], service_locale, false);
  }
  set_locale_val("RANGEMENU_WIDTH", service_locale, true);
}

function set_locale_val(vid,service_locale,iflg){
 var v = iflg ? 0 : "";
 try{
   v=eval(vid);
 }catch(e){
   return v;
 }
 try{
   var wrk=eval(vid+"="+vid+"_"+service_locale.toUpperCase());
   if(iflg) {
       eval(vid+"="+wrk);
   } else {
     if(wrk.length > 0) {
       v = wrk;
     } else {
       eval(vid+"='"+v+"'");
     }
   }
 }catch(e){
   return v;
 }
 return v;
}
