var phoHeights = new Array();
phoHeights['photo1.jpg'] = '0px';
phoHeights['photo2.jpg'] = '-316px';
phoHeights['photo3.jpg'] = '-632px';
phoHeights['photo4.jpg'] = '-948px';
phoHeights['photo5.jpg'] = '-1264px';
phoHeights['photo6.jpg'] = '-1580px';
phoHeights['photo7.jpg'] = '-1896px';
phoHeights['photo8.jpg'] = '-2212px';
var photoQueue = new Array;
var isRunning = false;
var tempVar = 0;
var currentImage = 'photo1.jpg';

function togglePhoto(x) {
	if (x) {
	  if (!emptyStack(photoQueue)) {
		  photoQueue.pop();
	  }
	  photoQueue.push(x);
	}
	if (isRunning) {
		return;
	} else {
		isRunning = true;
        var newImage = photoQueue.pop();
		setTimeout("blendimage('pho-view-wrap','pho-view', '"+newImage+"', 100)",200);
	}
}

function nextImage(max)
{
	var tmpnbr = getImageNumber(currentImage);
	if ((tmpnbr + 1) <= max)
	{
		togglePhoto('photo'+(tmpnbr + 1)+'.jpg');
	}
}

function prevImage()
{
	var tmpnbr = getImageNumber(currentImage);
	if ((tmpnbr -1 ) >= 1)
	{
		togglePhoto('photo'+(tmpnbr - 1)+'.jpg');
	}
}

function getImageNumber(imageName)
{
	var tmpstr = imageName.replace('photo','');
	return parseInt(tmpstr.replace('.jpg',''));									  
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	currentImage = imagefile;
	
	//set the current image as background
	document.getElementById(divid).style.background = document.getElementById(imageid).style.background;

    //shift image to back
	setZindex(imageid, -1);
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	//document.getElementById(imageid).style.background = 'url(images/photoAll.jpg) 0px '+phoHeights[imagefile]+' no-repeat;';
	document.getElementById(imageid).style.background = 'url(images/photoAll.jpg) 0px '+phoHeights[imagefile]+' no-repeat';
	
	//fade in image
	for(i = 0; i <= 100; i++) {
        //shift image to front
	    setZindex(imageid, 1);
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer+=2;
		if (i==100 && !emptyStack(photoQueue)) {
			isRunning = false;
			setTimeout("togglePhoto()",50);
		} else if (i==100 && emptyStack(photoQueue)) {
			isRunning = false;
			return;
		} else {
			//continue iteration
		}   
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
	if (opacity == 100) {
		setFlat();
	}
}

function setFlat() {
//	setZindex('photoView', -1);
}

function preLoad(x) {
	for (i=1; i<=x; i++) {
		loadImage("photo"+i+".jpg");
		loadImage("thumb"+i+".jpg");
	}
	return;
}

function loadImage(s) {
	if (document.images) {
	  eval("pic"+tempVar+"= new Image();");
	  eval("pic"+tempVar+".src = \""+s+"\"");
	  tempVar++;
	}
}

function setZindex(x, n) {
	document.getElementById(x).style.zIndex = n;
	return;
}

function emptyStack(x) {
	return (x.length == 0);
}
	
	function checkStack() {
		alert(photoQueue);
	}