/* IvyFour GALLERY plugin 
*/

// Global variables
var loadingImage = 'images/layout/MessageBox/loading.gif';		// LOADING image location
var loadingText = 'wczytywanie obrazka...';				// LOADING image text
var preloadedImages = new Array();					// For preloaded images list

/* ## CLICKS (THUMBNAILS): Catch thumbnail clicks and load the ZOOM */
function galleryZoom(image,imageName) { 

	// Build image source URL from the 'image' parameter
	var imageSource = $F('site_addr') + $F('galleryMainDir') + "/" + $F('galleryCurrentDir') + '/window/' + image;
	var imageSourceClean = decodeURIComponent(imageSource);		// speed up! decode only once

	// Check if we already have this image in our preloaded list 
	//  and decide whether to display it now, or to preload and display when it after

	 // SHOW
	 if(preloadedImages[imageSourceClean]) { galleryZoomShow(image,imageSource,imageName); }

	 // PRELOAD, ADD TO LIST & SHOW
	 else {

		// Draw the LOADING image + text and clear the onClick events
		$('galleryImage').src = $F('site_addr') + loadingImage;
		$('galleryDescription').innerHTML = loadingText;
		$('galleryImage').onclick = function() { return false; }

		// Preload the image
		var preloadImg = new Image;
		preloadImg.src = imageSource;

		// Display and add to list when loaded!
		preloadImg.onload = function() {
			preloadedImages[imageSourceClean] = true;	// speed up! add to local array (as boolean to process less data in the future)
			setPreloadedSession(imageSourceClean);		// store to $_SESSION
			galleryZoomShow(image,imageSource,imageName);	// show it on screen
		}
	}
}
	// ## HELPER: Show the image
	function galleryZoomShow(image,imageSource,imageName) {

		// Draw the correct image + text and set the onClick events
		$('galleryImage').src = imageSource;
		if(imageName) { $('galleryDescription').innerHTML = imageName; }
		$('galleryImage').onclick = new Function('galleryFullScreen("' + image + '");');
	}

/* ## CLICKS (ZOOM): Open the link in a new window */
function galleryFullScreen(image) { 

	var imageAddr = $F('site_addr') + $F('galleryMainDir') + "/" + $F('galleryCurrentDir') + '/' + image;
	var newWindow = window.open(imageAddr, '_blank');
}

/* ## ENGINE: Add preloaded image to SESSIONs (using AJAX) */
function setPreloadedSession(imageUrl) {

	// Set some vars
	var url = $F('site_addr') + 'components/Gallery_AjaxPreloadedSet.php';
	var pars = 'preloaded='+imageUrl+';';

	// Build AJAX request to store data
	var preloadSet = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: pars,
			onComplete: parsePreloadedSession
		});
}
	// ## HELPER: Parse preloaded session storage
	function parsePreloadedSession(request) {

		// DEBUG: check what we stored so far
		//alert('Stored in session so far: '+request.responseText);
	}

/* ## ENGINE: Send AJAX request to check sessions for previously preloaded images */
function getPreloadedList() {

	// Set some vars
	var url = $F('site_addr') + 'components/Gallery_AjaxPreloadedGet.php';
	var pars = 'foo=bar';

	// Build AJAX request to gather data
	var preloadQuery = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: pars,
			onComplete: parsePreloadedList
		});
}
	// ## HELPER: Parse the preloaded list from previous function
	function parsePreloadedList(request) {

		// DEBUG: check the string we get on start
		//alert('SessionGET response: '+request.responseText);

		// Execute only if no errors
		if(request.responseText != 'error') {

			// Convert the response to an array
			var preloadedList = new Array();
			preloadedList = $A(request.responseText);

			// Temporary var
			var preloadedNow = '';

			// Iterate through letters finding ';' and store correct entries to preloadedImages[] array	// SLOW SLOW SLOW! :(
			preloadedList.each( function(item) {

				// Add letter to some temporary variable if not ';'
				if(item != ';') { preloadedNow += item; }	

				// Add whole sentence to preloadedImages[] array and clearup tmp vars if ';'
				else {
					preloadedImages[preloadedNow] = preloadedNow;
					preloadedNow = '';
				}
			});
		}
	}

/* ## ENGINE: Plugin local runtime  */
function localRuntime() {

	// Build the preloaded list
	preloadedImages[decodeURIComponent($('galleryImage').src)] = true;						// first image (loaded by HTML)
	getPreloadedList();												// more images (from $_SESSION)

	// Preload the 'loading' image
	var loadingImg = new Image;
	loadingImg.src = loadingImage;
}

/* ## ENGINE: Local window events */
window.onload = function() { localRuntime(); }

