/**
 * Gallery Functions
 * pbreitenmoser - 07.07.2008
 */

/* Create the object */
var xmlHTTP = createRequestObject();

/* Find the browser name */
var browser = navigator.appName;

/* Create array which will be populated with the images.
 * With it, only the path to the image can be loaded, rather than all content */
var images = new Array();

/* The image the array pointer currently is at */
var current_image = 0;

/* Helper function */
function writeHTTPState () {
	document.getElementById("slideshow_description").innerHTML = http.readyState;
}

/**
 *	callSlideShowLayer
 *  Stores the passed galleryId into a hidden field so that it can be accessed again later on.
 *  Calls the previously hidden gallery layer.
 *	Triggers the image delivery service.
 *
 *	@param: galleryid 	the id of the gallery to be activated
 */
function callSlideShowLayer(galleryid) {
	/* Darken the screen behind the new Layer */
	grayOut(true);

	/* Store id */
	document.getElementById('galleryid').value = galleryid;

	/* Show layer */
	document.getElementById('slideshow').style.visibility = 'visible';

	/* Call the layer up front */
	document.getElementById('slideshow').style.zIndex = 100000;
	/* Call image service */
	deliverImage(galleryid);

}

function closeSlideShowLayer(){
	/* Darken the screen behind the new Layer */
	grayOut(false);

	/* Show layer */
	document.getElementById('slideshow').style.visibility = 'hidden';

	/* Call the layer up front */
	document.getElementById('slideshow').style.zIndex = -100000;

}

/**
 * delieverImage
 * Serves as image delivery serive. The function handles all the process needed for
 * the image call.
 *
 * @param: galleryid	the id of the gallery to be accessed
 */
function deliverImage(galleryid) {

	/* Use the object to call the php file wich returns the images */
	xmlHTTP.open('get', '_modules/_gallery/image_delivery.php?galleryid=' + galleryid);
	xmlHTTP.send(null);

	/* Call the function once the object is ready */
	xmlHTTP.onreadystatechange = function() {
		/* Response is not reay until it is 4 */
		if (xmlHTTP.readyState != 4)
			return;
		if (xmlHTTP.status == '200')
       		handleImages();
	}
}

/**
 * createRequestObject
 * Creates an object which handles the HTTP Request
 */
function createRequestObject() {
	/* Declare the variable to hold the object.*/
	var request_o;

	if (browser == 'Microsoft Internet Explorer') {
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}

	/* Return object */
	return request_o;
}

/**
 * handleImages
 * Handles the return values from the php file
 */
function handleImages(){
	/**
	 *	Make sure that the transaction has finished. The XMLHttpRequest object
	 *  has a property called readyState with several states:
	 *	0: Uninitialized
	 *	1: Loading
	 *	2: Loaded
	 *	3: Interactive
	 *	4: Finished
	 */

	/* As soon as the script finished loading the response */
	if(xmlHTTP.readyState == 4){
		/* 	We have got the response from the server-side script,
			let's see just what it was. using the responseText property of the XMLHttpRequest object. */
		var response = xmlHTTP.responseText;

		/* Parse XML */
		if (browser == 'Microsoft Internet Explorer') {

		    xmlobject = new ActiveXObject("Microsoft.XMLDOM");

			xmlobject.async = "false";
			xmlobject.loadXML(xmlHTTP.responseText);

		}
		else {
			xmlobject = document.implementation.createDocument("","",null);
			xmlobject = new DOMParser().parseFromString(xmlHTTP.responseText, "text/xml");
		}

		/* Create XML Object */


		/* Get settings node */
		var settings = xmlobject.getElementsByTagName("settings");


		/* Store gallery settings */
		var path = settings[0].getElementsByTagName('path')[0].childNodes[0] != null ? settings[0].getElementsByTagName('path')[0].childNodes[0].nodeValue : '[%empty_node%]';
		var name = settings[0].getElementsByTagName('name')[0].childNodes[0] != null ? settings[0].getElementsByTagName('name')[0].childNodes[0].nodeValue : '[%empty_node%]';
		var short_description = settings[0].getElementsByTagName('short_description')[0].childNodes[0] != null ? settings[0].getElementsByTagName('short_description')[0].childNodes[0].nodeValue : '[%empty_node%]';
		var description = settings[0].getElementsByTagName('description')[0].childNodes[0] != null ? settings[0].getElementsByTagName('description')[0].childNodes[0].nodeValue : '[%empty_node%]';

		/* Empty image array */
		images.length = 0;

		/* Get imagepaths from xml */
		var gallery_images = xmlobject.getElementsByTagName("images");

		/* Loop through all images */
		for (var i = 0; i < gallery_images[0].getElementsByTagName('filename').length; i++) {
			/* Populate image array */
			if (gallery_images[0].getElementsByTagName('filename')[i].childNodes[0] != null)
				images[i] =  gallery_images[0].getElementsByTagName('filename')[i].childNodes[0].nodeValue;
			else
				continue;
		}

		/* Set readable gallery information (text) */
		setReadable(name, short_description, description);

		/* Set viewable gallery information (images) */
		showImage(images[0]);


	}
}

/**
 * setReadbale
 * Pass all text information to the gallery layer.
 *
 * @param: name					the name of the gallery
 * @param: short_description	the short description (displayed on the right)
 * @param: description			the description (displayed on the left)
 */
function setReadable (name, short_description, description) {

	/* Reset current values */
	document.getElementById('slideshow_title').innerHTML = '';
	document.getElementById('slideshow_short_description').innerHTML = '';
	document.getElementById('slideshow_long_description').innerHTML = '';

	/* Set the text */
	document.getElementById('slideshow_title').innerHTML = (name != '[%empty_node%]') ? '<span style="color: #c9252b; font-size: 13px;">Projekt: </span>' + name : '';
	document.getElementById('slideshow_short_description').innerHTML = (short_description != '[%empty_node%]') ? short_description : '';
	document.getElementById('slideshow_long_description').innerHTML = (description != '[%empty_node%]') ? description : '';

	/* Set the links */
	document.getElementById('image_indicator_2').innerHTML = images.length;
}

/**
 * showImage
 * Set the image for the gallery The images can be fetched from the image array
 *
 * @param: image	the image to be displayed
*/
function showImage(image) {
	/* Show the image */
	document.getElementById('slideshow_image').innerHTML = '<img src="_includes/phpThumb/phpThumb.php?src=../../accounts/nabel/galleries/' + image + '&w=720&zc=1" alt="" />';
}

/**
 * nextImage()
 * function for 'next image'
 */
function nextImage() {

	/* Validate if desired action is possible */
	if (current_image+1 >= images.length)
		current_image = -1;

	/* Display next image */
	showImage(images[current_image+1]);
	current_image++;

	/* Write image count */
	document.getElementById('image_indicator_1').innerHTML = current_image+1;
}

/**
 * PreviousImage()
 * function for 'previous image'
 */
function previousImage() {

	/* Validate if desired action is possible */
	if (current_image-1 < 0)
		current_image = images.length;

	/* Display next image */
	showImage(images[current_image-1]);
	current_image--;
}


