// JavaScript Document

/* Fields */

var flipBookLocks = new Array();

/* Functions */

function FlipBookA(id, fadeTime, linkClassNormal, linkClassCurrent)
{
	this.lockID = id;
	this.book = toLayer(id);
	this.fadeTime = (fadeTime == null) ? 0 : fadeTime;
	this.page = id + '_Page_0';
	this.link = null;
	this.linkClassNormal = linkClassNormal;
	this.linkClassCurrent = linkClassCurrent;
	
	flipBookLocks[this.lockID] = false;
	
	var bookNodes = this.book.childNodes;
	for(var i = 0; i < bookNodes.length; i++)
	{
		if(bookNodes[i].id == id + '_Links')
		{
			var first = true;
			var linkNodes = bookNodes[i].childNodes;
			for(var j = 0; j < linkNodes.length; j++)
			{
				if(linkNodes[j].tagName == 'A')
				{
					if(first)
					{
						linkNodes[j].className = this.linkClassCurrent;
						this.link = linkNodes[j];
						first = false;
					}
					else
					{
						linkNodes[j].className = this.linkClassNormal;
					}
					// Configure the behavior of the links
					linkNodes[j].book = this;
					linkNodes[j].onclick = function()
					{
						// Name of this page
						var linkString = new String(this.href);
						var parts = linkString.split('#', 2);
						
						if(!flipBookLocks[this.book.lockID] && parts[1] != this.book.page)
						{
							// Execute fades
							if(this.book.fadeTime > 0)
							{
								// Fade out, then fade in
								fadeLayer(this.book.page, 1, 0, this.book.fadeTime);
								setTimeout("fadeLayer('" + parts[1] + "', 0, 1, " + this.book.fadeTime + ")", this.book.fadeTime);
								
								// Lock the links while animating
								flipBookLocks[this.book.lockID] = true;
								setTimeout("flipBookLocks['" + this.book.lockID + "'] = false;", this.book.fadeTime * 2);
							}
							else
							{
								// Hide and show directly
								showLayer(parts[1]);
								hideLayer(this.book.page);
							}
							
							// Set current
							this.book.page = parts[1];
							
							// Change classes of links
							this.book.link.className = this.book.linkClassNormal;
							this.className = this.book.linkClassCurrent;
							this.book.link = this;
						}
						
						// Remove focus for visual reasons
						this.blur();
						return false;
					}
				}
			}
		}
		else if(bookNodes[i].id == id + '_Pages')
		{
			var first = true;
			var pageNodes = bookNodes[i].childNodes;
			for(var j = 0; j < pageNodes.length; j++)
			{
				var idString = new String(pageNodes[j].id);
				if(idString.match(id + '_Page_') == id + '_Page_')
				{
					//pageNodes[j].style.position = 'absolute';
					if(first)
					{
						first = false;
						showLayer(pageNodes[j].id);
					}
					else
					{
						hideLayer(pageNodes[j].id);
					}
				}
			}
		}
	}
}
