﻿/* *****************************************************************************
gt_slideshow.js
Cross-browser slideshow code using opacity and Trident alpha filter effects. 

Based on "Cross-Browser BlendTrans Filter Javascript" version 1.3 (7 June 2004) 
downloaded from http://brainerror.net/media/scripts/js/blendtrans/blendtrans.js

Differences: two images rather than an image and a div; use of globals; automated 
file path lookups; changed brower hacks as appropriate for 2007.

Note:
Giantech red std  = #c72133
Giantech red dark = #871623

originator  = Wayne Chang (Nov 2007)
last modify = Wayne Chang (Nov 2007)
***************************************************************************** */

// ===========================================================================================================
// SETUP
// ===========================================================================================================

// -----------------------------------------------------------------------------------------------------------
// global vars
// -----------------------------------------------------------------------------------------------------------
var g_shw = null;			// slideshow off by default; activated by page load.
var g_shw_playing;			// tracks whether the repeating slideshow is playing
var g_shw_looped = true;	// track when slideshow has played one complete loop

var g_shw_timer = 5000;		// fade every 5 seconds (5000 ms)
var g_shw_speed = 700;		// time one fade takes to complete

var g_item_type;			// these three vars set by body onload
var g_item_name;
var g_item_num = 2;			// start at 2 to skip first image for first fade

var g_item_num_max = null;	// determines when slideshow wraps back to first image

// -----------------------------------------------------------------------------------------------------------
// init_globals1: get slideshow source type and name prefix from filename
// -----------------------------------------------------------------------------------------------------------
function init_globals1() {
	var the_url = window.location.href;
	var counter = the_url.length;

	var filename_start;
	var filename_end;

	// walk backwards from filename extension; dynamic instead of assuming '.html' extension.
	while ( ( the_url.substring( counter, counter - 1 ) ) != "." ) {
		counter--;
	}
	filename_end = counter;

	// walk backwards from filename to last slash in full URL
	while ( ( the_url.substring( counter, counter - 1 ) ) != "/") {
		counter--;
	}
	filename_start = counter;

	// set item name global here; remove trailing period with -1 and remove leading type indicator with +2.
	g_item_name = the_url.substring( filename_end - 1, filename_start + 2 );

	// set item type global here; remove trailing underscore with +1; no leader removal.
	g_item_type = the_url.substring( filename_start + 1, filename_start );
}

// -----------------------------------------------------------------------------------------------------------
// init_globals2: get slideshow item qty from number of actual tertiary nav links
// -----------------------------------------------------------------------------------------------------------
function init_globals2() {
	var nav_ter_array = [];
	var nav_ter_counter = 0;

	do {
		nav_ter_array[ nav_ter_counter ] = document.getElementById( 'item_nav_ter_' + ( nav_ter_counter + 1 ) );
		nav_ter_counter++;
	}
	while( nav_ter_array[ nav_ter_counter - 1 ] != undefined );

	g_item_num_max = nav_ter_array.length;
}


// ===========================================================================================================
// FADE FUNCTIONS
// ===========================================================================================================

// -----------------------------------------------------------------------------------------------------------
// set_opacity: set opacity of specified id
// -----------------------------------------------------------------------------------------------------------
function set_opacity( the_opacity, the_id ) {
	var the_target = document.getElementById( the_id );

	// set opacity, Trident
	if( navigator.appName.indexOf( 'Microsoft' ) != -1 && parseInt( navigator.appVersion ) >= 4 )
 		the_target.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + the_opacity + ')';
	else
		// set opacity, normal browsers
		the_target.style.opacity = ( the_opacity / 100 );
}

//------------------------------------------------------------------------------------------------------------
// ok_to_fade: test for full or zero opacity
//------------------------------------------------------------------------------------------------------------
function ok_to_fade() {
	var is_ok = false;
	var the_target = document.getElementById( 'hero_incoming' );

	if( navigator.appName.indexOf( 'Microsoft' ) != -1 && parseInt( navigator.appVersion ) >= 4 )
	{
//  		if( (the_target.runtimeStyle.filter.alpha == 0) || (the_target.runtimeStyle.filter.alpha == 100) )
 			is_ok = true;
	}
	else
	{
		if( the_target.style.opacity == 0 || the_target.style.opacity == 1 )
			is_ok = true;
	}

	return is_ok;
}


// ===========================================================================================================
// PLAYBACK CONTROLS
// ===========================================================================================================



// -----------------------------------------------------------------------------------------------------------
// start_over: end the slideshow, wait 7 seconds, then start it again
// -----------------------------------------------------------------------------------------------------------
function start_over( ) {
	window.clearInterval( g_shw );
	
	shw_goto( g_item_type, g_item_name, 1 );
	g_shw = window.setInterval( "shw_advance()", g_shw_timer );
}



// -----------------------------------------------------------------------------------------------------------
// shw_start: start loop
// -----------------------------------------------------------------------------------------------------------
function shw_start( ) {
	init_globals1(); // get slideshow source type and name prefix
	init_globals2(); // get slideshow item qty

	if( g_shw == null )
		g_shw = window.setInterval( "shw_advance()", g_shw_timer );

	g_shw_playing = true;
}

// -----------------------------------------------------------------------------------------------------------
// shw_stop
// -----------------------------------------------------------------------------------------------------------
function shw_stop( t ) {
	if ( g_shw != null )
	{
		window.clearInterval( g_shw );
		g_shw = null;

		g_shw_playing = false;
	}
}

// -----------------------------------------------------------------------------------------------------------
// shw_advance: check if at last image; go to image (next image determined by shw_goto)
// -----------------------------------------------------------------------------------------------------------
function shw_advance() {
	if( (g_item_num + 1) > g_item_num_max )
	{
		g_item_num = 1;
		g_shw_looped = true;
		return start_over();
	}

	shw_goto( g_item_type, g_item_name, g_item_num );
}

// -----------------------------------------------------------------------------------------------------------
// shw_goto: save current image, fade to specified image, then save new current image
// -----------------------------------------------------------------------------------------------------------
function shw_goto( g_item_type, g_item_name, item_num ) {
	// subdirectory for image path (derived from g_item_type)
	var path_stub = null;

	// id of the images
	var id_hero_current = 'hero_current';
	var id_hero_incoming = 'hero_incoming';

	// store the images
	var img_hero_current = document.getElementById( id_hero_current );
	var img_hero_incoming = document.getElementById( id_hero_incoming );

	// some timing vars
	var the_speed = Math.round( g_shw_speed / 100 );
	var the_timer = 0; 

	// set subdirectory for image path
	switch( g_item_type ) {
		case 'f':
		case 'p':	path_stub = '_projects'; // projects and features use same hero images
					break;
		case 'c':	path_stub = '_contact';
					break;
		case 'a':	path_stub = '_awards';
					break;
		default:	path_stub = '_projects';
	}

	if( ok_to_fade() ) // test for incomplete fade, to prevent stacking fades
	{
		g_item_num = item_num; // save current image before fading

		// path incoming image for fade-in
		img_hero_incoming.src = '../_media/' + path_stub + '/' + g_item_name + '_' + g_item_num + '.jpg';

		// save new current image
		g_item_num++;

		// set proper link highlight only if fade actually happened; calling here has beneficial side-effect 
		// of a live display of which picture is currently being shown by slideshow.
		shw_link_heat( item_num );

		// re-highlight the slideshow link if the slideshow is playing
		if( g_shw_playing )
			document.getElementById( 'item_nav_ter_slideshow' ).className = 'item_nav_ter_hot';
	}
}

