/*
 * Simple Album
 * version 1.0
 * Copyright © 2007,2009 Hawke AI
 *
 * Last Modified: 2009-07-09 | JMW
 *
 * JavaScript AJAX Request Module
 *
 * This module contains all the JavaScript to make the requests
 * to the PHP script correctly.
 *
 * The UI backend is found in saajax.js
 *
 * The actual AJAX request code (ajaxReq, ajaxState) is Copyright © 2006 Hawke AI
 *
 */
 
/* Define global variables */
var ajaxObj; // The AJAX object
var bigpic = 0; // integer which defines what big picture is displayed
var captArray = new Array(); // the array of captions returned by the PHP file
var direction = 1; // the direction that the cursor is to move.
var gridArray = new Array(); // the Array of thumbnail pictures returned for the grid
var picArray = new Array(); // the Array of pictures returned by the PHP file
var ssLoop = false; // variable that is called to make sure that slideshow loops
var ssRunning = false; // state of the slide show, true=running, false=stopped
var target = 'thumbs'; // determines whether the grid or the thumbnails are to be changed
var albumbegin = true; // defines whether we're at the beginning
var albumend = false; // defines whether we're at the end of the album

function ajaxReq(locator) {
 /*
  * Function Name: ajaxReq
  * Arguments Accepted: URL to XML source
  *
  * ajaxReq sets up the XML connection to the requested XML source and then
  * passes the information on to ajaxState for processing.
  *
  * Copyright © 2006,2007 Hawke AI
  */
	/* XML Request object for Mozilla and others */
	if (window.XMLHttpRequest) {
		ajaxObj = new XMLHttpRequest();
		ajaxObj.onreadystatechange = ajaxState;
		ajaxObj.open("GET", locator, true);
		ajaxObj.send(null);
	/* XML Request object of IE */
	} else if (window.ActiveXObject) {
		ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxObj) {
			ajaxObj.onreadystatechange = ajaxState;
			ajaxObj.open("GET", locator, true);
			ajaxObj.send(null);
		}
	/* display if program fails */
	} else {
		alert("We're sorry, the browser you are using does not support the JavaScript version necessary to display this page. Please either turn on the JavaScript capabilities of your browser or upgrade it.");
	}
}

function ajaxState() {
 /*
  * Function Name: ajaxState
  * Arguments Accepted: none
  *
  * ajaxState processeses the ajaxObj returned by ajaxReq triggers saProcessAjax
  * to process the data returned.
  */
	if (ajaxObj.readyState == 4) {
		if (ajaxObj.status == 200) {
			saProcessAjax(ajaxObj.responseText);
		} else {
			alert("Problem retrieving data: " + ajaxObj.statusText);
		}
	}
}

function saLoadThumbs(bf,n,move) {
 /*
  * Function Name: saLoadThumbs
  * Arguments Accepted: beginning file in array, number of pictures return,
  *						which way to move (forward of backward), boolean
  *                     to determine whether captions should be added
  *
  * saLoadThumbs makes the calls to start the loading of the thumbnails at the top of
  * the screen.
  */
  	var bfn; // new before file
	
  	/* split the file name out */
	if(bf.indexOf(',') > -1) {
		bfn = saSplitPicVar(bf,'filename');
	} else {
		bfn = bf;
	}
	/* build url string to AJAX source */
	var requeststring = assetDir + "sarequest.php?folder=" + saRequestDir +  "&bf=" + bfn + "&n=" + n + "&move=" + move;
	if (writeCapt) { requeststring += "&captions=true&language=" + sitelang; }

	/* load the spinners (if not yet loaded) */
	saSpinners("all");
	/* set which big picture to load 7 is the last 0 is the first */
	if (move == "back") {
		if (target == "grid") {
			bigpic = numGrid + 1;
		} else {
			bigpic = numThumbs + 1;
		}
		direction = -1;
	} else {
		bigpic = 0;
		direction = 1;
	}
	/* trigger AJAX request */
	ajaxReq(requeststring); 
}

function saProcessAjax(data) {
 /*
  * Function Name: saProcessAjax
  * Arguments Accepted: raw data from AJAX request
  *
  * saProcessAjax takes the AJAX data and processes it, triggering the
  * sa functions necessary to write the pictures and possibly captions
  * to the browser.
  */
	var contvar = false; // controls the loop
	var i=0;			 // iterator
	var pictures;        // raw string of data for displaying pictures
	var captions;        // raw string of data for writing captions
	var filename;		 // the file name split out of the image
	
	/* if the captions exist, split data accordingly */
	if (data.indexOf("||") > -1) { 
		pictures = data.substring(0, data.indexOf("||")); // splits pictures out
		picArray = pictures.split("|");
		captions = data.substring(data.indexOf("||")+2); // splits captions out
		captArray = captions.split("|");
		/* makes sure captArray has one member less than picArray */
		do { captArray.unshift("null"); } while (captArray.length < picArray.length-1) 

	/* if the captions don't exist, split data for pictures only */
	} else {
		picArray = data.split("|");
	}
	/* load thumbnails */
	do {
		i++;
		contvar=false;
		if (i < picArray.length-1) {
			var filename = saSplitPicVar(picArray[i], 'filename');
			if (target == "grid") {
				photoname = "grid_img_" + i;
			} else {
				photoname = "photo_" + i;
			}
			if (picArray[i] != "blank") {
				document.images[photoname].src = contentDir + "tn/" + filename;
				if (target == "grid") {
					if (IEnot7) {
						document.getElementById['tn_grid'+i].className = "thumbcontainer_IEnot7";
					} else {
						document.getElementById['tn_grid'+i].className = "thumbcontainer_univ";
					}
				}
			} else {
				document.images[photoname].src = skinDir + "sm_blank.jpg";
				if (target == "grid") {
					document.getElementById['tn_grid'+i].className = "";
					document.images[photoname].src = skinDir + "blank.gif";
				}
			}
			contvar = true;
		}
	} while (contvar);
	/* load big picture */
	if (target == "thumbs") {
		if ((picArray[picArray.length-1] == "true") && (direction == -1)) {
			i = numThumbs;
			while (picArray[i] == "blank") {
				i--;
			}
			saLoadBigPic(i);
		} else {
			saLoadBigPic(1);
		}
	}
	/* if the slideshow is enabled, start it automatically in the given time */
	if (ssAutostart) { setTimeout("saRunShow()",ssPause*1000); } 
}

function saSplitPicVar(picref, datum) {
 /*
  * Function Name: saSplitPicVar
  * Arguments Accepted: the string containing the picture data,
                        either 'filename', 'orientation', or 'number'
  *
  * Parses out the requested informatio from the presented picture reference
  * string and returns it to the data
  */

	var info; // the response information
	
	switch(datum) {
		case "filename":
			info = picref.substring(0,picref.indexOf(','));
			break;
		
		case "orientation":
			info = picref.substr(picref.indexOf(',')+1,1);
			break;
		
		case "number":
			info = picref.substring(picref.lastIndexOf(',')+1);
			break;
	}
	return info;
}

function saStart() {
 /*
  * Function Name: saStart
  * Arguments Accepted: none
  *
  * saStart is called as the onLoad function to start Simple Album
  */
	saSpinners("all");	// load spinners
	saLoadThumbs("blank",numThumbs,"fwd");
}

