var di = document.implementation;
var isOpera = window.opera && navigator.userAgent.match(new RegExp('Opera[/ ]7'));
/* イベントを追加する */
function addEvent(obj, eventType, func) {
    if (di && di.hasFeature('HTMLEvents', '2.0')) {
        /* DOM2 HTMLEvents対応 */
        obj.addEventListener(eventType, func, false);
    } else if (obj.attachEvent) {
        /* WinIE5-6用 */
        obj.attachEvent('on' + eventType, func);
    } else {
        /* MacIE5用 */
        obj['on' + eventType] = func;
    }
}

//====================
// CSS切替
//====================
// Macintosh                -> MacOS
// Windows95/98/NT/2000/XP  -> Windows
// UNIX                     -> UNIX
function getOSType()
{
    var uAgent = navigator.userAgent.toUpperCase();
    if (uAgent.indexOf("MAC") >= 0) return "MacOS";
    if (uAgent.indexOf("WIN") >= 0) return "Windows";
    if (uAgent.indexOf("X11") >= 0) return "UNIX";
    return "";
}

// Netscape Navigator -> Netscape
// Internet Explorer  -> Explorer
// Safari  -> Safari
// Opera   -> Opera
function getBrowserName()
{
    var aName = navigator.appName.toUpperCase();
    var uName = navigator.userAgent.toUpperCase();
    if (uName.indexOf("SAFARI") >= 0)  return "Safari";
    if (uName.indexOf("FIREFOX") >= 0)  return "Firefox";
    if (uName.indexOf("OPERA") >= 0)  return "Opera";
    if (aName.indexOf("NETSCAPE") >= 0)  return "Netscape";
    if (aName.indexOf("MICROSOFT") >= 0) return "Explorer";
    if (aName.indexOf("MOZILLA/4") >= 0) return "Netscape";
    if (aName.indexOf("MOZILLA") >= 0) return "Mozilla";
    return "";
}

function getBrowserVersion()
{
    var browser = getBrowserName();
    var version = 0;
    var s = 0;
    var e = 0;
    var appVer = navigator.appVersion;
    var uName  = navigator.userAgent.toUpperCase();
    if (browser == "Safari")
    {
        version = eval(appVer.substring(0,3)) - 4;
    }
    if (browser == "Opera")
    {
        s = uName.indexOf("OPERA ",0) + 6;
        e = uName.indexOf(" ",s);
        version = eval(uName.substring(s,e));
    }
    if (browser == "Netscape")
    {
        s = appVer.indexOf(" ",0);
        version = eval(appVer.substring(0,s));
        if (version >= 5) version++;
    }
    if (browser == "Explorer")
    {
        appVer  = navigator.userAgent;
        s = appVer.indexOf("MSIE ",0) + 5;
        e = appVer.indexOf(";",s);
        version = eval(appVer.substring(s,e));
    }
    return version;
}

function changeStyleSheet(path, base_name)
{
    var os      = getOSType();
    var browser = getBrowserName();
    var version = getBrowserVersion();
    var dirName;
    var adddirName;

    /* ページ読み込み完了時にフォントサイズ変更処理を実行 */
    addEvent((isOpera ? document : window), 'load', initialize);

    // CSS適用外のブラウザ判定
    if (os == "Windows")
    {
        // WindowsならJavaScriptでCSSを適用させない
        return"";
    }

    if (os == "MacOS")
    {
        if ((browser == "Explorer") && ((version >= 1) && (version <= 4)))
        {
            // IE Ver1～4はCSSを適用しない
            return"";
        }
        if ((browser == "Netscape") && ((version >= 1) && (version <= 4)))
        {
            // Netscape Ver1～4はCSSを適用しない
            return"";
        }
    }

    // 適用するCSSの判定
    if (os == "MacOS")
    {
        dirName = path+"mac/"+base_name+".css";
        document.write("<link rel='stylesheet' href='"+dirName+"' type='text/css' media='all'>");

        if (browser == "Explorer")
        {
            adddirName = path+"mac/"+base_name+"_ie.css";
            document.write("<link rel='stylesheet' href='"+adddirName+"' type='text/css' media='all'>");
        }
    }

    return"";
}


//====================
// 文字サイズ切替
//====================

// 文字サイズ切替部分 表示(トップページ用)
function displayFontSize()
{
	document.write("\<div id=\"fontsize\"\>");
	document.write("\<div class=\"title\"\>\<img src=\"images/fontsize_title.gif\" width=\"61\" height=\"16\" alt=\"文字サイズ\" /\>\</div\>");
	document.write("\<ul\>");
	document.write("\<li id=\"size-s\"\>\<a href=\"javascript:void(0)\" onclick=\"changeFontSize('small');\"\>\<img src=\"images/sp.gif\" width=\"20\" height=\"20\" alt=\"小\" /\>\</a\>\</li\>");
	document.write("\<li id=\"size-m\"\>\<a href=\"javascript:void(0)\" onclick=\"changeFontSize('middle');\"\>\<img src=\"images/sp.gif\" width=\"20\" height=\"20\" alt=\"中\" /\>\</a\>\</li\>");
	document.write("\<li id=\"size-l\"\>\<a href=\"javascript:void(0)\" onclick=\"changeFontSize('large');\"\>\<img src=\"images/sp.gif\" width=\"20\" height=\"20\" alt=\"大\" /\>\</a\>\</li\>");
	document.write("\</ul\>");
	document.write("\<br class=\"clr\" /\>");
	document.write("\</div\> \<!-- font-size --\>");
}

// 文字サイズ切替部分 表示(下層ページ用)
function displayFontSizeLower()
{
	document.write("\<div id=\"fontsize\"\>");
	document.write("\<div class=\"title\"\>\<img src=\"../images/fontsize_title.gif\" width=\"61\" height=\"16\" alt=\"文字サイズ\" /\>\</div\>");
	document.write("\<ul\>");
	document.write("\<li id=\"size-s\"\>\<a href=\"javascript:void(0)\" onclick=\"changeFontSize('small');\"\>\<img src=\"../images/sp.gif\" width=\"20\" height=\"20\" alt=\"小\" /\>\</a\>\</li\>");
	document.write("\<li id=\"size-m\"\>\<a href=\"javascript:void(0)\" onclick=\"changeFontSize('middle');\"\>\<img src=\"../images/sp.gif\" width=\"20\" height=\"20\" alt=\"中\" /\>\</a\>\</li\>");
	document.write("\<li id=\"size-l\"\>\<a href=\"javascript:void(0)\" onclick=\"changeFontSize('large');\"\>\<img src=\"../images/sp.gif\" width=\"20\" height=\"20\" alt=\"大\" /\>\</a\>\</li\>");
	document.write("\</ul\>");
	document.write("\<br class=\"clr\" /\>");
	document.write("\</div\> \<!-- font-size --\>");
}

function changeFontSize(size) {
	switch (size) {
	  case "small":
		document.getElementById("all").style.fontSize = "12px";
		document.getElementById("size-s").style.backgroundPosition = "0px -20px";
		document.getElementById("size-m").style.backgroundPosition = "0px 0px";
		document.getElementById("size-l").style.backgroundPosition = "0px 0px";
		break;
	  case "middle":
		document.getElementById("all").style.fontSize = "14px";
		document.getElementById("size-s").style.backgroundPosition = "0px 0px";
		document.getElementById("size-m").style.backgroundPosition = "0px -20px";
		document.getElementById("size-l").style.backgroundPosition = "0px 0px";
		break;
	  case "large":
		document.getElementById("all").style.fontSize = "16px";
		document.getElementById("size-s").style.backgroundPosition = "0px 0px";
		document.getElementById("size-m").style.backgroundPosition = "0px 0px";
		document.getElementById("size-l").style.backgroundPosition = "0px -20px";
		break;
	}

	// Cookieに書き込み
	document.cookie = "fontsize="+size+"; expires=Tue, 31-Dec-2030 23:59:59; path=/;";

	return"";
}


function getCookie(key) {
	var cookie = document.cookie; //クッキーを取得
	
	if (cookie.indexOf(key) == -1) { //クッキーなし
		return -1;
	}
	else {
		var pos_s = cookie.indexOf(key) + key.length + 1; //クッキーの値の開始位置
		var pos_e = cookie.indexOf(";", pos_s); //クッキーの値の終了位置
		if (pos_e == -1) { //末尾にセミコロンがあるか？
			var size = cookie.slice(pos_s); //無い
		}else{
			var size = cookie.slice(pos_s,pos_e); //有る
		}
	}
	return size;
}

function initialize(locate) {
	var size = getCookie("fontsize");
	switch (size) {
	  case "small":
	  case "middle":
	  case "large":
		// 正常に取得できたため何もしない
		break;
	  default:
		// 取得できなかったため、デフォルト値を設定
		size = "middle";
		break;
	}

	changeFontSize(size);
}


