﻿/*////// set Variable //////////////////////////////////////////////////////////////////////////*/

var navi_openImg = "/shared/templates/free/images/navi/lnavi_icon_open.gif";
var navi_closeImg = "/shared/templates/free/images/navi/lnavi_icon_close.gif"
var navi_openAlt = "メニューを開きます。";
var navi_closeAlt= "メニューを閉じます。";

/*////// shared Variable ///////////////////////////////////////////////////////////////////////*/

var textCookieName = "HikariText";

//GetElementClass
var getElementsByClassName = function(className, pElement){
	var d = document, nodes = [];
	if(d.getElementsByClassName){
		nodes = (pElement||d).getElementsByClassName(className);
		return nodes.length > 0 ? nodes : null;
	}else{
		var cls, item;
		var items = (pElement || d).getElementsByTagName("*");
		for(var i = 0, l = items.length; i < l; i++){
			item = items[i];
			if(item.className){
				cls = item.className.split(/\s+/);
				for(var k = 0, kl = cls.length; k < kl; k++){
					if(cls[k]==className){
						nodes[nodes.length] = item; break;
					}
				}
			}
		}
		return nodes.length > 0 ? nodes : null;
	}
}
// cookie取得
function getCookie(name){
	if(!name) return;
	var val = "";
	var cookieName = name+"=";
	var tmpCookie = document.cookie + ";";
	var start = tmpCookie.indexOf(cookieName);
	if (start != -1) {
		var end = tmpCookie.indexOf(";", start);
		val = tmpCookie.substring(start + cookieName.length, end);
	}
	return val;
}

// cookie保存
function setCookie(name,value){
	if(!name || !value) return;
	var cookieName = name+"=";
	var exp = new Date();
	exp.setTime(exp.getTime() + 31536000000);
	document.cookie = cookieName + value + "; path=/" + "; expires=" + exp.toGMTString();
}

/*////// optional function /////////////////////////////////////////////////////////////////////*/
// 文字拡大
function textSizeMaximum(){
	var currentSize = "130%";
	document.body.style.fontSize = currentSize;
	setCookie(textCookieName,currentSize);
}


//配列判定
var isArray = function(obj){
	var flg;
	if(obj instanceof Array){
		flg = true;	
	}else{
		!flg;
	}
	return flg;
};
//プリロード
var preLoadImages = function(){
	for(var i = 0; i < arguments.length; i++){
		if(isArray(arguments[i])){
			var preImages = new Array(arguments[i].length);
			for(var j = 0; j < arguments[i].length; j++){
				preImages[j] = new Image();
				preImages[j].src = arguments[i][j];
			}
		}else{
			var preImages = new Image();
			preImages.src = arguments[i];
		}	
	}	
};


//ナビゲーション
function naviFun(){
	var thisPath = new String(window.location.pathname);
	var thisPro = new String(window.location.protocol);
	var thishost = new String(window.location.host);
	var thisDomain = thisPro + "\/\/" + thishost;
	var swMenuObj = getElementsByClassName("swicth_menu");
	var hideObj = getElementsByClassName("area_hidden");
	var swObj = getElementsByClassName("switch");
//
	var preImg = new Image();
	preImg.src = navi_closeImg;
	
	defMenuFun();
	setPropertyFun(swMenuObj);
//
	function flgFun(menuName,num,num2,flg){
		if(menuName == swMenuObj){
			openSwitch(num,num2,flg);
		}else if(menuName == tabMenuObj){	
			openTab(num,num2,flg);
		}
	}
//
	function defMenuFun() {
		if (hideObj){
			for (var i=0;i<hideObj.length;i++){
				hideObj[i].style.display = "none";
			}
		}
		if (swObj){
			for (var i=0;i<swObj.length;i++){
				swObj[i].thisFlg = false;
			}
		}
	}
//
	function setPropertyFun(menuName){
		if(menuName){
			for(var i = 0; i < menuName.length; i++){
				menuName[i].list = getElementsByClassName("switch",menuName[i]);
				menuName[i].cont = getElementsByClassName("area_hidden",menuName[i]);
				for(var j = 0; j < menuName[i].list.length; j++){
					menuName[i].list[j].num = i;
					menuName[i].list[j].num2 = j;
					menuName[i].list[j].onclick = function(){flgFun(menuName,this.num,this.num2,this.thisFlg);}
				}	
			}
		}		
	}
//
	function openSwitch(num,num2,flg){
		var thisList = swMenuObj[num].list[num2];
		var thisCont = swMenuObj[num].cont[num2];
		var thisListImg = swMenuObj[num].list[num2].getElementsByTagName("img");
		if(!flg){
			thisCont.style.display = "block";
			if(thisListImg.length){
				for(var j = 0; j < thisListImg.length; j++){
					thisListImg[j].src = thisListImg[j].src.replace("_open","_close");
					thisListImg[j].alt = navi_closeAlt;
				}
			}
		}else{
			thisCont.style.display = "none";
			if(thisListImg.length){
				for(var j = 0; j < thisListImg.length; j++){
					thisListImg[j].src = thisListImg[j].src.replace("_close","_open");
					thisListImg[j].alt = navi_openAlt;
				}
			}
		}
		thisList.thisFlg = !flg;
	}
//
}

// smartRollover
function smartOver() {
	var strArray = new Array("tmp_gnavi");
	var roArea;
	for(var i=0;i<strArray.length;i++) {
		roArea = document.getElementById(strArray[i]);
		if(!roArea) continue;
		smartRollover(strArray[i]);
	}
}
function smartRollover(idName) {
	if(document.getElementsByTagName) {
		var images = document.getElementById(idName).getElementsByTagName("img");
		for(var i=0; i < images.length; i++) {
			if(images[i].getAttribute("src").match("_off.")) {
				var fileName = new Array(images[i].getAttribute("src").replace("_off.", "_on."));
				var preImages = new Array();
				for (var j=0; j<fileName.length; j++) {
					preImages[j] = new Image();
					preImages[j].src = fileName[j];
				}
				images[i].onmouseover = function() {
					this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));
				}
				images[i].onmouseout = function() {
					this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
				}
			}
		}
	}
}
// ウィンドウ幅を取得して表示幅を設定
function wrapperWidth(){
	//
	var maxWidth = 1280;
	var maxSetWidth = '1280px';
	var minWidth = 760;
	var minSetWidth = '760px';
	var defaultSetWidth = '100%';
	//
	var wid = document.body.clientWidth;
	var obj = document.getElementById('tmp_wrapper');	
	//
	if(!checkBrowser()) return;
	if(!wid || !obj) return;
	if(wid > maxWidth) {
		obj.style.width = maxSetWidth;
	} else if(wid < minWidth) {
		obj.style.width = minSetWidth;
	} else {
		obj.style.width = defaultSetWidth;
	}
}

// IE6.0の場合実行
function checkBrowser(){
	var uName = navigator.userAgent;
	if (uName.indexOf("MSIE 6.0") > -1){
		return true;
	}
	return false;
}
// フォーカスで文字を消去
function searchText(){
	if(!document.getElementById("tmp_query")) return;
	var obj = document.getElementById("tmp_query");
	obj.onfocus = function() {
		if(this.value == 'キーワードを入力') {
			this.value = '';
		}
	}
	obj.onblur=function() {
		if(this.value == '') {
			this.value = 'キーワードを入力';
		}
	}
}

/*////// event ////////////////////////////////////////////////////////////////////////////*/

// onload
window.onload = function() {
	naviFun();
	smartOver();
	wrapperWidth();
	searchText();
	loadCookie();
}

// onresize
window.onresize = function() {
	wrapperWidth();
}
// cookieの値でonload時に実行
function loadCookie(){
	var thisFont = getCookie(textCookieName);
	document.body.style.fontSize = thisFont;
}

