//100226メモリ不足のＰＣ上のＩＥ６以下対策
if (typeof document.documentElement.style.maxHeight != "undefined") {
// IE 7.0 以上 または Gecko などモダンブラウザー
;
}
else {
// IE 6.0 以下
document.write("<script type='text/javascript' src='http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js'><\/script>");

}



//文字サイズ変更追加スクリプト

//クッキー発行------------------

function setCookie(name, value, domain, path, expires, secure) {

	var str = name + "=" + escape(value);
	domain = document.domain.replace(/^[^\.]*/, "");
	str += "; domain=" + domain;

	path = "/";
	str += "; path=" + path;

	expires = new Date();
	expires.setTime(expires.getTime() + (30 * 1000 * 60 * 60 * 24));
	expires2 = expires.toGMTString();
	str += "; expires=" + expires2;

	if(secure && location.protocol == "https:") {
		str += "; secure";
	}

	str += ";";
	document.cookie = str;

}

//クッキー取得------------------
function getCookie(name) {

	var cookies = document.cookie.split(';');
	for (var i=0; i<cookies.length; i++) {
		var cookieList = cookies[i].split('=');
		var cookieName = cookieList[0];
		var cookieVal = cookieList[1];
		if (cookieName.indexOf(name,0)!=-1) {
			return unescape(cookieVal);
		}
	}
	return null;

}

function checkCookie() {

	if (window.navigator.cookieEnabled) {
		return true;
	}	else {
		return false;
	}

}


//切り替え------------------
function fontSizeSwitch(n) {

	document.getElementById('btn').innerHTML = "";

	if(n == "l") {
		applyCSS(document,"http://www.nihongokentei.jp/common_scripts/font-l.css");
	} else if(n == "m") {
		applyCSS(document,"http://www.nihongokentei.jp/common_scripts/font-m.css");
	} else if(n == "s"){
		applyCSS(document,"http://www.nihongokentei.jp/common_scripts/font-s.css");
	}

	//ボタン生成
	if(document.getElementById('fontsizeArea')){
		btnCreate(n);
	}

	//クッキー発行
	setCookie('fontsize', n);

}


//ボタン生成------------------

function btnSet(n) {

	//linkエレメント生成
	var id = 'fontsizeArea';
	var target = document.getElementById(id);
	var container = document.createElement('div');
	container.setAttribute('id', 'btn');
	target.insertBefore(container, target.firstChild);
	var btn = document.getElementById('btn');

	var ua=navigator.userAgent;
	if(ua.indexOf("MSIE 6")>-1){
		document.getElementById("btn").style.height="28px";
	}else{
		document.getElementById("btn").style.minHeight="28px";
	}
}


function btnCreate(n) {//alert(n);

	var btn = document.getElementById('btn');
	var html = '';
	if(n == "l"){
		html += '<div class="rft"><img src="http://www.nihongokentei.jp/common_images/globalparts/fontsize.gif" class="fontbx" alt="文字サイズの変更" />';
		html += '<a href="javascript:fontSizeSwitch(\'l\');" title="大" tabindex="200">';
		html += '<img src="http://www.nihongokentei.jp/common_images/globalparts/spacer.gif" border="0" class="size_l_on" width="22" height="16"/></a>';
		html += '<a href="javascript:fontSizeSwitch(\'m\');" title="中" tabindex="200">';
		html += '<img src="http://www.nihongokentei.jp/common_images/globalparts/spacer.gif" border="0"  class="size_m_off" width="22" height="16"/></a>';
		html += '<a href="javascript:fontSizeSwitch(\'s\');" title="小" tabindex="200">';
		html += '<img src="http://www.nihongokentei.jp/common_images/globalparts/spacer.gif" border="0"  class="size_s_off" width="22" height="16"/></a></div>';
	}
	if(n == "m"){
		html += '<div class="rft"><img src="http://www.nihongokentei.jp/common_images/globalparts/fontsize.gif" class="fontbx" alt="文字サイズの変更" />';
		html += '<a href="javascript:fontSizeSwitch(\'l\');" title="大" tabindex="200">';
		html += '<img src="http://www.nihongokentei.jp/common_images/globalparts/spacer.gif" border="0" class="size_l_off" width="22" height="16"/></a>';
		html += '<a href="javascript:fontSizeSwitch(\'m\');" title="中" tabindex="200">';
		html += '<img src="http://www.nihongokentei.jp/common_images/globalparts/spacer.gif" border="0"  class="size_m_on" width="22" height="16"/></a>';
		html += '<a href="javascript:fontSizeSwitch(\'s\');" title="小" tabindex="200">';
		html += '<img src="http://www.nihongokentei.jp/common_images/globalparts/spacer.gif" border="0"  class="size_s_off" width="22" height="16"/></a></div>';
	}
	if(n == "s"){
		html += '<div class="rft"><img src="http://www.nihongokentei.jp/common_images/globalparts/fontsize.gif" class="fontbx" alt="文字サイズの変更" />';
		html += '<a href="javascript:fontSizeSwitch(\'l\');" title="大" tabindex="200">';
		html += '<img src="http://www.nihongokentei.jp/common_images/globalparts/spacer.gif" border="0" class="size_l_off" width="22" height="16"/></a>';
		html += '<a href="javascript:fontSizeSwitch(\'m\');" title="中" tabindex="200">';
		html += '<img src="http://www.nihongokentei.jp/common_images/globalparts/spacer.gif" border="0"  class="size_m_off" width="22" height="16"/></a>';
		html += '<a href="javascript:fontSizeSwitch(\'s\');" title="小" tabindex="200">';
		html += '<img src="http://www.nihongokentei.jp/common_images/globalparts/spacer.gif" border="0"  class="size_s_on" width="22" height="16"/></a></div>';
	}
	btn.innerHTML = html;
}


function fontSizeInit() {
	//クッキー取得
	var n = getCookie('fontsize');
	if (n == null) {	
			n = 's';
	}

	if(n == "l") {
		applyCSS(document,"http://www.nihongokentei.jp/common_scripts/font-l.css");
	} else if(n == "m") {
		applyCSS(document,"http://www.nihongokentei.jp/common_scripts/font-m.css");
	} else if(n == "s"){
		applyCSS(document,"http://www.nihongokentei.jp/common_scripts/font-s.css");
	}

	if(document.getElementById('fontsizeArea')){
		//ボタン生成
		btnSet(n);
		btnCreate(n);
	}
	//
}

function fontSizeInitBase() {

	//クッキー取得
	var n = getCookie('fontsize');
	if (n == null) {
			n = 's';	
	}

	if(n == "l") {
		applyCSS(document,"http://www.nihongokentei.jp/common_scripts/font-l.css");
	} else if(n == "m") {
		applyCSS(document,"http://www.nihongokentei.jp/common_scripts/font-m.css");
	} else if(n == "s"){
		applyCSS(document,"http://www.nihongokentei.jp/common_scripts/font-s.css");
	}
}

function applyCSS(doc, cssfile, overwrite){

	if (typeof overwrite == "undefined") overwrite = 0;      // デフォルト:CSSの切替え

	// 既存のCSSを削除
	if (overwrite && doc.styleSheets.length >= 1) {
		var links = doc.getElementsByTagName("link");
		for (var i = 0 ; i < links.length ; i++) {
			if (links[i].rel == "stylesheet") links[i].parentNode.removeChild(links[i]);
		}
	}

	// CSS追加
	if (document.all) {
		doc.createStyleSheet(cssfile);
	} else {
		var link = doc.createElement("link");
		link.setAttribute("rel", "stylesheet");
		link.setAttribute("href", cssfile);
		link.setAttribute("type", "text/css");
		doc.getElementsByTagName('head')[0].appendChild(link);
	}
}

if (navigator.userAgent.indexOf('Firefox') >= 0) {
	document.addEventListener("DOMContentLoaded", fontSizeInitBase, false);
} else {
	window.onload = fontSizeInitBase;
}
