//------------------------------------------------------------------------------
//
//  Constants
//
//------------------------------------------------------------------------------

/**
 *  A list of the page ids.
 */
var PAGE_IDS = ["overview",
				"adhesives",
		   		"boxes",
     			"marking",
			    "safety",
			    "sanitation",
			    "steel",
			    "storage",
			    "stretch",
			    "tape",
			    "industrial"];

/**
 *  The delay in milliseconds between pages when the slideshow is running.
 */
var SLIDE_SHOW_DELAY = 9500;

/**
 *  The duration in seconds of the fade in effect when changing pages.
 */
var FADE_IN_DURATION = 0.5;

//------------------------------------------------------------------------------
//
//  Variables
//
//------------------------------------------------------------------------------

/**
 *  The id of the slideshow interval used to flip through pages.
 */
var slideShowInterval;

/**
 * The current page number.
 */
var pageNum = -1;

/**
 * The id of the currently displayed page.
 */
var currentId = "overview";

//------------------------------------------------------------------------------
//
//  Functions
//
//------------------------------------------------------------------------------

/**
 *  Randomly flip through all pages with a delay between each flip.
 */
function startSlideShow(delay)
{
   slideShowInterval = setInterval("nextPage()", delay);
}

/**
 * Stop the slideshow.
 */
function stopSlideShow()
{
	if (slideShowInterval)
	{
		clearInterval(slideShowInterval);
		slideShowInterval = "";
	}
}

/**
 *  Show the next page in the list.
 */
function nextPage()
{
	pageNum++;

	// Start back at the beginning when we've gone through the list
	if (pageNum == PAGE_IDS.length)
		pageNum = 0;

	setPage(PAGE_IDS[pageNum]);
}

/**
 * Set the current page to the page matching newId.
 */
function setPage(newId)
{
	var el;

	// Hide the current page
	el = $(currentId + "-page");
	if (el) el.style.display = "none";

	// Fade in the new page
	el = $(newId + "-page");
	if (el) el.appear({ duration: FADE_IN_DURATION });

	// Set the current tab back to a rollover link
	el = $(currentId + "-tab");
	if (el) el.innerHTML = "<a href=\"#\" onclick=\"" + currentId + "_clickHandler()\" class=\"" + currentId + "\"></a>";

	// Set the new tab to a static div
	el = $(newId + "-tab");
	if (el) el.innerHTML = "<div class=\"" + newId + "\"></div>";

	currentId = newId;
}

//------------------------------------------------------------------------------
//
//  Event handlers
//
//------------------------------------------------------------------------------

/**
 *  Initialize the page and start the slideshow.
 */
function onLoadHandler()
{
	// Shuffle the order of the pages
	for (var i = PAGE_IDS.length - 1; i > -1; i--)
	{
		var j = Math.floor(Math.random() * ( i + 1 ));

		var tempi = PAGE_IDS[i];
		var tempj = PAGE_IDS[j];

		PAGE_IDS[i] = tempj;
		PAGE_IDS[j] = tempi;
   }

   // Set the page num to the current page
   for (var i = 0; i < PAGE_IDS.length; i++)
   {
		if (PAGE_IDS[i] == currentId)
		{
			pageNum = i;
			break;
		}
	}

	startSlideShow(SLIDE_SHOW_DELAY);
}

/**
 *  A handler for the adhesives tab click event.
 */
function overview_clickHandler()
{
	stopSlideShow();
	setPage("overview");
}

/**
 *  A handler for the adhesives tab click event.
 */
function adhesives_clickHandler()
{
	stopSlideShow();
	setPage("adhesives");
}

/**
 *  A handler for the boxes tab click event.
 */
function boxes_clickHandler()
{
	stopSlideShow();
	setPage("boxes");
}

/**
 *  A handler for the industrial tab click event.
 */
function industrial_clickHandler()
{
	stopSlideShow();
	setPage("industrial");
}

/**
 *  A handler for the marking tab click event.
 */
function marking_clickHandler()
{
	stopSlideShow();
	setPage("marking");
}

/**
 *  A handler for the safety tab click event.
 */
function safety_clickHandler()
{
	stopSlideShow(); 
	setPage("safety");  
}

/**
 *  A handler for the sanitation tab click event.
 */
function sanitation_clickHandler()
{
	stopSlideShow(); 
	setPage("sanitation");  
}

/**
 *  A handler for the steel tab click event.
 */
function steel_clickHandler()
{
	stopSlideShow(); 
	setPage("steel");  
}

/**
 *  A handler for the storage tab click event.
 */
function storage_clickHandler()
{
	stopSlideShow(); 
	setPage("storage");  
}

/**
 *  A handler for the stretch tab click event.
 */
function stretch_clickHandler()
{
	stopSlideShow(); 
	setPage("stretch");  
}

/**
 *  A handler for the tape tab click event.
 */
function tape_clickHandler()
{
	stopSlideShow(); 
	setPage("tape");  
}