
//
//	Book Class
//
Book = spreadPage ? {
	'spread': true,
	'step': frontIsRight ? -2 : 2,
	'img': [NewEl('img', 'imgBookLeft'), NewEl('img', 'imgBookRight')],
	'shadow': NewImg('modern_images/shadow.png', 'Shadow')
} : {
	'spread': false,
	'step': frontIsRight ? -1 : 1,
	'img': NewEl('img', 'imgBook')
};

Book.div = NewEl('div', 'divBook');
Book.initialized = false;
Book.page = initPage;
Book.leftPage = 0;
Book.rightPage = 0;
Book.playing = false;
Book.padding = 45; //Kz2007-04-27
Book.top = Book.left = Book.width = 0;
Book.nextImg = new Array();

Book.Initialize = function() {
	if(this.spread) {
		Element.hide(this.img[0], this.img[1]);
		this.div.appendChild(this.img[0]);
		this.div.appendChild(this.img[1]);
	}
	else {
		Element.hide(this.img);
		this.div.appendChild(this.img);
	}

	document.body.appendChild(this.div);

	// Add by okoru
	this.frame		= NewEl('table', 'Frame');
	var rows		= new Array(this.frame.insertRow(0), this.frame.insertRow(1), this.frame.insertRow(2));
	var topCols		= new Array(NewCol(rows[0]), NewCol(rows[0]), NewCol(rows[0]));
	var middleCols	= new Array(NewCol(rows[1]), NewCol(rows[1]));
	var bottomCols	= new Array(NewCol(rows[2]), NewCol(rows[2]), NewCol(rows[2]));

	this.frame.style.position	= 'fixed'; //Kz2007-08-13

	this.frame.setAttribute('cellpadding', '0');
	this.frame.setAttribute('cellspacing', '0');

	topCols[0].style.background	= "url('modern_images/frame/lt.gif') no-repeat";
	topCols[1].style.borderTop	= '1px solid #949494';
	topCols[2].style.background	= "url('modern_images/frame/rt.gif') no-repeat";
	topCols[0].style.width		= topCols[2].style.width	= '8px';
	topCols[0].style.height		= topCols[2].style.height	= '6px';
	topCols[1].setAttribute('rowspan', '2');

	middleCols[0].style.background	= "url('modern_images/frame/lm.gif') repeat-y";
	middleCols[1].style.background	= "url('modern_images/frame/rm.gif') repeat-y";

	bottomCols[0].style.background	= "url('modern_images/frame/lb.gif') no-repeat";
	bottomCols[1].style.background	= "url('modern_images/frame/cb.gif') repeat-x";
	bottomCols[2].style.background	= "url('modern_images/frame/rb.gif') no-repeat";
	bottomCols[0].style.width = topCols[2].style.width		= '8px';
	bottomCols[0].style.height = topCols[2].style.height	= '7px';

	for(var i = 0; i < topCols.length; i ++)
		topCols[i].appendChild(NewText(' '));
	for(var i = 0; i < middleCols.length; i ++)
		middleCols[i].appendChild(NewText(' '));
	for(var i = 0; i < bottomCols.length; i ++)
		bottomCols[i].appendChild(NewText(' '));

	document.body.appendChild(this.frame);
	
	this.Posit();
	this.Render(initPage);

	if(this.spread) {
		Effect.Appear(this.img[0]);
		Effect.Appear(this.img[1]);
	}
	else
		Effect.Appear(this.img);

	// Add by okoru
	if(this.spread && isCenterShadow) {
		this.shadow.style.position	= 'relative';
		this.shadow.style.zIndex	= '100';
		this.shadow.style.left		= '50%';
		this.shadow.style.cursor	= 'pointer';
		this.shadow.alt = ' ';

		var shadowImageSrc = this.shadow.src;

		this.shadow.onclick = function() {
			if(this.alt == ' ') {
				this.src = 'modern_images/blank.gif';
				this.alt = '';
			}
			else {
				this.src = shadowImageSrc;
				this.alt = ' ';
			}
		}

		this.div.appendChild(this.shadow);
	}

	this.initialized = true;
}

Book.Posit = function() {
	if(this.initialized) {
		document.body.style.width = '';
		document.body.style.paddingLeft = '0';
	}

	var menuHeight	= Element.getHeight('divMenu');
	var emptyHeight	= window.innerHeight - menuHeight;
	var offset		= skinPosition == 'top' ? menuHeight : 0;
	var padding		= this.padding;
	var height = pageHeight_1Y;	//Kz2007-08-03

	this.div.style.top = (offset + padding) + 'px';
	this.div.style.zIndex	= '5';

	if(this.spread)
		this.img[0].height = this.img[1].height = height;
	else
		this.img.height = height;

	var width;

	if(this.spread) {
		var tWidth = pageWidth_1X; //Kz2007-08-03
		width = pageWidth_1X * 2; //Kz2007-08-03
		this.img[0].width = this.img[1].width = tWidth;
	}
	else {
		width = Math.round(height / pageHeight_1Y * pageWidth_1X);
		this.img.width	= width;
	}

	if(this.spread && isCenterShadow) {
		this.shadow.width = Math.round(height / pageHeight_1Y * 64);
		this.shadow.height = height;
		this.shadow.style.top = -height + 'px';
		this.shadow.style.marginLeft = -(this.shadow.width / 2) + 'px';
	}

	this.width	= width;
	this.height	= height;
	this.left	= Math.round((window.innerWidth - width) / 2);//AlignCenter(this.div);

	//Kz2007-11-28 Start
	if(isSkinSelection && skinPosition == 'top') {
		this.top	= Math.round((emptyHeight - height) / 2) + MaxImageHeight; //Kz2007-11-28
	} else {
		this.top	= Math.round((emptyHeight - height) / 2); //Kz2007-08-03
	}
	//Kz2007-11-28 End

	this.div.style.position			= 'fixed'; // Kz2007-08-13
	this.div.style.width			= width + 'px';
	this.div.style.height			= height + 'px';
	this.div.style.backgroundColor	= '#ccc';
	this.div.style.left				= this.left + 'px';
	this.div.style.top				= this.top + 'px';
//	alert(this.left);

	this.frame.style.top			= (this.top  - 1) + 'px';
	this.frame.style.left			= (this.left - 8) + 'px';
	this.frame.style.top			= (this.top  - 1) + 'px';
	this.frame.style.left			= (this.left - 8) + 'px';

	this.frame.getElementsByTagName('td')[1].style.width	= this.width + 'px';
	this.frame.getElementsByTagName('td')[1].style.height	= this.height + 'px';

	this.frame.getElementsByTagName('td')[3].style.height	=
	this.frame.getElementsByTagName('td')[4].style.height	= (this.height - 6) + 'px';
}

Book.Render = function(p) {
	if(this.spread && !(this.spread && this.page % 2 == 1))
		p	+= startRight;

	if(this.spread) {
		this.leftPage = p + (frontIsRight ? 1 : 0);
		this.rightPage = p + (frontIsRight ? 0 : 1);

		this.img[0].src = 'images/' + this.leftPage + '.jpg';
		this.img[1].src = 'images/' + this.rightPage + '.jpg';
	}
	else {
		this.div.style.backgroundImage = 'url(images/' + p + '.jpg)';
		this.img.src = 'images/' + p + '.jpg';
	}
}

Book.Action = function(p, page) {

//Kz2007-10-11 Start
	var userinfo = navigator.userAgent;

	var rosePage;
	var fellPage;
	
	if ((userinfo.indexOf('Opera') >= 0) || (userinfo.indexOf('Firefox') >= 0) || (userinfo.indexOf('Camino') >= 0)){
		rosePage = 10;
		
		if (pageWidth_1X >= 400) {
			fellPage = 20;
		} else {		
			fellPage = Math.floor(pageWidth_1X / 20);
		}			
	} else {
		rosePage = 2;
		fellPage = 2;
	}
//Kz2007-10-11 End

	if(!(this.spread && p % 2 == 1))
		p	+= startRight;

	if(typeof this.turningFlag == 'undefined') {
		this.formerPage	= p;

		this.nextPageTable = NewEl('table', 'pages');
		var tr = this.nextPageTable.insertRow(0);
		var td = new Array(NewCol(tr), NewCol(tr));
		this.nextPageTable.style.zIndex	= '10';
		this.nextPageTable.style.backgroundImage	= "url('modern_images/blank.gif')";

		this.nextPageTable.setAttribute('cellpadding', '0');
		this.nextPageTable.setAttribute('cellspacing', '0');

		this.nextPageTable.style.position		= 'absolute';
		this.nextPageTable.style.left			= '50%';
		this.nextPageTable.style.top			= 0;
		this.nextPageTable.style.marginLeft		= (- this.width / 2) + 'px';

		this.turningFlag	= new Array(false, false);

		var leftPage		= ((page - p > 0)? page : p) + (frontIsRight ? -1 : 0);
		var rightPage		= ((page - p > 0)? p : page) + (frontIsRight ? 2 : 1);

		td[0].style.width		= td[1].style.width = (this.width / 2) + 'px';
		td[0].style.height		= td[1].style.height = (this.height) + 'px';
		td[0].style.textAlign	= 'right';
		td[1].style.textAlign	= 'left';

		this.nextImg	= [NewImg('images/' + leftPage + '.jpg'), NewImg('images/' + rightPage + '.jpg')];

		td[0].appendChild(this.nextImg[0]);
		td[1].appendChild(this.nextImg[1]);

		this.nextImg[0].style.zIndex = this.nextImg[1].style.zIndex	= '10';

		if(page - this.formerPage > 0) {
			if(frontIsRight) {
				this.nextImg[0].width	= Math.round(this.height / pageHeight_1Y * pageWidth_1X);
				this.nextImg[1].width	= 1;
			}
			else {
				this.nextImg[0].width	= 1;
				this.nextImg[1].width	= Math.round(this.height / pageHeight_1Y * pageWidth_1X);
			}
			this.rightPage = page + 1;
			this.img[(frontIsRight ? 0 : 1)].src = 'images/' + this.rightPage + '.jpg';
		}
		else {
			if(!frontIsRight) {
				this.nextImg[0].width	= Math.round(this.height / pageHeight_1Y * pageWidth_1X);
				this.nextImg[1].width	= 1;
			}
			else {
				this.nextImg[0].width	= 1;
				this.nextImg[1].width	= Math.round(this.height / pageHeight_1Y * pageWidth_1X);
			}
			this.leftPage = page;
			this.img[(frontIsRight ? 1 : 0)].src = 'images/' + this.leftPage + '.jpg';
		}
		this.nextImg[0].height = this.nextImg[1].height = this.height;

		this.div.appendChild(this.nextPageTable);
	}

	if(!this.turningFlag[0] && !this.turningFlag[1]) {
		this.turning		= setInterval('Book.Action(0, ' + page + ')', actionSpeed);
		this.turningFlag[0]	= true;

		return;
	}

	if(this.turningFlag[0]) {
		if(this.nextImg[(page - this.formerPage > 0 ? 1 : 0) ^ (frontIsRight ? 1 : 0)].width <= 1) {
			this.nextImg[(page - this.formerPage > 0 ? 1 : 0) ^ (frontIsRight ? 1 : 0)].parentNode.removeChild(this.nextImg[(page - this.formerPage > 0 ? 1 : 0) ^ (frontIsRight ? 1 : 0)]);
			this.turningFlag[0]	= false;
			this.turningFlag[1]	= true;

			return;
		}
		this.nextImg[(page - this.formerPage > 0 ? 1 : 0) ^ (frontIsRight ? 1 : 0)].width	/= rosePage; //Kz2007-10-11
		return;
	}

	if(this.turningFlag[1]) {
		if(this.nextImg[(page - this.formerPage > 0 ? 0 : 1) ^ (frontIsRight ? 1 : 0)].width * 2 > Math.round(this.height / pageHeight_1Y * pageWidth_1X))
			this.nextImg[(page - this.formerPage > 0 ? 0 : 1) ^ (frontIsRight ? 1 : 0)].width	= Math.round(this.height / pageHeight_1Y * pageWidth_1X);

		if(this.nextImg[(page - this.formerPage > 0 ? 0 : 1) ^ (frontIsRight ? 1 : 0)].width == Math.round(this.height / pageHeight_1Y * pageWidth_1X)) {
			clearInterval(this.turning);

			if(page - this.formerPage > 0) {
				this.leftPage = page;
				this.img[(frontIsRight ? 1 : 0)].src = 'images/' + this.leftPage + '.jpg';
			}
			else {
				this.rightPage = page + 1;
				this.img[(frontIsRight ? 0 : 1)].src = 'images/' + this.rightPage + '.jpg';
			}

			this.nextPageTable.parentNode.removeChild(this.nextPageTable);
			this.turningFlag	= undefined;

			return;
		}
		this.nextImg[(page - this.formerPage > 0 ? 0 : 1) ^ (frontIsRight ? 1 : 0)].width	*= fellPage;  //Kz2007-10-11
		return;
	}
}

Book.GoTo = function(p) {
	formerPage	= this.page;
	this.page	= parseInt(p);
	page  = this.page + (startRight ? (parseInt(p) % 2 == 0 ? 1 : 0) : (parseInt(p) % 2 == 0 ? -1 : 0)); // Kz2007-06-29

	// Kz2007-07-10 Start
	if(this.spread && !Zoom.zoomed) {
		// Kz2007-07-03
		if (startRight == 0) {
			if (this.page % 2 == 1) {
					if	((this.page != formerPage) && ((this.page + 1) != formerPage)) { 
						this.Action(formerPage, page);
					}
			} else {
					if	((this.page != formerPage) && ((this.page -1) != formerPage)){
						this.Action(formerPage, page);
					}		
			}
		} else {
			if (this.page % 2 == 1) {
					if	((this.page != formerPage) && ((this.page - 1) != formerPage)) {
						this.Action(formerPage, page);
					}
			} else {
					if	((this.page != formerPage) && ((this.page +1) != formerPage)){
						this.Action(formerPage, page);
					}		
			}		
		}
		// Kz2007-07-03	
	} else {
		this.Render(this.page);
	}
	// Kz2007-07-10 End

	var adjustVal; // Kz2007-07-20
	(startRight) ? (adjustVal = 1) : (adjustVal = 0); // Kz2007-07-20

	$('tdPageNumber').firstChild.nodeValue = ((this.page - modifiedPage) < 0 ? '0':(this.page - modifiedPage)) + '/' + (lastPage - adjustVal  - modifiedPage); // KzFolio2007-07-20
	$('inputGotoTarget').value = (this.page - firstPage + 1 - modifiedPage) < 0 ? '0':(this.page - firstPage + 1 - modifiedPage) ; // KzFolio2007-07-20

// Sound per page
	//if(soundMode && !isBGM && Sound.isPlaying) {
	if(soundMode && !isBGM && !Sound.embed.removed) {
//		alert('33');
		clearInterval(Sound.goNext);
//		alert('44');
		Sound.Stop();
		Sound.Play();
//		alert('55');
	}

	if(Zoom.zoomed)
		Zoom.Render();

	Link.Render();
	Bookmark.Render();
}

Book.Prev = function() {
	if(startRight && this.page % 2 == 0) // Kz2007-07-03
		this.page++;
	if(!startRight && this.page % 2 == 0) // Kz2007-07-11
		this.page--;									// Kz2007-07-11
		
	if(this.page == 1 || this.page - this.step < 1 - startRight) {
		alert('先頭ページです。');
		return false;
	}

	if(this.step > 0 && this.page - this.step >= 1 - startRight)
		this.GoTo(this.page - this.step);
	else if(this.step < 0 && this.page + this.step >= 1 - startRight)
		this.GoTo(this.page + this.step);
	else if(this.playing)
		this.Stop();
}

Book.Next = function() {
	if(startRight && this.page % 2 == 0) // Kz2007-07-03
		this.page++;
	if (!startRight && this.page % 2 == 0)  // Kz2007-07-11
		this.page--;									  // Kz2007-07-11		
		
	if(this.page >= lastPage || this.page + startRight >= lastPage || this.page + this.step > lastPage) {
//		$('imgAutoPlay').turn();
		alert('最終ページです。');

		if($('imgPageSound'))
			$('imgPageSound').turn(true);

		if((soundMode || !isBGM || !Sound.embed.removed) && $('imgPageSound')) {
		    clearInterval(Sound.goNext);
		    Sound.Stop();
		}

		this.Stop();
		return false;
	}

	if(this.step > 0 && this.page + this.step <= lastPage)
		this.GoTo(this.page + this.step);
	else if(this.step < 0 && this.page - this.step <= lastPage)
		this.GoTo(this.page - this.step);
	else if(this.playing)
		this.Stop();
}
Book.GoFirst = function() {
	if(this.page > firstPage) // Kz2007-04-27
		this.GoTo(firstPage);
}

Book.GoLast = function() {
	var to = lastPage;

	if(this.spread && lastPage % 2 == 0)
		to--;

	if(this.page < to)
		this.GoTo(to);
}

Book.Play = function(sec) {
	if(this.playing)
		$('imgAutoPlay').turn(true);
		
	this.playingID = setInterval('Book.Next()', parseInt(sec) * 1000);
	
	//imgAutoPlay.src = imgOn;
	this.playing = true;
}

Book.Stop = function() {
	if(this.playing)
		clearInterval(this.playingID);

	//imgAutoPlay.src = imgOff;
	$('imgAutoPlay').turn(false);
	this.playing = false;
}

//
//	Zoom Class
//

Zoom = {
	'zoomed': false,
	'offsetY': 0,
	'offsetX': 0,
	'table': NewEl('table', 'tableZoom'),
	'tr': [],
	'td': [],
	'tdLeft': [],
	'tdRight': [],
	'width': pageWidth_2X,
	'tableWidth': Book.spread ? pageWidth_2X * 2 : pageWidth_2X,
	'scrollXStep': 0,
	'scrollYStep': 0,
	'scrollSpeed': 1,
	'prepared': firstPage - 1,
	'top': 0,
	'left': 0
};

Zoom.Initialize = function() {
	var row, col, img;

	this.table.style.position = 'absolute';
	this.table.style.zIndex = '200';
	Element.hide(this.table);
	this.table.cellSpacing = 0;
	this.table.cellPadding = 0;
	this.table.height = pageHeight_2Y;
	this.table.width = this.tableWidth;

	for(var i = 0; i < sliceNumber; i++) {
		row = NewEl('tr', 'trZoom_' + i);
		row.style.height = sliceImgHeight + 'px';

		if(Book.spread) {
			col = NewCol(row, 'tdZoom_' + i + 'L');
			col.style.height = sliceImgHeight + 'px';
			this.tdLeft.push(col);

			row.appendChild(col);

			col = NewCol(row, 'tdZoom_' + i + 'R');
			col.style.height = sliceImgHeight + 'px';
			this.tdRight.push(col);

			row.appendChild(col);
		}
		else {
			col = NewCol(row, 'tdZoom_' + i);
			col.style.height = sliceImgHeight + 'px';
			this.td.push(col);

			row.appendChild(col);
		}

		this.tr.push(row);
		this.table.appendChild(row);
	}

	var menuHeight	= Element.getHeight('divMenu');
	var emptyHeight	= window.innerHeight - menuHeight;
	var offset		= skinPosition == 'top' ? menuHeight : 0;

	var arrowTop	= NewImg('image/scrolling/WheelTop_.gif');
	var arrowRight	= NewImg('image/scrolling/WheelRight_.gif');
	var arrowBottom	= NewImg('image/scrolling/WheelBottom_.gif');
	var arrowLeft	= NewImg('image/scrolling/WheelLeft_.gif');

	arrowTop.width = arrowBottom.width = arrowRight.height = arrowLeft.height	= 58;
	arrowTop.height = arrowBottom.height = arrowRight.width = arrowLeft.width	= 38;

	arrowTop.style.position = arrowRight.style.position = arrowBottom.style.position = arrowLeft.style.position	= 'absolute';
	arrowTop.style.zIndex = arrowRight.style.zIndex = arrowBottom.style.zIndex = arrowLeft.style.zIndex			= '350';
	arrowTop.style.cursor = arrowRight.style.cursor = arrowBottom.style.cursor = arrowLeft.style.cursor			= 'default';

	arrowTop.style.left			= arrowBottom.style.left		= '50%';
	arrowTop.style.marginLeft	= arrowBottom.style.marginLeft	= '-29px';

	arrowRight.style.top		= arrowLeft.style.top			= '50%';
	arrowRight.style.marginTop	= arrowLeft.style.marginTop		= '-19px';

	arrowTop.style.top			= Book.padding + offset + 'px';
	arrowBottom.style.top		= (emptyHeight + offset - Book.padding - 38) + 'px';

	arrowRight.style.left		= (window.innerWidth - Book.padding - 38) + 'px';
	arrowLeft.style.left		= Book.padding + 'px';

	this.arrows		= new Array(arrowTop, arrowRight, arrowBottom, arrowLeft);

	document.body.appendChild(this.table);
}

Zoom.Prepare = function() {
	var tmpImg;

	if(Book.spread) {
		for(var i = 0; i < sliceNumber; i++) {
			tmpImg = NewImg(this.ImgSrc(i, Book.leftPage));
			tmpImg.onload = this.OnPrepare;
			this.tdLeft[i].appendChild(tmpImg);

			tmpImg = NewImg(this.ImgSrc(i, Book.rightPage));
			tmpImg.onload = this.OnPrepare;
			this.tdRight[i].appendChild(tmpImg);
		}
	}
	else {
		for(var i = 0; i < this.td.length; i++) {
			tmpImg = NewImg(this.ImgSrc(i, Book.page + spreadPage));
			tmpImg.onload = this.OnPrepare;
//			alert(this.ImgSrc(i, Book.page));
			this.td[i].appendChild(tmpImg);
		}
	}

	this.prepared = Book.page;
}

Zoom.OnPrepare = function() {
	var userinfo = navigator.userAgent;//Kz2007-11-07 Start

	if ((userinfo.indexOf('Safari') >= 0 )  && (userinfo.indexOf('Version') >= 0 )) {  //For Safari 3.0
		this.parentNode.style.backgroundImage = 'url(' + this.src + ')';
		while(this.parentNode.childNodes.length > 1) 
			this.parentNode.removeChild(this.parentNode.firstChild);
	} else {																						//Kz2007-11-07 End
		this.parentNode.style.backgroundImage = 'url(' + this.src + ')';
		while(this.parentNode.childNodes.length > 1)
			this.parentNode.removeChild(this.parentNode.lastChild);
		if(this.parentNode.childNodes.length == 1)
			this.parentNode.removeChild(this.parentNode.lastChild);
	}
}

Zoom.Render = function() {
	this.Prepare();
	this.Posit();
	Effect.Appear(this.table);
}

Zoom.Posit = function() {
	var left, top;

	if(this.tableWidth > window.innerWidth)
		left = -1 * this.offsetX;
	else
		left = Math.round(((window.innerWidth - this.tableWidth) / 2));

	this.table.style.left = left + 'px';

	var menuHeight	= Element.getHeight('divMenu');
	var emptyHeight	= window.innerHeight - menuHeight;

	if(pageHeight_2Y > emptyHeight && skinPosition == 'top')
		top = -1 * (this.offsetY - menuHeight);
	else if(pageHeight_2Y > emptyHeight)
		top = -1 * this.offsetY;
	else if(skinPosition == 'top')
		top = Math.round(emptyHeight - this.tableWidth) / 2 + menuHeight;
	else
		top = Math.round((emptyHeight - this.tableWidth) / 2);

	this.top	= top;
	this.left	= left;

	this.table.style.top = top + 'px';
	Link.Render();
}

Zoom.Arrows = function() {
	this.arrowsDiv	= NewEl('div', 'arrows');

	for(var i = 0; i < this.arrows.length; i ++)
		this.arrowsDiv.appendChild(this.arrows[i]);

	this.arrows[0].onmouseover	= function() {
		Zoom.Arrows.direction	= 0;
		Zoom.Arrows.StartScroll(Zoom.Arrows.direction);
	}
	this.arrows[1].onmouseover	= function() {
		Zoom.Arrows.direction	= 1;
		Zoom.Arrows.StartScroll(Zoom.Arrows.direction);
	}
	this.arrows[2].onmouseover	= function() {
		Zoom.Arrows.direction	= 2;
		Zoom.Arrows.StartScroll(Zoom.Arrows.direction);
	}
	this.arrows[3].onmouseover	= function() {
		Zoom.Arrows.direction	= 3;
		Zoom.Arrows.StartScroll(Zoom.Arrows.direction);
	}

	this.arrows[0].onmouseout = this.arrows[1].onmouseout = this.arrows[2].onmouseout = this.arrows[3].onmouseout	= function() {
//		alert('out');
		Zoom.Arrows.EndScroll();
		Zoom.Arrows.scroll	= false;
	}

	document.body.appendChild(this.arrowsDiv);
}

Zoom.Arrows.Scroll = function(direction, speed) {
	var speedY	= Math.round(pageHeight_2Y / speed);
	var speedX	= Math.round(pageWidth_2X / speed);

//	alert(direction);

	switch(direction) {
		case 0 :
			var x	= Zoom.offsetX;
			var y	= Zoom.offsetY - speedY;

			break;
		case 1 :
			var x	= Zoom.offsetX + speedX;
			var y	= Zoom.offsetY;

			break;
		case 2 :
			var x	= Zoom.offsetX;
			var y	= Zoom.offsetY + speedY;

			break;
		case 3 :
			var x	= Zoom.offsetX - speedX;
			var y	= Zoom.offsetY;

			break;
	}
	Zoom.Scroll(x, y);

	var emptyHeight	= window.innerHeight - Element.getHeight('divMenu');

	if(Zoom.Arrows.direction == 3 && x <= 0
	|| Zoom.Arrows.direction == 1 && x >= Zoom.tableWidth - window.innerWidth)
	{
//		alert('end ' + x.toString() + ' - ' + (Zoom.tableWidth - window.innerWidth).toString() + ', ' + y.toString() + ' - ' + (pageHeight_2Y - emptyHeight).toString());
		Zoom.Arrows.EndScroll();
		Zoom.Arrows.scroll		= null;
	}
	else if(Zoom.Arrows.direction == 0 && y <= 0
	||      Zoom.Arrows.direction == 2 && y >= pageHeight_2Y - emptyHeight)
	{
		Zoom.Arrows.EndScroll();
		Zoom.Arrows.scroll		= null;
	}
}

Zoom.Arrows.StartScroll = function(direction) {
//	alert(Zoom.Arrows.scrolling + ' - ' + Zoom.Arrows.scroll);

	if((Zoom.Arrows.scrolling || Zoom.Arrows.scroll == null) && typeof Zoom.Arrows.scroll != 'undefined')
		return;

	Zoom.Arrows.scroll		= window.setInterval('Zoom.Arrows.Scroll(' + direction + ', 10)', 100);
	Zoom.Arrows.scrolling	= true;
}

Zoom.Arrows.EndScroll = function() {
	window.clearInterval(Zoom.Arrows.scroll);
	Zoom.Arrows.scroll		= false;
	Zoom.Arrows.scrolling	= false;
}

Zoom.ZoomIn = function(pointX, pointY) {
	if(this.zoomed)
		return;

	this.zoomed = true;
	document.body.style.cursor = 'move';

	var x = 0, y = 0;

	if(typeof pointX != 'undefined')
		x = Math.round(pointX / pageWidth_1X * pageWidth_2X - (window.innerWidth / 2));

	if(typeof pointY != 'undefined')
		y = Math.round(pointY / pageHeight_1Y * pageHeight_2Y - (window.innerHeight / 2));

//	if(Book.shadow)
//		Book.shadow.style.display	= 'none';

	this.Arrows();
	this.Scroll(x, y);
	this.Render();
	Bookmark.Render();
}

Zoom.ZoomOut = function() {
	if(!this.zoomed)
		return;

//	if(Book.shadow)
//		Book.shadow.style.display	= 'inline';

	this.ScrollStop();
	this.zoomed = false;
	document.body.style.cursor = 'default';
	Link.Render();
	Effect.Fade(this.table);
	Effect.Fade(this.arrowsDiv);
	Bookmark.Render();
}

Zoom.Scroll = function(x, y) {
	var emptyHeight	= window.innerHeight - Element.getHeight('divMenu');

	if(window.innerWidth >= this.tableWidth)
		x = 0;
	if(emptyHeight >= pageHeight_2Y)
		y = 0;

	if(x <= 0)
		x = 0;
	else if(x >= this.tableWidth - window.innerWidth)
		x = this.tableWidth - window.innerWidth;

	if(y <= 0)
		y = 0;
	else if(y >= pageHeight_2Y - emptyHeight)
		y = pageHeight_2Y - emptyHeight;

	// Kz2007-11-08 Start
	switch(true) {
		case x >= Zoom.tableWidth - window.innerWidth :
			this.arrows[1].src	= 'modern_images/blank.gif';
			break;
			
		default :
			this.arrows[1].src	= 'image/scrolling/WheelRight_.gif';
	}

	switch(true) {
		case x <= 0 :
			this.arrows[3].src	= 'modern_images/blank.gif';
			break;

		default :
			this.arrows[3].src	= 'image/scrolling/WheelLeft_.gif';
	}

	switch(true) {
		case y <= 0 :
			this.arrows[0].src	= 'modern_images/blank.gif';
			break;

		default :
			this.arrows[0].src	= 'image/scrolling/WheelTop_.gif';
	}

	switch(true) {
		case y >= pageHeight_2Y - emptyHeight :
			this.arrows[2].src	= 'modern_images/blank.gif';
			break;

		default :
			this.arrows[2].src	= 'image/scrolling/WheelBottom_.gif';
	}
	// Kz2007-11-08 End	

	this.offsetX = x;
	this.offsetY = y;
	this.Posit();
}

Zoom.ScrollUp = function(y) {
	if(typeof y == 'undefined')
		y = 1;

	this.ScrollDown(-1 * y);
}

Zoom.ScrollDown = function(y) {
	if(typeof y == 'undefined')
		y = 1;

	this.Scroll(this.offsetX, this.offsetY + y);
}

Zoom.ScrollRight = function(x) {
	if(typeof x == 'undefined')
		x = 1;

	this.ScrollLeft(-1 * x);
}

Zoom.ScrollLeft = function(x) {
	if(typeof x == 'undefined')
		x = 1;

	this.Scroll(this.offsetX + x, this.offsetY);
}

Zoom.ScrollStart = function(x, y) {
	if(this.scrollXStep != 0 || this.scrollYStep != 0)
		this.ScrollStop();

	if(x == 0 && y == 0)
		this.ScrollStop();

	
	this.scrollXStep = x;
	this.scrollYStep = y;

	this.scrollIntervalId = window.setInterval('Zoom.Scrolling()', this.scrollSpeed);
}

Zoom.Scrolling = function() {
	if(this.scrollXStep == 0 && this.scrollYStep == 0) {
		this.ScrollStop();
		return;
	}

	var emptyHeight	= window.innerHeight - Element.getHeight('divMenu');

	if(this.scrollXStep != 0 && window.innerWidth >= this.width) {
		this.ScrollStart(0, this.scrollYStep);
		return;
	}

	if(this.scrollYStep != 0 && emptyHeight >= pageHeight_2Y) {
		this.ScrollStart(this.scrollXStep, 0);
		return;
	}

	if(this.scrollXStep < 0 && this.offsetX <= 0 || this.scrollXStep > 0  && this.offsetX >= this.width - window.innerWidth) {
		this.ScrollStart(0, this.scrollYStep);
		return;
	}

	if(this.scrollYStep < 0 && this.offsetY <= 0 || this.scrollYStep > 0 && this.offsetY >= pageHeight_2Y - emptyHeight) {
		this.ScrollStart(this.scrollXStep, 0);
		return;	
	}

	this.Scroll(this.offsetX + this.scrollXStep, this.offsetY + this.scrollYStep);
}

Zoom.ScrollStop = function() {
	if(this.scrollXStep == 0 && this.scrollYStep == 0)
		return;

	window.clearInterval(this.scrollIntervalId);
	this.scrollXStep = 0;
	this.scrollYStep = 0;
}

Zoom.ImgSrc = function(i, p) {
	if(typeof p == 'undefined')
		p = Book.page;

	return 'images/' + p + '-' + (i + 1) + '.jpg';
}

//
//	Bookmark Class
//

var Bheight = Math.round(pageHeight_1Y / 14); //Kz2007-08-08
var Bmargin = Math.round(Bheight /4); //Kz2007-08-08
var Bwidth = Math.round(pageWidth_1X /4); //Kz2007-08-10

Bookmark = {
	'marks': [],
	'div': [],
	'limit': 10,
	'margin': Bmargin, //Kz2007-08-08 
	'padding': 12, //Kz2007-08-30
	'tabHeight': Bheight, //Kz2007-08-08
	'tabWidth': 38, //Kz2007-08-30
	'fullWidth': Bwidth, //Kz2007-08-10
	'button': NewImg('image/bookmark/bookmark.gif', 'imgBookmarkButton'),
	'buttonHidden': true
};

Bookmark.Initialize = function() {
	var div;

	for(var i = 0; i < this.limit; i++) {
		div = NewEl('div', 'divBookmark_' + i);
		div.style.zIndex	= 3; // Kz2007-04-27
		div.markId = i;
		div.onclick = Bookmark.MarkClick;
		Element.addClassName(div, 'bookmark');
		Element.hide(div);
		document.body.appendChild(div);
//		div.style.height = this.tabHeight + 'px';
		this.div.push(div);
	}

	Element.hide(this.button);
	this.Render();
	this.button.style.zIndex	= 30;
	document.body.appendChild(this.button);
}

Bookmark.Render = function(page, effect) {
	if(typeof page == 'undefined')
		page = Book.page;

	for(var i = 0; i < this.limit; i++) {
		if(Zoom.zoomed) {
			Effect.Fade(this.div[i]);
			continue;
		}

		if(this.marks.length <= i) {
			Element.hide(this.div[i]);
			continue;
		}

		while(this.div[i].hasChildNodes())
			this.div[i].removeChild(this.div[i].lastChild);

		this.div[i].style.left	= '50%';

		if ( ((startRight) && (this.marks[i] <= (page +1))) ||  ((!startRight) && (this.marks[i] <= page)) ) { // kz2007-07-12
			this.div[i].style.marginLeft = (- Book.width / 2 - this.tabWidth - this.padding) + 'px';
			this.div[i].style.paddingLeft	= this.padding + 'px';
//			this.div[i].style.left = (Book.left - this.tabWidth - 15) + 'px';
			this.div[i].style.textAlign = 'left';
		}
		else {
			this.div[i].style.marginLeft = (Book.width / 2 - this.padding) + 'px'; // Kz2007-07-18
			this.div[i].style.paddingRight	= this.padding + 'px';
//			this.div[i].style.left = (Book.left + Book.width + 15) + 'px';
			this.div[i].style.textAlign = 'right';
		}

		this.div[i].style.top = ((Book.top - this.tabHeight / 2) + this.tabHeight * (i + 1) + this.margin * i) + 'px'; //Kz2007-08-10

		// Kz2007-07-12 Start
		if (startRight){
			if( ((page % 2 == 1) && (this.marks[i] != page)) || ((page % 2 == 0) &&(this.marks[i] != (page+1))) ){
				this.div[i].style.zIndex	= 3;									// Kz2007-04-27
				this.div[i].style.width = this.tabWidth + 'px';
				this.div[i].appendChild(NewText( ((this.marks[i]) -1 -modifiedPage) < 0 ? '-':((this.marks[i]) -1 -modifiedPage) ));	//for startRight == 1 // KzFolio2007-07-20
				this.div[i].style.backgroundColor	= 'transparent';
				this.div[i].style.backgroundImage	= 'url(modern_images/bookmark-subbg.png)';
			} else {
				this.div[i].style.zIndex	= 6;									// Kz2007-04-27
				this.div[i].style.width = this.fullWidth + 'px';
				this.div[i].style.paddingTop = Math.round(Bheight /4) + 'px';  //Kz2007-08-10 
				this.div[i].style.paddingBottom = Math.round(Bheight /4) + 'px';  //Kz2007-08-10 
				this.div[i].style.height= '1em';
				this.div[i].style.backgroundColor	= 'transparent';
				this.div[i].style.backgroundImage	= 'url(modern_images/bookmark-bg.png)';
			} 
		} else { 	// startRight==0
			if( ((page % 2 == 1) && (this.marks[i] != page)) || ((page % 2 == 0) &&(this.marks[i] != (page -1))) ){
				this.div[i].style.zIndex	= 3;									// Kz2007-04-27
				this.div[i].style.width = this.tabWidth + 'px';
				this.div[i].appendChild(NewText( ((this.marks[i]) -modifiedPage) < 0 ? '-':((this.marks[i]) -modifiedPage) ));		//for startRight == 0 // KzFolio2007-07-20
				this.div[i].style.backgroundColor	= 'transparent';
				this.div[i].style.backgroundImage	= 'url(modern_images/bookmark-subbg.png)';
			} else {
				this.div[i].style.zIndex	= 6;									// Kz2007-04-27
				this.div[i].style.width = this.fullWidth + 'px';
				this.div[i].style.paddingTop = Math.round(Bheight /4) + 'px';  //Kz2007-08-10 
				this.div[i].style.paddingBottom = Math.round(Bheight /4) + 'px';  //Kz2007-08-10 
				this.div[i].style.height= '1em';
				this.div[i].style.backgroundColor	= 'transparent';
				this.div[i].style.backgroundImage	= 'url(modern_images/bookmark-bg.png)';
			} 
		}
		// Kz2007-07-12 End

		Effect.Appear(this.div[i]);
	}
}

Bookmark.Toggle = function(page) {
	if(typeof page == 'undefined')
		page = Book.page;

		// Kz2007-07-17 Start
		if (startRight && page % 2 == 0) {
			page++;
		} else if (!startRight && page % 2 == 0) {
			page--;
		}
		// Kz2007-07-17 End

	if(this.marks.indexOf(page) < 0)
		Bookmark.Add(page);
	else
		Bookmark.Remove(page);
}

Bookmark.Add = function(page) {
	if(typeof page == 'undefined')
		page = Book.page;

	if(this.marks.indexOf(page) >= 0)
		return;

	if(this.marks.length < this.limit)
		this.marks.push(page);
	else
		alert ('使用できるしおりの枚数は ' + this.limit + ' 枚までです。');

//		return false;


	this.Render();
}

Bookmark.Remove = function(page) {
	if(typeof page == 'undefined')
		page = Book.page;

	var dump = this.marks;
	this.marks = [];

	for(var i = 0; i < dump.length; i++) {
		if(dump[i] != page)
			this.marks.push(dump[i]);
	}

	this.Render();
}

Bookmark.Posit = function() {
	var buttonHeight = typeof Element.getHeight(this.button) == 'undefined' ? 37 : Element.getHeight(this.button);

//Kz2007-09-05
	var userinfo = navigator.userAgent;
	if ((userinfo.indexOf('Opera') >= 0) || (userinfo.indexOf('Firefox') >= 0) || (userinfo.indexOf('Camino') >= 0) ||  ((userinfo.indexOf('Safari') >= 0 )  && (userinfo.indexOf('Version') >= 0 )) ){  //Kz2007-11-06
		buttonHeight = buttonHeight + 37;
	}
//Kz2007-09-05

	this.button.style.left	= Book.left + 'px';	// Kz2007-04-27

	if(Book.spread)
		this.button.style.top	= (Book.top + Book.img[0].height - buttonHeight) + 'px'; // Kz2007-04-27
	else
		this.button.style.top	= (Book.top + Book.img.height - buttonHeight) + 'px'; // Kz2007-04-27
}

Bookmark.ShowButton = function() {
	this.buttonHidden = false;
	this.Posit();
	Effect.Appear(this.button);
}

Bookmark.HideButton = function() {
	this.buttonHidden = true;
	Effect.Fade(this.button);
}

Bookmark.MarkClick = function() {
	Book.GoTo(Bookmark.marks[this.markId]);
}

//
//	Link Class
//

Link = {
	'Colors': {
		'R': 'red',
		'G': 'green',
		'B': 'blue',
		'C': 'cyan',
		'M': 'magenta',
		'Y': 'yellow'
	},
	'a': []
};

Link.Render = function(p) {
	if(typeof userLinkData == 'undefined')
		return;

	if(typeof p == 'undefined') {
		p = Book.page;
		if(p % 2 == 1 && startRight && spreadPage){
			p	-= 1;
		}  else if (p % 2 == 0 && !startRight && spreadPage){	// Kz2007-07-03
			p	-= 1;	// Kz2007-07-03
		}  
	}

//	alert(p);

	for(var i = 0; i < this.a.length; i++)
		document.body.removeChild(this.a[i]);
	this.a = [];

	var a, match1, matchType, matchTarget, matchColor, j = 0;
	var aLeft, aTop, aWidth, aHeight, isRight;

	for(var i = 0; i < userLinkData.length; i++) {
		match1 = userLinkData[i].match(/([0-9]+) +([LMG]) +(-?[0-9]+) +([0-9]+) +([0-9]+) +([0-9]+) +(.*)/i);

//		alert(parseInt(match1[1]) + ', ' + (p + Book.step));

		if(Book.spread && parseInt(match1[3]) < 0)
			continue;

		if(Book.spread && parseInt(match1[1]) == p + Book.step + (frontIsRight ? 1 - Book.step : -1))
			isRight = Book.step > 0;
		else if(Book.spread && parseInt(match1[1]) == p)
			isRight = Book.step < 0;
		else if(parseInt(match1[1]) == p)
			isRight = false;
		else if(parseInt(match1[1]) == p + Book.step + (frontIsRight ? 1 - Book.step : -1))
			isRight = true;
		else
			continue;

//		alert(p + ', ' + match1[1]);

		matchType	= match1[2].toUpperCase();
		matchColor	= match1[7].substr(match1[7].lastIndexOf(' ') + 1).toUpperCase();
		matchTarget	= match1[7].substr(0, match1[7].lastIndexOf(' '));

		a = NewEl('a', 'aLink_' + (j++));
		Element.addClassName(a, 'link ' + this.Colors[matchColor]);
		a.boxColor = this.Colors[matchColor];
/////////////////////////
//		a.appendChild(NewText(i + ', ' + match1[1]));

		switch(matchType) {
			case 'G' :
				a.href = '?' + (eval(matchTarget) + modifiedPage); // KzFolio2007-07-20
				a.onclick = this.PageLink;
				// KzFolio2007-07-20 Start
				if (matchTarget > 0) { 
					a.title	= 'Go to Page: ' + matchTarget;
				} else {
					a.title	= 'Go to Page';
				}
				// KzFolio2007-07-20 End
				break;

			case 'M' :
				media = soundDir + matchTarget;
				a.onclick	= function() { $('imgSoundArea').turn(true); }
				a.href = "javascript:void(window.open('modern_media.html?media=" + media + "', media, 'width = 500, height = 340, status = no, toolbar = no, menubar = no'))";
				a.title	= 'Open Media: "' + media + '"';
				break;

			case 'L' :
				media = matchTarget;
				//a.href	= media;
				//a.target	= '_blank';
				a.href = "javascript:void(window.open('"+ media + "', 'url_link'))";
				a.title		= 'Go to Site: "' + media + '"';
				break;
		}

		if(Zoom.zoomed) {
			var aPer= Zoom.width / pageWidth_1X;
			aLeft	= Zoom.left + Math.round(match1[3] * aPer)
					+ (isRight ? Zoom.width : 0);
			aTop	= Zoom.top + Math.round(match1[4] * aPer);
			aWidth	= Math.round(match1[5] * aPer);
			aHeight	= Math.round(match1[6] * aPer);
		}
		else {
			var aPer= (Book.spread ? Book.img[0].width : Book.img.width)
					/ pageWidth_1X;
			aLeft	= Book.left + Math.round(match1[3] * aPer)
					+ (isRight ? Book.img[0].width : 0);
			aTop	= Book.top + Math.round(match1[4] * aPer);
			aWidth	= Math.round(match1[5] * aPer);
			aHeight	= Math.round(match1[6] * aPer);
		}
//		alert(Book.img[0].width + ', ' + (pageWidth_1X * aPer));

		a.style.left	= aLeft + 'px';
		a.style.top		= aTop + 'px';
		a.style.width	= aWidth + 'px';
		a.style.height	= aHeight + 'px';

		if(Zoom.zoomed)
			a.style.zIndex	= '300';
		else
			a.style.zIndex	= '7';

		a.onmouseover	= this.MouseOver;
		a.onmouseout	= this.MouseOut;

		this.a.push(a);
		document.body.appendChild(a);
	}
/*//////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
	var a = NewEl('div', 'a');
	a.style.width = Book.left + 'px';
	a.style.height = Math.round(Book.left / 2) + 'px';
	a.style.border = '1px solid blue';
	a.style.position = 'absolute';
	a.style.top = '0';
	a.style.left = '0';

	document.body.appendChild(a);
///////////////////////////////////////////////
///////////////////////////////////////////////
//////////////////////////////////////////////*/
}

Link.PageLink = function() {
	var ref = this.href;
	this.href = '#';
	Book.GoTo(parseInt(ref.match(/_?([0-9]+)$/)[1]));
	return false;
}

Link.MouseOver = function() {
	Zoom.event = false;
	this.style.backgroundImage = 'url(modern_images/link-' + this.boxColor + '.png)';
}

Link.MouseOut = function() {
	this.style.backgroundImage = '';
	Zoom.event = true;
}