/* ---------- global variables ---------- */

var src2name_re          = new RegExp("_f[12]","g");
var image_directory_name = "images/";
var swap                 = new Array();
var rest                 = new Array();
var SARI_className       = "SARI";
var SARI_safemode        = 1;
var SARI_safemode_array  = new Array("img","input");

/*
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 ----------
the item in the Array "startup_items" will be executed in order as a function, when the document is loaded.
when you want to add an attribute "onload" in body tag (and that sucks), you'd better make it a function and push into the Array "startup_items".
*/
var startup_items = new Array();
window.onload = function(){ for(var i=0 ; i<startup_items.length ; i++) startup_items[i](); };





/* ---------- common library ---------- */

function puWindow(url,nam,wid,hei,prop){
	var offset = 100;
	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=yes,scrollbars=no,resizable=yes";
	puWindow(url,nam,wid,hei,prop);
}

function popup1(url,nam,wid,hei){
	prop = "status=yes,scrollbars=yes,resizable=yes";
	puWindow(url,nam,wid,hei,prop);
}

function popup_map(url,nam,wid,hei){
	prop = "status=no,scrollbars=auto,resizable=yes";
	puWindow(url,nam,wid,hei,prop);
}





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 = new 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;
}




function getImageName(obj){
	/*
	this returns a character sequence which deleted the one for rollover effect from the sauce file name.
	this returns false, if "obj" doesn't have a property "src".
	*/
	if(!obj.src) return false;
	return obj.src.split(image_directory_name)[1].split(src2name_re)[0];
}




function SARI2(){
	if(document.layers) return;
	/*
	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 :-)
	*/
	// expensive mode
	if(SARI_safemode == 0){
		var tmp = getElementsByTagAndClassName("*",SARI_className);
	}
	// normal mode
	else{
		var tmp = new Array();
		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);
		}
	}
	
	for(var i=0 ; i<tmp.length ; i++){
		SwapAndRestoreImage(tmp[i]);
	}
}
startup_items[startup_items.length] = SARI2;




function SwapAndRestoreImage(obj){
	/*
	when you want to use the rollover effect in any JavaScript, You'd better use this function.
	you can use any node as the argument.
	if the node is not "IMG" or "INPUT type=image", the 1st Image object of children is recognized as an object of the effect.
	if the sauce file name of an Image object is not a regular form or there is no Image object in children of the node, this will not be executed.
	although you can use this as the onmouseover or onmouseout attribute, i don't reccomend you it.
	*/
	var target;
	if(obj.nodeName == "IMG" || (obj.nodeName == "INPUT" && obj.type == "image")){
		target = obj;
	}
	else{
		if(!obj.getElementsByTagName("img")[0]) return;
		target = obj.getElementsByTagName("img")[0];
	}
	
	if(target.src.indexOf("_f1") == -1 && target.src.indexOf("_f2") == -1) return;
	
	// preload sequence
	var ImageName = getImageName(target);
	swap[ImageName] = document.createElement("img");
	swap[ImageName].src = target.src.replace(src2name_re,"_f2");
	rest[ImageName] = document.createElement("img");
	rest[ImageName].src = target.src;
	
	// rollover sequence
	target.onmouseover = function(){ if(swap[getImageName(this)]) this.src = swap[getImageName(this)].src; }
	target.onmouseout  = function(){ if(rest[getImageName(this)]) this.src = rest[getImageName(this)].src; }
}



/* ---------- new PreLoading Images ---------- */

var PLArray  = new Array();             // pre-loading path
var img_swap = new Array();                // swap image objects
var img_rest = new Array();                // restore image objects

function newPreLoad(){
	for(var i=0 ; i<arguments.length ; i++) PLArray[PLArray.length] = arguments[i];
}
function newPreLoad_exec(){
	for(var i=0 ; i<PLArray.length ; i++){
		var imgNam = PLArray[i].split("images/")[1];
		
		img_swap[imgNam] = new Image();
		img_rest[imgNam] = new Image();
		img_swap[imgNam].src = PLArray[i] + "_f2.gif";
		img_rest[imgNam].src = PLArray[i] + "_f1.gif";
		
	}
}
startup_items[startup_items.length] = newPreLoad_exec;



/* ---------- SwapAndRestoreImage ---------- */

function SARI(obj,act){
	if(!obj.tagName){
		var target = document.images[obj];
	}
	else if(obj.tagName == "A"){
		var target = obj.getElementsByTagName("img")[0];
	}
	else{
		var target = obj;
	}
	target_id = target.src.split("images/")[1].split(".")[0].replace(src2name_re,"");
	if(act == "swap"){
		target.src = img_swap[target_id].src;
	}
	else{
		target.src = img_rest[target_id].src;
	}
}






//window.alert("作業中につきエラーご容赦");

var sn = new Array();
var MyUL = new Array(null,null,null,null,null,null,null);
var subnavi_timer = 0;

var sn_url = new Array();
sn_url[0] = new Array("/product/material/serath.html","/product/material/silica.html","/product/material/a-pax.html","/product/material/silicon.html","/product/material/serisite.html","/product/material/wollastonite.html","/product/material/nannen.html","/product/material/greenbeads.html","/product/material/pattern.html","/product/material/blast.html","/product/material/sn.html","/product/material/isoplast.html","/product/feature/no03.html","/product/material/other.html");
sn_url[1] = new Array("/product/material/serath.html","/product/material/silica.html","/product/material/silicon.html","/processing/index.html");
sn_url[2] = new Array("/product/material/wollastonite.html","/product/material/silica.html","/product/material/nannen.html","/product/material/serath.html");
sn_url[3] = new Array("/product/material/serath.html","/product/material/serisite.html","/processing/index.html","/processing/index.html");
sn_url[4] = new Array("/product/material/a-pax.html","/product/material/wollastonite.html","/product/material/silicon.html","/product/material/serath.html","/product/material/other.html#Cr2O3","/product/material/other.html#MnO2","/product/material/other.html#B2O3","/product/material/other.html#Li2O","/product/material/other.html#TiO2","/processing/index.html");
sn_url[5] = new Array("/product/material/blast.html","/product/material/greenbeads.html","/product/material/pattern.html","/product/material/isoplast.html","/product/material/sn.html","/product/feature/no03.html");
sn_url[6] = new Array();

var sn_text = new Array();
sn_text[0] = new Array("セラフ","シリカ","珪酸ジルコニウム","金属シリコン","セリサイト","ウォラストナイト","難燃剤","グリーンビーズ","パターンモールド","ブラスト材","スライディングノズル用充填材","ISOPLAST・TIXOBOND","STC式ブロワ式ブラスト","その他製品");
sn_text[1] = new Array("セラフ","シリカ","金属シリコン","精密加工全般");
sn_text[2] = new Array("ウォラストナイト","シリカ","難燃剤","セラフ");
sn_text[3] = new Array("セラフ","セリサイト","各種表面処理の受託加工","顔料の粉砕・分級の受託加工");
sn_text[4] = new Array("珪酸ジルコニウム","ウォラストナイト","金属シリコン","セラフ","クロームフラワー","二酸化マンガン","コレマナイト","ペタライト","ルチールフラワー","分級・粉砕加工");
sn_text[5] = new Array("ブラスト材","グリーンビーズ","パターンモールド","ISOPLAST・TIXOBOND","スライディングノズル用充填材","STC式ブロワ式ブラスト");
sn_text[6] = new Array();

function subnavi_item(t,i){
	var src_string = "/images/_sub_nav_but_" + t + "_" + i;
	this.li      = document.createElement("li");
	this.a       = document.createElement("a");
	this.a.href  = sn_url[t][i-1];
	this.img     = document.createElement("img");
	this.img.alt = sn_text[t][i-1];
	
	if(window.location.href.indexOf(sn_url[t][i-1]) != -1){
		src_string += "_f2.gif";
		this.img.src = src_string;
		this.li.appendChild(this.img);
	}
	else{
		src_string += "_f1.gif";
		this.img.src = src_string;
		this.a.appendChild(this.img);
		this.li.appendChild(this.a);
		SwapAndRestoreImage(this.img);
	}
	
	return this;
}

function subnavi(){
	if(document.layers) return;
	if(!getElementsByTagAndClassName("td","left-navi")) return;

	// creating navi elements sequence
	for(var t=0 ; t<sn_text.length ; t++){
		MyUL[t] = document.createElement("ul");
		MyUL[t].className = "subnavi";
		MyUL[t].onmouseover = function(){ clearTimeout(subnavi_timer); }
		MyUL[t].onmouseout  = function(){ hideSubnavi(this); }
		
		for(var i=1 ; i<=sn_text[t].length ; i++){
			var c = sn.length;
			sn[c] = new subnavi_item(t,i);
			MyUL[t].appendChild(sn[c].li);
		}
		document.getElementsByTagName("body")[0].appendChild(MyUL[t]);
	}
	
	// setting event-handler sequence
	var tmp = getElementsByTagAndClassName("td","left-navi")[0].getElementsByTagName("img");
	for(var i=0 ; i<tmp.length ; i++){
		tmp[i].id = "parent-" + i;
		tmp[i].onmouseover = function(){
			var num = this.id.split("-")[1];
			if(MyUL[num] != null) var target = MyUL[num];
			
			if(nav == "Moz5"){
				var x = (document.body.clientWidth - 639) / 2 + 157;
				var y = this.offsetTop + 116;
				target.style.left = x + "px";
				target.style.top  = y + "px";
			}
			else{
				var x = (document.body.clientWidth - 639) / 2 + 157;
				if(nav == "MSIE" && ver < 6) x -= 16;
				var y = this.offsetTop + 116;
				target.style.pixelLeft = x;
				target.style.pixelTop  = y;
			}
			
			showSubnavi(target);
		}
		tmp[i].onmouseout  = function(){
			var num = this.id.split("-")[1];
			if(MyUL[num] != null) var target = MyUL[num];
			hideSubnavi(target);
		}
	}
}
if(window.location.href.indexOf("/product/") != -1){
	startup_items[startup_items.length] = subnavi;
}



function showSubnavi(obj){
	clearTimeout(subnavi_timer);
	for(var i=0 ; i<MyUL.length ; i++){
		if(MyUL[i] != obj){
			MyUL[i].style.display = "none";
		}
		obj.style.display = "block";
	}
}

var toHide_obj;
function hideSubnavi(obj){
	toHide_obj = obj;
	var exec_code = "hideSubnavi_exec();";
	subnavi_timer = setTimeout(exec_code,500);
}


function hideSubnavi_exec(){
	toHide_obj.style.display = "none";
}




