/*
 Copyright 2005 XtraLean Software Inc. Jan 2005
 ShutterBug helper functions
 */

var globalCanFade = true;
var isSafari = false;

function clickLink (address) {
	var max = address.length;
	var i;
	var clickTo = '';
	var c = 0;
	for (i=0; i<max; i++) {
		c = 0;
		while (i<max && address.charCodeAt(i) != 97) {
			c = c*25;
			c = c + address.charCodeAt(i)-98;
			i++;
		}
		clickTo += String.fromCharCode(c);
	}
	parent.location = clickTo;
}

function rollOver (ident, normal, over) {
	this.ident = ident;
	this.normalImage = new Image();
	this.normalImage.src = normal;
	this.overImage = new Image();
	this.overImage.src = over;
	
	this.mouseOver = function () {
		var image = document.getElementById(this.ident);
		if (image) {
			image.src = this.overImage.src;
		}
	}
	
	this.mouseOut = function () {
		var image = document.getElementById(this.ident);
		if (image) {
			image.src = this.normalImage.src;
		}
	}
}


function setOpacity(opacity, id) {
	var object = id.style;
	if (globalCanFade) {
		object.display = "";
		object.MozOpacity = (opacity/100);
		object.opacity = (opacity/100);
		object.filter = "alpha(opacity=" + opacity + ")";
	} else {
		if (opacity <= 99)
			object.display = "none";
		else {
			object.display = "";
			if (isSafari)
				object.opacity = 100;
		}
	}
}

function crossFade(fade, imageA, imageB)
{
	var imageAfade = fade;
	var imageBfade = 100-fade;
	if (imageAfade == 100)
		imageAfade = 99.9;
	if (imageBfade == 100)
		imageBfade = 99.9;
	setOpacity(imageAfade, imageA);
	setOpacity(imageBfade, imageB);
}

function fadeIn(fade, imageA)
{
	if (fade == 100)
		fade = 99.9;
	setOpacity(fade, imageA);
}

function slide(number,disable) {
	if (parseInt(disable) == 1) {
		this.src = 'pictures/image' + number + '.jpg';
	} else {
		this.src = 'pictures/slide' + number + '.jpg';
	}
	this.textid = 'stext' + number;
	this.imgid = 'ssviewer' + number;
	this.loaded = false;
	
	this.load = function() {
		if (!this.loaded) {
			var image = document.getElementById(this.imgid);
			if (image) {
				if (isSafari) {
					var object = image.style;
					object.display = "";
					object.opacity = 0;
				}
				image.src = this.src;
				this.loaded = true;
			}
		}
	}
}

function slideshow( slideshowname ) {
	this.name = slideshowname;
	this.repeat = true;
	this.prefetch = 2;
	this.image;
	this.timeout = 3000;
	this.slides = new Array();
	this.current = 0;
	this.lastlastslide = -1;
	this.lastslide = -1;
	this.firstslide = 0;
	this.fade = 0;
	this.fadeprogress = 0;
	this.fadetimeoutid = 0;
	this.imageIn = 0;
	this.imageOut = 0;
	this.playLabel = 'Play';
	this.stopLabel = 'Stop';
	this.playControl = 0;
	this.autoPlay = 0;
	
	this.timeoutid = 0;
	this.add_slide = function(number,disable) {
		var i = this.slides.length;
		this.slides[i] = new slide (number,disable);
		if (this.prefetch == -1) {
			slide.load();
		}
	}
	
	this.play = function() {
		if (this.timeoutid != 0) 
			clearTimeout(this.timeoutid);
		this.timeoutid = setTimeout( this.name + ".loop()", this.timeout);
		if (this.playControl) {
			this.playControl.href = "javascript:"+this.name+".stop()";
			setTimeout(this.name + ".setStopLabel()", 25);
		}
	}
	
	this.setStopLabel = function() {
		this.playControl.innerHTML = this.stopLabel;
	}
	
	this.setPlayLabel = function() {
		this.playControl.innerHTML = this.playLabel;
	}
	
	this.stop = function() {
		if (this.timeoutid != 0) {
			clearTimeout(this.timeoutid);
			this.timeoutid = 0;
		}
		if (this.playControl) {
			this.playControl.href = "javascript:"+this.name+".play()";
			setTimeout(this.name + ".setPlayLabel()", 25);
		}
	}
	
	this.crossFade = function() {
		crossFade(this.fadeprogress, this.imageIn, this.imageOut);
		if (this.fadeprogress < 100) {
			this.fadetimeoutid = setTimeout(this.name +".crossFade()", 50);
		}
		this.fadeprogress += 5;
	}
	
	this.fadeIn = function() {
		fadeIn(this.fadeprogress, this.imageIn);
		if (this.fadeprogress < 100) {
			this.fadetimeoutid = setTimeout(this.name +".fadeIn()", 50);
		}
			
		this.fadeprogress += 5;
	}
	
	this.update = function(autoPlay) {
		if (this.fadetimeoutid != 0) {
			clearTimeout(this.fadetimeoutid);
			this.fadetimeoutid = 0;
			this.fadeprogress = 100;
		}
		var slide = this.slides[ this.current ];
		slide.load();
		
		if (this.lastslide != -1) {
			
			this.imageOut = document.getElementById(this.slides[this.lastslide].imgid);
			this.imageIn = document.getElementById(slide.imgid);
			
			if (this.lastlastslide != -1) 
				fadeIn(0, document.getElementById(this.slides[this.lastlastslide].imgid));
			fadeIn(100, this.imageOut);
			
			this.lastlastslide = this.lastslide;
			
			this.imageOut.style.zIndex = 1;
			this.imageIn.style.zIndex = 2;
			
			if (this.fade == 0 || globalCanFade == false) {
				crossFade(100, this.imageIn, this.imageOut);
			} else {
				this.fadeprogress = 0;
				this.crossFade();
			}
			caption = document.getElementById(this.slides[this.lastslide].textid);
			if (caption) {
				caption.style.display = "none";
			}
		} else {
			this.imageIn = document.getElementById(slide.imgid);
			if (this.fade == 0 || globalCanFade == false) {
				fadeIn(100, this.imageIn);
			} else {
				this.fadeprogress = 0;
				this.fadeIn();
			}
		}
		
		caption = document.getElementById(slide.textid);
		if (caption) {
			caption.style.display = "";
			
			if (this.prefetch > 0) {
				var next, prev, count;
				next = this.current;
				prev = this.current;
				count = 0;
				do {
					if (++next >= this.slides.length) next = this.firstslide;
					if (--prev < 0) prev = this.slides.length - 1;
					this.slides[next].load();
					this.slides[prev].load();
				} while (++count < this.prefetch);
			}
		}
		if (autoPlay == 1)
			this.play();
	}
	
	this.init = function() {
		if (window.clientInformation) {
			if (window.clientInformation.appName.indexOf("croso") > 0)
				if (window.clientInformation.platform.indexOf("PPC") > 0) {
					globalCanFade = false;
				}
		}
		if (navigator.vendor) {
			if (navigator.vendor.indexOf("Apple") >= 0) {
				var jk = navigator.appVersion.indexOf("Safari/");
				if (jk != -1) {
					var version = parseInt(navigator.appVersion.substr(jk+7,3));
					if (version < 312)
						globalCanFade = false;
				}
				isSafari = true;
			}
		}
		this.playControl = document.getElementById('ssplay');
		
		var string1 = location.hash;
		var snum = string1.substring(1,string1.length);
		if (snum) {
			this.current = parseInt(snum);
			if (this.current < this.firstslide)
				this.current = this.firstslide;
		}
		
		setTimeout(this.name+".update("+this.autoPlay+")", 250);
	}
	
	
	this.goto_slide = function(n) {
		if (this.current != n) {
			if (n == -1) {
				n = this.slides.length - 1;
			}
			if (n < this.firstslide)
				n = this.firstslide;
			
			if (n < this.slides.length && n >= this.firstslide) {
				this.lastslide = this.current;
				this.current = n;
			}
			this.stop();
			this.update(0);
		}
	}
	
	this.next = function() {
		lastcurrent = this.current;
		if (this.current < this.slides.length - 1) {
			this.lastslide = this.current;
			this.current++;
		} else if (this.repeat) {
			this.lastslide = this.current;
			this.current = this.firstslide;
		}
		if (lastcurrent == this.current)
			return;
		this.stop();
		this.update(0);
	}
	
	this.previous = function() {
		lastcurrent = this.current;
		if (this.current > this.firstslide) {
			this.lastslide = this.current;
			this.current--;
		} else if (this.repeat) {
			this.lastslide = this.current;
			this.current = this.slides.length - 1;
		}
		if (lastcurrent == this.current)
			return;
		this.stop();
		this.update(0);
	}
	
	this.loop = function() {
		if (this.current < this.slides.length - 1) {
			this.lastslide = this.current;
			this.current++;
		} else if (this.repeat) {
			this.lastslide = this.current;
			this.current = this.firstslide;
		}
		this.update(0);
		this.timeoutid = setTimeout( this.name + ".loop()", this.timeout);
	}
}


