// -----------------------------------------------------------------------------------
// COPYRIGHT (C) Paul Davis 2005. All rights reserved. 
// You may not copy or distribute this script without the authors permission.
// -----------------------------------------------------------------------------------

function getAsNumber (astring) {
	astring = String(astring);
	var count = (arguments.length > 1) ? arguments[1] - 1 : 0;
	var matches = astring.match(/[0-9\-]+/g);
	if (!matches || (count > matches.length))
		return 0;
	return Number(matches[count]);
}

// -----------------------------------------------------------------------------------

function popUp (path) {
	var js = "var d = document.getElementById('pic'); var x = Math.max(d.offsetWidth, d.clientWidth); var y = Math.max(d.offsetHeight, d.clientHeight); window.resizeTo(x + 40 + window.outerWidth - window.innerWidth, y + 40 + window.outerHeight - window.innerHeight);";
	var w = window.open('', 'Picture', 'toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=no,status=no');
	w.document.write('<html><head></head><body><img id="pic" src="' + path + '" onload="' + js + '"></body></html>');
	w.document.close();
}

// -----------------------------------------------------------------------------------

function SlideShow (varname, elid, atype, amode, aslidetime, atranstime, valuearray) {

	this.name = varname;
	this.type = atype;	// image, text
	this.mode = amode;	// transition type
		
	this.value = valuearray;
	this.cursor = 0;
	this.count = 2;		// concurrrent images to display in div
	this.spacing = 0;	// space between elements - calculated co-ords
	// attributes of images/divs
	this.width = 0;		// width of each image : 0 = auto
	this.height = 0;	// height of each image : 0 = auto
	this.onmouseover = '';
	this.onmouseout = '';
	this.onclick = '';
	
	this.slidetime = aslidetime;
	this.slideinterval = 0;
	this.transtime = atranstime;
	this.transinterval = 0;
	
	this.ie = (navigator.appName.indexOf("Microsoft") > -1);
	this.fps = (this.ie) ? 15 : 25;
	
	this.opacity = 1;
	this.offset = 0;
	this.direction = -1;
	this.vertical = false;
	this.flag = false;
	
	this.el = null;		// the containing div or block
	this.el_width = 0;
	this.el_height = 0;
	
	// methods

	this.init = function () {
		if (document.getElementById(elid))
			this.el = document.getElementById(elid);
		else
			alert("SlideShow error : Can't find element with id = ".elid);
		this.el_width  = Math.max(this.el.clientWidth,  this.el.offsetWidth );
		this.el_height = Math.max(this.el.clientHeight, this.el.offsetHeight);
	}
		
	this.goto = function (index, transition) {
		if (index != this.cursor) {
			var nextcursor = (index == -1) ? this.cursor + 1 : index;	
			if ((nextcursor >= this.value.length) || (nextcursor < 0))
				nextcursor = 0;
			this.cursor = nextcursor;
			if (transition) {
				this.transinterval = setInterval(this.name + '.' + this.mode + '();', Math.round(1000 / this.fps));
			} else {
				if (this.slidetime > 0)
					clearInterval(this.slideinterval);
				clearInterval(this.transinterval);
				this.change();
			}
		}
	};
	
	this.play = function () {
		eval(this.name + '.' + this.mode + '();');
		this.stop();
		if (this.slidetime == 0)
			this.goto(-1, true);
		else
			this.slideinterval = setInterval(this.name + '.goto(-1, true);', this.slidetime);
	};
	
	this.stop = function () {
		if (this.slidetime > 0)
			clearInterval(this.slideinterval);
		clearInterval(this.transinterval);
		this.setOpacity(1);
	};
	
	this.change = function () { // brings last child forward
		this.el.insertBefore(this.el.firstChild, this.el.lastChild.nextSibling);
		if (this.type == 'image')
			this.el.lastChild.src = this.value[this.cursor];
		else
			this.el.lastChild.innerHTML = this.value[this.cursor];
	};
	
	this.setOpacity = function () {
		if (arguments.length > 0)
			this.opacity = arguments[0];
		var el = this.el.firstChild;
		if (arguments.length > 1)
			el = arguments[1];
		if (this.ie)
			el.style.filter = 'alpha(opacity=' + Math.floor(Number(this.opacity) * 100) + ')';
		else
			el.style.opacity = this.opacity;		
	};
	
	this.newElement = function () {
		var newtype = (this.type == 'image') ? 'img' : 'div';
		var newel = document.createElement(newtype);
		if (this.type == 'image')
			newel.src = this.value[this.cursor];
		else
			newel.innerHTML = this.value[this.cursor];	
		newel.style.position = 'absolute';
		newel.style.display = 'block';
		newel.style.overflow = 'hidden';
		newel.style.top = '0px';
		newel.style.left = '0px';
		newel.style.width = (this.width) ? this.width + 'px' : 'auto';
		newel.style.height = (this.height) ? this.height + 'px' : 'auto';
		if (this.onmouseover)
			eval('newel.onmouseover = function () { ' + this.onmouseover + ' };');
		if (this.onmouseout)
			eval('newel.onmouseout = function () { ' + this.onmouseout + ' };');
		if (this.onclick) {
			eval('newel.onclick = function () { ' + this.onclick + ' };');
			newel.style.cursor = 'pointer';	
		}
		return newel;
	}
		
	this.getDimension = function (el, width) {
		if (width) {	
			if (this.type == 'image')
				return getAsNumber(el.width);
			else
				return Math.max(el.clientWidth, el.offsetWidth);
		} else {
			if (this.type == 'image')
				return getAsNumber(el.height);
			else
				return Math.max(el.clientHeight, el.offsetHeight);
		}
	}
		
	// transitions ----------------------------------------------------------------
	
	this.convey = function (ishorizontal, isbackward) {
		var attribute = (ishorizontal) ? 'left' : 'top';
		var dimension = (ishorizontal) ? this.el_width : this.el_height;
		var loaded = (this.el.childNodes.length >= this.count);
		var delta = 0;
		if (loaded)
			delta = dimension / (this.transtime / 1000) / this.fps;
		var dim = (this.el.firstChild) ? this.getDimension(this.el.firstChild, ishorizontal) : 0;
		var oldoffset = this.offset;
		this.offset -= delta;
		if (!loaded || (Math.floor(oldoffset) != Math.floor(this.offset))) {
			delta = Math.ceil(delta);
			if ((!isbackward && (this.offset <= (-dim - delta))) || (isbackward && (this.offset >= (dimension + delta))) || (!loaded && ((this.type != 'image') || (!this.el.lastChild || this.el.lastChild.complete)))) {
				// change images - old one (firstchild) out, new one in as lastchild
				this.cursor = (this.cursor >= (this.value.length - 1)) ? 0 : this.cursor + 1;
				// new image
				var lastkid = this.el.lastChild;
				var newel = this.newElement();
				newel.style[attribute] = '-100000px';
				this.el.appendChild(newel);
				if (lastkid) {
					if (isbackward)
						newel.style[attribute] = (getAsNumber(lastkid.style[attribute]) - this.getDimension(newel, ishorizontal) - this.spacing) + 'px';
					else
						newel.style[attribute] = (getAsNumber(lastkid.style[attribute]) + this.getDimension(lastkid, ishorizontal) + this.spacing) + 'px';
				} else {
					if (isbackward)
						newel.style[attribute] = (dimension - this.getDimension(newel, ishorizontal)) + 'px';
					else
						newel.style[attribute] = '0px';
				}
				// remove old image
				if (this.el.childNodes.length > this.count)
					this.el.removeChild(this.el.firstChild);
			}
			this.offset = getAsNumber(this.el.firstChild.style[attribute]);
			// shuffle all 
			delta = (isbackward) ? -delta : delta;
			for (var i = 0; i < this.el.childNodes.length; i++)
				this.el.childNodes[i].style[attribute] = (getAsNumber(this.el.childNodes[i].style[attribute]) - delta) + 'px';
		}
	};
		
	this.conveyleft = function () {	
		this.convey(true, false);
	};
		
	this.conveyright = function () {	
		this.convey(true, true);
	};
		
	this.conveyup = function () {	
		this.convey(false, false);
	};
		
	this.conveydown = function () {	
		this.convey(false, true);
	};
		
	this.fadeaway = function () {
		this.count = 2;
		if (this.el.childNodes.length == 0) {
			var newel = this.newElement();
			this.el.appendChild(newel);
			this.cursor = (this.cursor >= (this.value.length - 1)) ? 0 : this.cursor + 1;
			var newel = this.newElement();
			this.el.appendChild(newel);
			this.cursor = (this.cursor >= (this.value.length - 1)) ? 0 : this.cursor + 1;
		} else {
			var delta = this.transtime / this.fps / 1000;
			this.opacity -= delta;
			if (this.opacity < 0) {
				this.opacity = 0;
				clearInterval(this.transinterval);
				this.change();
				this.setOpacity(1, this.el.lastChild);
				this.el.lastChild.style.zIndex = 999;		
				this.el.firstChild.style.zIndex = 1000;		
			} else
				this.setOpacity();
		}
	};
		
	this.fade = function () {
		this.count = 1;
		if (this.el.childNodes.length == 0) {
			var newel = this.newElement();
			this.el.appendChild(newel);
			this.cursor = (this.cursor >= (this.value.length - 1)) ? 0 : this.cursor + 1;
		} else {
			var delta = this.transtime / this.fps / 1000 * 2;
			this.opacity += this.direction * delta;
			if (this.opacity < 0) {
				this.opacity = 0;
				this.direction = 1;
				this.change();
			} 
			if (this.opacity > 1) {
				this.opacity = 1;
				this.direction = -1;
				clearInterval(this.transinterval);
			}
			this.setOpacity();
		}
	};
		
	this.slideoff = function (direction) {
		var dimension = (direction == 'left') ? this.el_width : this.el_height;
		this.count = 2;
		if (this.el.childNodes.length == 0) { // load
			var newel = this.newElement();
			this.el.appendChild(newel);
			this.cursor = (this.cursor >= (this.value.length - 1)) ? 0 : this.cursor + 1;
			var newel = this.newElement();
			newel.style[direction] = dimension + 'px';
			this.el.appendChild(newel);
			this.cursor = (this.cursor >= (this.value.length - 1)) ? 0 : this.cursor + 1;
		} else { // transition
			var delta = this.transtime / this.fps / 1000 * 2 * dimension;
			this.offset -= delta;
			if (this.offset < (-dimension)) {
				this.offset = dimension;
				this.el.firstChild.style[direction] = dimension + 'px';
				this.change();
				this.direction = 1;
			} 
			if ((this.offset < 0) && (this.direction == 1)) {
				this.offset = 0;
				this.el.firstChild.style[direction] = '0px';
				this.direction = -1;
				clearInterval(this.transinterval);
			}
			this.el.firstChild.style[direction] = this.offset + 'px';
		}
	};


	this.slideoffleft = function () {
		this.slideoff('left');
	};
		
	this.slideofftop = function () {
		this.slideoff('top');
	};
		
}
