// -----------------------------------------------------------------------------------
// COPYRIGHT (C) Paul Davis 2009. 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 getPageDimension (width) {
	var x, y;
	if (window.innerHeight && window.scrollMaxY) {	
		x = window.innerWidth + window.scrollMaxX;
		y = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ 
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
	return (width) ? x : y;
}

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

function getHTMLDimension (html, width) {

		var newel = document.createElement("div");
		newel.innerHTML = html;
		newel.style.position = 'absolute';
		newel.style.display  = 'block';
		newel.style.top  = '100px';
		newel.style.left = '100px';
		newel.style.overflow = 'hidden';
		newel.style.width = 'auto';
		newel.style.height = 'auto';
		
		var tempel = document.body.insertBefore(newel, document.body.firstChild);
		
		var result = 0;
		if (width)
			result = Math.max(tempel.clientWidth,  tempel.offsetWidth );
		else
			result = Math.max(tempel.clientHeight, tempel.offsetHeight);
		//document.body.removeChild(tempel);
		delete(newel);

		return result;

}

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

function SlideShow (varname, elid) {

	this.name = varname;
	this.mode = "fade";		// transition type: conveyleft, conveyright, conveyup, conveydown, fadeaway, fade, slideoffleft, slideofftop
	
	this.port = null;	// screen container for images/divs and controls (parent of this.el)
	this.black = null;	// screen fade element (outside this.port)
	this.progress = null;	// element containing the current image number
	
	this.value = new Array();
	this.cursor = 0;
	this.nextcursor = 1;
	this.count = 2;		// concurrrent images to display in div
	this.spacing = 0;	// space between elements - calculated co-ords
	this.centering = false;	// automatic horizontyal centering
	// attributes of images/divs
	this.width = 0;		// width of each div : 0 = auto
	this.height = 0;	// height of each div : 0 = auto
	this.onmouseover = '';
	this.onmouseout = '';
	this.onclick = '';
	
	this.slidetime = 5000;
	this.slideinterval = 0;
	this.transtime = 1000;
	this.transinterval = 0;
	this.playing = false;
	this.booted = false;
	
	this.ie = (navigator.appName.indexOf("Microsoft") > -1);
	this.safari = (navigator.userAgent.indexOf("AppleWebKit") > -1);
	//console.log(navigator);
	this.fps = (this.ie) ? 15 : 25;
	
	this.opacity = 1;
	this.offset = 0;
	this.direction = -1;
	this.vertical = false;
	this.flag = false;
	this.nextprev = 0;
	
	this.reflection = 0;
	this.reflectbg = '#ffffff';
	this.reflectopacity = '0.25';
	this.reflectgap = 2;
	this.reflectgradients = 10;
	
	this.dockup = 20;
	this.dockfontsmall = 14;
	this.dockfontbig = 24;
		
	this.elid = elid;
	this.el = null;		// the containing div or block
	this.el_width  = 0;
	this.el_height = 0;

	// methods
	
	// system -------------------------------------------------------
	
	this.prime = function (elid) {
		elid = (elid) ? elid : this.elid;
		if (!this.el || (elid != this.elid)) {
			if (document.getElementById(elid)) {
				this.elid = elid;
				this.el = document.getElementById(elid);
			} else {
				alert("SlideShow error : Can't find element with id = ".elid);
			}
		}
		if (this.el) {
			this.el_width  = Math.max(this.el.clientWidth,  this.el.offsetWidth );
			this.el_height = Math.max(this.el.clientHeight, this.el.offsetHeight);
			return true;
		}
		return false;
	}
		
	// ----------------------------------------------------------------

	this.dump = function () {
		clearInterval(this.slideinterval);
		clearInterval(this.transinterval);
		this.playing = false;
		while (this.value.length)
			this.value.shift();
		this.booted = false;
	}
	
	// ----------------------------------------------------------------

	this.imageHTML = function (path, width, height) {
		var w = (width ) ? 'width:'  + width  + 'px;' : '';
		var h = (height) ? 'height:' + height + 'px;' : '';
		var imghtml = '<img src="' + path.replace(/"/, "&quot;") + '" style="display:block;' + w + h + '"/>';
		if (this.reflection) {
			// get image width
			var refwidth = 0;
			if (width)
				refwidth = width;
			else {
				var img = new Image();
				img.src = path;
				refwifth = img.width;
				if (height)
					refwidth = Math.round(img.width * (height / img.height));
			}
			// set gap
			var gap = (this.reflectgap) ? '<div style="width:' + refwidth + 'px;height:' + this.reflectgap + 'px;font-size:1px;line-height:1px;"> </div>' : '';
			// do it
			if (this.ie) {
				var imgflip = '<img src="' + path.replace(/"/, "&quot;") + '" style="display:block;' + w + h + 'filter:Flipv;filter=alpha(Opacity=' + Math.round(this.reflectopacity * 100) + ', FinishOpacity=0, Style=1, StartX=0, StartY=0, FinishX=0, FinishY=' + this.reflection + ');"/>';
				return imghtml + gap + '<div style="position:relative;display:block; overflow:hidden; width:' + refwidth + 'px; height:' + this.reflection + 'px;">' + imgflip + '</div>';
			/*
			} else if (this.safari) { //  also for chrome
				return '<img src="' + path.replace(/"/, "&quot;") + '" style="display:block;' + w + h + '-webkit-box-reflect: below ' + this.reflectiongap + 'px -webkit-gradient(linear, left top, left bottom, from(black), color-stop(0.5, transparent), to(' + this.reflectbg + '))"/>';
			*/
			} else {
				var imgflip = '<img src="' + path.replace(/"/, "&quot;") + '" style="display:block;' + w + h + '-webkit-transform:scaleY(-1);-moz-transform:scaleY(-1);"/>';
				var refinc = Math.ceil(this.reflection / this.reflectgradients);
				var refstyle = 'position:absolute;width:100%;height:' + refinc + 'px;background:' + this.reflectbg + ';';
				var gradient = '';
				for (var i = 0; i < this.reflectgradients; i++) {
					var top = refinc * i;
					var opa = 1 - this.reflectopacity + ((this.reflectopacity / this.reflectgradients) * i);
					gradient += '<div style="' + refstyle + 'top:' + top + 'px;opacity:' + opa + ';filter:alpha(opacity=' + Math.round(opa * 100) + ')"></div>';
				}
				return imghtml + gap + '<div style="position:relative;display:block; overflow:hidden; width:100%; height:' + this.reflection + 'px;">' + imgflip + gradient + '</div>';
			}
		} else {
			return imghtml;
		}
	}
	
	// ----------------------------------------------------------------

	this.divHTML = function (html) {
		var w = 'width: 100%;';
		var h = 'height: 100%;';
		if (this.reflection) {
			// get image width
			var refwidth = this.width;
			// set gap
			var gap = (this.reflectgap) ? '<div style="width:' + refwidth + 'px;height:' + this.reflectgap + 'px;font-size:1px;line-height:1px;"> </div>' : '';
			// do it
			if (this.ie) {
				var flip = '<div style="display:block;' + w + h + 'filter:Flipv;filter=alpha(Opacity=' + Math.round(this.reflectopacity * 100) + ', FinishOpacity=0, Style=1, StartX=0, StartY=0, FinishX=0, FinishY=' + this.reflection + ');">' + html + '</div>';
				return html + gap + '<div style="position:relative;display:block; overflow:hidden; width:' + refwidth + 'px; height:' + this.reflection + 'px;">' +flip + '</div>';
			/*
			} else if (this.safari) { //  also for chrome
				return '<img src="' + path.replace(/"/, "&quot;") + '" style="display:block;' + w + h + '-webkit-box-reflect: below ' + this.reflectgap + 'px -webkit-gradient(linear, left top, left bottom, from(black), color-stop(0.5, transparent), to(' + this.reflectbg + '))"/>';
			*/
			} else {
				var flip = '<div style="display:block;' + w + h + '-webkit-transform:scaleY(-1);-moz-transform:scaleY(-1);">' + html + '</div>';
				var refinc = Math.ceil(this.reflection / this.reflectgradients);
				var refstyle = 'position:absolute;width:100%;height:' + refinc + 'px;background:' + this.reflectbg + ';';
				var gradient = '';
				for (var i = 0; i < this.reflectgradients; i++) {
					var top = refinc * i;
					var opa = 1 - this.reflectopacity + ((this.reflectopacity / this.reflectgradients) * i);
					gradient += '<div style="' + refstyle + 'top:' + top + 'px;opacity:' + opa + ';"></div>';
				}
				return html + gap + '<div style="position:relative;display:block; overflow:hidden; width:100%; height:' + this.reflection + 'px;">' + html + gradient + '</div>';
			}
		} else {
			return html;
		}
	}
	
	// ----------------------------------------------------------------

	this.preload = function (imagepaths) {
		if (imagepaths) {
			if (typeof(values) == "string")
				imagepaths = imagepaths.split(",");
			var imgs = new Array();
			for (var i = 0; i < imagepaths.length; i++) {
				imgs[i] = new Image();
				imgs[i].src = imagepaths[i];
			}
		}
	}
	
	// ----------------------------------------------------------------

	this.load = function (values, width, height) {
		this.dump();
		if (values) {
			if (typeof(values) == "string") {
				var imagepaths = values.split(",");
				for (var i = 0; i < imagepaths.length; i++)
					this.value[i] = this.imageHTML(imagepaths[i], width, height);
				
			} else
				for (var i = 0; i < values.length; i++)
					this.value[i] = this.divHTML(values[i]);
		}
	}
	
	// controls ------------------------------------------------------
	
	this.forward = function (offset, start) {
		if (!this.value.length)
			return 0;
		var offset = (arguments.length) ? arguments[0] : 1;
		if (offset < 0)
			return this.back(offset);
		while (offset > this.value.length)
			offset -= this.value.length;
		start = (arguments.length > 1) ? arguments[1] : this.cursor;
		var val = start + offset;
		if (val >= this.value.length)
			return val - this.value.length;
		return val;
	}
	
	// ----------------------------------------------------------------

	this.back = function (offset, start) {
		if (!this.value.length)
			return 0;
		var offset = (arguments.length) ? arguments[0] : 1;
		if (offset < 0)
			return this.forward(offset);
		while (offset > this.value.length)
			offset -= this.value.length;
		start = (arguments.length > 1) ? arguments[1] : this.cursor;
		var val = start - offset;
		if (val < 0)
			return this.value.length + val;
		return val;
	}
	
	// ----------------------------------------------------------------

	this.goto = function (index, transtime) {
		if (index != this.cursor) {
			this.nextcursor = (index == -1) ? this.forward() : index;	
			this.nextcursor = (index == -2) ? this.back() : this.nextcursor;	
			if (transtime) {
				clearInterval(this.transinterval);
				this.transinterval = setInterval(this.name + '.' + this.mode + '(' + transtime + ');', Math.round(1000 / this.fps));
				this.cursor = this.nextcursor;
			} else {
				if (this.slidetime > 0)
					clearInterval(this.slideinterval);
				clearInterval(this.transinterval);
				this.change();
			}
			this.screenProgress();
		}
	};
	
	// ----------------------------------------------------------------

	this.previous = function (transtime) {
		if (this.nextprev != 0)
			return;
		this.nextprev = -1;
		this.goto(-2, transtime);
		if (this.playing) {
			clearInterval(this.slideinterval);
			this.slideinterval = setInterval(this.name + '.goto(-1, ' + this.transtime + ');', this.slidetime);
		}
	};
	
	// ----------------------------------------------------------------

	this.prev = function (transtime) {
		this.previous(transtime);
	};
	
	// ----------------------------------------------------------------

	this.next = function (transtime) {
		if (this.nextprev != 0)
			return;
		this.nextprev = 1;
		this.goto(-1, transtime);
		if (this.playing) {
			clearInterval(this.slideinterval);
			this.slideinterval = setInterval(this.name + '.goto(-1, ' + this.transtime + ');', this.slidetime);
		}
	};	
	
	// ----------------------------------------------------------------

	this.play = function (start) {
		if (!this.booted)
			this.boot(start);
		this.stop();
		this.playing = true;
		if (this.slidetime == 0) {
			this.transinterval = setInterval(this.name + '.' + this.mode + '(' + this.transtime + ');', Math.round(1000 / this.fps));
		} else
			this.slideinterval = setInterval(this.name + '.goto(-1, ' + this.transtime + ');', this.slidetime);
	};
	
	// ----------------------------------------------------------------

	this.stop = function () {
		if (this.slidetime > 0)
			clearInterval(this.slideinterval);
		clearInterval(this.transinterval);
		this.playing = false;
		this.setOpacity(1);
	};
	
	// ----------------------------------------------------------------

	this.boot = function (start) {
		this.booted = true;
		if (!isNaN(start))
			this.cursor = (start) ? start : 0;
		this.nextcursor = this.cursor;
		this.cursor = this.back();
		eval(this.name + '.' + this.mode + '();');
		this.goto(-1);
	}
	
	// ----------------------------------------------------------------

	this.change = function () {
		this.el.insertBefore(this.el.firstChild, this.el.lastChild.nextSibling);
		var el = this.el.lastChild;
		el.innerHTML = this.value[this.nextcursor];
		this.cursor = this.nextcursor;
	};
	
	// ----------------------------------------------------------------

	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 newel = document.createElement("div");
		newel.innerHTML = this.value[this.nextcursor];
		if (this.centering) {
			newel.style.textAlign = 'center';
			newel.style.verticalAlign = 'middle';
			newel.style.display  = 'inline';
		} else {
			newel.style.position = 'absolute';
			newel.style.display  = 'block';
			newel.style.top  = '0px';
			newel.style.left = '0px';
		}
		newel.style.overflow = 'hidden';
		newel.style.width = 'auto';
		if (this.width)
			newel.style.width = (typeof(this.width) == 'number') ? this.width + 'px' : this.width;
		newel.style.height = 'auto';
		if (this.height)
			newel.style.height = (typeof(this.height) == 'number') ? this.height + 'px' : this.height;
		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)
			return Math.max(el.clientWidth, el.offsetWidth);
		else
			return Math.max(el.clientHeight, el.offsetHeight);
	}
		
	// transitions ----------------------------------------------------------------
		
	this.bumpnzoom = function () { // assumes fixed width
		var bigwidth = this.el_width - ((this.count - 1) * (this.width + this.spacing));
		var ratio = bigwidth / this.width;
		var bigheight = Math.round(this.height * (bigwidth / this.width));
		var leftcount = Math.ceil(this.count / 2);
		var rightcount = Math.floor(this.count / 2) + 1;
		if (this.el.childNodes.length == 0) {
			// load initially
			for (var i = 0; i <= (this.count + 1); i++) {
				var j = this.back(leftcount, i);
				var enlarge_offset = (i > leftcount) ? bigwidth - this.width : 0;
				var left = ((this.width + this.spacing) * (i - 1)) + enlarge_offset;
				var newel = this.newElement();
				newel.style.left = left + 'px';
				if (j == 0) {
					newel.style.width = bigwidth + 'px';
					newel.style.height = bigheight + 'px';
					newel.style.fontSize = this.dockfontbig + 'px';
				} else {
					newel.style.width = this.width + 'px';
					newel.style.height = this.height + 'px';
					newel.style.top = Math.floor(this.dockup) + 'px';
					newel.style.fontSize = this.dockfontsmall + 'px';
				}
				newel.innerHTML = this.value[j];
				newel.id = "cursor" + j;
				//eval('newel.onclick = function () { return false; };');
				eval('newel.onclick = function () { ' + this.onclick + ' };');
				this.el.appendChild(newel);
			}
			//eval('this.el.childNodes[' + leftcount +'].onclick = function () { ' + this.onclick + ' };');
			this.stop();
		} else {
			var distance = this.width + this.spacing;
			var bigdistance = bigwidth + this.spacing;
			var increment = (this.transtime / 1000) * this.fps;
			var delta_left = distance / increment;
			var delta_left_big = bigdistance / increment;
			var delta_width = (bigwidth - this.width) / increment;
			var delta_height = (bigheight - this.height) / increment;
			var delta_top = this.dockup / increment;
			if (this.offset == 0) // start
				this.offset = distance;
			this.offset -= delta_left;
			if (this.offset > 0) {
				for (var i = 0; i < this.el.childNodes.length; i++) {
					var elm = this.el.childNodes[i];
					if(this.nextprev > 0) { // forward
						if (i == leftcount) {
							elm.style.top = (getAsNumber(elm.style.top) + delta_top) + 'px';
							elm.style.width = (getAsNumber(elm.style.width) - delta_width) + 'px';
							elm.style.height = (getAsNumber(elm.style.height) - delta_height) + 'px';
							elm.style.left = (getAsNumber(elm.style.left) - delta_left) + 'px';
							elm.style.fontSize = Math.round(this.dockfontsmall + ((this.dockfontbig - this.dockfontsmall) * (this.offset / distance))) + 'px';
						} else if (i == (leftcount + 1)) {
							elm.style.top = (getAsNumber(elm.style.top) - delta_top) + 'px';
							elm.style.width = (getAsNumber(elm.style.width) + delta_width) + 'px';
							elm.style.height = (getAsNumber(elm.style.height) + delta_height) + 'px';
							elm.style.left = (getAsNumber(elm.style.left) - delta_left_big) + 'px';
							elm.style.fontSize = Math.round(this.dockfontsmall + ((this.dockfontbig - this.dockfontsmall) * ((distance - this.offset) / distance))) + 'px';
						} else
							elm.style.left = (getAsNumber(elm.style.left) - delta_left) + 'px';
					} else { // backward
						if (i == leftcount) {
							elm.style.top = (getAsNumber(elm.style.top) + delta_top) + 'px';
							elm.style.width = (getAsNumber(elm.style.width) - delta_width) + 'px';
							elm.style.height = (getAsNumber(elm.style.height) - delta_height) + 'px';
							elm.style.left = (getAsNumber(elm.style.left) + delta_left_big) + 'px';
							elm.style.fontSize = Math.round(this.dockfontsmall + ((this.dockfontbig - this.dockfontsmall) * (this.offset / distance))) + 'px';
						} else if (i == (leftcount - 1)) {
							elm.style.top = (getAsNumber(elm.style.top) - delta_top) + 'px';
							elm.style.width = (getAsNumber(elm.style.width) + delta_width) + 'px';
							elm.style.height = (getAsNumber(elm.style.height) + delta_height) + 'px';
							elm.style.left = (getAsNumber(elm.style.left) + delta_left) + 'px';
							elm.style.fontSize = Math.round(this.dockfontsmall + ((this.dockfontbig - this.dockfontsmall) * (this.offset / distance))) + 'px';
						} else
							elm.style.left = (getAsNumber(elm.style.left) + delta_left) + 'px';
					}
				} 
			} else {
				this.stop();
				this.offset = 0;
				var newel = this.newElement();
				newel.style.left = (this.el_width + this.spacing) + 'px';
				newel.style.top = Math.floor(this.dockup) + 'px';
				if (this.nextprev > 0) {
					newel.innerHTML = this.value[this.forward(rightcount)];
					newel.id = "cursor" + this.forward(rightcount);
					this.el.appendChild(newel);
					this.el.removeChild(this.el.firstChild);
				} else {
					newel.innerHTML = this.value[this.back(leftcount)];
					newel.id = "cursor" + this.back(leftcount);
					this.el.insertBefore(newel, this.el.firstChild);
					this.el.removeChild(this.el.lastChild);
				}
				for (var i = 0; i <= (this.count + 1); i++) {
					var enlarge_offset = (i > leftcount) ? bigwidth - this.width : 0;
					var left = ((this.width + this.spacing) * (i - 1)) + enlarge_offset;
					this.el.childNodes[i].style.left = left + 'px';
					this.el.childNodes[i].style.top = ((leftcount == i) ? 0 : this.dockup) + 'px';
					this.el.childNodes[i].style.width = ((leftcount == i) ? bigwidth : this.width) + 'px';
					this.el.childNodes[i].style.height = ((leftcount == i) ? bigheight : this.height) + 'px';
					this.el.childNodes[i].style.fontSize = ((leftcount == i) ? this.dockfontbig : this.dockfontsmall) + 'px';
					//eval('this.el.childNodes[i].onclick = function () { ' + onclickunfocused + ' };');
					eval('this.el.childNodes[i].onclick = function () { ' + this.onclick + ' };');
				}
				//eval('this.el.childNodes[' + leftcount +'].onclick = function () { ' + this.onclick + ' };');
				this.nextprev = 0;
			}
		}
	};
				
	// ----------------------------------------------------------------
	
	this.dock = function (cursor, mode) {
		var increment = (this.transtime / 1000) * this.fps;
		if (this.el.childNodes.length == 0) {
			console.log(1);
			// load initially
			this.count = this.value.length;
			for (var i = 0; i < this.count; i++) {
				this.flag = false;
				this.cursor = 0;
				var left = this.spacing + ((this.width + this.spacing) * i);
				var newel = this.newElement();
				newel.style.left = left + 'px';
				newel.style.top = 'auto';
				newel.style.bottom = '0px';
				newel.innerHTML = this.value[i];
				console.log( this.value[i]);
				eval('newel.onmouseover = function () { ' + this.name + '.dock(' + i + ', 0); };');
				//eval('newel.onmouseout  = function () { ' + this.name + '.dock(' + i + ', 2); };');
				this.el.appendChild(newel);
			}
			this.stop();
		} else if (arguments.length > 1) {
			console.log(2);
			this.cursor = cursor;
			this.transinterval = setInterval(this.name + '.dock(' + mode + ');', Math.round(1000 / this.fps));
		} else {
			console.log(3);
			var changed = false;
			for (var i = 0; i < this.el.childNodes.length; i++) {
				var ratio = 1;
				if ((this.cursor - i) == 0) ratio = 0;
				if (Math.abs(this.cursor - i) == 1) ratio = 1 / 3;
				if (Math.abs(this.cursor - i) == 2) ratio = 2 / 3;
				var elm = this.el.childNodes[i];
				var heightcheck = getNumber(elm.style.height);
				var center = Math.round((this.width * 0.5) + ((this.width + this.spacing) * i));
				var diff = Math.round(this.spacing * 1.5);
				var h = (this.height + this.reflection + Math.round((1 - ratio) * diff));
				changed = changed || (h != heightcheck);
				elm.style.height = h + 'px';
				var w = Math.round(this.width * h / (this.height + this.reflection));
				elm.style.width = w + 'px';
				elm.style.left = Math.round(center - (w / 2)) + 'px';
			}
			if (!changed) {
				this.stop();
			}
		} 
	};
		
	// ----------------------------------------------------------------
	
	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)) {
				// change images - old one (firstchild) out, new one in as lastchild
				this.nextcursor = this.forward();
				// 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.cursor = this.nextcursor;
		}
	};
		
	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) {
			this.nextcursor = 0;
			var newel = this.newElement();
			this.el.appendChild(newel);
			this.nextcursor = this.forward();
			var newel = this.newElement();
			this.el.appendChild(newel);
			this.nextcursor = this.forward();
		} 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 (transtime) {
		this.count = 1;
		if (this.el.childNodes.length == 0) {
			var newel = this.newElement();
			this.el.appendChild(newel);
			this.nextcursor = this.forward();
		} else {
			transtime = (transtime) ? transtime : this.transtime;
			var delta = 1000 / this.fps / transtime * 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
			this.nextcursor = 0;
			var newel = this.newElement();
			this.el.appendChild(newel);
			this.nextcursor = this.forward();
			var newel = this.newElement();
			newel.style[direction] = dimension + 'px';
			this.el.appendChild(newel);
			this.nextcursor = this.forward();
		} 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');
	};

	// screen fade and controls etc ----------------------------------------------------------------
	
	this.screenBlack = function (elid) {
		elid = (elid) ? elid : this.name + 'fade';
		if (!this.black || (this.black.id != elid)) {
			var el = document.getElementById(elid);
			if (el) {
				this.black = el;
				return true;
			}
		}
		return false;
	}
	
	// ----------------------------------------------------------------

	this.screenPort = function (elid) {
		elid = (elid) ? elid : this.name + 'port';
		if (!this.port || (this.port.id != elid)) {
			var el = document.getElementById(elid);
			if (el) {
				this.port = el;
				return true;
			}
		}
		return false;
	}
	
	// ----------------------------------------------------------------

	this.screenProgress = function (elid) {
		if (elid) {
			if (!this.progress || (this.progress.id != elid)) {
				if (document.getElementById(elid))
					this.progress = document.getElementById(elid);
			}
		}
		if (this.progress) {
			this.progress.innerHTML = this.cursor + 1;
			return true;
		}
		return false;
	}
	
	// ----------------------------------------------------------------

	this.screenStart = function (portleft, start, blackid, portid, progressid) {
		this.screenBlack(blackid);
		this.screenPort(portid);
		if (this.black && this.port) {
			this.centering = true;
			this.port.style.display = 'block';
			this.prime();
			this.height = '100%';
			this.boot(start);
			this.black.style.display = 'block';
			this.black.style.left = '0px';
			this.black.style.width = getPageDimension(true) + 'px';
			this.black.style.height = getPageDimension(false) + 'px';
			this.port.style.left = portleft;
			window.scrollTo(0, 0);
		}
		progressid = (progressid) ? progressid : this.name + 'progress';
		this.screenProgress(progressid);
	}
	
	// ----------------------------------------------------------------

	this.screenClose = function () {
		if (this.port) {
			this.port.style.display = 'none';
			this.port.style.left = '-10000px';
		}
		if (this.black) {
			this.black.style.display = 'none';
			this.black.style.left = '-10000px';
		}
	}
	
	// ----------------------------------------------------------------
		
}

