/*
======= Icon Viewer 2.30 ========
==== Created by Josh Freedman ====
====  Web 1 Marketing, Inc.   ====
====   Copyright (C) 2005     ====
====   All rights reserved.   ====
====  www.web1marketing.com   ====
==================================

Use of these scripts is free so long as you include proper
attribution (the block above) and a link from your site to
www.web1marketing.com.

Use these scripts at your own risk. The author assumes no
responsibility for support or for consequences of their use.

Comments and suggestions welcome: www.joshfreedman.com.

IT IS HIGHLY RECOMMENDED THAT NO CHANGES BE MADE TO THIS FILE AND
THAT ALL PARAMETER VALUES BE CUSTOMIZED IN THE PHOTO_LIST.JS FILES.

==== Version Notes ====
Version 2.30 Added optional index parameter
Version 2.22 Fixed clickMode and iconAlt behaviors
Version 2.21 Moved precaching icon to bottom of page
Version 2.20 Added showFirst, showLast, click icon to advance, caption as icon ALT text
Version 2.13 Added slideshow features
Version 2.12 Parameterized ID's, wrapping optional
Version 2.11 Added caching of next icon
Version 2.10 Added index functionality
Version 2.00 Functional browsing of icons forward and back
*/

// ====================
// ==== Parameters ====
// ====================
var showIndex = new Boolean(true); // Enable index display
var indexSeparator = ' '; // String between index items
var showCaption = new Boolean(true); // Enable caption display
var preCache = new Boolean(true); // Activates pre-caching of next icon
var iconID = 'icon'; // SPAN or DIV ID for icon
var captionID = 'caption'; // SPAN or DIV ID for caption
var indexID = 'index'; // SPAN or DIV ID for index
var wrapOn = new Boolean(true); // Wrapping from last icon to first and vice-versa
var slideMode = new Boolean(false); // Enables slideshow mode
var slideDelay = 10; // Default delay between slides inseconds
var clickMode = new Boolean(true); // Disable icon clicking to advance
var iconALT = new Boolean(true); // Disable caption as icon ALT tag text

// ==========================
// ==== Third Party Code ====
// ==========================

/*
Webmonkey GET Parsing Module
Language: JavaScript 1.0

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Patrick Corcoran
Author Email: patrick@taylor.org
*/

function createRequestObject() {
 FORM_DATA = new Object();
 // The Object ("Array") where our data will be stored.
 separator = ',';
 // The token used to separate data from multi-select inputs
 query = '' + this.location;
 query = query.substring((query.indexOf('?')) + 1);
 // Keep everything after the question mark '?'.
 if (query.length < 1) { return false; }  // Perhaps we got some bad data?
 keypairs = new Object();
 numKP = 1;
 while (query.indexOf('&') > -1) {
  keypairs[numKP] = query.substring(0,query.indexOf('&'));
  query = query.substring((query.indexOf('&')) + 1);
  numKP++;
 }
 keypairs[numKP] = query;
 for (i in keypairs) {
  keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
  // Left of '=' is name.
  keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
  // Right of '=' is value.
  while (keyValue.indexOf('+') > -1) {
   keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
   // Replace each '+' in data string with a space.
  }
  keyValue = unescape(keyValue);
  // Unescape non-alphanumerics
  if (FORM_DATA[keyName]) {
   FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
  } else {
   FORM_DATA[keyName] = keyValue;
  }
 }
 return FORM_DATA;
}

FORM_DATA = createRequestObject();

// =============================
// ==== The heart of it all ====
// =============================


var glbCacheTimer;
var glbSlideTimer;

// Contains index of the current icon, initialized to the first icon (1)
var glbCurrentIcon = 1;

// Array holding icon filenames
var icons = new Array ();

// Array holding icon captions
var captions = new Array ();

// Array holding link names
var linkNames = new Array ();

function getObjectByID(id) {
  // Cross-browser function to return the object with the specific id

  if (document.all) { // IE
    return document.all[id];
  } else { // Netscape
    return document.getElementById(id);
  }
}


function showIcon(index) {
  // Shows the icon with identified index
  var theURL = "" + this.location;

  // Strip parameters, if any present, from end of URL.
  if (theURL.indexOf("?")>0) {
    theURL = theURL.substring(0,theURL.indexOf("?"));
  }

  // Append the new icon index as a parameter.
  theURL += "?icon=" + index;

  // Append the slideshow mode as a parameter.
  if (slideMode == true) {
    theURL += "&slideMode=true";
    theURL += "&slideDelay=" + slideDelay;
  }

  // Go to the constructed URL which has the new
  // icon's index as a parameter.
  this.location = theURL;
}

function showNext() {
  if (glbCurrentIcon >= icons.length) {
    if (wrapOn == true) {
      glbCurrentIcon = 1;
      showIcon (glbCurrentIcon);
    }
  } else {
    glbCurrentIcon += 1;
    showIcon (glbCurrentIcon);
  }
}

function showPrevious() {
  if (glbCurrentIcon <= 1) {
    if (wrapOn == true) {
      glbCurrentIcon = icons.length;
      showIcon (glbCurrentIcon);
    }
  } else {
    glbCurrentIcon += -1;
    showIcon (glbCurrentIcon);
  }
}

function showFirst() {
	glbCurrentIcon = 1;
	showIcon (glbCurrentIcon);
}

function showLast() {
	glbCurrentIcon = icons.length;
	showIcon (glbCurrentIcon);
}

function initIcon() {
  // Display the icon
  var iconLocation = getObjectByID(iconID);
  var imgString = '';

  if (clickMode == true) {imgString += "";}
  imgString += "<img border='0' id='mainIcon' src='"+ icons[glbCurrentIcon-1] +"'";
  if (iconALT == true) {imgString += ' alt="'+captions[glbCurrentIcon-1].replace(/"/g,"'").replace(/<[^>]*>/g,"")+'"';}
  imgString += ">";
  if (clickMode == true) {imgString += "</a>";}
  iconLocation.innerHTML = imgString;

  // Create caption if enabled.
  if (showCaption == true) {
    var iconCaption = getObjectByID(captionID);
    iconCaption.innerHTML = captions[glbCurrentIcon-1];
  }

  // Build the index if enabled.
  if (showIndex == true) {buildIndex();}

  // Pre-cache if enabled.
  if ((preCache == true) && (glbCurrentIcon < icons.length)) {
    // Start timer for cache loader routine to check if main icon is loaded
    glbCacheTimer = setTimeout('cache(' + glbCurrentIcon + ');', 500);
  }

  // Slideshow mode, if enabled.
  if (slideMode == true) {
    glbSlideTimer = setTimeout('showNext();', (slideDelay * 1000));
  }
}

function cache(iconID) {
  // Check to see if main icon has loaded
  if (getObjectByID('mainIcon').complete) {
    // Clear the timer
    clearTimeout(glbCacheTimer);
    // Load the next icon.
    getObjectByID('cache').src= icons[iconID];
  } else {
    // Not loaded, so reset timer
    glbCacheTimer = setTimeout('cache(' + glbCurrentIcon + ');', 500);
  }
}

function addIcon(filename, caption, linkName) {
  // Add filenames and captions to their respective arrays.
  var len = icons.length;
  icons[len] = filename;
  captions[len] = caption;
  if (typeof linkName == "undefined") {
	linkNames[len] = len + 1;
  } else {
	linkNames[len] = linkName;
  }
}

function buildIndex() {
  // Creates a clickable list of icon numbers.
  var indexString = '';
  var i;

  for (i = 1; i < icons.length+1; i++) {
    // If not the first icon, add separator
    if (i>1) {indexString += indexSeparator}
    // Make current icon # bold and don't make it a link
    if (i == glbCurrentIcon) {
      indexString += '<b>' + linkNames[i-1] + '</b>';
    } else { // Make all other numbers links
      indexString += '' + linkNames[i-1] + '</a>';
    }
  }
  // Display the index
  getObjectByID(indexID).innerHTML = indexString;
}

function enableSlideMode (newDelay) {
  // Turns slide mode on
  slideMode=Boolean(true);
  if (newDelay > 0) {
    slideDelay = newDelay;
  }
  showIcon(glbCurrentIcon); //necessary to reset URL parameters.
//  glbSlideTimer=setTimeout('showNext();',(slideDelay*1000));
}

function disableSlideMode() {
  slideMode = Boolean(false);
  clearTimeout (glbSlideTimer);
  showIcon(glbCurrentIcon); //necessary to reset URL parameters.
}

// Get icon index from URL, if there is one, otherwise start at 1
if (FORM_DATA["icon"]>0) {
  glbCurrentIcon = Number(FORM_DATA["icon"]);
} else {
  glbCurrentIcon = 1;
}

// Get slide mode from URL, if there is one
if (FORM_DATA["slideMode"] == "true") {
  slideMode = Boolean(true);
  slideDelay = FORM_DATA["slideDelay"];
}
