addLoadEvent(initShow);

function initShow(){
	showSetup();
}
var loc,imagepath,totalimages,view,showBox,start,steps,pics,display,counter,previous,nextcounter,secs,timerID,timerRunning,delay;
function setVars(x,num){
	loc = null;	
	imagepath = 'photos/'+x+'/';	
	totalimages = num;	
	view = document.getElementById('view');
	showBox = document.getElementById('showBox');
	start = null;
	steps = 1;
	pics = Array();
	display = null;
	counter = null;
	previous = null;
	nextcounter = null;
	secs = null;
	timerID = null;
	timerRunning = false;
	delay = 1000;
	doSlide();
}
doSlide = function(){
	if(!document.getElementById("ctrls")) insertControles();
	view.setAttribute("src",imagepath+'1.jpg');
	init();
	InitializeTimer();
}
insertControles = function(){
	var ctrls = '<li><a name="previous" href="#">Previous</a></li><li><a name="stop" href="#">Stop</a></li><li><a name="play" href="#">Play</a></li><li><a name="next" href="#">Next</a></li>';
	var ul = document.createElement("ul");
	ul.setAttribute("id","ctrls");
	showBox.insertBefore(ul,view);
	var control = document.getElementById("ctrls");
	control.innerHTML = ctrls;
	var links = control.getElementsByTagName("a");
	for(x in links){
		links[x].onclick = function(){
			switch(this.name){
				case 'previous':
					retreat();
					break;
				case 'stop':
					StopTheClock();
					break;
				case 'play':
					advance();
					InitializeTimer();
					break;
				case 'next':
					advance();
			}
			return false;
		}
	}
}



function init()
{
	counter = 1;
	display = view;
	loadImage(1);
	loadImage(2);
	loadImage(totalimages);
}

function loadImage(index) {
	if (! pics[index]) {
		pics[index] = new Image();
		pics[index].src = imagepath+index+'.jpg';
	}
}
function advance()
{
	if (counter == totalimages) {
		previous = counter;
		counter = 1;
	} else if (counter == totalimages - 1) {
		previous = counter;
		counter++
	} else {
		previous = counter;
		counter++;
		loadImage(counter + 1);
	}
	
	initSlide();
	StopTheClock();
}
function retreat()
{
	if (counter == 1) {
		previous = counter;
		counter = totalimages;
		loadImage(totalimages - 1);
		nextcounter = counter - 1;
	} else if (counter == 2) {
		previous = counter;
		counter--;
	} else {
		previous = counter;
		counter--;
		loadImage(counter - 1);
	}
	
	initSlide();
	StopTheClock();
}

function initSlide()
{
	width_diff = pics[counter].width - pics[previous].width;
	width_increment = width_diff / steps;
	height_diff = pics[counter].height - pics[previous].height;
	height_increment = height_diff / steps;
	slide(100, width_increment, height_increment, 1);
}

function slide(opacity, width_increment, height_increment, tally_steps)
{
	if (tally_steps != steps) {
		tally_steps++;
		display.width += width_increment;
		display.height += height_increment;
		setOpacity(opacity)
		opacity -= 100/steps;
		window.setTimeout("slide("+opacity+","+width_increment+","+height_increment+","+tally_steps+")", 50);
	} else {
		display.style.backgroundImage = 'url('+imagepath+'1.jpg)';
		setOpacity(0);
		display.src = imagepath+counter+'.jpg';
		display.width = pics[counter].width;
		display.height = pics[counter].height;
		fadeIn(10);
	}
}

function setOpacity(opacity) 
{
	opacity = (opacity == 100)?99.999:opacity;
	display.style.filter = "alpha(opacity:"+opacity+")";
	display.style.KHTMLOpacity = opacity/100;
	display.style.MozOpacity = opacity/100;
	display.style.opacity = opacity/100;
}
function fadeIn(opacity){
	if (opacity <= 100)	{
		setOpacity(opacity);
		opacity += 5;
		window.setTimeout("fadeIn("+opacity+")", 50);
	}
}
function fadeOut(opacity){
	if (opacity <= 100)	{
		setOpacity(opacity);
		opacity -= 5;
		window.setTimeout("fadeOut("+opacity+")", 50);
	}
}
function InitializeTimer(){
	secs = 5;
    StopTheClock();
    StartTheTimer();
}

function StopTheClock(){
    if(timerRunning) clearTimeout(timerID);
    timerRunning = false;
}

function StartTheTimer(){
    if (secs==0){
        StopTheClock();
		advance();
		InitializeTimer();
    }else{
        secs = secs - 1;
        timerRunning = true;
        timerID = self.setTimeout("StartTheTimer()", delay);
    }
}
