var curSet = 0;
var curPhoto = 0;
var curAlbum;

function loadSet(x,y) {
  // These 4 lines are used to control the state of the numerical incrementation
  // of multiple photo pages. This was updated from just having curSet + 1 as all
  // but the most recent versions of Javascript treated curSet as a String and 
  // concatanated the values as strings rather than using mathematical functions.
  NextSet = new Number;
  PrevSet = new Number;
  NextSet = parseInt(x) + 1;
  PrevSet = parseInt(x) - 1;
	for (i=0; i<6; i++) { //load new thumbnails
		if (curAlbum[x][i]) {
			document.getElementById("photo" + i).innerHTML = "<a href='#' onclick='event.cancelBubble=true;swapPhoto(" + i + "); return false'><img src='/img/" + curAlbum[x][i].photo + "_off.jpg' width='80' height='80' alt='' name='thumb" + i + "'></a>";
		} else {
			document.getElementById("photo" + i).innerHTML = "<img src='/img/spacer.gif' width='80' height='80' alt=''>";
		}
	}
	
	// For the Album-Set Navigation buttons we've added in two things.  One, the references
	// to PrevSet and NextSet as defined above to prevent errors in older JScript versions.
	// Two, we've nested the loadSet() call within an "event.cancelBubble=true" and 
	// "return false" statements.  This keeps the page from Navigating on picture selection
	// and triggering the addition of the # (from the HREF) in the URL - which could impact
	// the parsing of the photo QueryString value on reloads.
	
	if (x == 0) { //deactivate up button if at topmost set, otherwise activate
		document.getElementById("upbutton").innerHTML = "<img src='/img/up_btn_down.gif' width='165' height='15' alt='UP'>";
	} else {
		document.getElementById("upbutton").innerHTML = "<a href='#' onclick='event.cancelBubble=true;loadSet(PrevSet,0);return false'><img src='/img/up_btn.gif' width='165' height='15' alt='UP'></a>";
	}
	if (x == (curAlbum.length - 1)) { //deactivate down button if at final set, otherwise activate
		document.getElementById("downbutton").innerHTML = "<img src='/img/down_btn_down.gif' width='165' height='15' alt='DOWN'>";
	} else {
		document.getElementById("downbutton").innerHTML = "<a href='#' onclick='event.cancelBubble=true;loadSet(NextSet,0);return false'><img src='/img/down_btn.gif' width='165' height='15' alt='DOWN'></a>";
	}
	curSet = x;
	var firstinset = curSet * 6 + 1;
	var lastinset = firstinset + curAlbum[curSet].length - 1;
	countPhotos(firstinset,lastinset);
	if (!y) {
		y = 0;
	}
	swapPhoto(y);
}

function swapPhoto(x) {
	if (x < 0) { //case for back button, goes to previous set
		loadSet(curSet - 1,5);
	} else if (x > (curAlbum[curSet].length - 1)) { //case for next button, goes to next set
		curPhoto = 0;
		loadSet(curSet + 1,0);
	} else {
		if (curAlbum[curSet][curPhoto]) { //reset thumbnails, main photo, caption
			document.images["thumb" + curPhoto].src = "/img/" + curAlbum[curSet][curPhoto].photo + "_off.jpg";
		}
		document.images["thumb" + x].src = "/img/" + curAlbum[curSet][x].photo + "_on.jpg";
		document.images["mainphoto"].src = "/img/" + curAlbum[curSet][x].photo + ".jpg";
		curPhoto = x;
		if ((curSet == 0) && (curPhoto == 0)) { //if at first photo in album, deactivate back button, otherwise activate
			document.getElementById("back").innerHTML = "&#139;&#160;Prev";
		} else {
			document.getElementById("back").innerHTML = "<a href='#' onclick='event.cancelBubble=true;swapPhoto(curPhoto - 1);return false' class='pink'>&#139;&#160;Prev</a>";
		}
		if ((curSet == (curAlbum.length - 1)) && (curPhoto == (curAlbum[curSet].length - 1))) { //if at last photo in album, deactivate next button, otherwise activate
			document.getElementById("next").innerHTML = "Next&#160;&#155;";
		} else {
			document.getElementById("next").innerHTML = "<a href='#' onclick='event.cancelBubble=true;swapPhoto(curPhoto + 1);return false' class='pink'>Next&#160;&#155;</a>";
		}
	}
}


function countPhotos(x,y) {
	total = 0;
	for (i=0; i<curAlbum.length; i++) {
		total = total + curAlbum[i].length;
	}
	if (x == y) {
		tmp = x;
	} else {
		tmp = x + "-" + y;
	}
	document.getElementById("photocounter").innerHTML = tmp + " of " + total + " Photos";
}

function init() {
  // See init2() below.  It is now being called from the Photo Pages.
  // This function has been rewritten because only the most recent JScript supports
  // the Request object, and it is not yet part of the W3C standard.  On client
  // browsers that do not support it, it yields Script Errors as the "request object
  // is not found."
  try {
	  if(request) {
		  tmpx = (request["photos"].charAt(0)) * 1;
		  tmpy = (request["photos"].charAt(1)) * 1;
		  loadSet(tmpx,tmpy);
	  } else {
		  loadSet(0);
	  }
	}
  catch(E){
    loadSet(0);
  }
}

// Bio Page Thumbnail functionality
function genThumbs() {
	tmp = '<table cellspacing="0" cellpadding="0" border="0"><tr>';
	if (curAlbum[0][0]) {
		tmp += '<td><a href="honorees_' + curAlbum.name + '_photos.aspx?photos=00"><img src="/img/' + curAlbum[0][0].photo + '_off.jpg" width="80" height="80" alt="" border="0"></a></td>';
	}
	if (curAlbum[0][1]) {
		tmp += '<td class="pad10lft"><a href="honorees_' + curAlbum.name + '_photos.aspx?photos=01"><img src="/img/' + curAlbum[0][1].photo + '_off.jpg" width="80" height="80" alt="" border="0"></a></td>';
	}
	if (curAlbum[0][2]) {
		tmp += '<td class="pad10lft"><a href="honorees_' + curAlbum.name + '_photos.aspx?photos=02"><img src="/img/' + curAlbum[0][2].photo + '_off.jpg" width="80" height="80" alt="" border="0"></a>';
	}
	tmp += '</td></tr></table>';
	document.write(tmp);
}


// Updated Functionality "Overrides"
//
// This replaces init() above.  It effectively overwrites the init() procedure, being called
// from the Photo Pages, allowing the processing of the x/y coordiantes of the photos to be
// handled in ASP.Net rather than stripped via the JScript - which all browsers do not support.

function init2(x,y) {
  // The logical statement checks for values in the X/Y parameters passed in
  // from the Photo Page.  Here's the snippet of ASP.Net code being used within
  // the Photo Pages to populate the X/Y from the QueryString:
  /*
		<script language="javascript" type="text/javascript">
		  <%
		    string photo = Request.QueryString["photos"];
	      string x = "";
	      string y = "";
		    try{
		      // Populates X/Y from a valid QS.
		      x = photo.Substring(0,1);
		      y = photo.Substring(1,1);
		      }
		    catch{
		    }
		      // Produces init2('', '') When no QS Present.
          Response.Write("init2('" + x + "','" + y + "');");
		  %>  
    </script>
  */
  if ( (x != '') && (y != '') ) {  
    // Load the Image Set and Photo.
    loadSet(x,y);
  }
  else {
    // Load the Default Image Set.
    loadSet(0);
  }
}

