/*
======= Photo Viewer 2.30 ========
==== Created by Josh Freedman ====
====  Web 1 Marketing, Inc.   ====
====   Copyright (C) 2005     ====
====   All rights reserved.   ====
====  www.web1marketing.com   ====
==================================

*/

var showIndex = new Boolean(true); 
var indexSeparator = ' '; 
var showCaption = new Boolean(true); 
var preCache = new Boolean(true); 
var pictureID = 'photo'; 
var captionID = 'caption'; 
var indexID = 'index'; 
var wrapOn = new Boolean(true); 
var slideMode = new Boolean(false); 
var slideDelay = 10; 
var clickMode = new Boolean(true); 
var imageALT = new Boolean(true); 


/*
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();

 separator = ',';

 query = '' + this.location;
 query = query.substring((query.indexOf('?')) + 1);

 if (query.length < 1) { return false; }  
 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('='));

  keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);

  while (keyValue.indexOf('+') > -1) {
   keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);

  }
  keyValue = unescape(keyValue);

  if (FORM_DATA[keyName]) {
   FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
  } else {
   FORM_DATA[keyName] = keyValue;
  }
 }
 return FORM_DATA;
}

FORM_DATA = createRequestObject();


var glbCacheTimer;
var glbSlideTimer;


var glbCurrentPhoto = 1;


var photos = new Array ();


var captions = new Array ();


var linkNames = new Array ();

function getObjectByID(id) {


  if (document.all) { 
    return document.all[id];
  } else {
    return document.getElementById(id);
  }
}


function showPhoto(index) {

  var theURL = "" + this.location;


  if (theURL.indexOf("?")>0) {
    theURL = theURL.substring(0,theURL.indexOf("?"));
  }


  theURL += "?photo=" + index;


  if (slideMode == true) {
    theURL += "&slideMode=true";
    theURL += "&slideDelay=" + slideDelay;
  }


  this.location = theURL;
}

function showNext() {
  if (glbCurrentPhoto >= photos.length) {
    if (wrapOn == true) {
      glbCurrentPhoto = 1;
      showPhoto (glbCurrentPhoto);
    }
  } else {
    glbCurrentPhoto += 1;
    showPhoto (glbCurrentPhoto);
  }
}

function showPrevious() {
  if (glbCurrentPhoto <= 1) {
    if (wrapOn == true) {
      glbCurrentPhoto = photos.length;
      showPhoto (glbCurrentPhoto);
    }
  } else {
    glbCurrentPhoto += -1;
    showPhoto (glbCurrentPhoto);
  }
}

function showFirst() {
	glbCurrentPhoto = 1;
	showPhoto (glbCurrentPhoto);
}

function showLast() {
	glbCurrentPhoto = photos.length;
	showPhoto (glbCurrentPhoto);
}

function initPhoto() {

  var photoLocation = getObjectByID(pictureID);
  var imgString = '';

  if (clickMode == true) {imgString += "<a href='javascript:void(showNext());'>";}
  imgString += "<img border='0' id='mainImage' src='"+ photos[glbCurrentPhoto-1] +"'";
  if (imageALT == true) {imgString += ' alt="'+captions[glbCurrentPhoto-1].replace(/"/g,"'").replace(/<[^>]*>/g,"")+'"';}
  imgString += ">";
  if (clickMode == true) {imgString += "</a>";}
  photoLocation.innerHTML = imgString;


  if (showCaption == true) {
    var photoCaption = getObjectByID(captionID);
    photoCaption.innerHTML = captions[glbCurrentPhoto-1];
  }


  if (showIndex == true) {buildIndex();}


  if ((preCache == true) && (glbCurrentPhoto < photos.length)) {
 
    glbCacheTimer = setTimeout('cache(' + glbCurrentPhoto + ');', 500);
  }

 
  if (slideMode == true) {
    glbSlideTimer = setTimeout('showNext();', (slideDelay * 1000));
  }
}

function cache(photoID) {
 
  if (getObjectByID('mainImage').complete) {

    clearTimeout(glbCacheTimer);

    getObjectByID('cache').src= photos[photoID];
  } else {
    
    glbCacheTimer = setTimeout('cache(' + glbCurrentPhoto + ');', 500);
  }
}

function addPhoto(filename, caption, linkName) {

  var len = photos.length;
  photos[len] = filename;
  captions[len] = caption;
  if (typeof linkName == "undefined") {
	linkNames[len] = len + 1;
  } else {
	linkNames[len] = linkName;
  }
}

function buildIndex() {

  var indexString = '';
  var i;

  for (i = 1; i < photos.length+1; i++) {

    if (i>1) {indexString += indexSeparator}

    if (i == glbCurrentPhoto) {
      indexString += '<b>' + linkNames[i-1] + '</b>';
    } else { 
      indexString += '<a href="javascript:void(showPhoto(' + i + '));">' + linkNames[i-1] + '</a>';
    }
  }

  getObjectByID(indexID).innerHTML = indexString;
}

function enableSlideMode (newDelay) {

  slideMode=Boolean(true);
  if (newDelay > 0) {
    slideDelay = newDelay;
  }
  showPhoto(glbCurrentPhoto); 
}

function disableSlideMode() {
  slideMode = Boolean(false);
  clearTimeout (glbSlideTimer);
  showPhoto(glbCurrentPhoto); 
}


if (FORM_DATA["photo"]>0) {
  glbCurrentPhoto = Number(FORM_DATA["photo"]);
} else {
  glbCurrentPhoto = 1;
}


if (FORM_DATA["slideMode"] == "true") {
  slideMode = Boolean(true);
  slideDelay = FORM_DATA["slideDelay"];
}

