// 背景スライド ========================
//	Update 2010-03-14
//	Version 1.0.5
// =====================================

var image_count = 12;
var image_width = 1278;
var image_height = 852;
var image_mode = 1278;
var read_time = 500;
var show_time = 2500;
var read_count = 1;
var bg_count = 0;

var readBackgroundImage = function() {
	bg_count++;
	var num = (read_count % image_count) + 1;

	var bg = document.createElement("div");
	bg.setAttribute("id", "background" + bg_count);
	bg.style.zIndex = -1;
	bg.style.position = "absolute";
	bg.style.left = 0;
	bg.style.top = 0;
	bg.style.width = image_width + "px";
	bg.style.height = image_height + "px";
	bg.style.overflow = "hidden";

	var link = document.createElement("a");
	link.setAttribute("href", "/enmusubi.html");

	var image = document.createElement("img");
	image.setAttribute("src", "/kinoha/images/" + image_mode + "/" + num + ".jpg");
	image.style.width = "100%";
	image.style.height = "100%";

	bg.appendChild(link);
	link.appendChild(image);
	document.getElementById("display").appendChild(bg);

	read_count++;
	setTimeout("showBackgroundImage()", show_time);
};

var showBackgroundImage = function() {
	if(read_count % image_count == 1) {
		location.href = "enmusubi.html";
		exit;
	}
	document.getElementById("background" + bg_count).style.zIndex = 1;
	removeBackgroundImage();
	setTimeout("readBackgroundImage()", read_time);
};

var removeBackgroundImage = function() {
	var cnt = bg_count - 1
	var pre = document.getElementById("background" + cnt);
	if(pre) {
		pre.style.zIndex = -1;
		document.getElementById("display").removeChild(pre);
	}
};

var startBackgroundImage = function() {
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	setTimeout("readBackgroundImage()", read_time);
};

var checkHeight = function() {
	var height = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
	image_width = Math.floor(1.5 * height);
	image_height = height;
	if(height < 800) {
		image_mode = 1000;
	} else {
		image_mode = 1278;
	}
	document.getElementById("display").style.width = image_width + "px";
}

var setDisplay = function() {
	checkHeight();
	document.getElementById("background0").style.width = image_width + "px";
	document.getElementById("background0").style.height = image_height + "px";
}

var resizeDisplay = function() {
	checkHeight();
	var cnt = bg_count - 1;
	var pre = document.getElementById("background" + cnt);
	if(pre) {
		pre.style.width = image_width + "px";
		pre.style.height = image_height + "px";
	}
	var bg = document.getElementById("background" + bg_count);
	if(bg) {
		bg.style.width = image_width + "px";
		bg.style.height = image_height + "px";
	}
}

// onload ======================================================================

if(window.addEventListener) {
	window.addEventListener("load", startBackgroundImage, false);
	window.addEventListener("load", setDisplay, false);
	window.addEventListener("resize", resizeDisplay, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", startBackgroundImage);
	window.attachEvent("onload", setDisplay);
	window.attachEvent("onresize", resizeDisplay);
}
