﻿// グローバル変数初期化
var contextReady = false;
var ns6=document.getElementById&&!document.all?1:0;
var viewFlag;

var imageBuffer;	//変更されたボタンの元画像URL
var current;	//イベント発生元オブジェクト
var timerID;

// _dom : kind of DOM.
//        IE4 = 1, IE5+ = 2, NN4 = 3, NN6+ = 4, others = 0
var _dom = document.all?(document.getElementById?2:1):
(document.getElementById?4:
(document.layers?3:0));


// コンテキストの作成
function makeContext() {
	var callButtons = document.getElementsByTagName("input");

	//イベントハンドラの設定
	for (var i = 0; i < callButtons.length; i++) {
		var callButton = callButtons[i];
		if (callButton.getAttribute("rel") == "popup01") {
			callButton.onclick = popen;
		}
		else if (callButton.getAttribute("rel") == "popup02") {
			callButton.onclick = popen;
		}
		else if (callButton.getAttribute("rel") == "popup03") {
			callButton.onclick = popen;
		}
		else if (callButton.getAttribute("rel") == "popup04") {
			callButton.onclick = popen;
		}
		else if (callButton.getAttribute("rel") == "popup05") {
			callButton.onclick = popen;
		}
		else if (callButton.getAttribute("rel") == "popup06") {
			callButton.onclick = popen;
		}
		else if (callButton.getAttribute("rel") == "popup07") {
			callButton.onclick = popen;
		}
		else if (callButton.getAttribute("rel") == "popup08") {
			callButton.onclick = popen;
		}
		else if (callButton.getAttribute("rel") == "popup09") {
			callButton.onclick = popen;
		}
		else if (callButton.getAttribute("rel") == "popup10") {
			callButton.onmouseup = popen;
		}
		else if (callButton.getAttribute("rel") == "popup11") {
			callButton.onmouseup = popen;
		}
		else if (callButton.getAttribute("rel") == "popup12") {
			callButton.onclick = popen;
		}
		else if (callButton.getAttribute("rel") == "popup01_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup02_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup03_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup04_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup05_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup06_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup07_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup08_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup09_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup10_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup11_close") {
			callButton.onclick = hidePop;
		}
		else if (callButton.getAttribute("rel") == "popup12_close") {
			callButton.onmouseup=hidePop;
		}
	}

	//オーバーレイ背景準備
	setTranslucent ();

	//準備完了
	contextReady = true;
}

//オーバーレイ背景準備
function setTranslucent () {
	var objBody = document.getElementsByTagName("body").item(0);

	var objTranslucent = document.createElement("div");
	objTranslucent.setAttribute('id','translucent');
	objTranslucent.style.display = 'none';
	objTranslucent.style.position = 'absolute';
	objTranslucent.style.top = '0';
	objTranslucent.style.left = '0';
 	objTranslucent.style.width = '100%';
	objBody.insertBefore(objTranslucent, objBody.firstChild);
}

//イベント
function popen (elm) {
//debug.print("this:"+this.tagName);
//debug.print("elm"+elm);
	if (imageBuffer) {
		current.src = imageBuffer;
	}
	if(!this.tagName) current=elm;
	else current = this;
//debug.print("curr:"+current.src)
//debug.print("buff;"+imageBuffer)
	var buttonTop = getDivTop(current);
	var buttonLeft = getDivLeft(current);
//debug.print( getDivTop(current) + "," + getDivLeft(current));
	buttonRel = current.getAttribute("rel");
//debug.print(buttonRel);
	imageBuffer = current.src;

	//ボタン代替画像のURL生成
	var urlLength = current.src.length - 4;
	var urlBuffer = current.src.substr(0, urlLength);
	var urlExtension = current.src.substr(urlLength + 1, urlLength + 4);
//debug.print(urlBuffer + '_on.' + urlExtension)
	current.src = urlBuffer + '_on.' + urlExtension;

	clearPop();

	var arrayPageSize = getPageSize();

	var scrollHeight = getScrollHeight();
	var scrollWidth = getScrollWidth();
	var windowHeight = arrayPageSize[3];
	var windowWidth = arrayPageSize[2];


	//本体情報取得
	var showContext = ns6?document.getElementById(buttonRel):document.all(buttonRel);

	//残像取得
	var dummyBox1 = ns6?document.getElementById("afterImage01"):document.all("afterImage01");
	var dummyBox2 = ns6?document.getElementById("afterImage02"):document.all("afterImage02");
	var dummyBox3 = ns6?document.getElementById("afterImage03"):document.all("afterImage03");

	//本体表示位置取得
	var popTop = (windowHeight / 2) + scrollHeight - (getDivHeight(showContext) / 2);
	showContext.style.top = popTop + 'px';
	if (buttonRel == 'popup10' || buttonRel == 'popup11' || buttonRel == 'popup12' || buttonRel == 'popup13') {
		var popLeft = (windowWidth / 2) + scrollWidth - (getDivWidth(showContext) / 2);
	}
	else {
		var popLeft = 460 - (getDivWidth(showContext) / 2);
	}
	showContext.style.left = popLeft + 'px';
//debug.print( popTop + "," + popLeft);

	//残像表示位置取得
	dummyBox1.style.top = (((popTop - buttonTop) / 20) * 3) + buttonTop + 'px';
	dummyBox1.style.left = (((popLeft - buttonLeft) / 20) * 3) + buttonLeft + 'px';
	dummyBox2.style.top = (((popTop - buttonTop) / 20) * 6) + buttonTop + 'px';
	dummyBox2.style.left = (((popLeft - buttonLeft) / 20) * 6) + buttonLeft + 'px';
	dummyBox3.style.top = (((popTop - buttonTop) / 20) * 12) + buttonTop + 'px';
	dummyBox3.style.left = (((popLeft - buttonLeft) / 20) * 12) + buttonLeft + 'px';

	var objBody = document.getElementsByTagName("body").item(0);
	objBody.setAttribute('className', 'show' + buttonRel);

	//残像表示(要最適化)
	timerID1 = setInterval(showShadow1, 50);
	timerID2 = setInterval(hideShadow1, 100);
	timerID3 = setInterval(showShadow2, 100);
	timerID4 = setInterval(hideShadow2, 150);
	timerID5 = setInterval(showShadow3, 150);
	timerID6 = setInterval(hideShadow3, 200);

	//背景オーバーレイ表示
	if (buttonRel == 'popup03' || buttonRel == 'popup12') {
		timerID7 = setInterval(showTranslucent, 200);
	}
	//本体表示
	timerID = setInterval('showPop(buttonRel)', 200);
}

//ポップアップ表示
function showPop (targetID) {
	clearInterval(timerID);
	var showContext = ns6?document.getElementById(targetID):document.all(targetID);
	showContext.style.visibility = "visible";
	showContext.style.zIndex = 10001;

	//「お気に入りに追加」の場合
	if (targetID == 'popup02' || targetID == 'popup11' || targetID=="popup13") {
		timerID = setInterval(hidePop, 2000);
	}
	else if(targetID=='popup12'){
		cn = ns6?document.getElementById('searchBox'):document.all('searchBox');
		cn.style.visibility = "visible";
	}
}

//ポップアップ消去
function hidePop() {
	clearInterval(timerID);
	current.src = imageBuffer;
//	imageBuffer=null;
	var objBody = document.getElementsByTagName("body").item(0);
	objBody.setAttribute('className', 'def');
	clearPop();
}

//オーバーレイ背景表示
function showTranslucent () {
	var objTranslucent = ns6?document.getElementById('translucent'):document.all('translucent');
	var arrayPageSize = getPageSize();
	objTranslucent.style.height = arrayPageSize[1] + 'px';
	objTranslucent.style.display = 'block';
	clearInterval(timerID7);
}


//残像1表示
function showShadow1 () {
	var dummyBox1 = ns6?document.getElementById("afterImage01"):document.all("afterImage01");
	dummyBox1.style.visibility = "visible";
	clearInterval(timerID1);
}

//残像1消去
function hideShadow1 () {
	var dummyBox1 = ns6?document.getElementById("afterImage01"):document.all("afterImage01");
	dummyBox1.style.visibility = "hidden";
	clearInterval(timerID2);
}

//残像2表示
function showShadow2 () {
	var dummyBox1 = ns6?document.getElementById("afterImage02"):document.all("afterImage02");
	dummyBox1.style.visibility = "visible";
	clearInterval(timerID3);
}

//残像2消去
function hideShadow2 () {
	var dummyBox1 = ns6?document.getElementById("afterImage02"):document.all("afterImage02");
	dummyBox1.style.visibility = "hidden";
	clearInterval(timerID4);
}

//残像3表示
function showShadow3 () {
	var dummyBox3 = ns6?document.getElementById("afterImage03"):document.all("afterImage03");
	dummyBox3.style.visibility = "visible";
	clearInterval(timerID5);
}

//残像3消去
function hideShadow3 () {
	var dummyBox3 = ns6?document.getElementById("afterImage03"):document.all("afterImage03");
	dummyBox3.style.visibility = "hidden";
	clearInterval(timerID6);
}

//ポップアップ全消去
function clearPop () {
	var showContext = ns6?document.getElementById("popup01"):document.all("popup01");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup02"):document.all("popup02");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup03"):document.all("popup03");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup04"):document.all("popup04");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup05"):document.all("popup05");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup06"):document.all("popup06");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup07"):document.all("popup07");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup08"):document.all("popup08");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup09"):document.all("popup09");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup10"):document.all("popup10");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup11"):document.all("popup11");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup12"):document.all("popup12");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("popup13"):document.all("popup13");
	showContext.style.visibility = "hidden";
	var showContext = ns6?document.getElementById("searchBox"):document.all("searchBox");
	showContext.style.visibility = "hidden";

	var objTranslucent = ns6?document.getElementById('translucent'):document.all('translucent');
	objTranslucent.style.display = 'none';
//タイマーIDをクリア
	if(timerID) clearInterval(timerID);

}

//タグ縦サイズ取得
function getDivHeight(div){
	// _dom : kind of DOM.
	//        IE4 = 1, IE5+ = 2, NN4 = 3, NN6+ = 4, others = 0
	if(_dom==4 || _dom==2) return div.offsetHeight;
	if(_dom==1)            return div.style.pixelHeight;
	if(_dom==3)            return div.clip.height;
	return 0;
}

//タグ横サイズ取得
function getDivWidth (div){
	// _dom : kind of DOM.
	//        IE4 = 1, IE5+ = 2, NN4 = 3, NN6+ = 4, others = 0
	if(_dom==4 || _dom==2) return div.offsetWidth;
	if(_dom==1)            return div.style.pixelWidth;
	if(_dom==3)            return div.clip.width;
	return 0;
}

//タグ縦位置取得
function getDivTop(div) {
	var positionTop = div.offsetTop;
	var parentTag = div.offsetParent;
	for (var i = 0; parentTag; i++) {
		positionTop = positionTop + parentTag.offsetTop;
		parentTag = parentTag.offsetParent;
	}
	return positionTop;
}

//タグ横位置取得
function getDivLeft(div) {
	var positionLeft = div.offsetLeft;
	var parentTag = div.offsetParent;
	for (var i = 0; parentTag; i++) {
		positionLeft = positionLeft + parentTag.offsetLeft;
		parentTag = parentTag.offsetParent;
	}
	return positionLeft;
}

//ページ縦スクロールサイズ取得
function getScrollHeight() {
	if (self.pageYOffset) {
		scrollHeight = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		scrollHeight = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		scrollHeight = document.body.scrollTop;
	}
	return scrollHeight;
}

//ページ横スクロールサイズ取得
function getScrollWidth() {
	if (self.pageXOffset) {
		scrollWidth = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollLeft){	 // Explorer 6 Strict
		scrollWidth = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		scrollWidth = document.body.scrollLeft;
	}
	return scrollWidth;
}


//ページ縦横サイズ、ウィンドウ縦横サイズ取得
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

