/* ----------------------------------------------------------------------- *
 * AyeBee.js
 * ---------
 *
 * Javascript routines for the AyeBee.com website.
 *
 * Author: Ian Brown
 * Copyright © 2005 Ayebee Systems
 * ----------------------------------------------------------------------- */
 
var paraNums = [0,0,0,0,0,0,0];
var maxLevels = 5;
var inPara = false;
var currId = '';

var page = 0;
var prev = 0;
var next = 5;
var width = 0;
var height = 0;

var altstyle = getCookieAltstyle();

var downImg = 'images/RoundDown' + (altstyle ? '2' : '') + '.gif';
var rightImg = 'images/RoundRight' + (altstyle ? '2' : '') + '.gif';

document.write('<link href="ayebee' + (altstyle ? '2' : '') + '.css" type="text/css" rel="stylesheet">');

function frameTitle(title)
{
	document.write('<tr><td colspan=' + maxLevels + '><strong>' + title + '</strong></td></tr>');
}

function switchStyles()
{
	setCookieAltstyle(!altstyle, 240);
	location.reload();
	void 0;
}

function getCookieAltstyle()
{
	return (document.cookie.indexOf('altstyle=true') > -1);
}

function setCookieAltstyle(altstyleNew, hours)
{
	if (altstyleNew)
	{
		var expires = new Date((new Date()).getTime() + (hours * 3600000));
		document.cookie = 'altstyle=true; expires=' + expires.toUTCString();
	}
	else
		document.cookie = 'altstyle=false; expires=Fri, 02-Jan-1970 00:00:00 GMT';
}

function expandNode(nodeSectIn)
{
	var obj;
	var i, j;
	var expand = true;
	var nodeSect = nodeSectIn;

	// is node or any sub-node expanded ?
	if (nodeSect == currId.substr(0, nodeSect.length))
		// treat node as currently expanded
		expand = false;

	// is it a sub-node that is being contracted ?
	if (!expand)
		if ((i = nodeSect.substr(1).lastIndexOf('-')) > -1)
		{
			// expand the parent node instead
			nodeSect = nodeSect.substr(0, i+1);
			expand = true;
		}
		else
		{
			// expand top level
			nodeSect = '';
			expand = true;
		}
			
	// save current expanded node
	if (expand)
		currId = nodeSect;
	else
		currId = false;

	// store parent for each expanded node
	var levelIds = ['','','','','','',''];
	var levelCount = 0;
	if (nodeSect != '')
	{
		levelIds[levelCount++] = nodeSect;
		var temp = nodeSect;
		while ((i = temp.substr(1).lastIndexOf('-')) > -1)
		{
			temp = nodeSect.substr(0, i+1);
			levelIds[levelCount++] = temp;
		}
	}
	levelIds[levelCount++] = '';
		
	// iterate through document
	var showNode;
	var expandNode;
	var nodeId;
	var parent;
	var tags = document.getElementsByTagName('tr');
	for (i = 0; i < tags.length; i++)
	{
		try
		{
			// contract all panels
			if (tags[i].id.substr(0, 4) == 'pnl-')
			{
				tags[i].style.display = 'none';
			}
			else
			{
				// expand appropiate text headers
				if (tags[i].id.substr(0, 4) == 'txt-')
				{
					showNode = false;
					nodeId = tags[i].id.substr(3);

					if (expand)
					{		
						parent = '';
						if ((j = nodeId.substr(1).lastIndexOf('-')) > -1)
							parent = nodeId.substr(0, j+1);

						for (j = 0; j < levelCount; j++)
							if (parent == levelIds[j])
								showNode = true;
					}					
					
					tags[i].style.display = (showNode ? '' : 'none');

					expandNode = false;				
					if (showNode)
						if (nodeId == nodeSect.substr(0, nodeId.length))
							expandNode = true;
							
					if ((obj = document.getElementById('img' + nodeId)) != null)
						obj.src = (expandNode ? downImg : rightImg);
					
					if (showNode)
						showMe(tags[i]);
				}
			}
		}
		catch(e)
		{
			alert(e);
		}
	}
		
	// expand selected node
	if (expand)
		if ((obj = document.getElementById('pnl' + nodeSect)) != null)
		{
			obj.style.display = '';
			showMe(obj);
		}

	void 0;
}

function expandNextNode(nodeSect)
{
	var found = false;
	var tags = document.body.getElementsByTagName('tr');
	
	for (i = 0; i < tags.length; i++)
		if (tags[i].id.substr(0, 4) == 'pnl-')
		{
			if (found)
			{
				expandNode(tags[i].id.substr(3));
				break;
			}
			
			if (tags[i].id.substr(3) == nodeSect)
				found = true;
		}
		
	void 0;
}

function showMe(obj)
{
	var srTop = 0;
	for (var node = obj; node; node = node.offsetParent)
		srTop += node.offsetTop;
	var srBot = srTop + obj.offsetHeight;
	var curTop = self.document.body.scrollTop;
	var curBot = curTop + self.document.body.clientHeight; 
	if ((srBot > curBot) || (srTop < curTop))
	{
		var scrBy = srBot - curBot;
		if (srTop < (curTop + scrBy))
			scrBy = srTop - curTop;
		self.scrollBy(0, scrBy);
	}
}

function startParas()
{
	document.write('<table cols=' + maxLevels + ' width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" ID="tbl-1">');
}

function paraTitle(level, title, comment)
{
	if (inPara)
	{
		postPara(false);
		inPara = false;
	}
	
	currId = '';
	paraNums[level-1]++;
	for (var i = 0; i < level; i++)
		currId += '-' + paraNums[i];
	
  document.write('<tr id="txt' + currId + '" name="txt' + currId + '" style="display:' + (level > 1 ? 'none' : '') + '">');
  if (level > 1)
		document.write('<td colspan=' + (level-1) + '>&nbsp;</td>');
  document.write('<td valign=center align=left width=24><a href="javascript:void()" onclick="expandNode(\'' + currId + '\');return false;">');
  document.write('<img id="img' + currId + '" name="img' + currId + '" src="' + rightImg + '" border="0" /></a>&nbsp;&nbsp;');
  document.write('</td>');
  document.write('<td colspan=' + (maxLevels-level) + ' valign="top">');
  document.write('<a href="javascript:void()" onclick="expandNode(\'' + currId + '\');return false;"><font size="2">' + title + '</font></a>&nbsp;&nbsp;&nbsp;<font size=2>' + comment + '</font>');
  document.write('</td>');
  document.write('</tr>');
  document.write('<tr id="pnl' + currId + '" name="pnl' + currId + '" style="display:none">');
  document.write('<td colspan=' + level + '></td>');
  document.write('<td colspan=' + (maxLevels-level) + ' class="panel">');
  document.write('<ul>');
  
	inPara = true;
}

function postPara(last)
{
  document.write('</ul>');
  if (!last)
	  document.write('<div align=right><a href="javascript:void()" onclick="expandNextNode(\'' + currId + '\');return false;">Next&nbsp;</a></div>');
  document.write('</td>');
  document.write('</tr>');
  
  inPara = false;
}

function endParas()
{
	if (inPara)
	{
		postPara(true);
		inPara = false;
	}

	// force fixed column widths, irrespective of which panels are visible
	document.write('<tr height=100%>');
	for (var i = 1; i < maxLevels; i++)
		document.write('<td width=24>&nbsp;</td>');
	document.write('<td>&nbsp;</td>');
	document.write('</tr>');

	document.write('</table>');
	
	currId = '';
}

function switchContent(url)
{
	var iframe = document.getElementById('ifrContent');
	if (iframe != null)
		iframe.src = url;
	void 0;
}

function restartAnimation(image, blankImage)
{
	var obj;
	var src;
	if ((obj = document.images[image]) != null)
		if ((src = obj.src) != null)
		{
			obj.src = blankImage;
			obj.src = src;
		}
	void 0;
}

function showSource()
{
	document.write('<p>');
  document.write('<a href="javascript:void()" onclick="displaySource();return false;">view page code</a>');
}

function displaySource()
{
	var popup = window.open('', 'CODE', 'height=700, width=800');
	popup.document.writeln('<FORM NAME="SPAM"><TEXTAREA NAME="SRC" ROWS=45 COLS=82></TEXTAREA></FORM>');
	popup.document.SPAM.SRC.value=document.documentElement.innerHTML.replace(/\x00/g,'[CHR$(0)]');
	popup.document.close();
	void 0;
}
