var loaded = 0;
var paused = 0;
var running = false;
var clearMe;
var j = 0;
var p = Pic.length;
var count = 0;
var js;
var css;
window.onload = function() {
	js = document.getElementById('pauser');
	css = js.style;
	var total = preloadimages(); // preload the images
	document.getElementById('pauser').onmouseup = function() {
		if (paused) {
			paused = 0;
			this.firstChild.nodeValue = 'click to pause';
			runSlideShow();
			this.blur();
			return null;
			}
		else {
			clearTimeout(clearMe);
			paused = 1;
			this.firstChild.nodeValue = 'click to run';
			this.blur();
			return null;
			}
	}
	document.getElementById('previous').onmouseup = function() {
			if(!running) {
				clearTimeout(clearMe);
				j = j - 2;
				if (j<0) j = p + j;
				runSlideShow(true);
			}
	}
	document.getElementById('next').onmouseup = function() {
			if(!running) {
				clearTimeout(clearMe);
				runSlideShow(true);
			}
	}

}

//pre load the images
function preloadimages(){
	var i;
	prePic = new Array();
	for (i=0;i<p;i++){
		prePic[i]=new Image();
		prePic[i].src=Pic[i];
		prePic[i].onload = function() {
			if (++loaded == (p - 1)) {
				setTimeout('document.getElementById("blenddiv").style.backgroundPosition = "0% 0%";', transition_speed);
				runSlideShow(); // start the show if we're all loaded up
			}
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	if (opacity == 100) opacity = 99.99;
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
} 
function fade(imageid,from,to,length) {
		var opac = from;
		for(var timer = 0;timer <= length;timer+=smoothness) { // only go up to 99 to stop image flicker in mozilla
			opac = easing(timer, from, to, length,transition);
			setTimeout("changeOpac(" + opac + ",'" + imageid + "')",timer);
		}
}
function blendimage(id, imageid, imageNum, millisec) {
	running = true;
	var x=document.getElementById(imageid)
	//make image transparent
	changeOpac(0, imageid);

	//load new image if we need to
	x.src = prePic[imageNum].src;

	//fade in image
	fade(imageid,0,100,millisec);

	//set the current image as background (but we need to wait until after the image has transisted)
	setTimeout("document.getElementById('"+id+"').style.backgroundImage = 'url(\"" + prePic[imageNum].src + "\")'",millisec+smoothness);
	setTimeout('running = false;', millisec+smoothness);
}

function runSlideShow(forced){
	if (paused && !forced && css && js) return;
	blendimage('blenddiv','blendimage', j++, transition_speed)
	if (j >= p) j=0;
	clearTimeout(clearMe);
	clearMe = setTimeout('runSlideShow()', (transition_timer * 1000) + transition_speed); // this is where you set the timer
}
function easing(t, b, c, d, type) {
/*
--
Easing Equations (c) 2003 Robert Penner, all rights reserved.
This work is subject to the terms in http://www.robertpenner.com/easing_terms_of_use.htl.
--
*/
	switch (type) {
		case 'in': {
			return c*(t/=d)*t + b; // in
			break;
		}
		case 'inout': {
			if (t < d/2) return 2*c*t*t/(d*d) + b;
			var ts = t - d/2;
			return -2*c*ts*ts/(d*d) + 2*c*ts/d + c/2 + b;		
		}
		case 'out': {
			return -c*t*t/(d*d) + 2*c*t/d + b;
		}
		default: {
			return c*t/d + b; //linear
		}
	}
}
