function SetLayerVisibility(lay, visible) {
	if(document.layers) {
		lay.visibility = (visible)? 'inherit' : 'hide';
		lay.display    = (visible)? '' : 'none';
	} else if(document.all || document.getElementById) {
		lay.style.visibility = (visible)? 'inherit' : 'hidden';
		lay.style.display    = (visible)? '' : 'none';
	}
}

function GetLayerFromName(name) {
	if(document.layers) {
		var s='';
		for(var i=1; i<arguments.length ;i++) {
			s+='document.layers.'+arguments[i]+'.';
		}
		return eval(s+'document.layers.'+name);
	} else if(document.all) {
		return document.all(name);
	} else if(document.getElementById) {
		return document.getElementById(name);
	}
}

function getElementWidth(el) {
	var rect = el.getBoundingClientRect();
	return	rect.right - rect.left;
}

/**********************************************************
* 関数名　： getElementHeight
* 引数　　： エレメントオブジェクト
* 戻り値　： 返却オブジェクト
* 処理概要： エレメントの高さを取得する
* 更新履歴： 2011.07.12 新規作成
***********************************************************/
function getElementHeight(el) {
	var rect = el.getBoundingClientRect();
	return	rect.bottom - rect.top;
}

function getElementScreenPosition(el) {
	var html = document.documentElement;
	var rect = el.getBoundingClientRect();
	var left = rect.left - html.clientLeft;
	var top  = rect.top - html.clientTop;
	return {left:left, top:top};
}

/**********************************************************
* 関数名　： MoveToLayer
* 引数　　： 1:レイヤーオブジェクト　2:移動位置x　3:移動位置y
* 処理概要： ポップアップメニューを移動する
* 更新履歴： 2011.07.12 修正
***********************************************************/
function MoveToLayer(lay, x, y) {
	if(document.layers) {
		lay.moveTo(x, y);
	} else if(document.all) {
		lay.style.pixelLeft = x;
		lay.style.pixelTop = y;
	} else if(document.getElementById) {
		/*2011.07.12
		if (true || -1 != navigator.userAgent.indexOf("Safari")) {
			var obj = document.getElementById("flashblock");
			var pos = getElementScreenPosition(obj);
			var w = getElementWidth(lay);
			if (pos.left < (x + w)) {
				x = pos.left - w;
			}

		}
		*/
		lay.style.left = x + 'px';
		lay.style.top = y + 'px';
	}
}

var selarea = null;
var isinit = false;

var e_xp = 0;
var e_yp = 0;
var c_w = 0;
var c_h = 0;

var fHokkaido = false;
//2011.07.12追加
//ポップアップメニュー基準点から地図画像までの距離
var dx = 266;
var dy = 138;
//ポップアップメニュー基準点のID
var pmenu_org_id = "pmenu_org";
//地図画像のID
var image_map_id = "image_map";
var timer = null;
var msec = 750;
//ポップアップメニュー自動クローズ設定
var autoHide = true;

function HideTip() {
	if(isinit) {
		if (!selarea) {
			return false;
		}
		SetLayerVisibility(selarea, false);
		MoveToLayer(selarea, 0, 0);
		hideShim();
		selarea = null;	//2011.07.12追加
		timer = null;	//2011.07.12追加
	}
	return false;
}

/**********************************************************
* 関数名　： setHide
* 処理概要： ポップアップメニュークローズのタイマーをセットする
* 更新履歴： 2011.07.12 新規作成
***********************************************************/
function setHide() {
	if (!autoHide || navigator.userAgent.indexOf("iPad") != -1) {
		return;
	}
	if (selarea) {
		if (!timer) {
			timer = setTimeout('HideTip()', msec);
		}
	}
}

/**********************************************************
* 関数名　： clearHide
* 処理概要： ポップアップメニュークローズのタイマーをクリアする
* 更新履歴： 2011.07.12 新規作成
***********************************************************/
function clearHide() {
	if (!autoHide || navigator.userAgent.indexOf("iPad") != -1) {
		return;
	}
	if (timer) {
		clearTimeout(timer);
		timer = null;
	}
}

/**********************************************************
* 関数名　： MoveHotelList
* 処理概要： ポップアップメニューの移動量を補正
* 更新履歴： 2011.07.12 修正
***********************************************************/
function MoveHotelList() {
	if(isinit && selarea) {
		MoveToLayer(selarea, 0, 0);
		var ww = c_w, wh = c_h, w = 0, h = 0, x = e_xp, y = e_yp;
		SetLayerVisibility(selarea, true);
		/* 2011.07.12
		if(document.layers) {
			w = selarea.clip.width;
			h = selarea.clip.height;
		} else if(document.all || document.getElementById) {
			w = selarea.offsetWidth;
			h = selarea.offsetHeight;
			h = 0;
		}
		if(ww-x-10<w) {
			if(fHokkaido) {
				if(ww<600) {
					x = 440;
				} else {
					x = 440 + (ww-600)/2;
				}
			} else {
				x = x - w;
			}
		}
		if(y+h>wh) {
			y = y - h;
		}
		*/
		//2011.07.12追加
		if (document.getElementById) {
			var image_map = document.getElementById(image_map_id);
			var pmenu_w = getElementWidth(selarea);
			var image_w = getElementWidth(image_map);
			var pmenu_h = getElementHeight(selarea);
			var image_h = getElementHeight(image_map);
			//ポップアップメニューの右端が地図画像の右端をはみ出す場合
			if (x + pmenu_w > dx + image_w) {
				x = (dx + image_w) - pmenu_w;
			}
			//ポップアップメニューの下端が地図画像の下端をはみ出す場合
			if (y + pmenu_h > dy + image_h) {
				y = (dy + image_h) - pmenu_h;
				if (y < 0) {
					y = 0;
				}
			}
		}
		
		MoveToLayer(selarea, x, y);
	}
}

/**********************************************************
* 関数名　： MouseClick
* 引数　　： 1:イベント　２：都道府県名
* 引数　　： ３：エレメントオブジェクト　
* 戻り値　： false
* 処理概要： マウスクリック時呼び出し関数
* 更新履歴： 2011.07.12 修正
***********************************************************/
function MouseClick(e, name) {
	if(isinit) {
		if(name=='Hokkaido') {
			fHokkaido = true;
		} else {
			fHokkaido = false;
		}
		if(selarea) {
			SetLayerVisibility(selarea, false);
			MoveToLayer(selarea, 0, 0);
		}
		selarea = GetLayerFromName(name);
		if(document.layers) {
			c_w = window.innerWidth;
			c_h = window.innerHeight;
			e_xp = e.pageX;
			e_yp = e.pageY;
		} else if(document.all) {
			c_w = document.body.clientWidth + document.body.scrollLeft;
			c_h = document.body.clientHeight + document.body.scrollTop;
			e_xp = e.clientX + document.body.scrollLeft;
			e_yp = e.clientY + document.body.scrollTop;
		} else if(document.getElementById) {
			c_w = window.innerWidth;
			c_h = window.innerHeight;
			e_xp = e.clientX + window.scrollX;
			e_yp = e.clientY + window.scrollY;
		}
		//2011.07.12追加
		if (arguments.length >= 3) {
			if (document.getElementById) {
				var obj1 = document.getElementById(pmenu_org_id);
				var obj2 = document.getElementById(image_map_id);
				var rect1 = getElementScreenPosition(obj1);
				var rect2 = getElementScreenPosition(obj2);
				//ポップアップメニュー基準点から地図画像までの距離を算出
				dx = rect2.left - rect1.left;
				dy = rect2.top  - rect1.top;
			}
			var obj = arguments[2];
			//AREAタグのCOORDS属性より都道府県の位置を取得
			var coords = getMapAreaCoords(obj);
			var idx = 0;
			//COORDS属性の配列のインデックスを指定
			switch (arguments[1]) {
				case 'Hokkaido'://北海道
					idx = 10;
					break;
				case 'Niigata':	//新潟県
					idx = 6;
					break;
				case 'Ishikawa'://石川県
					idx = 6;
					break;
				case 'Gunma':	//群馬県
					idx = 2;
					break;
				default:		//その他
					idx = 0;
			}

			e_xp = dx + coords[idx    ];
			e_yp = dy + coords[idx + 1];
		}
		//都道府県の位置を引数で指定する場合
		if (arguments.length >= 5) {
			e_xp = dx + arguments[3];
			e_yp = dy + arguments[4];
		}
		MoveHotelList();
		setShim(selarea);
		SetLayerVisibility(selarea, true);
		clearHide();	//2011.07.12追加
	}
	return false;
}

function writeCookie(theName,theValue) {
	setDay = new Date();
	setDay.setTime(setDay.getTime() + (7*1000*60*60*24));
	expDay = setDay.toGMTString();
	document.cookie = theName + "=" + theValue + "; path=/; expires=" + expDay;
}

function writeLangCookie(theValue) {
	writeCookie("language", theValue);
}

var areaObj = null;

function updateShim()
{
	if (-1 == navigator.userAgent.toUpperCase().indexOf("MSIE")) {
		return;
	}

	var shim = document.getElementById("shim");
	
	if (null == areaObj || "none" == shim.style.display) {
		return;
	}
	shim.style.pixelLeft	= areaObj.style.pixelLeft;
	shim.style.pixelTop		= areaObj.style.pixelTop;
	shim.style.width		= areaObj.offsetWidth;
	shim.style.height		= areaObj.offsetHeight;
}

function setShim(obj){
	if (-1 == navigator.userAgent.toUpperCase().indexOf("MSIE")) {
		return;
	}

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

	shim.style.pixelLeft = obj.style.pixelLeft;
	shim.style.pixelTop = obj.style.pixelTop;
	shim.style.width = obj.offsetWidth;
	shim.style.height = obj.offsetHeight;
	shim.style.display = 'block';

	areaObj = obj;
}

function hideShim(){
	if (-1 == navigator.userAgent.toUpperCase().indexOf("MSIE")) {
		return;
	}

	var cf = document.getElementById("shim");
	cf.style.display="none";
	
	areaObj = null;
}

function languageCheck(language,ver) {
	var theName = "language";
	if (ver =='kor') {
		if (language == "japan") {
			var theValue = "ja";
			writeCookie(theName,theValue);
			document.location.href = 'http://www.toyoko-inn.com/index.html';
		} else if (language == "china") {
			var theValue = "zh_tw";
			writeCookie(theName,theValue);
			document.location.href = 'http://www.toyoko-inn.com/china/index.html';
		} else if (language == "china_cn") {
			var theValue = "zh_cn";
			writeCookie(theName,theValue);
			document.location.href = 'http://www.toyoko-inn.com/china_cn/index.html';
		} else if (language == "english") {
			var theValue = "en";
			writeCookie(theName,theValue);
			document.location.href = 'http://www.toyoko-inn.com/eng/index.html';
		}
	} else {
		if (language == "japan") {
			var theValue = "ja";
		} else if (language == "korean") {
			var theValue = "ko";
		} else if (language == "china") {
			var theValue = "zh_tw";
		} else if (language == "china_cn") {
			var theValue = "zh_cn";
		} else if (language == "english") {
			var theValue = "en";
		}
		writeCookie(theName,theValue);
		return true;
	}
}

/**********************************************************
* 関数名　： getMapAreaCoords
* 引数　　： AREAタグオブジェクト
* 戻り値　： 配列
* 処理概要： AREAタグのCOORDS属性の値を配列で返却する
* 更新履歴： 2011.07.12 新規作成
***********************************************************/
function getMapAreaCoords(obj) {
	var ar = new Array();
	var coords = obj.getAttribute("coords");
	if (coords) {
		ar = coords.split(",");
		//数値変換
		for (var i in ar) {
			ar[i] = eval(ar[i]);
		}
	}
	return ar;
}

