var photoGallerySlideShowSpeed = 2400  // delay (in ms) between photo gallery slideshow advances
var slideShowIntervalID
var slideShowRunning = false
var slideShowLoaded = false
var aPhotos = new Array()  // array of photo objects
var aThumbs = new Array()  // array of thumbnail photo id's
var currentThumb = 0
var currentPhoto = 0
var thumbCount = 0
var qs = new Querystring()
	
window.onload = finalPhotoGalleryStuff;
	
function finalPhotoGalleryStuff() {

	/*number of rows * number of columns*/
	thumbCount = document.getElementById('tblThumbs').rows.length * document.getElementById('tblThumbs').rows.item(0).cells.length;
	createThumbs(Math.min(thumbCount, aPhotos.length));
	preloadThumbs();
	
	/*
		setThumbs - sets the selected aPhotos thumb to be the first to appear in aThumbs
		photoIndex - finds the position of the photo with matching ID in aPhotos
	*/
	setThumbs(photoIndex(qs.get("ID","")));
	clickThumb(0);

	preloadSlideShowImages();

    if (qs.get("action","")=='print') {
		printPage();
    };	
	
};

/* * * * * * Photo Gallery * * * * * */

function ImagePreloader(imgs,callback) {
	// store the callback
   this.callback = callback;
   // initialize internal state.
   this.nLoaded = 0;
   this.nProcessed = 0;
   this.aImages = new Array;
   // record the number of images.
   this.nImages = imgs.length;
   // for each image, call preload()
   this.preload = ImagePreloader.prototype.preload;
   this.onComplete = ImagePreloader.prototype.onComplete;
   for (var i=0; i < imgs.length; ++i) {
		this.preload(imgs[i].src)
		}
   }
 
ImagePreloader.prototype.preload = function(imgsrc) {
   // create new Image object and add to array
   var oImage = new Image;
   this.aImages.push(oImage);
   // set up event handlers for the Image object
   oImage.onload = ImagePreloader.prototype.onload;
   oImage.onerror = ImagePreloader.prototype.onerror;
   oImage.onabort = ImagePreloader.prototype.onabort;
   // assign pointer back to this.
   oImage.oImagePreloader = this;
   oImage.bLoaded = false;
   // assign the .src property of the Image object
   oImage.src = imgsrc;
	}
 
ImagePreloader.prototype.onComplete = function() {
   this.nProcessed++;
   if ( this.nProcessed == this.nImages ) {
      //what's failing here on the home page?
      try {
		this.callback(this.aImages, this.nLoaded);
	  }
	  catch (e) {
	  }	  
		}
	}

ImagePreloader.prototype.onload = function()
{
 this.bLoaded = true;
 this.oImagePreloader.nLoaded++;
 this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function() {
 this.oImagePreloader.bError = true;
 this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function()
{
 this.bAbort = true;
 this.oImagePreloader.onComplete();
}

/* moved to marvin.js because it is used by residential case studies */
/* 
function photoGalleryPopup(ID,Gallery,Action) {
	if (!Gallery) {Gallery=''}
	if (!Action) {Action=''}
	newWindow = window.open('photoPopup.aspx?ID=' + ID + '&Gallery=' + Gallery + '&action=' + Action,'caseStudy','top=45,left=80,width=775,height=540,menubar=yes,scrollbars=no',false);
}
*/

function objPhoto(id,title,urlThumb,urlLarge,copy) {
	this.id = id;
	this.title = title;
	this.urlThumb = urlThumb;
	this.urlLarge = urlLarge;
	this.copy = copy;
	this.selectImage = selectImage;
	this.setThumb = setThumb;
	this.isSelected = false;	//JH - image request gallery selections
	}
	
	function selectImage() {
	
		/*	
			Opera does not support the onload event for the image tag 
			so we have to call the ImageRequestGallery code responsible 
			for toggling between the request/unrequest image states 
			when the selected image changes (see ImageRequestGallery.ascx).
			It's wrapped in a try/catch block because this control is not
			on the other gallery pages.
			
			*********************************************************
			TODO - This will be much better handled by raising an 
			event. However, I have not yet found an easy to implement
			method for creating custom events in Javascript. The best
			method I have found for decoupling code is available at:
			www.truerwords.net/articles/web-tech/custom_events.html
			(demo at media.truerwords.net/events/)
			*********************************************************
		*/
		try {
			toggleRequestImage();
		} catch (e) {
			//alert(e.message);
		};
		
		document.getElementById('imgBigImage').src = this.urlLarge;
		
		document.getElementById('imgBigImage').alt = this.title;
		
		document.getElementById('lblTitle').innerHTML = this.title;
		document.getElementById('lblDetails').innerHTML = this.copy;
		if (document.getElementById('ahrefEmailPhoto')) {
			document.getElementById('ahrefEmailPhoto').href = 'javascript:emailContent(\'http://www.marvin.com/photoPopup.aspx?ID=' + this.id + '\',\'' + this.title + '\')'
		}
		currentPhoto = this.id;
		checkArrows();
		}
		
	function setThumb(thumb) {
		document.getElementById('imgThumb'+thumb).src = this.urlThumb;
		}

function preloadThumbs() {
	for (var p = 0; p < aPhotos.length; ++p) {
		aPhotos[p].imgThumb = new Image()
		aPhotos[p].imgThumb.src = aPhotos[p].urlThumb;
		}
	}
	
function preloadSlideShowImages() {
	var a = new Array()
	for (var p = 0; p < aPhotos.length; ++p) {
		a[p] = new Image()
		a[p].src = aPhotos[p].urlLarge;
		}
	ImagePreloader(a,finishPreloadSlideShowImages);
	if (window.navigator.userAgent.indexOf('Opera') > 0) {finishPreloadSlideShowImages()};
	}

function finishPreloadSlideShowImages() {
	if (aPhotos.length > 1) {document.getElementById('ahrefSlideShow').innerHTML = 'Play Slideshow'};
	
	slideShowLoaded = true;
	}
	
function checkArrows() {
	
	if (aThumbs[currentThumb] > 0) {
	 // Not on the first image of the gallery, Show Back arrow
		if (document.getElementById('imgImageArrowPrevious') && aPhotos.length > 1) {
		 document.getElementById('imgImageArrowPrevious').src = 'images/back_off.gif'}
		} else {
		  // On the first image of the gallery, Hide Back arrow
		if (document.getElementById('imgImageArrowPrevious')) {
		 document.getElementById('imgImageArrowPrevious').src = 'images/clear.gif'}
		}
	if (aThumbs[currentThumb] >= (aPhotos.length - 1)) {
	 // On the last image of the gallery, Hide Next arrow
		if (document.getElementById('imgImageArrowNext')) {
		 document.getElementById('imgImageArrowNext').src = 'images/clear.gif'}
		} 
		else {
	  // Not on the last image of the gallery, Show Next arrow
		 if (document.getElementById('imgImageArrowNext')) {
		  document.getElementById('imgImageArrowNext').src = 'images/next_off.gif'}
		 } 
	if (aThumbs[0] == 0) {
	 // Hide Back link if on first set of images ?? not sure
		document.getElementById('imgArrowPrev').src = 'images/clear.gif'
		} else {
		document.getElementById('imgArrowPrev').src = 'images/back_off.gif'
		}
	if (aThumbs[aThumbs.length - 1] >= (aPhotos.length - 1)) {
	 // Hide link to Next set of images if nine or fewer images
		document.getElementById('imgArrowNext').src = 'images/clear.gif'
		} else {
		document.getElementById('imgArrowNext').src = 'images/next_off.gif'
		}
	if (aPhotos.length <= 1) {
	 // Hide Play Slideshow if only one image or no images
		if (document.getElementById('ahrefSlideShow')) {
		 document.getElementById('ahrefSlideShow').innerHTML = ''}
		}
	setProgress();
	}
	
function setProgress() {
	if (document.getElementById('lblProgress')) {
		var lastThumb
		lastThumb = aThumbs[0] + aThumbs.length - 1
		if (lastThumb >= aPhotos.length) {
			lastThumb = aPhotos.length-1
		}
		document.getElementById('lblProgress').innerHTML = (aThumbs[0]+1) + '-' + (lastThumb+1) + ' of ' + aPhotos.length 
		}
	}
	
function setThumbs(initial) {  
	//resets thumbnail set starting with photo array id "initial"
	for (var t = 0; t < aThumbs.length; ++t) {
		aThumbs[t] = t + initial;
		if (aThumbs[t] < aPhotos.length) {
			aPhotos[aThumbs[t]].setThumb(t);
		} else {
			document.getElementById('imgThumb'+t).src = 'images/clear.gif';
		}
	}
	currentThumb=0;
}
	
function createThumbs(count) {
	for (var t = 0; t < count; ++t) {
		aThumbs[t]=t;
	}
}

function advanceThumbs(interval) {  
	//advances thumbnail set by the positive or negative "interval" value passed
	if ((aThumbs[0] + interval) < 0) {interval = -1};
	if (((aThumbs[0] + interval) >= 0) && ((aThumbs[0] + interval) <= aPhotos.length)) {
		setThumbs(aThumbs[0] + interval);
		}
	resetThumbClasses();
	//findPhoto(currentPhoto);  this line was preventing the checkArrows from working properly
	checkArrows();
	}
	
function findPhoto(id) {  //finds and selects a photo by record ID if it exists in current thumbnail set
	for (var t=0; t < aThumbs.length;++t) {
		if (aPhotos[aThumbs[t]].id == id) {
			clickThumb(t);
		}
	}
}
function photoIndex(imageID) {  //finds and selects a photo by record ID if it exists in current thumbnail set
	for (var t=0; t < aPhotos.length;++t) {
		if (aPhotos[t].id == imageID) {
			return t;
		}
	}
	return 0;
}
		
function slideAdvance() {
	if (aThumbs[currentThumb] >= (aPhotos.length-1)) {
		setThumbs(0);
		} else {
		clickThumb(currentThumb + 1)
		}
	clickThumb(currentThumb);
	}

function slideShow() {
	if (slideShowLoaded) {
		if (slideShowRunning) {
			stopSlideShow()
			} else if (aPhotos.length>1) {
			slideShowRunning = true;
			slideShowIntervalID = window.setInterval(slideAdvance,photoGallerySlideShowSpeed);
			if (document.getElementById('ahrefSlideShow')) {document.getElementById('ahrefSlideShow').innerHTML = 'Stop Slideshow'}
			}
		}
	}
	
function stopSlideShow() {
	slideShowRunning = false
	window.clearInterval(slideShowIntervalID);
	document.getElementById('ahrefSlideShow').innerHTML = 'Play Slideshow';
	checkArrows();
	}
	
function resetThumbClasses() {
	for (var t = 0; t < aThumbs.length; ++t) {
		document.getElementById('imgThumb' + t).className='photothumb';
		}
	}

function clickThumb(thumb) {
	if ((thumb < 0) && (aThumbs[0] > 0)) {  //click a thumbnail before the current set
		advanceThumbs(aThumbs.length * -1);
		clickThumb(aThumbs.length + thumb)
	} else if (thumb > (aThumbs.length-1)) {  //click a thumbnail after the current set
		advanceThumbs(aThumbs.length);
		clickThumb(thumb - aThumbs.length);
	} else if (aThumbs[thumb] < aPhotos.length) {  //click a thumbnail within the current set
		if (aThumbs[thumb] <= aPhotos.length) {
			currentThumb = thumb;
			aPhotos[aThumbs[thumb]].selectImage();
			resetThumbClasses();
			document.getElementById('imgThumb' + thumb).className='photoThumbSelected';
			}
		}
	}
	
function clickMore(location) {
	if (window.opener) {
		window.opener.location = location;
		window.close();
		} else {
		window.location = location;
		}
	}