/*
 * Simple Album
 * Copyright © 2007 Hawke AI
 *
 * JavaScript UI module
 *
 * This module contains all the JavaScript to make the user interface
 * work correctly.
 *
 * Configuration settings are found in saconfig.js
 * The AJAX backend is found in saajax.js
 *
 * functions are listed alphabetically
 */
function saBottomButtons() {
 /*
  * Function Name: saBottomButtons
  * Arguments Accepted: none
  *
  * saBottomButtons determines whether or not the bottom individual picture navigation buttons
  * need to be zeroed out, i.e. if we're at the first or last picture of the album.
  */
    var contvar = false; // keeps the loop going
	var i = 0;           // loop iterator
	/* retrieve the actual file name of the big picture to test */
	var bigpicname = document.images["bigpic"].src.substring(document.images["bigpic"].src.lastIndexOf("/")+1);
	/* if the big picture is same as the first picture at the beginning of the array, set the bottom
	 * back button to a blank so you can't navigate backwards past it. */
	if ((picArray[0] == "true") && (bigpicname == picArray[1])) {
		document.images["bot_back"].src = skinDir + "b_backbutton_b.jpg";
	} else {
		document.images["bot_back"].src = skinDir + "b_backbutton_n.jpg";
	}
	
	// check to see if slideshow is enabled. If not, set bottom button to blank
	if (ssPause == 0) {
		document.images["bot_ss"].src = skinDir + "b_playbutton_b.jpg";
	// set the bottom button to the default (play) button
	} else {
		// show pause button if the slideshow is running
		if (ssRunning) {
			document.images["bot_ss"].src = skinDir + "b_pausbutton_n.jpg";
		// the play button is the default
		} else {
			document.images["bot_ss"].src = skinDir + "b_playbutton_n.jpg";
		}

	}
	
	// default button: go forward is a rollover
	document.images["bot_fwd"].src = skinDir + "b_fwdbutton_n.jpg";
	
	// check to see if we are at the end of the list of pictures 
	if (picArray[picArray.length-1] == "true") {
		var curpicnum;
		// loop through the picArray to see where in the array the current picture is
		do {			
			i++;
			contvar=false;
			if (i<7) {
				if (picArray[i] == bigpicname) {
					curpicnum = i;
				}
				contvar=true;
			}
		} while (contvar);
		/* if the current picture is the last one in the array, set the next button to blank
		 * so you can't navigate past it */
		if ((curpicnum == 6) || (picArray[curpicnum+1] == "blank")) {
			document.images["bot_fwd"].src = skinDir + "b_fwdbutton_b.jpg";
		}
	}
}

function saLittlePicClick(picnum) {
 /*
  * Function Name: saLittlePicClick
  * Arguments Accepted: picture number
  *
  * saLittlePicClick determines whether a picture is blank or not and loads the big picture
  * if it isn't.
  */
  	var picObj = document.images["photo_" + picnum]; // reference to the image object
	// if the image is not a blank bicture or  a loading picture, allow a click */
	if ((picObj.src.indexOf("sm_blank.jpg")<0) || (picObj.src.indexOf("sm_loading.gif")>0)) {
		saLoadBigPic(picnum); // loads the selected big picture below.
	}

}

function saLoadBigPic(number) {
 /*
  * Function Name: saLoadBigPic
  * Arguments Accepted: picture number to load
  *
  * saLoadBigPic loads the designated big picture and sets the current picture number
  * to the picture loaded.
  */	
	// check to make sure that we're not calling a blank image
	if (picArray[number] == "blank") {
		while (picArray[number] == "blank") {
			number--;
		}
	}
    // change orientation of picture file
	// load the loading picture while downloading the large picture and re-size
	// the orientation of the picture
	if (saSplitPicVar(picArray[number],"orientation") == "p") {
		changeOrientation('p');
		document.images["bigpic"].src = skinDir + "lg_loading_port.gif";
	} else {
		changeOrientation('l');
		document.images["bigpic"].src = skinDir + "lg_loading_land.gif";				
	}
	// make the call for the actual large picture requested
	document.images["bigpic"].src = contentDir + saSplitPicVar(picArray[number],"filename");
	// update the global variable to the current big picture
	bigpic = number;
	// change the thumbnail position
	changeThumbHighlight(number);
	// write image totals
	picXofY('img_txt_xy',saSplitPicVar(picArray[number],'number'),totalImages,0);
	// write the caption, if requested.
	if (writeCapt) saWriteCaption(number);
}

function saNavBigPic(direction) {
 /*
  * Function Name: saNavPicClick
  * Arguments Accepted: +1 or -1
  *
  * saNavBigPic moves the pictures ahead or back one.
  */
    // find the next bigpic number using the global variable
	bigpic = bigpic + direction;
	// see if we need to load the previous n thumbnails
	if (bigpic == 0) {
		if ((picArray[0] == "true") && (picArray[picArray.length-1] == "true")) {
			saLoadBigPic(picArray.length-2);
		} else if ((picArray[0] == "true") && (picArray[picArray.length-1] != "true")) {
			bigpic = picArray.length-2;
			saLoadThumbs("loop",numThumbs,"back");
		} else {
			saLoadThumbs(picArray[1],numThumbs,"back");
		}
		// see if we need to load the next n thumbnails
	} else if ((bigpic == picArray.length-1) || (picArray[bigpic] == "blank")) {
		if ((picArray[0] == "true") && (picArray[picArray.length-1] == "true")) {
			saLoadBigPic(1);
		} else if ((picArray[0] != "true") && (picArray[picArray.length-1] == "true")) {
			bigpic = 1;
			saLoadThumbs("loop",numThumbs,"fwd");
		} else {
			saLoadThumbs(picArray[picArray.length-2],numThumbs,"fwd");
		}
	// otherwise just load the next picture in the array.
	} else {
		saLoadBigPic(bigpic);
	}
}

function saNavClick(button) {
 /*
  * Function Name: saNavClick
  * Arguments Accepted: button name
  *
  * saBigNavClick tests to see if the button should accept the command and forwards it, if necessary.
  */
    // make sure that the button is not turned off
	if (document.images[button].src.indexOf("_b.jpg") < 0) {
		// check to see if slideshow is running and if it is, stop it.
		if (ssRunning) saRunShow();
		// select the correct button and carry out navigation
		switch (button) {
			case "previmg" :
				saNavBigPic(-1); // move one big picture back
				break;
			case "nextimg" :
				saNavBigPic(+1);  // move one big picture forwoard
				break;
			case "prevfive" :
				if (picArray[0] == "true") {
					if(picArray[picArray.length-1] == "true") {
						saLoadBigPic(picArray.length-2);
					} else {
						saLoadThumbs("loop",numThumbs,"back");
					}
				} else {
					saLoadThumbs(picArray[1],numThumbs,"back");
				}
				break;
			case "nextfive" :
				if (picArray[picArray.length-1] == "true") {
					saLoadThumbs("loop",numThumbs,"fwd");
				} else {
					saLoadThumbs(picArray[picArray.length-2],numThumbs,"fwd");
				}
				break;
		}
	}
}

function saRunShow() {
 /*
  * Function Name: saRunShow
  * Arguments Accepted: none
  *
  * saRunShow starts and stops the slide show.
  */
	// check to see if slideshow is enabled
	if (ssPause > 0) {
		// check id slideshow is running
		if (!ssRunning || ssLoop) {
			// start show
			ssRunning = true;
			// check to see if we're at last slide, if so rewind to first slide
			if ((picArray[picArray.length-1] == "true") && ((bigpic == picArray.length-2) || (picArray[bigpic+1] == "blank"))) {
				ssLoop = true; // makes sure that saRunShow doesn't stop when called twice
				ssAutostart = true; // triggers saRunShow when new thumbnails load
				saLoadThumbs("blank",numThumbs,"fwd");
			} else {
				saSlideShow(); // call the slideshow display function
			}
		} else {
			// stop show
			ssRunning = false;
		}
	}
}

function saSlideShow() {
 /*
  * Function Name: saSlideShow
  * Arguments Accepted: none
  *
  * saSlideShow is the function that actually runs the slideshow.
  * It will call itself via the setTimeout function.
  */
  	objPic = document.images["bigpic"]; // object connector to big picture
	timeout = ssPause * 1000; // timeout is the millisecond version of ssPause
  	// start if slide show is set to run
	if (ssRunning) {
		/* if both ssAutostart and ssLoop are set, it means we looped around from
		 * the last picture. These need to be turned off to keep from infinite-looping
		 * the the slide show. */
		if (ssAutostart && ssLoop) {
			ssAutostart = false;
			ssLoop = false;
		}
		// if we're at the last picture stop show
		if ((picArray[picArray.length-1] == "true") && ((bigpic == picArray.length-2) || (picArray[bigpic+1] == "blank"))) {
			saRunShow(); // stop the slide show
			// reset slideshow play button
			slideshowButton('norm');
		// otherwise iterate up to next next
		} else {
			/* check to see if the next photo is actually loaded before calling next
			 * photo (this is to facilitate slow connections) */
			if (((objPic.src != assetDir + "lg_loading_land.gif") || (objPic.src != assetDir + "lg_loading_land.gif"))
																	  && (objPic.complete)) {
				saNavBigPic(+1);
			}
			// call the function again to move to next slideshow
			window.setTimeout("saSlideShow()", timeout);
		}
	}
}

function saSpinners(whichones) {
 /*
  * Function Name: saSpinners
  * Arguments Accepted: "all" or "bigpic"
  *
  * saSpinners loads the "picture loading" spinners into the two picture frames. 
  * "all" determines that all the top and the bottom spinners must be loaded.
  * "bottom determines that only the lower picture spinner must be loaded.
  */
	var contvar = false;  // keeps the loop going
	var i = 0;			  // iterator variable
	/* if "all" is selected, loop through all pictures ("photo"+i) and replace
	 * them with the loading picture image. */
	if (whichones == "all") {
		var currentimage = "";
		do {
			i++;
			contvar=false;
			if (i<picArray.length-1) {
				currentimage = "photo_" + i;
				document.images[currentimage].src = skinDir + "/sm_loading.gif";
				contvar=true;
			}
		} while (contvar);
	}

	// default is the loading picture for the bottom image.
	changeOrientation('l');
	document.images["bigpic"].src = skinDir + "/lg_loading_land.gif";
}

function saTopButtons() {
 /*
  * Function Name: saTopButtons
  * Arguments Accepted: none
  *
  * saTopButtons calculates whether or not the top navigational buttons need to be
  * zeroed out because we are at the beginning/end of the album.
  */
	/* check to see if we're at the beginning of the thumbnails list and set the button
	 * to be unclickable so you can't navigate past the beginning. */
	if (picArray[0] == "true") {
		document.images["top_back"].src = skinDir + "/t_backbutton_b.jpg";
	} else {
		document.images["top_back"].src = skinDir + "/t_backbutton_n.jpg";
	}
	/* check to see if we're at the end of the thumbnails list and set the button
	 * to be unclickable so you can't navigate past the end. */
	if (picArray[picArray.length-1] == "true") {
		document.images["top_fwd"].src = skinDir + "/t_fwdbutton_b.jpg";
	} else {
		document.images["top_fwd"].src = skinDir + "/t_fwdbutton_n.jpg";
	}
}

function saWriteCaption(picnum) {
 /*
  * Function Name: saWriteCaptions
  * Arguments Accepted: picture number
  *
  * saWriteCaption writes out the caption of a given picture
  */
  var captObj = document.getElementById("caption"); // a reference to the caption element
  captObj.firstChild.nodeValue=captArray[picnum]; // replace the content of the caption element
}

