/* ---------- global variables ---------- */

var SARI_safemode        = 1;
var SARI_safemode_array  = ["img","input"];
var SARI_className       = "SARI";
var mailto_className     = "mailto";
var lightbox_className   = "LB";

/*
SARI_safemode:
1 : searching for nodes with "SARI_className" from the nodes that have tag name in "SARI_safemode_array"
0 : searching for it from all of the nodes. but it's so expensive
you'd better choose "1", unless some special reasons can be found.
*/









/* ---------- start up items ----------
when the event "window.onload" is generated, items of "startup_items" will be executed as a function one after another. 
To execute some scripts when the event "onload" is generated, you should make it a function and push it into the "startup_items".
*/
var startup_items = [];
window.onload = function(){ for(var i=0 ; i<startup_items.length ; i++) startup_items[i](); };
var onresize_items = [];
window.onresize = function(){ for(var i=0 ; i<onresize_items.length ; i++) onresize_items[i](); };










/* ---------- switch css ---------- */
if(!document.layers){
	if(window.location.href.split("/")[3] == "index.php" || window.location.href.split("/")[3] == ""){
		if(browser.nav == "MSIE") document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"hom_css/WinIE.css\" media=\"screen,print\">");
	}
	else{
		if(browser.nav == "MSIE") document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/common/css/WinIE.css\" media=\"screen,print\">");
		if(browser.nav == "MSIE") document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/WinIE.css\" media=\"screen,print\">");
	}
	if(browser.nav == "Safari") document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/common/css/safari.css\" media=\"screen,print\">");
}










/* ---------- common library ---------- */

function puWindow(url,nam,wid,hei,prop){
	var offset = 0;
	var w = window.screen.width;
	var h = window.screen.height;
	var l = (w-wid)/2;
	var t = ((h-hei)/2)-offset;
	sty = prop;
	sty+= ",width=";
	sty+= wid;
	sty+= ",height=";
	sty+= hei;
	sty+= ",left=";
	sty+= l;
	sty+= ",top=";
	sty+= t;
	window.open(url,nam,sty);
}

function popup0(url,nam,wid,hei){
	prop = "status=no,scrollbars=no,resizable=no";
	wid = (wid <= window.screen.width)  ? wid : window.screen.width  * 0.8 ;
	hei = (hei <= window.screen.height) ? hei : window.screen.height * 0.8 ;
	puWindow(url,nam,wid,hei,prop);
}

function popup1(url,nam,wid,hei){
	prop = "status=yes,scrollbars=yes,resizable=yes";
	wid = (wid <= window.screen.width)  ? wid : window.screen.width  * 0.8 ;
	hei = (hei <= window.screen.height) ? hei : window.screen.height * 0.8 ;
	puWindow(url,nam,wid,hei,prop);
}

function fullscreen(url,name) {
	var str = ",top=0,left=0,scroll=no,scrollbars=no,fullscreen=yes,status=no";
	if(navigator.userAgent.toLowerCase().indexOf("safari") != -1){
		str += ",resizable=yes";
	}
	else{
		str += ",resizable=no";
	}
	window.open(url, name, "width="+screen.availWidth+",height="+screen.availHeight+str);
}





function getElementsByTagAndClassName(tag_name,class_name){
	/*
	this is not a method but a function.
	This returns it as Array, when the corresponding elements are discovered.
	This returns false, when the corresponding tag name or class name is not able to be discovered.
	when you do not want to limit the kind of tag, you can use "*" for the 1st argument but it's so expensive.
	*/
	var return_array = [];
	if(!document.getElementsByTagName(tag_name)) return false;
	
	var tmp = document.getElementsByTagName(tag_name);
	for(var i=0 ; i<tmp.length ; i++){
		var class_array = tmp[i].className.split(" ");
		for(var c=0 ; c<class_array.length ; c++){
			if(class_array[c] == class_name){
				return_array[return_array.length] = tmp[i];
			}
		}
	}
	if(return_array.length < 1) return false;
	return return_array;
}









/* ---------- mailto link ---------- */

function mailto2link(){
	/*
	the mail address written in html with A element may be collected by fuckin spamers.
	therefore, we should not write the mail address directly in html. 
	you'd better replace "@" with "&#64;" and use SPAN element with "mailto_className" as class attribute.
	*/
	var tmp = getElementsByTagAndClassName("span",mailto_className);
	for(var i=0 ; i<tmp.length ; i++){
		tmp[i].onclick = function(){
			var address = (function(){
				if(this.childNodes[0].nodeName == "IMG"){
					return this.childNodes[0].alt;
				}
				else{
					return this.childNodes[0].nodeValue;
				}
			})();
			
			window.location.href = "mailto:" + address;
		}
	}
}
startup_items[startup_items.length] = mailto2link;










/* ---------- image rollover ---------- */

function SARI(){
	/*
	when you want to use the rollover effect in html coding, you'd better set "SARI_className" as the class attribute of the corresponding element.
	you have nothing to do to preload swap images :-)
	*/
	
	
	function SR(obj){
		
		var SR = this;
		
		this.obj = obj;
		this.f1 = document.createElement("img");
		this.f2 = document.createElement("img");
		
		var pattern = /_f[12]\./;
		
		this.f1.src = this.obj.src;
		this.f2.src = this.obj.src.replace(pattern,"_f2.");
		
		function setEvent(){
			SR.obj.onmouseover = function(){
				this.src = SR.f2.src;
			}
			SR.obj.onmouseout = function(){
				this.src = SR.f1.src;
			}
		}
		
		
		if(browser.nav == "MSIE" || browser.nav == "Opera"){
			setEvent();
		}
		else{
			this.f2.onload = setEvent;
		}
		
		
	}
	
	// expensive mode
	if(SARI_safemode == 0){
		var tmp = getElementsByTagAndClassName("*",SARI_className);
	}
	// normal mode
	else{
		var tmp = [];
		for(var sm=0 ; sm<SARI_safemode_array.length ; sm++){
			var tmp_safe = getElementsByTagAndClassName(SARI_safemode_array[sm],SARI_className);
			if(tmp_safe != false) tmp = tmp.concat(tmp_safe);
		}
	}
	
	var SR_set = [];
	for(var i=0 ; i<tmp.length ; i++){
		SR_set[i] = new SR(tmp[i]);
	}
}
startup_items[startup_items.length] = SARI;







/* ---------- font size controll ---------- */

function fontSizeControll(){
	
	/*
	this is a controller of Font Size Utility.
	*/
	
	if(!document.getElementById("fontsize")) return false;
	
	function button_preload(){
		var obj = document.getElementById("fontsize").getElementsByTagName("img");
		switch(get_cookie("fontsize")){
			case "small":
				obj[1].src = obj[1].src.replace("_f1","_f2");
				break;
				
			case "large":
				obj[3].src = obj[3].src.replace("_f1","_f2");
				break;
				
			default:
				obj[2].src = obj[2].src.replace("_f1","_f2");
				break;
		}
		
		obj[1].onclick = function(){
			buttonRollover(this);
			size_change(0);
		}
		obj[2].onclick = function(){
			buttonRollover(this);
			size_change(1);
		}
		obj[3].onclick = function(){
			buttonRollover(this);
			size_change(2);
		}
		
		
		function buttonRollover(tmp){
			for(var i = 1 ; i < 4 ; i++){
				obj[i].src = obj[i].src.replace("_f2","_f1");
			}
			tmp.src = tmp.src.replace("_f1","_f2");
		}
	}
	button_preload();
	
	var fontsize;
	days = 1;
	
	function size_change(n){
		
		var size = [];
		size[0] = "small";
		size[1] = "";
		size[2] = "large";
		
		
		if(n == 0){
			fontsize = "small";
			set_cookie("fontsize",fontsize);
		}
		else if(n == 1){
			fontsize = "middle";
			set_cookie("fontsize",fontsize);
		}
		else if(n == 2){
			fontsize = "large";
			set_cookie("fontsize",fontsize);
		}
		
		document.body.className = size[n];
	}
	
	
	function get_cookie(cn) {
		get_data = document.cookie;
		cv = [];
		gd = get_data.split(";");
		for (i in gd) {
			a = gd[i].split("=");
			a[0] = a[0].replace(" ","");
			cv[a[0]] = a[1];
		}
		if (cv[cn]) return cv[cn];
		else return "";
	}
	
	
	function set_cookie(cn,val) {
		ex = new Date();
		ex = new Date(ex.getTime() + (1000 * 60 * 60 * 24 * days));
		y = ex.getYear(); if (y < 1900) y += 1900;
		hms = ex.getHours() + ":" + ex.getMinutes() + ":" + ex.getSeconds();
		p = String(ex).split(" ");
		ex = p[0] + ", " + p[2] + "-" + p[1] + "-" + y + " " + hms + " GMT;";
		document.cookie = cn + "=" + val +"; path=/; expires=" + ex;
	}
	
	
	function set(){
		var get_data = get_cookie("fontsize");
		if(get_data != ""){
			if(get_data == "small"){
				size_change(0);
			}
			else if(get_data == "middle"){
				size_change(1);
			}
			else if(get_data == "large"){
				size_change(2);
			}
		}
	}
	set();
}
startup_items[startup_items.length] = fontSizeControll;




function navi(){
	var string = "";
	string += "<div id=\"nav_g\" class=\"clearfix\">";
	string += "<ul";
	string += "><li><a href=\"/products/\"><img src=\"/common/img/_hea_nav_01_f1.gif\" alt=\"商品を探す\"></a></li";
	string += "><li><a href=\"/solution/\"><img src=\"/common/img/_hea_nav_02_f1.gif\" alt=\"サービスを探す\"></a></li";
	string += "><li><a href=\"/special/\"><img src=\"/common/img/_hea_nav_03_f1.gif\" alt=\"スペシャルコンテンツ\"></a></li";
	string += "><li><a href=\"javascript:openDigicataF(0);\"><img src=\"/common/img/_hea_nav_04_f1.gif\" alt=\"WEB版総合カタログ\"></a></li";
	string += "><li><a href=\"/showroom/\"><img src=\"/common/img/_hea_nav_05_f1.gif\" alt=\"ショールーム・ライブオフィス\"></a></li";
	string += "><li><a href=\"/company/\"><img src=\"/common/img/_hea_nav_06_f1.gif\" alt=\"企業情報\"></a></li";
	string += "><li><a href=\"/information/\"><img src=\"/common/img/_hea_nav_07_f1.gif\" alt=\"お問い合わせ\"></a></li";
	string += "></ul>";
	string += "</div>";
	document.write(string);
	global_navi();
}
function no_navi(){
	var string = "";
	string += "<div id=\"nav_g2\" class=\"clearfix\">";
	string += "</div>";
	document.write(string);
	global_navi();
}




function foot_navi(){
	var string = "";
	string += "<div id=\"foot_navi\">";
	string += "<ul";
	string += "><li><a href=\"/\">HOME</a></li";
	string += "><li><a href=\"/products/\">商品を探す</a></li";
	string += "><li><a href=\"/solution/\">サービスを探す</a></li";
	string += "><li><a href=\"/special/\">スペシャルコンテンツ</a></li";
string += "><li><a href=\"javascript:openDigicataF(0);\">WEB版総合カタログ</a></li";
	string += "><li><a href=\"/showroom/\">ショールーム・ライブオフィス</a></li";
	string += "><li><a href=\"/company/\">企業情報</a></li";
	string += "><li><a href=\"/information/\">お問い合わせ</a></li";
	string += "><li class=\"last-child\"><a href=\"/sitemap/\">サイトマップ</a></li";
	string += "></ul>";
	string += "<p class=\"return-top\"><a href=\"#top\"><img src=\"/common/img/_foo_but_top.gif\" alt=\"先頭へ\"></a></p>";
	string += "<br class=\"clearance\">";
	string += "</div>";
	document.write(string);
}




function fontsize(){
	var string = "";
	string += "	<dl id=\"fontsize\"";
	string += "	><dt><img alt=\"文字のサイズ\" src=\"/common/img/_siz_cap.gif\"></dt";
	string += "	><dd><img alt=\"小\" src=\"/common/img/_siz_but_01_f1.gif\"></dd";
	string += "	><dd><img alt=\"中\" src=\"/common/img/_siz_but_02_f1.gif\"></dd";
	string += "	><dd><img alt=\"大\" src=\"/common/img/_siz_but_03_f1.gif\"></dd";
	string += "	></dl>";
	string += "	<br class=\"clearance\">";
	document.write(string);
}


/* ---------- global navi ---------- */

function global_navi(){

	if(document.getElementById("nav_g") == false) return;
	
	var obj  = document.getElementById("nav_g");
	var ul   = obj.getElementsByTagName("ul");
	var img  = obj.getElementsByTagName("img");
	var path = location.href;
	var directory  = location.href.split(location.hostname)[1];
	
	for(var i = 0 ; i < img.length ; i++){
		var target = img[i];
		
		if(directory == "/products/" && target.parentNode.href && target.parentNode.href.indexOf("/products/office/") != -1){
			target.src = target.src.replace("_f1","_f2");
		}
		
		if(path == target.parentNode.href || path.indexOf(target.parentNode.href) != -1){
			target.src = target.src.replace("_f1","_f2");
		}
		else{
			target.onmouseover = function(){ this.src = this.src.replace("_f1","_f2"); }
			target.onmouseout  = function(){ this.src = this.src.replace("_f2","_f1"); }
		}
	}
	
	for(var i = 1 ; i < ul.length ; i++){
		var target = ul[i];
		var parent_obj = target.parentNode.getElementsByTagName("img")[0];
		if(parent_obj.src.indexOf("_f1") != -1){
			target.style.display = "none";
		}
		else{
			target.style.display = "block";
		}
	}
	
	function SR(obj){
		
		var SR = this;
		
		this.obj = obj;
		this.f1 = document.createElement("img");
		this.f2 = document.createElement("img");
		
		var pattern = /_f[12]\./;
		
		this.f1.src = this.obj.src;
		this.f2.src = this.obj.src.replace(pattern,"_f2.");
		
		function setEvent(){
			SR.obj.onmouseover = function(){
				this.src = SR.f2.src;
			}
			SR.obj.onmouseout = function(){
				this.src = SR.f1.src;
			}
		}
		
		
		if(browser.nav == "MSIE"){
			setEvent();
		}
		else{
			this.f2.onload = setEvent;
		}
		
		
	}
	var SR_set = [];
	for(var i=0 ; i<img.length ; i++){
		SR_set[i] = new SR(img[i]);
	}
}








/* ---------- ssl check ---------- */

function ssl_check(){

	var path = location.href;
	var directory  = location.href.split(location.hostname)[1];
	var obj = document.getElementsByTagName("a");
	var obj_img = document.getElementsByTagName("img");
	//httpsの中に入る「資料請求」と「お問い合わせ」のディレクトリの間で実行
	//aタグ内のhrefに「https」があったら「http」に置換する
	if(directory.indexOf("/request/") != -1 || directory.indexOf("/information/inquiry/") != -1){
		for(var i = 0 ; i < obj.length ; i++){
			if(obj[i].href.indexOf("/request/") == -1){
				obj[i].href = obj[i].href.replace("https:","http:");
			}
		}
		
		//imgのsrcにhttpがあったらhttpsに置換する
		for(var s = 0 ; s < obj_img.length ; s++){
			obj_img[s].src = obj_img[s].src.replace("http:","https:");
		}
	}
	
	//ページ内に「資料請求」と「お問い合わせ」のリンクが記述されているとき「https」で遷移させる
	for(var i = 0 ; i < obj.length ; i++){
		if(obj[i].href.indexOf("/information/inquiry/") != -1 || obj[i].href.indexOf("/request/") != -1){
			obj[i].href = obj[i].href.replace("http:","https:");
		}
	}

}
//startup_items[startup_items.length] = ssl_check;






/* ---------- link check ---------- */

function link_check(){

	var obj = document.getElementsByTagName("a");
	
	for(var i = 0 ; i < obj.length ; i++){
		if(obj[i].href.indexOf("/information/inquiry/") != -1 || obj[i].href.indexOf("/request/") != -1){
			obj[i].target = "_blank";
			obj[i].href = "https://www.kokuyo.co.jp/faq/privacy.html";
		}
	}

}
startup_items[startup_items.length] = link_check;







/* catalog check controll */

function catalog_check(){
	
	if(!getElementsByTagAndClassName("ul","catalog-list")) return false;
	var obj = getElementsByTagAndClassName("ul","catalog-list");
	var catalog_count = 0;
	var catalog_max  = 3;
	
	for(var i = 0 ; i < obj.length ; i++){
		
		var li =  obj[i].getElementsByTagName("li");
		for(s = 0 ; s < li.length ; s++){
			
			var input = li[s].getElementsByTagName("input")[0];
			var thum = input.parentNode.parentNode.getElementsByTagName("img")[0];
			var parent_li = input.parentNode.parentNode;

			input.checked && ++catalog_count; 
			
			input.onclick = function(){
				
				if(this.checked){
					catalog_count += 1;
					this.checked = true;
					num_check();
				}
				else{
					catalog_count -= 1;
					this.checked = false;
					num_check();
				}
				
			}
			thum.input = input;
			thum.onclick = function(){
				if(this.input.checked){
					catalog_count -= 1;
					this.input.checked = false;
					num_check();
				}
				else if(thum.src.indexOf("_f1") != -1 && catalog_count < catalog_max){
					catalog_count += 1;
					this.input.checked = true;
					num_check();
				}
				else{
					return false;
				}
				
			}
			
		}
		
	}
	num_check(); 
	
	function num_check(){
	
		for(var i = 0 ; i < obj.length ; i++){
			
			var li =  obj[i].getElementsByTagName("li");
			for(s = 0 ; s < li.length ; s++){
			
				var input = li[s].getElementsByTagName("input")[0];
				var span  = li[s].getElementsByTagName("span")[0];
				var thum = input.parentNode.parentNode.getElementsByTagName("img")[0];
				var parent_li = input.parentNode.parentNode;
				
				if(input.checked || catalog_count < catalog_max){
					
					input.disabled = false;
					parent_li.style.color = "#666666";
					span.style.background = "#999999";
					thum.src = thum.src.replace("_f2","_f1");
					thum.style.cursor = "pointer";
					
				}
				else if(catalog_count >= catalog_max){
					
					input.disabled = true;
					parent_li.style.color = "#cccccc";
					span.style.background = "#cccccc";
					thum.src = thum.src.replace("_f1","_f2");
					thum.style.cursor = "default";
					
				}
			}
			
		}
	
	}
}
startup_items[startup_items.length] = catalog_check;





/* thumbnail controll */

function thumbnail(){
	if(!getElementsByTagAndClassName("img","thumbnail")) return false;
	var thumb = getElementsByTagAndClassName("img","thumbnail");
	var photo = getElementsByTagAndClassName("img","thumbnail_photo")[0];
	var imgPath = photo.src.split("_f1")[0];
	var extension = photo.src.split("_f1")[1];
	
	for(var i = 0 ; i < thumb.length ; i++){
		thumb[i].i = i + 1;
		
		var image = new Array();
		image[i] = document.createElement("img");
		image[i].src = imgPath + "_f" + i + extension;
		
		thumb[i].onclick = function(){
			
			var photo_num = photo.src.split(".jpg")[0];
			photo_num = photo_num.substr(photo_num.length-1,1);
			photo.src = photo.src.replace("_f" + photo_num,"_f" + this.i);
		}
	}
}
startup_items[startup_items.length] = thumbnail;




/* ---------- MAP SARI ---------- */

function MAP_SARI(){
	if(!getElementsByTagAndClassName("area","MAP_SARI")) return false;
	var obj = getElementsByTagAndClassName("img","MAP_SARI");
	var map = getElementsByTagAndClassName("area","MAP_SARI");
	for(var i = 0 ; i < map.length ; i++){
		var target = map[i];
		target.i = i;
		target.onmouseover = function(){ obj[this.i].src = obj[this.i].src.replace("_f1","_f2"); }
		target.onmouseout  = function(){ obj[this.i].src = obj[this.i].src.replace("_f2","_f1"); }
	}
}
startup_items[startup_items.length] = MAP_SARI;


