/*-----------------------------------------------------------------------------
-	This file contains proprietary and confidential information from WebAssist.com
-	corporation.  Any unauthorized reuse, reproduction, or modification without
-	the prior written consent of WebAssist.com is strictly prohibited.
-
-	Copyright 2006 WebAssist.com Corporation.  All rights reserved.
------------------------------------------------------------------------------*/
function WA_LoadAlbumXML(){
	if (window.XMLHttpRequest) {
		WAPA_XMLHTTP = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		WAPA_XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (WAPA_XMLHTTP) {
		WAPA_XMLHTTP.onreadystatechange = WAPA_handleStateChange;
		WAPA_XMLHTTP.open("GET", WAPA_albumXML, true);
		WAPA_XMLHTTP.send(null);
	}
}

function WAPA_handleStateChange(){
	if (WAPA_XMLHTTP.readyState == 4) {
		if (WAPA_XMLHTTP.responseText.indexOf('<wa') != 0) {
//				alert('bad news'); didnt' get XML response.
				return;
		}
		var response = WAPA_XMLHTTP.responseXML.documentElement;
		var children = response.childNodes;
		var numNodes = children.length;
		var photosFolder = WAPA_dotDotSlash + response.getAttribute("photosfolder");
		var wapa_photoObjs = new Array();
		for(var i=0;i<numNodes;i++){
			if(children[i].tagName == 'wa_photoassist_photo'){ // avoid text nodes
				var src = photosFolder + children[i].getAttribute('file');
				var title = unescape(children[i].getAttribute('title'));
				var caption = unescape(children[i].getAttribute('caption'));
				var date = unescape(children[i].getAttribute('date'));
				var width = children[i].getAttribute('xdim');
				var height = children[i].getAttribute('ydim');
				var thumbWidth = children[i].getAttribute('txdim');
				var thumbHeight = children[i].getAttribute('tydim');
				var thumbType = children[i].getAttribute('thumbtype');
        var tThumb = children[i].getAttribute('thumb');
        var thumbSrc = "";
        if ((!tThumb || tThumb == "") && thumbType != 0) {
          tThumb = children[i].getAttribute('file');
        }
        if (tThumb) {
          if (tThumb.toLowerCase().indexOf("file://") == 0) {
            thumbSrc = tThumb;
          }
          else {
  				  thumbSrc = photosFolder + 'Thumbs/' + tThumb;
          }
        }
				wapa_photoObjs.push(new WA_Photo(src, width, height, title, date, caption, thumbSrc, thumbWidth, thumbHeight));
			}
		}
		WAPA_setupAlbum(wapa_photoObjs, photosFolder);
	}
}

function WA_Photo(src, photoW, photoH, title, date, caption, thumbSrc, thumbW, thumbH){
	this.src = src;
	this.width = photoW;
	this.height = photoH;
	this.title = title;
	this.date = date;
	this.caption = caption;
	this.thumbSrc = thumbSrc;
	this.thumbWidth = thumbW;
	this.thumbHeight = thumbH;
}

function WA_PhotoAlbum(photoObjs, displayIds, thumbnailType, rowsPerPage, colsPerPage, defaultThumbW, defaultThumbH, photosFolder ){
  this.navSet = 0;
  this.navSetSize = 0;
  this.navLinkArray = new Array();
	this.photos = photoObjs;
	this.displayIDs = displayIds;
	this.thumbnailType = thumbnailType;
	this.currentPhotoIndex = 0;
	this.currentPage = 0;
	this.rowsPerPage = rowsPerPage;
	this.colsPerPage = colsPerPage;
	this.defaultThumbWidth = defaultThumbW;
	this.defaultThumbHeight = defaultThumbH;
	this.pagingSeparator = ' &nbsp;|&nbsp; ';
	this.mode = "slideshow";
	this.slideShow = new WAPA_Slideshow(this);
	this.WAPA_RequestVars = new Object();
	this.txSrc = photosFolder + 'Images/_tx_.gif';
	
	this.showPhoto = WA_PhotoAlbum_showPhoto;
	this.firstPhoto = WA_PhotoAlbum_firstPhoto;
	this.lastPhoto = WA_PhotoAlbum_lastPhoto;
	this.nextPhoto = WA_PhotoAlbum_nextPhoto;
	this.previousPhoto = WA_PhotoAlbum_previousPhoto;
	this.showThumbnailPreview  = WA_PhotoAlbum_showThumbnailPreview;
	this.clearThumbnailPreview = WA_PhotoAlbum_clearThumbnailPreview;
	this.setThumbnailPage = WA_PhotoAlbum_setThumbnailPage;
	
	this.initialize = WA_PhotoAlbum_initialize;
	this.setRequestVars = WA_PhotoAlbum_setRequestVars;
	this.setupDisplayObjects = WA_PhotoAlbum_setupDisplayObjects;
	this.getThumbnailPageForIndex = WA_PhotoAlbum_getThumbnailPageForIndex;
	this.getNumberOfPages = WA_PhotoAlbum_getNumberOfPages;
	this.getThumbsHTML = WA_PhotoAlbum_getThumbsHTML;
	this.highlightThumbnail = WA_PhotoAlbum_highlightThumbnail;
	
	this.togglePlayer   = WA_PhotoAlbum_togglePlayer;
	this.updateNavHTML  = WA_PhotoAlbum_updateNavHTML;
  this.previousNavSet = WA_PhotoAlbum_previousNavSet;
  this.navPhoto       = WA_PhotoAlbum_navPhoto;
  this.nextNavSet     = WA_PhotoAlbum_nextNavSet;
}

function WA_PhotoAlbum_initialize(){
	this.setRequestVars();
	this.setupDisplayObjects();
	this.displayThumbs.innerHTML = this.getThumbsHTML();
	var imageParam = this.WAPA_RequestVars["image"];
	if(imageParam &&  !isNaN(imageParam)){
		var imageParamInt = parseInt(imageParam);
		if(imageParamInt > 0){
			if(imageParamInt > this.photos.length){
				imageParamInt = this.photos.length;
			}
		}
		else{
			imageParamInt = 1;
		}
		this.showPhoto(imageParamInt-1);
	}
	else{
		this.firstPhoto();
	}
	if(this.WAPA_RequestVars["slideshow"] && this.WAPA_RequestVars["slideshow"] == 'start'){
	    this.mode = "viewer";
	}
	this.togglePlayer();
	WAPA_Slideshow_indicatorHighlight(2);
	if(this.WAPA_RequestVars["slideshow"] && this.WAPA_RequestVars["slideshow"] == 'start'){
		this.slideShow.start();
	}
}

function WA_PhotoAlbum_setRequestVars(){
	var qs = document.location.search;
	if (qs != '') {
		var pairs = qs.substring(1).split(/&amp;|&/);
		for (var i=0; i<pairs.length; i++) {
			pairs[i] = pairs[i].split("=");
			if (pairs[i][0] != ""){ this.WAPA_RequestVars[pairs[i][0]] = pairs[i][1];}
		}
	}
	
}

function WA_PhotoAlbum_setupDisplayObjects(){
	this.displayImg = document.getElementById(this.displayIDs.photoImg);
	this.displayPhotoTitle = document.getElementById(this.displayIDs.photoTitle);
	this.displayPhotoCaption = document.getElementById(this.displayIDs.photoCaption);
	this.displayPhotoDate = document.getElementById(this.displayIDs.photoDate);
	this.displayX = document.getElementById(this.displayIDs.display_X);
	this.displayY = document.getElementById(this.displayIDs.display_Y);
	this.displayThumbs = document.getElementById(this.displayIDs.displayThumbs);
	this.displayThumbnailTitle = document.getElementById(this.displayIDs.displayThumbnailTitle);
	this.displayThumbnailPreview = document.getElementById(this.displayIDs.displayThumbnailPreview);
	this.displaySNav = document.getElementById(this.displayIDs.displaySNav);
	this.displayVNav = document.getElementById(this.displayIDs.displayVNav);
  this.navLinkArray = new Array();
  //code for disabling rollover area when none or thumbnails thumbs
  if (this.thumbnailType == 1 || this.thumbnailType == 3) {
    var tWrapper = document.getElementById("WAPA_ThumbnailPreviewWrapper");
    if (tWrapper) {
      tWrapper.innerHTML = "";
    }
  }
  //code for numeric nav
  var keepGoing = true;
  for (this.navSetSize=0; keepGoing; this.navSetSize++) {
    var theNavLink = document.getElementById("WAPA_ViewerNav_SetPhoto" + this.navSetSize);
    if (theNavLink) {
      this.navLinkArray.push(theNavLink);
    }
    else {
      keepGoing = false;
      break;
    }
  }
}

function WA_PhotoAlbum_showPhoto(idx){
	this.currentPhotoIndex = idx;

	var currentImage = this.photos[idx];
  this.displayImg.src = this.txSrc;
  this.displayImg.alt = escape(this.photos[idx].title);
	if(currentImage.width != -1 ){
		this.displayImg	.width = currentImage.width;
	}
	else{
		this.displayImg.removeAttribute('width');
	}
	if(currentImage.height != -1 ){
		this.displayImg	.height = currentImage.height;
	}
	else{
		this.displayImg.removeAttribute('height');
	}
	this.displayImg.src = currentImage.src;

	this.displayPhotoTitle.innerHTML = currentImage.title;
	this.displayPhotoCaption.innerHTML = currentImage.caption;

	if(this.displayPhotoDate){
		this.displayPhotoDate.innerHTML = currentImage.date;
	}
	
	this.displayX.innerHTML = idx+1;
	this.displayY.innerHTML = this.photos.length;
	var thumbPage = this.getThumbnailPageForIndex(idx);
	if(thumbPage != this.currentPage){
		this.setThumbnailPage(thumbPage, idx);
	}
	else{
		this.highlightThumbnail(idx);
	}
  this.updateNavHTML();
}

function WA_PhotoAlbum_firstPhoto(){
	this.showPhoto(0);
}

function WA_PhotoAlbum_lastPhoto(){
	this.showPhoto(this.photos.length-1);
}

function WA_PhotoAlbum_nextPhoto(){
	var numPhotos = this.photos.length;
	
	if(++this.currentPhotoIndex >= numPhotos){
		this.currentPhotoIndex = 0;
	}
	this.showPhoto(this.currentPhotoIndex);
}

function WA_PhotoAlbum_previousPhoto(){
	var numPhotos = this.photos.length;
	
	if(--this.currentPhotoIndex < 0){
		this.currentPhotoIndex = numPhotos-1;
	}
	this.showPhoto(this.currentPhotoIndex);
}

function WA_PhotoAlbum_togglePlayer() {
  if (this.mode == "slideshow") {
    if (this.displaySNav) {
      this.displaySNav.className = "WAPA_SlideshowToggle";
      this.displaySNav.height    = 0;
    }
    if (this.displayVNav) this.displayVNav.className = "WAPA_SlideshowToggle_Set";
    this.slideShow.pause();
    this.mode = "viewer";
  }
  else {
    if (this.displayVNav) {
      this.displayVNav.className = "WAPA_SlideshowToggle";
      this.displayVNav.height    = 0;
    }
    if (this.displaySNav) this.displaySNav.className = "WAPA_SlideshowToggle_Set";
    this.mode = "slideshow";
  }
  this.updateNavHTML();
}

function WA_PhotoAlbum_updateNavHTML() {
  if (this.mode == "slideshow") {
    if (this.displaySNav) {
      var theLinkS = document.getElementById("WAPA_Slideshow_PlayPauseText");
      var theLinkI = document.getElementById("WAPA_Slideshow_PlayPauseIcon");
      var theImage = document.getElementById("WAPA_Slideshow_PlayPauseImage");
      if (theImage) {
        if (this.slideShow.isRunning && !this.slideShow.isPaused) {
          if (theImage.alt && theImage.alt != "") {
            theImage.alt = "pause";
          }
          theImage.src = theImage.src.substring(0, theImage.src.lastIndexOf("/")+1) + "pause_reg.gif";
        }
        else {
          if (theImage.alt && theImage.alt != "") {
            theImage.alt = "play";
          }
          theImage.src = theImage.src.substring(0, theImage.src.lastIndexOf("/")+1) + "next_reg.gif";
        }
      }
      if (theLinkI) {
        if (this.slideShow.isRunning && !this.slideShow.isPaused) {
          theLinkI.innerHTML = "||";
        }
        else {
          theLinkI.innerHTML = "&gt;";
        }
      }
      if (theLinkS) {
        if (this.slideShow.isRunning && !this.slideShow.isPaused) {
          theLinkS.innerHTML = "pause";
        }
        else {
          theLinkS.innerHTML = "play";
        }
      }
    }
  }
  else {
    if (this.displayVNav) {
      if (this.navLinkArray.length > 0) {
        this.navSet  = Math.floor(this.currentPhotoIndex / this.navSetSize);
        var startNum = (this.navSet * this.navSetSize) + 1;
        for (var n=0; n<this.navSetSize; n++) {
          if (((startNum + n)-1) < this.photos.length) {
            this.navLinkArray[n].innerHTML = (startNum + n);
          }
          else {
            this.navLinkArray[n].innerHTML = "";
          }
          if (((startNum + n)-1) == this.currentPhotoIndex) {
            this.navLinkArray[n].className = "WAPA_ViewerNav_SetPhotoSel";
          }
          else {
            this.navLinkArray[n].className = "";
          }
        }
      }
    }
  }
}

function WAPA_NavImageOver(tImage) {
  if (tImage && tImage.src) {
    var tSrc = tImage.src;
    if (tSrc.lastIndexOf("_reg.") > 0)
      tSrc = tSrc.substring(0, tSrc.lastIndexOf("_reg.")) + "_roll" + tSrc.substring(tSrc.lastIndexOf("_reg.") + 4);
    tImage.src = tSrc;
  }
}

function WAPA_NavImageOut(tImage) {
  if (tImage && tImage.src) {
    var tSrc = tImage.src;
    if (tSrc.lastIndexOf("_roll.") > 0)
      tSrc = tSrc.substring(0, tSrc.lastIndexOf("_roll.")) + "_reg" + tSrc.substring(tSrc.lastIndexOf("_roll.") + 5);
    tImage.src, tSrc;
  }
}

function WAPA_SpeedImageOver(tImage) {
  if (tImage && tImage.className) {
    tImage.className = tImage.className + "_Over";
  }
}

function WAPA_SpeedImageOut(tImage) {
  if (tImage && tImage.className && tImage.className.lastIndexOf("_Over") >= 0) {
    tImage.className = tImage.className.substring(0, tImage.className.lastIndexOf("_Over"));
  }
}

function WA_PhotoAlbum_previousNavSet() {
  if (this.navSet > 0) {
    this.navSet--;
    this.showPhoto((this.navSet * this.navSetSize));
  }
}

function WA_PhotoAlbum_navPhoto(theNavIndex) {
  var tPhoto = (this.navSet * this.navSetSize) + theNavIndex;
  if (tPhoto < this.photos.length) {
    this.showPhoto(tPhoto);
  }
}

function WA_PhotoAlbum_nextNavSet() {
  var numSets = Math.floor(this.photos.length / this.navSetSize);
  if (this.navSet < numSets) {
    this.navSet++;
    this.showPhoto((this.navSet * this.navSetSize));
  }
}

function WA_PhotoAlbum_showThumbnailPreview(idx){
	if(this.displayThumbnailPreview){
		var src = this.photos[idx].thumbSrc;
		if(src!=""){
			this.displayThumbnailPreview.src = src;
			this.displayThumbnailPreview.height = this.photos[idx].thumbHeight;
			this.displayThumbnailPreview.width = this.photos[idx].thumbWidth;
		}
		else{
			this.displayThumbnailPreview.src = this.txSrc;
			this.displayThumbnailPreview.width = 0;
			this.displayThumbnailPreview.height = 0;
		}
	}
	if(this.displayThumbnailTitle){
		this.displayThumbnailTitle.innerHTML = this.photos[idx].title;
	}
}

function WA_PhotoAlbum_clearThumbnailPreview(){
	if(this.displayThumbnailPreview){
		this.displayThumbnailPreview.src = this.txSrc;
		this.displayThumbnailPreview.width = 0;
		this.displayThumbnailPreview.height = 0;
	}
	if(this.displayThumbnailTitle){
		this.displayThumbnailTitle.innerHTML = '&nbsp;';
	}
}

function WA_PhotoAlbum_highlightThumbnail(idx){
	var numPhotos = this.photos.length;
	if(this.rowsPerPage == -1){
		var startIdx = 0;
	}
	else{
		var photosPerPage = this.rowsPerPage*this.colsPerPage;
		var startIdx = this.currentPage*photosPerPage;
		if(numPhotos > (startIdx + photosPerPage)){
			numPhotos = startIdx + photosPerPage;
		}
		else{
			numPhotos = startIdx + (numPhotos-startIdx);
		}

	}

	switch(this.thumbnailType){
	
		case 1:
		// no navigation
			break;
	
		case 2:
		case 3:
			for(var i =startIdx;i<numPhotos; i++){
				var img = document.getElementById('WAPA_Thumbnail' + i);
	
				if(i==idx){
					img.parentNode.className = 'WAPA_Thumbnail_Selected';
					img.parentNode.parentNode.focus();
				}
				else{
					img.parentNode.className = 'WAPA_ThumbnailLink';
				}
			}
			break;
		
		case 4:
			for(var i =startIdx;i<numPhotos; i++){
				var img = document.getElementById('WAPA_ThumbnailLink' + i);
				if(i==idx){
					img.parentNode.className = 'WAPA_Thumbnail_Selected';
					img.parentNode.focus();
				}
				else{
					img.parentNode.className = 'WAPA_ThumbnailLink';
				}
			}
			break;
	}
}

function WA_PhotoAlbum_setThumbnailPage(pageNumber, idx){
	this.currentPage = pageNumber;
	this.displayThumbs.innerHTML = this.getThumbsHTML(this.currentPage);
	if(idx){
		this.showPhoto(idx);
	}
	else{
		idx = (this.rowsPerPage == -1) ? 0 : this.currentPage*(this.rowsPerPage*this.colsPerPage);
		this.showPhoto(idx);
	}
}

function WA_PhotoAlbum_getThumbnailPageForIndex(idx){
	return (this.rowsPerPage == -1) ? 0 : Math.floor(idx/(this.rowsPerPage*this.colsPerPage));
}

function WA_PhotoAlbum_getNumberOfPages(){
	return (this.rowsPerPage == -1) ? 1 : Math.ceil(this.photos.length/(this.rowsPerPage * this.colsPerPage));
}

function WA_PhotoAlbum_getThumbsHTML(pageNumber){
	var numPhotos = this.photos.length;
	if(this.rowsPerPage == -1){
		var startIndex = -1 * (pageNumber||this.currentPage) * this.rowsPerPage * this.colsPerPage;
	}
	else{
		var startIndex = (pageNumber||this.currentPage) * this.rowsPerPage * this.colsPerPage;
		numPhotos = this.getNumberOfPages() * this.rowsPerPage * this.colsPerPage;
	}

	var anchorStart ='<a id="WAPA_ThumbnailLink@@WA_Photo_IDX@@" class="WAPA_ThumbnailLink" href="#" title="@@WA_Photo_ALT@@" onmouseover="wapa_album.showThumbnailPreview(@@WA_Photo_IDX@@);" onfocus="this.onmouseover();" onmouseout="wapa_album.clearThumbnailPreview();" onblur="this.onmouseout();" onclick="wapa_album.showPhoto(@@WA_Photo_IDX@@); return false;">';
	var anchorEnd = '</a>';
	var thumbsHTML = '';
				
	switch(this.thumbnailType){
		case 1:
			thumbsHTML = '';
			break;
		case 2:
			var startChunk = '<table cellspacing="0" cellpadding="0"><tr>\n';
			var endChunk = '</tr></table>';
			var itemStart = '<td>' + anchorStart + '<img width="10" height="10" id="WAPA_Thumbnail@@WA_Photo_IDX@@" class="WAPA_Thumbnail" border="0" alt="@@WA_Photo_ALT@@" src="';
			var emptyItemStart = '<td><img width="10" height="10" id="WAPA_Thumbnail@@WA_Photo_IDX@@" class="WAPA_Thumbnail_Empty" border="0" alt="" src="';
			var itemEnd = '" />' + anchorEnd + '</td>\n';
			var currentRow = 1;

			for(var i=startIndex;i< numPhotos; i++){
				if(i < this.photos.length){
					var title = this.photos[i].title;
					thumbsHTML += itemStart.replace(/@@WA_Photo_IDX@@/g, i).replace(/@@WA_Photo_ALT@@/g, title) + this.txSrc + itemEnd;
				}
				else{
					thumbsHTML += emptyItemStart.replace(/@@WA_Photo_IDX@@/g, i) + this.txSrc + itemEnd;
				}

				if(i!= 0 && (i+1)%this.colsPerPage == 0 && i != numPhotos-1){
					thumbsHTML += '</tr>\n';
					currentRow++;
					if(this.rowsPerPage > 0 && currentRow>this.rowsPerPage){
						break;
					}
				}
			}
			
			thumbsHTML = startChunk + thumbsHTML + endChunk;
			break;
		case 3:
			var startChunk = '<table cellspacing="0" cellpadding="0"><tr>\n';
			var endChunk = '</tr></table>';
			var itemStart = '<td>' + anchorStart + '<img id="WAPA_Thumbnail@@WA_Photo_IDX@@" class="WAPA_Thumbnail" width="@@WA_Thumb_Width@@" height="@@WA_thumbHeight@@" border="0" alt="@@WA_Photo_ALT@@" src="';
			var emptyItemStart = '<td><img id="WAPA_Thumbnail@@WA_Photo_IDX@@" class="WAPA_Thumbnail_Empty" width="@@WA_Thumb_Width@@" height="@@WA_thumbHeight@@" border="0" alt="" src="';
			var itemEnd = '" />' + anchorEnd + '</td>\n';
			var currentRow = 1;

			for(var i=startIndex;i< numPhotos; i++){
				if(i < this.photos.length){
					var w = this.photos[i].thumbWidth;
					var h = this.photos[i].thumbHeight;
					var src = this.photos[i].thumbSrc;
					var title = this.photos[i].title;
					if(src==""){
						src = this.txSrc;	
					}				
					thumbsHTML += itemStart.replace(/@@WA_Photo_IDX@@/g, i).replace(/@@WA_Thumb_Width@@/g,  w).replace(/@@WA_thumbHeight@@/g, h).replace(/@@WA_Photo_ALT@@/g, title) + src + itemEnd;
				}
				else{
					var w = this.defaultThumbWidth;
					var h = this.defaultThumbHeight;
					var src = this.txSrc;
					thumbsHTML += emptyItemStart.replace(/@@WA_Photo_IDX@@/g, i).replace(/@@WA_Thumb_Width@@/g,  w).replace(/@@WA_thumbHeight@@/g, h) + src + itemEnd;
				}
				if(i!= 0 && (i+1)%this.colsPerPage == 0 && i != numPhotos-1){
					thumbsHTML += '</tr>\n';
					currentRow++;
					if(this.rowsPerPage > 0 && currentRow>this.rowsPerPage){
						break;
					}
				}
			}
			
			thumbsHTML = startChunk + thumbsHTML + endChunk;
			break;
		case 4:
			var startChunk = '<table><tr>\n';
			var endChunk = '</tr></table>';
			var currentRow = 1;
			for(var i=startIndex;i< numPhotos; i++){
				if(i < this.photos.length){
					var title = this.photos[i].title;
					thumbsHTML += '<td>' + anchorStart.replace(/@@WA_Photo_IDX@@/g, i).replace(/@@WA_Photo_ALT@@/g, title) + (((i+1) <10)? ('0'+ (i+1)) : (i+1)) + anchorEnd + '&nbsp;</td>';
				}
				else{
					thumbsHTML += '<td>&nbsp;&nbsp;&nbsp;</td>';
				}
				if(i!= 0 && (i+1)%this.colsPerPage == 0 && i != numPhotos-1){
					thumbsHTML += '</tr>\n';
					currentRow++;
					if(this.rowsPerPage > 0 && currentRow>this.rowsPerPage){
						break;
					}
				}
			}
			
			thumbsHTML = startChunk + thumbsHTML + endChunk;
			break;
	}
	
	var pagingItems = new Array();
	var numPages = this.getNumberOfPages();
	var shouldExlcudePaging = (numPages == 1);
	if(!shouldExlcudePaging){
    pagingItems.push("Set: ");
		for(var m=0;m<=(numPages-1);m++){
      if (m != 0) pagingItems.push(this.pagingSeparator);
			if((m) == this.currentPage){
				pagingItems.push(m+1);
			}
			else{
				pagingItems.push('<a href="#" class="WAPA_ThumbnailPagingLink" onclick="wapa_album.setThumbnailPage(' + m + '); return false;">' + (m+1) + '</a>');
			}
		}
		thumbsHTML += pagingItems.join("");
	}

	return thumbsHTML;
}

function WAPA_Slideshow(photoAlbum){
	this.album = photoAlbum;
	this.speed = 5;
	this.slowestSpeed = 5;
	this.fastestSpeed = 1;
	this.speedIncrement = 1;
	this.timer = null;
	this.isRunning = false;
	this.isPaused = false;
	
	this.start = WAPA_Slideshow_start;
	this.stop =  WAPA_Slideshow_stop;
	this.pause = WAPA_Slideshow_pause;
  this.playpause = WAPA_Slideshow_playpause;
	this.setSpeed = WAPA_Slideshow_setSpeed;
	this.slowDown = WAPA_Slideshow_slowDown;
	this.speedUp = WAPA_Slideshow_speedUp;

	this.next = WAPA_Slideshow_next;
	this.callLater = WAPA_Slideshow_callLater;
}

function WAPA_Slideshow_start(){
  if (!this.isRunning || (this.isRunning && this.isPaused)) {
  	this.isRunning = true;
  	if(this.isPaused){ // resumes from current position if started after a pause
  		this.isPaused = false;
  	}
  	this.next();
  }
}

function WAPA_Slideshow_next(){
	if(this.isRunning && !this.isPaused){
		var fnRef = this.callLater(this.album, 'nextPhoto', this, 'next');
		this.timer = setTimeout(fnRef, this.speed*1000);
	}
}

function WAPA_Slideshow_callLater(album, method, slideshow, ssMethod){
    return (function(){
        /* This inner function is to be executed with - setTimeout
           - and when it is executed it can read, and act upon, the
           parameters passed to the outer function:-
        */
    if (method) {
		  album[method]();
    }
    if (ssMethod) {
		  slideshow[ssMethod]();
    }
    });
}

function WAPA_Slideshow_stop(){
	this.isRunning = false;
	clearTimeout(this.timer);
}

function WAPA_Slideshow_pause(){
	if(this.isRunning){
		this.isPaused = true;
		clearTimeout(this.timer);
    this.album.updateNavHTML();
	}
}

function WAPA_Slideshow_playpause() {
	if(this.isRunning && this.isPaused){
    this.start();
		var fnRef = this.callLater(this.album, 'updateNavHTML');
		this.pptimer = setTimeout(fnRef, 1);
  }
  else {
    this.pause();
		var fnRef = this.callLater(this.album, 'updateNavHTML');
		this.pptimer = setTimeout(fnRef, 1);
  }
}

function WAPA_Slideshow_setSpeed(newSpeed){
	if(newSpeed > this.slowestSpeed){
		newSpeed = this.slowestSpeed;
	}
	if(newSpeed < this.fastestSpeed){
		newSpeed = this.fastestSpeed;
	}
	this.speed = newSpeed;
  WAPA_Slideshow_indicatorHighlight((newSpeed - 5) * -1);
}

function WAPA_Slideshow_slowDown(){
	if(this.speed < this.slowestSpeed){
		var newSpeed = this.speed+this.speedIncrement; 
		if(newSpeed > this.slowestSpeed){
			newSpeed = this.slowestSpeed;	
		}
		this.setSpeed(newSpeed);
	}
}

function WAPA_Slideshow_speedUp(){
	if(this.speed > this.fastestSpeed){
		var newSpeed = this.speed-this.speedIncrement;
		if(newSpeed < this.fastestSpeed){
			newSpeed = this.fastestSpeed;	
		}
		this.setSpeed(newSpeed);
	}
}

// Not a method of the WAPA_Slideshow obejct, but rather a helper function
function WAPA_Slideshow_indicatorHighlight(indicatorIdx){
	var indicator = document.getElementById('WAPA_SlideshowSpeedIndicatorWrapper');
	var indicatorImgs = indicator.getElementsByTagName('img');
	var numImgs = indicatorImgs.length
  if (numImgs == 0) {
    indicatorImgs = indicator.getElementsByTagName('span');
    numImgs = indicatorImgs.length;
  }
	for(var i=0; i< numImgs; i++){
		if(i <= indicatorIdx){
			indicatorImgs[i].className = 'WAPA_SlideshowSpeedIndicator_Set';
		}
		else{
			indicatorImgs[i].className = 'WAPA_SlideshowSpeedIndicator';
		}
	}
}

function WAPA_setupControls(photosFolder){
	wapa_controls = new Array();
	wapa_controls.push(new WAPA_Control('WAPA_albumsControl', photosFolder, 'Images/albums_reg.gif', 'Images/albums_roll.gif', 'Images/albums_sel.gif'));
	wapa_controls.push(new WAPA_Control('WAPA_slideshowControl', photosFolder, 'Images/slideshow_reg.gif', 'Images/slideshow_roll.gif', 'Images/slideshow_sel.gif'));
	wapa_controls.push(new WAPA_Control('WAPA_viewerControl', photosFolder,  'Images/browse_reg.gif', 'Images/browse_roll.gif', 'Images/browse_sel.gif'));
	wapa_controls.activate = WAPA_Controls_Activate;

	if(wapa_album.mode =='slideshow'){
		wapa_controls[1].setActive(true);
		wapa_controls[2].setActive(false);
	}
	else{
		wapa_controls[1].setActive(false);
		wapa_controls[2].setActive(true);
	}

}

function WAPA_Control(id, photosFolder, regSrc, rollSrc, selSrc, active){
	this.object = document.getElementById(id);
	this.regSrc = photosFolder + regSrc;
	this.rollSrc = photosFolder + rollSrc;
	this.selSrc = photosFolder + selSrc;
	this.active = (this.object.src == this.selSrc) ? true:false;
	this.hover = WAPA_Control_Hover;
	this.off = WAPA_Control_Off;
	this.setActive = WAPA_Control_SetActive;
}

function WAPA_Control_Hover(){
	if(!this.active){
		this.object.src = this.rollSrc;
	}
}

function WAPA_Control_Off(){
	if(!this.active){
		this.object.src = this.regSrc;
	}
}

function WAPA_Control_SetActive(isActive){
	if(isActive){
		this.active = true;
		this.object.src = this.selSrc;
	}
	else{
		this.active = false;
		this.object.src = this.regSrc;
	}
}

function WAPA_Controls_Activate(idx){
	for(var i=0;i<wapa_controls.length;i++){
		if(i==idx){
			if(idx==1 && !wapa_controls[i].active){
				wapa_album.slideShow.start();
				wapa_album.togglePlayer();
        wapa_album.updateNavHTML();
			}
			if(idx==2 && !wapa_controls[i].active){
				wapa_album.slideShow.stop();
				wapa_album.togglePlayer();
        wapa_album.updateNavHTML();
			}
			wapa_controls[i].setActive(true);
		}
		else{
			wapa_controls[i].setActive(false);
		}

	}
}
