//////////////////////////////////
// SUBNAV
var subnavLifetime	= 3;	// number of seconds before a subnav disappears
var wrapperId		= 'container';
var offsetX			= 13;	// x offset in pixels from the left of the main nav
var offsetY			= -12;	// y offset in pixels from the bottom of the main nav

var subnavsActive = new Array();
var subnavs = {
'do':		[
	{'url': '/do-punk/consultancy-services/',	'title': 'consultancy services'},
	{'url': '/do-punk/speaking-engagements/',	'title': 'speaking engagements'},
	{'url': '/do-punk/punk-junk/',				'title': 'punk junk'}
	],
'think':	[
	{'url': '/think-punk/book-excerpts/',	'title': 'book excerpts'},
	{'url': '/think-punk/leftovers/',		'title': 'leftovers'},
	{'url': '/punk-blog/',					'title': 'punk blog'}
	]
};

function rollNav(id)
{
	// complain about invalid nav ids
	if (!subnavs[id])
	{
		alert('Invalid id ('+ id +') passed to rollNav');
		return;
	}
	var subnavId = id + 'Subnav';
	// if the subnav has already been created, just reset its countdown timer
	if ($(subnavId))
	{
		$(subnavId).countdown = subnavLifetime;
		return;
	}
	// creating a new nav, so clear any old ones
	clearSubnavs();
	// get the location and dimensions of the parent nav element
	var parent = $(id);
	Position.prepare();
	var pos		= Position.cumulativeOffset(parent);
	var size	= Element.getDimensions(parent);
	var subLeft	= pos[0] + offsetX;
	var subTop	= pos[1] + size.height + offsetY;

	// create the subnav block
	var subnavBlock = document.createElement('div');
	subnavBlock.id				= subnavId;
	subnavBlock.className		= 'subnav';
	subnavBlock.style.position	= 'absolute';
	subnavBlock.style.left		= subLeft +'px';
	subnavBlock.style.top		= subTop +'px';
//	subnavBlock.style.width		= size.width +'px';
	subnavBlock.countdown		= subnavLifetime;
	for (var i = 0; i < subnavs[id].length; i++)
	{
		var linkData = subnavs[id][i];
		var link = document.createElement('a');
		link.href = linkData.url;
		link.innerHTML = linkData.title;
		link.parentBlock = subnavBlock;
		link.onmouseover = function()
		{
			this.parentBlock.countdown = subnavLifetime;
		}
		subnavBlock.appendChild(link);
		if (subnavs[id][i + 1])
			subnavBlock.appendChild(document.createTextNode(' | '));
	}
	$(wrapperId).appendChild(subnavBlock);
	subnavsActive.push(subnavBlock);
}

function monitorSubnavs()
{
	for (var i = 0; i < subnavsActive.length; i++)
	{
		var element = subnavsActive[i];
		element.countdown--;
		if (element.countdown <= 0)
		{
			Element.remove(element);
			subnavsActive.splice(i, 1);
			i--;
		}
	}
	setTimeout("monitorSubnavs()", 1000);
}

function clearSubnavs()
{
	for (var i = 0; i < subnavsActive.length; i++)
	{
		Element.remove(subnavsActive[i]);
		subnavsActive.splice(i, 1);
		i--;
	}
}
window.onload = monitorSubnavs;


//////////////////////////////////
// RANDOM IMAGE DISPLAY
var randImageUrl	= '/wp-content/themes/default/images/';
var randImageWidth	= 170;
var randImageHeight	= 376;
var randImages = [
'illus01.gif',
'illus02.gif',
'illus03.gif',
'illus04.gif',
'illus05.gif',
'illus06.gif',
'illus07.gif',
'illus08.gif',
'illus09.gif',
'illus10.gif',
'illus11.gif',
'illus12.gif',
'illus13.gif',
'illus14.gif',
'illus15.gif',
'illus16.gif',
'illus17.gif',
'illus18.gif',
'illus19.gif',
'illus20.gif',
'illus21.gif',
'illus22.gif'
];
function showRandomImage()
{
	var idx = Math.round(Math.random() * (randImages.length - 1));
	var imgTag = '<img src="'+ randImageUrl + randImages[idx] +'" alt="Buy the Book!" width="'+ randImageWidth +'" height="'+ randImageHeight +'"/>';
	document.write(imgTag);
}