/* Javascript for SLTc website.
 * sltc.js (c) 2004- Corydoras softwares., 2008-2011 SLTc.
 *
 * History:
 *  2011.08.03
 *	add addLoadEvent, addUnloadEvent
 *  2011.07.21
 *	add loadFontsize, saveFontsize, setFontsize
 *	add getCookie, setCookie, clearCookie
 *  2009.04.12
 *	infoVisible() => tInfoVisible()
 *  2008.04.21
 *	1st release.
 */

// for switch image --------------------------------------------------
/* Preload images and store object
 * usage:	imgPreload(object, "filename1" [,"filename2"...]);
 */
function imgPreload() {
	if(imgPreload.arguments.length < 2)
		return;
	o = imgPreload.arguments[0];
	for(var i = 1; i < imgPreload.arguments.length; i++) {
		o[i - 1] = new Image();
		o[i - 1].src = imgPreload.arguments[i];
	}
}

// for show/hide toggle ----------------------------------------------
/* Show/Hide infobox
 *	id	id
 *	fr	frame (option)
 */
function infoVisible(id, fr) {
	return tInfoVisible(id, fr);
}
function tInfoVisible(id, fr) {
	var	ofr;
	if(fr) {
		ofr = fr.document.getElementById(id);
	} else {
		ofr = document.getElementById(id);
	}
	if(ofr.style.display == 'block') {
		ofr.style.display = 'none';
	} else {
		ofr.style.display = 'block';
	}
}

// for Google Maps ---------------------------------------------------
var	fMaps = new Object();	// 再入抑止 flag
var	oMaps = new Object();	// 地図を格納する変数

/* Google 地図を指定位置に float 表示する
 * id	表示先の id
 * n	北緯（南緯の場合はマイナス値で指定）
 * e	東経（西経の場合はマイナス値で指定）
 * z	縮尺（省略時は 13）
 * mes	地図上に pop-up 表示したい文字列（省略可）
 */
function gmapSet(id, n, e, z, mes) {
	if(typeof GBrowserIsCompatible != 'function')
		return;
	if(!GBrowserIsCompatible())
		return;
	oMaps[id] = new GMap2(document.getElementById(id));
	oMaps[id].setCenter(new GLatLng(n, e), z);
	oMaps[id].addControl(new GLargeMapControl());
	oMaps[id].addControl(new GMapTypeControl());
	oMaps[id].addControl(new GScaleControl());
	if(typeof(fMaps[id]) == "undefined") {
		fMaps[id] = true;
		if(z == 0)
			z = 13;	// 縮尺の規定値
		document.getElementById(id).style.display = "block";	// 表示する
		// 地図表示
		if(typeof(gMaps[id]) != "undefined") {
			document.getElementById(id).src = gMaps[id];
		} else {
			gMaps[id] = new GMap2(document.getElementById(id));
			gMaps[id].addControl(new GSmallMapControl());
			gMaps[id].setCenter(new GLatLng(n, e), z);
			gMaps[id].enableDoubleClickZoom(true);
			if(typeof(mes) != "undefined")
				gMaps[id].openInfoWindow(gMaps[id].getCenter(), document.createTextNode(mes));
		}
	} else {
		if(fMaps[id]) {
			fMaps[id] = false;
			document.getElementById(id).style.display = "none";
		} else {
			fMaps[id] = true;
			document.getElementById(id).style.display = "block";
			if(z == 0)	z = 13;
			oMaps[id].setCenter(new GLatLng(n, e), z);
		}
	}
}

/* for Cookie --------------------------------------------------------
 * http://www.tohoho-web.com/wwwcook3.htm
 */
/* Cookie を読む
 * key	名前
 * val	値（内容）
 */
function getCookie(key) {
	var  t1, t2, x1, x2, x3;
	t1 = " " + document.cookie + ";";
	x1 = x2 = 0;
	len = t1.length;
	while (x1 < len) {
		x2 = t1.indexOf(";", x1);
		t2 = t1.substring(x1 + 1, x2);
		x3 = t2.indexOf("=");
		if(t2.substring(0, x3) == key) {
			return(unescape(t2.substring(x3 + 1, x2 - x1 - 1)));
		}
		x1 = x2 + 1;
	}
	return("");
}

/* Cookie を焼く（記録する）
 * key	名前
 * val	値（内容）
 * path	パス（省略可＝現在のパス、ドメイン全体に有効にしたい場合は / を指定）
 * days	有効時間（日単位、省略した場合はセッション限り有効、マイナス値は削除）
 */
function setCookie(key, val, path, days) {
	var s;

	s = key + "=" + escape(val) + ";";
	if(path != "") {
		s += " path=" + path + ";";
	} else {
		s += " path=" + location.pathname + ";";
	}
	if(days > 0) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		s += " expires=" + date.toGMTString() + ";";
	} else if(days < 0) {
		s += " expires=Tue, 31-Dec-2030 23:59:59;";
	}
	document.cookie = s;
}

// Cookie を壊す（消去する）
// key	名前
function clearCookie(key) {
	document.cookie = key + "=" + "xx; expires=Tue, 1-Jan-1980 00:00:00;";
}

/* for fontsize ------------------------------------------------------
 * ここで定義された関数群は、id="fontsizenavi" のdiv（またはtd）要素が
 * 定義された（または /docs/navihead.hti が include された）ページにて
 * body onLoad で initFontsize() が実行された場合に有効。
 */
// Cookieに保存されているフォントサイズ指定を有効にする
function initFontsize() {
	var o;
	if(o = document.getElementById('fontsizenavi'))
		o.innerHTML = '文字の大きさ <span style="font-size: 100%;"><a href="javascript:setFontsize(\'90%\');" title="文字の大きさ：小 (90%)"><span style="font-size: 90%;">■</span></a> / <a href="javascript:setFontsize(\'\');" title="文字の大きさ：標準 (100%)"><span style="font-size: 100%;">□</span></a> / <a href="javascript:setFontsize(\'110%\');" title="文字の大きさ：大きめ (110%)"><span style="font-size: 110%;">■</span></a> / <a href="javascript:setFontsize(\'120%\');" title="文字の大きさ：大 (120%)"><span style="font-size: 120%;">■</span></a> / <a href="javascript:setFontsize(\'150%\');" title="文字の大きさ：特大 (150%)"><span style="font-size: 150%;">■</span></a></span>';
	loadFontsize();
}

// Cookieに保存されているフォントサイズ指定を有効にする
function loadFontsize() {
	var fs = getCookie("Fontsize");
	if(fs != "")
		document.body.style.fontSize = fs;
}

// フォントサイズ指定をCookieに保存する
function saveFontsize() {
	setCookie("Fontsize", document.body.style.fontSize, "/");
}

// 基本フォントサイズを変更する
// fs	フォントサイズ（省略時100%）
function setFontsize(fs) {
	if(fs == "")
		fs = "100%";
	document.body.style.fontSize = fs;
	setCookie("Fontsize", fs, "/");
}

/* Overlay -----------------------------------------------------------
 * Overlay dialog emulation
 */
function toggleOverlay(msg) {
	if(!document.getElementsByTagName)
		return;

	var oOverlay = document.getElementById("overlay");
	// div id="overlay" が無ければ作る
	if(!oOverlay) {
		var oBody = document.getElementsByTagName("body").item(0);
		oOverlay = document.createElement("div");
		oOverlay.setAttribute('id','overlay');
		oOverlay.style.display = 'none';
		oOverlay.style.position = 'absolute';
		oOverlay.style.top = '0';
		oOverlay.style.left = '0';
		oOverlay.style.width = '100%';
		oOverlay.style.height = '100%';
		oOverlay.style.backgroundColor = '#444';
		oOverlay.style.zIndex = '90';
		oOverlay.innerHTML = '<div></div>';
		oOverlay.onclick = toggleOverlay;
		oBody.appendChild(oOverlay);

	}
	// div id="overlay" の表示を切り替える
	if(typeof msg == 'string')
		oOverlay.getElementsByTagName("div")[0].innerHTML = msg;
	oOverlay.style.display = (oOverlay.style.display == "none") ? "block" : "none";
}

/* for Override onLoad -----------------------------------------------
 * body onLoad にて実行したい関数を追加する
 */

/* Adds event to window.onload without overwriting currently assigned onload functions.
 * Function found at Simon Willison's weblog - http://simon.incutio.com/
 */
function addLoadEvent(addfunc) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			addfunc();
		}
	}
}

/* Adds event to window.onunload without overwriting currently assigned onload functions.
 */
function addUnloadEvent(addfunc) {
	var oldonunload = window.onunload;
	if(typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload = function () {
			oldonunload();
			addfunc();
		}
	}
}

// override onLoad if exec all pages
//addLoadEvent(initFontsize);	// run initFontsize onLoad


