// image-sequence.js

/*
 * 
 */
function ImageSequence(sequence, displayImg, loadingImg, captionElement) {
	this.sequence = sequence;
	this.displayImg = displayImg;
	this.loadingImg = loadingImg;
	this.captionElement = captionElement;

	this.showLoading = function() {
		if(!displayImg.style.zIndex) {
			displayImg.style.zIndex = 5;
		}
		loadingImg.style.zIndex = displayImg.style.zIndex+1;
	}
	this.hideLoading = function() {
		if(!displayImg.style.zIndex) {
			displayImg.style.zIndex = 5;
		}
		loadingImg.style.zIndex = displayImg.style.zIndex-1;
	}
	this.next = function() {
		sequence.next();
	}
	this.previous = function() {
		sequence.previous();
	}
	this.getOriginalSrc = function() {
		return sequence.getCurrent().originalSrc;
	}
	this.getCaption = function() {
		return sequence.getCurrent().caption;
	}
	this.getCredit = function() {
		return sequence.getCurrent().credit;
	}
	this.getDate = function() {
		return sequence.getCurrent().date;
	}
	
	this.openCurrentImage = function() {
		var url = "'"+sequence.getCurrent().originalSrc+"'";
		var owidth = sequence.getCurrent().originalWidth;
		var oheight = sequence.getCurrent().originalHeight;
		var params = "'status=0, toolbar=0, location=0, menubar=0, resizable=1, scrollbars=1, width=" + ((owidth < 1024) ? owidth : 1024)
			+ ", height=" + ((oheight < 768) ? oheight : 768) + "'";
		var openOriginal = 'window.open('
				+ url + ', null, ' + params + ')';
		eval(openOriginal);
	}
		
	this.updateImageDisplay = function(changeEvent) {
		//this.showLoading();
		if(!displayImg.style.zIndex) {
			displayImg.style.zIndex = 5;
		}
		loadingImg.style.zIndex = displayImg.style.zIndex+1;
		displayImg.src = changeEvent.newValue.sizedSrc;
		if(captionElement) {
			captionElement.innerHTML = changeEvent.newValue.caption;
		} else {
			alert('caption not found!');
		}
	}
	this.doInit = function() {
		sequence.onChange = this.updateImageDisplay;
		displayImg.onload = this.hideLoading;
	}
	// run the init on object creation
	this.doInit();
}

function ImageRotation(sequence, imageA, imageB, displayTime) {
	this.sequence = sequence;
	this.imageA = imageA;
	this.imageB = imageB;
	this.displayTime = displayTime;
	if(! this.displayTime) { this.displayTime = 7000; } // 7 seconds
	this.timer = null;
	this.start = function() {
		this.timer = setTimeout('this.start()', this.displayTime);
		alert('timer started');
	}
	this.stop = function() {
		clearTimeout(this.timer);
	}
}

function ImageMetadata(sizedSrc, originalSrc, originalWidth, originalHeight, caption, credit, date) {
	this.sizedSrc = sizedSrc;
	this.originalSrc = originalSrc;
	this.originalWidth = originalWidth;
	this.originalHeight = originalHeight;
	this.caption = caption;
	this.credit = credit;
	this.date = date;
}

function escapeApostrophe(text) {
	var escaped = '';
	if(text) {
		if(text.indexOf("'") < 0) {
			escaped = text;
		} else {
			for(var i = 0; i < text.length; i++) {
				if(text.substr(i, 1) == "'") {
				escaped += "\\'";
				} else {
					escaped += text.substr(i, 1);
				}
			}
		}
	}
	return escaped;
}
