// This is the javascript file that goes with /index.jsp
var debug = false;
var verbose = false;

var currentImage = 0;
var isSlideshow = false;
var winZoom;

function initPage( strSetImage )
{
	// initializes HTML page
	if (debug && verbose) alert( "entering initPage" );

	// init focus

	// init images
	if ( strSetImage == "IMAGE_RANDOM" )
	{
		setImage( -1 );
	}
	else if ( strSetImage == "IMAGE_FIRST" )
	{
		setImage( 0 );
	}
	else if ( strSetImage == "IMAGE_PARAM" )
	{
		var strImageNum = location.getParameter("imagenum");
		
		if ( strImageNum != null && parseInt(strImageNum) != NaN )
		{
			setImage( parseInt(strImageNum) );
		}
		else
		{
			setImage( 0 );
		}
	}
	else if ( strSetImage == "IMAGE_NONE" )
	{
		// do nothing;
	}
	else
	{
		setImage( 0 );
	}
	
	
	return true;
}

function changeBackground( sUrl )
{
	document.body.style.backgroundImage = "url(" + sUrl + ")";
	return true;
}

function setImage( iImageNumber )
{
	if (debug && verbose) alert( "entering setImage" );
	
	if ( iImageNumber == -1 )
	{
		// get random image
		iImageNumber = ( Math.round( Math.random() * (imageGallery.length - 1) ) );
	}
	
	var strImageName = imageGallery[iImageNumber][0];
	var strImageUrl = imageGallery[iImageNumber][1];
	var strImageCaption = imageGallery[iImageNumber][2];
	
	var objExistingElement = document.getElementById("img_" + strImageName);
	
	if ( objExistingElement )
	{
		if (debug && verbose) alert( "element found with ID [" + strImageName + "]: " + objExistingElement );
		
		if ( objExistingElement.src )
		{
			if (debug && verbose) alert( "element has src property - setting to [" + strImageUrl + "]");
			objExistingElement.src = "/images/" + strImageName + "/" + strImageUrl;
		}
		else
		{
			if (debug) alert( "element does not have src property" );
		}
	}
	else
	{
		if (debug) alert( "no element found with ID [img_" + strImageName + "]" );
	}
	
	objExistingElement = document.getElementById("input_" + strImageName);
	
	if ( objExistingElement )
	{
		if (debug && verbose) alert( "element found with ID [input_" + strImageName + "]: " + objExistingElement );
		
		if ( objExistingElement.innerText )
		{
			if (debug && verbose) alert( "element has value property - setting to [" + strImageCaption + "]");
			objExistingElement.innerText = strImageCaption;
		}
		else
		{
			if (debug) alert( "element does not have value property" );
		}
	}
	else
	{
		if (debug) alert( "no element found with ID [img_" + strImageName + "]" );
	}

	currentImage = iImageNumber;

	return true;
}

function zoomImage()
{
	winZoom = window.open( "/zoom.html?imagenum=" + currentImage, "winZoom", "width=540,height=380,toolbar=no,scrollbars=no,status=no,resizeable=no,menubar=no" );
}

function setImagePrevious()
{
	if (debug && verbose) alert( "entering setImagePrevious" );
	
	isSlideshow = false;
	
	var prevImage = currentImage - 1;
	
	if ( prevImage < 0 ) prevImage = imageGallery.length - 1;
	
	setImage(prevImage);

	return true;
}

function initSlideshow()
{
	isSlideshow = !isSlideshow;

	if ( isSlideshow ) advanceSlideshow();
}

function advanceSlideshow()
{
	if ( isSlideshow )
	{
		var nextImage = currentImage + 1;
		if ( nextImage >= imageGallery.length ) nextImage = 0;
		setImage(nextImage);
		setTimeout("advanceSlideshow();", 5000);
	}
}

function setImageNext()
{
	if (debug && verbose) alert( "entering setImageNext" );
	
	isSlideshow = false;
	
	var nextImage = currentImage + 1;
	
	if ( nextImage >= imageGallery.length ) nextImage = 0;
	
	setImage(nextImage);

	return true;
}

location.getParameter = function(sParam) {

    var sKey = sParam + "=";

    var oParams = this.search.substring(1).split("&");
    for(var i = 0; i < oParams.length; i++)
        if(oParams[i].indexOf(sKey) == 0)
            return oParams[i].substring(sKey.length);
    return null;
};
