//
// Determine the browser type for id support
//

// Requires browser.js.

// Map the popups in a global array
var popups = new Array();
var scale = new Array();
var currentIndex = 0;
var	rightHeight = 329;
var	leftHeight = 159;
var cachedImages = new Array();

//
// Popup functionality
//

// Show the indicated popup
function showPopup(index)
{
	hidePopup(true); // clear any existing content
	sizePopup(scale[index]);
	previous_image = getRef('previous-image'); 
	next_image = getRef('next-image');

	// Adjust the navigation according to index.
	if (index == 0)
		previous_image.className = 'previous-link-empty';
	else
		previous_image.className = 'previous-link';

	// Adjust the navigation according to index.
	if (index == (popups.length - 1))
		next_image.className = 'next-link-empty';
	else
		next_image.className = 'next-link';

	p = getRef('popup');
	p.style.visibility = 'visible';

	// Used cached images if possible.
	if ( typeof( cachedImages[index] ) != 'undefined' )
	{
		document.images['popup_image'].src = cachedImages[index].src;
	}
	else
	{
		document.images['popup_image'].src = popups[index];
	}

	pc = getRef('popup_' + index);
	pc.style.visibility = 'visible';
	currentIndex = index; // save state
}

// Show the next popup by index
function showNext()
{
	if ((currentIndex + 1) == popups.length) return;
	hidePopup(true);
	showPopup((currentIndex + 1));
}

// Show the previous popup by index
function showPrevious()
{
	if ((currentIndex - 1) < 0) return;
	hidePopup(true);
	showPopup((currentIndex - 1));
}

// Hide all popup layers
function hidePopup(content_only)
{
	if ( !content_only )
	{
		p = getRef('popup');
		p.style.visibility = 'hidden';
	}

	// Loop through possible popup content layers, hiding each
	for ( i = 0; i < popups.length; i++ )
	{
		pc = getRef('popup_' + i);
		pc.style.visibility = 'hidden';
	}
}

function sizePopup(px)
{
	right = getRef('right-strut');
	left = getRef('left-strut');
	right.style.height = rightHeight + px + 'px';
	left.style.height = leftHeight + px + 'px';
}

//
// Add event handlers to the popup controls.
//
popupEvents = function()
{
	previous_link = getRef('previous-link');
	next_link = getRef('next-link');

	previous_link.onmouseover = function()
	{
		previous_image = getRef('previous-image'); 
		if (previous_image.className != 'previous-link-empty')
			previous_image.className = 'previous-link-over';
	}

	next_link.onmouseover = function()
	{
		next_image = getRef('next-image'); 
		if (next_image.className != 'next-link-empty')
			next_image.className = 'next-link-over';
	}

	previous_link.onmouseout = function()
	{
		previous_image = getRef('previous-image'); 
		if (previous_image.className != 'previous-link-empty')
			previous_image.className = 'previous-link';
	}

	next_link.onmouseout = function()
	{
		next_image = getRef('next-image'); 
		if (next_image.className != 'next-link-empty')
			next_image.className = 'next-link';
	}

	// Preload the popup images after page loads.
	for ( i = 0; i < popups.length; i++ )
	{
		cachedImages[i] = new Image;
		cachedImages[i].src = popups[i];
	}
}

// Attach workaround to NS browsers
if (NS && window.addEventListener)
	window.addEventListener("load", popupEvents, false);
else
	attachEvent("onload", popupEvents);
