window.onload = function () {
	btnOverEvent();
	if(document.getElementById('home')){
		setEvent(document.getElementById('formTitle2'), 'onclick', formChanger(1));
	}
}

/*============================================================**

	setEvent(obj,eventType,func)
	
	obj:		オブジェクトを指定。
	eventType:	onclickやonfocusなど
	func:		onclickやonfocusなどのイベント時に呼び出すfunction

**============================================================*/

var isIE = (document.documentElement.getAttribute("style") == document.documentElement.style);

function setEvent(obj,eventType,func){
	if(isIE) {
		obj.setAttribute(eventType,new Function(func));
	} else {
		obj.setAttribute(eventType,func);
	}
}


/*============================================================**

	btnOverEvent()
	
	onload時に呼び出してください。
	ページ内の画像をすべて調べ、
	ファイル名の末尾が「_0」の画像で、
	尚且つ親要素がアンカーの場合にロールオーバー処理が付加されます。
	また、マウスオーバー時の画像はファイル名末尾が「_1」の画像が呼び出されます。

**============================================================*/

var preloadImg = new Object();
var preNum = 0;

function btnOverEvent(){
	var images = document.getElementsByTagName('img');
	for(var i = 0,num = images.length;i < num; i++){
		var img = images[i];
		var imgSrc = img.src;
		if(imgSrc.indexOf('_0.') > -1 && img.parentNode.href){
			var newImgSrc = imgSrc.split('_0.')[0] + '_1.' + imgSrc.split('_0.')[1];

			preloadImg[preNum] = new Image();
			preloadImg[preNum].src = newImgSrc;

			var funcOnImg = 'changeImg(this,\'' + newImgSrc + '\')\;return false\;';
			var funcOutImg = 'changeImg(this,\'' + imgSrc + '\')\;return false\;';
			
			if(!(img.parentNode.onclick && String(img.parentNode.onclick).indexOf('fontChangeScript.buttonAction') >= 0)){	//フォントサイズ変更ボタン以外に適用
				setEvent(img.parentNode,'onmouseover',funcOnImg);
				setEvent(img.parentNode,'onmouseout',funcOutImg);
			}
			preNum++;
		}
	}
}

function changeImg(eventAnchor,imgSrc){
	eventAnchor.getElementsByTagName('img')[0].src = imgSrc;
}

/*============================================================**
	/js/common.jsを格納するディレクトリを取得
**============================================================*/

var head = document.getElementsByTagName('head')[0];
var scripts = head.getElementsByTagName('script');
for(var s = 0,num = scripts.length; s < num; s++){
	if(scripts[s].src.indexOf('common.js')){
		var dirPath = scripts[s].src.split('js/common.js')[0];
	}
}
//alert (dirPath);

/*============================================================**
	フォントサイズ変更用スクリプト
**============================================================*/

var fontChangeScript = {
	sizeSet: {	
		'fontSmall'		:	'80%',
		'fontMidium'	:	'90%',
		'fontLarge'		:	'110%'
	},
	makeButton: function(){
		this.dd1 = document.createElement('dd');
		this.dd2 = document.createElement('dd');
		this.dd3 = document.createElement('dd');
		if(this.setSize){
			if(this.setSize == this.sizeSet.fontSmall){
				this.dda1 = '<a href="#" onclick="fontChangeScript.buttonAction(\'' + this.sizeSet.fontSmall + '\',this);return false;"><img src="' + dirPath + 'images/btn_small_1.gif" alt="\u5C0F" width="27" height="24" /></a>'
			}else{
				this.dda1 = '<a href="#" onclick="fontChangeScript.buttonAction(\'' + this.sizeSet.fontSmall + '\',this);return false;" onmouseover="fontChangeScript.changeImg(this);" onmouseout="fontChangeScript.changeImg(this);"><img src="' + dirPath + 'images/btn_small_0.gif" alt="\u5C0F" width="27" height="24" /></a>'
			}
			if(this.setSize == this.sizeSet.fontMidium){
				this.dda2 = '<a href="#" onclick="fontChangeScript.buttonAction(\'' + this.sizeSet.fontMidium + '\',this);return false;"><img src="' + dirPath + 'images/btn_mid_1.gif" alt="\u4E2D" width="27" height="24" /></a>'
			}else{
				this.dda2 = '<a href="#" onclick="fontChangeScript.buttonAction(\'' + this.sizeSet.fontMidium + '\',this);return false;" onmouseover="fontChangeScript.changeImg(this);" onmouseout="fontChangeScript.changeImg(this);"><img src="' + dirPath + 'images/btn_mid_0.gif" alt="\u4E2D" width="27" height="24" /></a>'
			}
			if(this.setSize == this.sizeSet.fontLarge){
				this.dda3 = '<a href="#" onclick="fontChangeScript.buttonAction(\'' + this.sizeSet.fontLarge + '\',this);return false;"><img src="' + dirPath + 'images/btn_big_1.gif" alt="\u5927" width="27" height="24" /></a>'
			}else{
				this.dda3 = '<a href="#" onclick="fontChangeScript.buttonAction(\'' + this.sizeSet.fontLarge + '\',this);return false;" onmouseover="fontChangeScript.changeImg(this);" onmouseout="fontChangeScript.changeImg(this);"><img src="' + dirPath + 'images/btn_big_0.gif" alt="\u5927" width="27" height="24" /></a>'
			}
		}else{
			this.dda1 = '<a href="#" onclick="fontChangeScript.buttonAction(\'' + this.sizeSet.fontSmall + '\',this);return false;" onmouseover="fontChangeScript.changeImg(this);" onmouseout="fontChangeScript.changeImg(this);"><img src="' + dirPath + 'images/btn_small_0.gif" alt="\u5C0F" width="27" height="24" /></a>'
			this.dda2 = '<a href="#" onclick="fontChangeScript.buttonAction(\'' + this.sizeSet.fontMidium + '\',this);return false;"><img src="' + dirPath + 'images/btn_mid_1.gif" alt="\u4E2D" width="27" height="24" /></a>'
			this.dda3 = '<a href="#" onclick="fontChangeScript.buttonAction(\'' + this.sizeSet.fontLarge + '\',this);return false;" onmouseover="fontChangeScript.changeImg(this);" onmouseout="fontChangeScript.changeImg(this);"><img src="' + dirPath + 'images/btn_big_0.gif" alt="\u5927" width="27" height="24" /></a>'
		}
		this.dd1.innerHTML = this.dda1;
		this.dd2.innerHTML = this.dda2;
		this.dd3.innerHTML = this.dda3;

		this.indd = document.getElementById('viewSelect').getElementsByTagName('dd')[0];

//alert(this.dd1.innerHTML);

		document.getElementById('viewSelect').insertBefore(this.dd1,this.indd);
		document.getElementById('viewSelect').insertBefore(this.dd2,this.indd);
		document.getElementById('viewSelect').insertBefore(this.dd3,this.indd);
		
		
//		this.changeItem = '<dl id="viewSelect">' +
//		this.changeItem = 
//			'<dt><img src="' + dirPath + 'images/img_mode.gif" width="67" height="24" alt="\u6587\u5B57\u306E\u5927\u304D\u3055" /></dt>' +
//			this.dd1 + this.dd2 + this.dd3 +
//			this.dd1 + this.dd2 + this.dd3;
//			'\n';
//			'</dl>\n';
//		document.write(this.changeItem);

	},
	buttonAction: function(size,bAnchor){
		this.body = document.getElementsByTagName('body')[0];
		if(this.body){
			this.body.style.fontSize = size;
		}
		this.buttons = bAnchor.parentNode.parentNode.getElementsByTagName('img');
		
		this.btnEvent = 'fontChangeScript.changeImg(this);';
		
		for(var i = 1; i < 4; i++){
			this.buttons[i].src = this.buttons[i].src.replace('_1.gif','_0.gif');
			setEvent(this.buttons[i].parentNode,'onmouseover',this.btnEvent);
			setEvent(this.buttons[i].parentNode,'onmouseout',this.btnEvent);
		}
		bAnchor.getElementsByTagName('img')[0].src = bAnchor.getElementsByTagName('img')[0].src.replace('_0.gif','_1.gif');
		
		this.deadOverEvent = 'return false;';
		setEvent(bAnchor,'onmouseover',this.deadOverEvent);
		setEvent(bAnchor,'onmouseout',this.deadOverEvent);
		
		this.fontDate = new Date();
		this.fontDate.setTime(this.fontDate.getTime() + (7*24*60*60*1000));
		document.cookie = 'font-size=' + size + '; path=/; expires=' + this.fontDate.toGMTString();
	},
	checkCookie: function(){
		if(document.cookie.indexOf('font-size=') >= 0){
			this.cookieAll = document.cookie + ';'
			this.cookieF = this.cookieAll.indexOf('font-size=');
			this.cookieE = this.cookieAll.indexOf(';',this.cookieF);
			this.fontSizeData = this.cookieAll.substring(this.cookieF,this.cookieE);
			this.setSize = this.fontSizeData.split('=')[1];
			document.write('<style type="text/css">body {font-size: ' + this.setSize + ';}</style>');
		}
	},
	changeImg: function(eventAnchor){
		this.targetImg = eventAnchor.getElementsByTagName('img')[0];
		if(this.targetImg.src.indexOf('_0.gif') >= 0){
			this.targetImg.src = this.targetImg.src.replace('_0.gif','_1.gif');
		}else if(this.targetImg.src.indexOf('_1.gif') >= 0){
			this.targetImg.src = this.targetImg.src.replace('_1.gif','_0.gif');
		}
	}
}

/*============================================================**
	クッキーチェック
**============================================================*/

fontChangeScript.checkCookie();


/*============================================================**
	トップページのフォームのセッティング
**============================================================*/

function formChanger(formNum) {
	var formNumber = formNum;
	if (formNumber == 1) {
		document.getElementById('formItem1').style.display = "none";
		document.getElementById('formItem2').style.display = "block";
		document.getElementById('formTitle1').innerHTML = '<a href="javascript:void(0);" onclick="formChanger(2)"><img src="/common/images/top_tab_btn1_0.gif" alt="講座・イベント・展覧会を探したい" width="316" height="30" /></a>';
		document.getElementById('formTitle2').innerHTML = '<img src="/common/images/top_tab_btn2_1.gif" alt="部屋を探したい" width="287" height="30" />';
	} else {
		document.getElementById('formItem2').style.display = "none";
		document.getElementById('formItem1').style.display = "block";
		document.getElementById('formTitle2').innerHTML = '<a href="javascript:void(0);" onclick="formChanger(1)"><img src="/common/images/top_tab_btn2_0.gif" alt="部屋を探したい" width="287" height="30" /></a>';
		document.getElementById('formTitle1').innerHTML = '<img src="/common/images/top_tab_btn1_1.gif" alt="講座・イベント・展覧会を探したい" width="316" height="30" />';
	}
}