var fIE5 = (navigator.appVersion.indexOf('MSIE 5')>=0);

function InitLayerPos(lay) {
	if(document.all) {
		lay.style.pixelLeft = lay.offsetLeft;
		lay.style.pixelTop  = lay.offsetTop;
	}
}

function SetLayerVisibility(lay, visible) {
	if(document.layers) {
		lay.visibility = (visible)? 'inherit' : 'hide';
	} else if(document.all || document.getElementById) {
		lay.style.visibility = (visible)? 'inherit' : 'hidden';
	}
}

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 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) {
		lay.style.left = x + 'px';
		lay.style.top = y + 'px';
	}
}

function WriteDocument(lay, str) {
	if(document.layers) {
		lay.document.open();
		lay.document.write(str);
		lay.document.close();
	} else if(document.all || document.getElementById) {
		lay.innerHTML = str;
	}
}


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.28追加
//ポップアップメニュー基準点から地図画像までの距離
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);
		selarea = null;
		timer = null;	//2011.07.28追加
	}
	return false;
}

/**********************************************************
* 関数名　： setHide
* 処理概要： ポップアップメニュークローズのタイマーをセットする
* 更新履歴： 2011.07.28 新規作成
***********************************************************/
function setHide() {
	if (!autoHide || navigator.userAgent.indexOf("iPad") != -1) {
		return;
	}
	if (selarea) {
		if (!timer) {
			timer = setTimeout('HideTip()', msec);
		}
	}
}

/**********************************************************
* 関数名　： clearHide
* 処理概要： ポップアップメニュークローズのタイマーをクリアする
* 更新履歴： 2011.07.28 新規作成
***********************************************************/
function clearHide() {
	if (!autoHide || navigator.userAgent.indexOf("iPad") != -1) {
		return;
	}
	if (timer) {
		clearTimeout(timer);
		timer = null;
	}
}

/**********************************************************
* 関数名　： MouseClick
* 引数　　： 1:イベント　２：都道府県名
* 引数　　： ３：エレメントオブジェクト　
* 戻り値　： false
* 処理概要： マウスクリック時呼び出し関数
* 更新履歴： 2011.07.28 修正
***********************************************************/
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 (!selarea) return;
		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.28追加
		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();
		clearHide();	//2011.07.28追加
	}
	return false;
}

/**********************************************************
* 関数名　： MoveHotelList
* 処理概要： ポップアップメニューの移動量を補正
* 更新履歴： 2011.07.28 修正
* 更新履歴： 2011.07.27 修正
***********************************************************/
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;
/*
		if(document.layers) {
			w = selarea.clip.width;
			h = selarea.clip.height;
		} else if(document.all || document.getElementById) {
//			ww = ww + document.body.scrollLeft;
//			wh = wh + document.body.scrollTop;
			w = selarea.offsetWidth;
			h = selarea.offsetHeight;
//			x = x + document.body.scrollLeft;
//			y = y + document.body.scrollTop;
		}
		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.28追加
		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;
				//2011.07.27追加
				//ポップアップメニュー基準点の上方を越えないようにする
				if (y < 0) {
					y = 0;
				}
			}
		}
		MoveToLayer(selarea, x, y);
		SetLayerVisibility(selarea, true);
	}
}

/**********************************************************
* 関数名　： getMapAreaCoords
* 引数　　： AREAタグオブジェクト
* 戻り値　： 配列
* 処理概要： AREAタグのCOORDS属性の値を配列で返却する
* 更新履歴： 2011.07.28 新規作成
***********************************************************/
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;
}

/**********************************************************
* 関数名　： getElementWidth
* 引数　　： エレメントオブジェクト
* 戻り値　： 返却オブジェクト
* 処理概要： エレメントの幅を取得する
* 更新履歴： 2011.07.28 新規作成
***********************************************************/
function getElementWidth(el) {
	var rect = el.getBoundingClientRect();
	return	rect.right - rect.left;
}

/**********************************************************
* 関数名　： getElementHeight
* 引数　　： エレメントオブジェクト
* 戻り値　： 返却オブジェクト
* 処理概要： エレメントの高さを取得する
* 更新履歴： 2011.07.28 新規作成
***********************************************************/
function getElementHeight(el) {
	var rect = el.getBoundingClientRect();
	return	rect.bottom - rect.top;
}

/**********************************************************
* 関数名　： getElementScreenPosition
* 引数　　： エレメントオブジェクト
* 戻り値　： 返却オブジェクト
* 処理概要： エレメントの位置を取得する
* 更新履歴： 2011.07.28 新規作成
***********************************************************/
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};
}

function resize() {
	if(isinit && selarea) {
		var ww = c_w, wh = c_h;
		if(document.all) {
			c_w = document.body.clientWidth + document.body.scrollLeft;
			c_h = document.body.clientHeight + document.body.scrollTop;
		} else if(document.layers || document.getElementById) {
			c_w = window.innerWidth;
			c_h = window.innerHeight;
		}
		if(ww<600) {
			ww = 600;
		}
		if(c_w>600) {
			e_xp = e_xp + (c_w - ww)/2;
		}
		MoveHotelList();
	}
}

function ChangeArea(obj) {
	ChangeOption(document.search.id, obj.options[obj.selectedIndex].value);
}

function ChangeKorArea(obj) {
	ChangeOption(document.sandr.prefecture, obj.options[obj.selectedIndex].value);
}

function SubmitSearch() {
	document.search.detail.value = 'SEARCH';
	document.search.submit();
}

function SubmitDetail() {
	document.search.detail.value = 'DETAIL';
	document.search.submit();
}

function PageJump() {
	if (navigator.appName == 'Netscape') {
		var language = navigator.language;
	} else {
		var language = navigator.browserLanguage;
	}
	var theName = "language";
	theCookie = document.cookie + ";";
	start = theCookie.indexOf(theName);
	if (start != -1) {
		if (language.indexOf('ja') > -1) {
			var theValue = "ja";
			writeCookie(theName,theValue);
		} else {
			var end = theCookie.indexOf(";",start);
			var flag = theCookie.substring(start + theName.length+1, end);
			if (flag == "ko") {
				document.location.href = 'http://www.toyoko-inn.kr/index.html';
			} else if (flag == "zh") {
				document.location.href = 'http://www.toyoko-inn.com/china/index.html';
			} else if (flag == "en") {
				document.location.href = 'http://www.toyoko-inn.com/eng/index.html';
			}
		}
	} else {
		if (language.indexOf('ko') > -1) {
			var theValue = "ko";
			writeCookie(theName,theValue);
			document.location.href = 'http://www.toyoko-inn.kr/index.html';
		} else if (language.indexOf('ja') > -1) {
			var theValue = "ja";
			writeCookie(theName,theValue);
		} else if (language.indexOf('zh') > -1) {
			var theValue = "zh";
			writeCookie(theName,theValue);
			document.location.href = 'http://www.toyoko-inn.com/china/index.html';
		} else {
			var theValue = "en";
			writeCookie(theName,theValue);
			document.location.href = 'http://www.toyoko-inn.com/eng/index.html';
		}
	}
}

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";
			writeCookie(theName,theValue);
			document.location.href = 'http://www.toyoko-inn.com/china/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";
		} else if (language == "english") {
			var theValue = "en";
		}
		writeCookie(theName,theValue);
		return true;
	}
}

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 getCookie(name) {
	var Flag = false;
	var begin,end;
	var i= 0;

	while(i<=document.cookie.length) {
		begin = i;
		end = begin + name.length;
		
		if(document.cookie.substring(begin,end) == name) {
			Flag = true;
			break;
		}
		i++;
	}
	
	if(Flag == true) {
		begin = end + 1;
		end = document.cookie.indexOf(";",begin);
		if(end < begin) {
			end = document.cookie.length;
		}
		return document.cookie.substring(begin,end);
	}
	return "";
}

function openPopup() {

	var Cookie=getCookie("pop1");
	if(Cookie != "no") {
//		window.open('popup.html','pop1','width=390,height=450,toolbar=0,scrollbars=no,resizable=no,top=50,left=150');
//		window.open('popup.html','pop1','width=300,height=376,toolbar=0,scrollbars=no,resizable=no,top=50,left=150');
//		window.open('popup.html','pop1','width=390,height=456,toolbar=0,scrollbars=no,resizable=no,top=50,left=150');
//		window.open('popup.html','pop1','width=250,height=530,toolbar=0,scrollbars=no,resizable=no,top=50,left=150');
	}

	var Cookie2=getCookie("pop2");
	if(Cookie2 != "no") {
//		window.open('popup2.html','pop2','width=330,height=420,toolbar=0,scrollbars=no,resizable=no,top=50,left=550');
//		window.open('popup2.html','pop2','width=330,height=420,toolbar=0,scrollbars=no,resizable=no,top=50,left=150');	
//		window.open('popup2.html','pop2','width=390,height=456,toolbar=0,scrollbars=no,resizable=no,top=50,left=550');
	}
}

