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

	
function objPhoto(id,title,urlThumb,urlLarge,copy,imageRef,aEnlargeRef,aPrintRef,aEmailRef) {
	this.id = id;
	this.title = title;
	this.urlThumb = urlThumb;
	this.urlLarge = urlLarge;
	this.copy = copy;
	this.imageRef = imageRef;
	this.aEnlargeRef = aEnlargeRef;
	this.selectImage = selectImage;
	this.aPrintRef = aPrintRef;
	this.aEmailRef = aEmailRef;
	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/)
			*********************************************************
		*/
		this.imageRef.src = this.urlLarge;
		//document.print();
		this.aEnlargeRef.href = "javascript:PopupPhotoByID(" + this.id + ",'large','')";
		this.aPrintRef.href = "javascript:PopupPhotoByID(" + this.id + ",'medium','print')";
		this.aEmailRef.href = "javascript:emailContent('"+document.location.href.substring(0,document.location.href.indexOf(''))+"photopopup.aspx?id="+this.id+"&type=medium','"+this.title+"')";
		if (typeof document.getElementById('lblTitle') != 'object') {document.getElementById('lblTitle').innerHTML = this.title};
		//alert('1');
		if (document.getElementById('lblTitle')) {document.getElementById('lblTitle').innerHTML = this.title};
		//alert('2');
		if (document.getElementById('lblDetails')) {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;
		//document.getElementById('divThumb'+thumb).style.backgroundColor = 'black';
		}

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) {
		if (document.getElementById('lblImagePrevious') && aPhotos.length>1) {document.getElementById('lblImagePrevious').innerHTML = 'Previous'}
		if (document.getElementById('imgBack') && aPhotos.length>1) {document.getElementById('imgBack').src = 'images/backbutton_off.gif'}
		} else {
		if (document.getElementById('lblImagePrevious')) {document.getElementById('lblImagePrevious').innerHTML = ''}
		if (document.getElementById('imgBack')) {document.getElementById('imgBack').src = 'images/clear.gif'}
		}
	if (aThumbs[currentThumb] < (aPhotos.length-1)) {
		if (document.getElementById('lblImageNext')) {document.getElementById('lblImageNext').innerHTML = 'Next'}
		if (document.getElementById('imgNext')) {document.getElementById('imgNext').src = 'images/nextbutton_off.gif'}
		} else {
		if (document.getElementById('lblImageNext')) {document.getElementById('lblImageNext').innerHTML = ''}
		if (document.getElementById('imgNext')) {document.getElementById('imgNext').src = 'images/clear.gif'}
		}
	if (aThumbs[0] == 0) {
		if (document.getElementById('imgBack')) {document.getElementById('imgBack').src = 'images/clear.gif'}
		if (document.getElementById('lblPrevious')) {document.getElementById('lblPrevious').innerHTML = ''}
		} else {
		if (document.getElementById('imgBack')) {document.getElementById('imgBack').src = 'images/backbutton_off.gif'}
		if (document.getElementById('lblPrevious')) {document.getElementById('lblPrevious').innerHTML = 'Previous'}
		}
	if (aThumbs[aThumbs.length-1] >= (aPhotos.length-1)) {
		if (document.getElementById('imgNext')) {document.getElementById('imgNext').src = 'images/clear.gif'}
		if (document.getElementById('lblNext')) {document.getElementById('lblNext').innerHTML = ''}
		} else {
		if (document.getElementById('imgNext')) {document.getElementById('imgNext').src = 'images/nextbutton_off.gif'}
		if (document.getElementById('lblNext')) {document.getElementById('lblNext').innerHTML = 'Next'}
		}
	if (aPhotos.length <= 1) {
		if (document.getElementById('ahrefSlideShow')) {document.getElementById('ahrefSlideShow').innerHTML = ''}
		}
	setProgress();
	}
	
function setProgress() {
	if (document.getElementById('lblProgress')) {
		var lastThumb = aThumbs[0] + aThumbs.length - 1
		if (lastThumb >= aPhotos.length) {lastThumb = aPhotos.length-1}
		if (aPhotos.length == 1) {
			document.getElementById('lblProgress').innerHTML = '1 Photo Found'
			} else {
			document.getElementById('lblProgress').innerHTML = (aThumbs[0]+1) + '-' + (lastThumb+1) + ' of ' + aPhotos.length + ' Photos'
			}
		}
	}
	
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';
			}
		}
	}
	
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);
		}
	checkArrows();
	resetThumbClasses();
	clickThumb(currentThumb);
	}
	
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 slideAdvance() {
	if (aThumbs[currentThumb] >= (aPhotos.length-1)) {
		setThumbs(0);
		} else {
		clickThumb(currentThumb + 1)
		}
	//aPhotos[aThumbs[currentThumb]].selectImage();
	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
		currentThumb = thumb;
		aPhotos[aThumbs[thumb]].selectImage();
		resetThumbClasses();
		document.getElementById('imgThumb' + thumb).className='photoThumbSelected';
		} else {  //click an empty thumbnail
		resetThumbClasses();
		}
	}
	
function clickMore(location) {
	if (window.opener) {
		window.opener.location = location;
		window.close();
		} else {
		window.location = location;
		}
	}