/**
	NOTE: photo_gallery_editor.js MUST be included AFTER this file!
 */
/**
	@note The following code is for ensuring that the editors in the admin
	section are closed when moving to a new image.  However, this need
	only be done on the admin page.  Since the slider functions are
	shared between the two pages, we are adding the following lines
	to make sure that the function for closing the editors is defined.
 */
function CancelEditor() {
	return;
}


// global variables
var arr_sliders = new Object();
var MAX_PHOTO_GALLERY_SLIDER_IMAGES;
var CURRENT_IMAGE_BUFFER = 'photo-gallery-current-image-container';
var CURRENT_IMAGE_TITLE = 'photo-gallery-current-image-title';
var CURRENT_IMAGE_CAPTION = 'photo-gallery-current-image-caption';
var CURRENT_IMAGE_COURTESY = 'photo-gallery-current-image-courtesy';
var CURRENT_IMAGE_TAG_INDEX = 'photo-gallery-current-image-tag-index';
var CURRENT_IMAGE_ID = 'photo-gallery-current-image-id';
var LOADING_IMAGE_BUFFER = 'photo-gallery-transition-image-container';
var THUMBNAIL_IMAGE_CONTAINER_PREFIX = 'photo-gallery-slide-image-container-';
var THUMBNAIL_LOADING_PREFIX = 'photo-gallery-transition-image-container-';

function ImageLoadingHandler() { /* {{{ */

	this.style.display = '';
	SwapBufferToCurrent();
	return;
} /* }}} */

function ThumbnailLoadingHandler() { /* {{{ */

	var str_img_index = ParseThumbnailId(this.id);
	ClearBuffer(document.getElementById(THUMBNAIL_LOADING_PREFIX + str_img_index));
	this.parentNode.removeChild(document.getElementById(THUMBNAIL_LOADING_PREFIX + str_img_index));
	this.style.display = '';

	return;
} /* }}} */

function SwapBufferToCurrent() { /* {{{ */
	document.getElementById(LOADING_IMAGE_BUFFER).style.display = 'none';
	document.getElementById(CURRENT_IMAGE_BUFFER).style.display = '';
	document.getElementById(CURRENT_IMAGE_TITLE).style.visibility = 'visible';
	document.getElementById(CURRENT_IMAGE_CAPTION).style.visibility = 'visible';

	return;
} /* }}} */

function SwapBufferToLoading() { /* {{{ */

	document.getElementById(CURRENT_IMAGE_TITLE).style.visibility = 'hidden';
	document.getElementById(CURRENT_IMAGE_CAPTION).style.visibility = 'hidden';
	document.getElementById(CURRENT_IMAGE_BUFFER).style.display = 'none';
	document.getElementById(LOADING_IMAGE_BUFFER).style.display = '';

	return;
} /* }}} */

function ClearCurrentBuffer() { /* {{{ */

	// get reference to current buffer
	var elm_buffer = document.getElementById(CURRENT_IMAGE_BUFFER);

	// clear buffer
	ClearBuffer(elm_buffer);

	// destroy reference
	elm_buffer = null;

	return;
} /* }}} */

function LoadCurrentImage(int_index, arr_images) { /* {{{ */

	var elm_buffer = null;	// reference to buffer
	var elm_image = null;	// reference to new image
	var str_height = null;	// height of new image
	var str_width = null;	// width of new image
	var str_src = null;		// source of new image
	var str_id = null;		// id of new image (not using)
	var str_class = null;	// class of new image (not using)
	var str_alt = null;		// alt text of new image

	// get image values
	str_height = arr_images[int_index].pg_height;
	str_width = arr_images[int_index].pg_width;
	str_src = arr_images[int_index].pg_src;
	str_alt = arr_images[int_index].alt;

	// get buffer reference
	elm_buffer = document.getElementById(CURRENT_IMAGE_BUFFER);

	// load image into buffer
	LoadImage(str_id, str_class, str_src, str_width, str_height, str_alt, ImageLoadingHandler, elm_buffer);

	// destroy reference
	elm_buffer = null;

	return;
} /* }}} */

function MakeCurrentImage(str_tag, int_index) { /* {{{{ */

	// make sure everything exists before attempting to use them
	if ((null != document.getElementById(CURRENT_IMAGE_BUFFER)) &&
		(null != document.getElementById(CURRENT_IMAGE_TITLE)) &&
		(null != document.getElementById(CURRENT_IMAGE_CAPTION)) &&
		(null != document.getElementById(CURRENT_IMAGE_TAG_INDEX)) &&
		(null != document.getElementById(CURRENT_IMAGE_ID)) &&
		(null != document.getElementById(CURRENT_IMAGE_COURTESY)) &&
		(null != arr_sliders[str_tag]) &&
		(null != arr_sliders[str_tag].images) &&
		(int_index < arr_sliders[str_tag].images.length))
	{
		SwapBufferToLoading();
		ClearCurrentBuffer();
		LoadCurrentImage(int_index, arr_sliders[str_tag].images);

		// setup the current image with new image settings
		document.getElementById(arr_sliders[str_tag].images[int_index].id).className = 'photo-gallery-selected-photo';
		document.getElementById(CURRENT_IMAGE_TITLE).innerHTML = arr_sliders[str_tag].images[int_index].title;
		document.getElementById(CURRENT_IMAGE_CAPTION).innerHTML = arr_sliders[str_tag].images[int_index].caption;
		document.getElementById(CURRENT_IMAGE_COURTESY).innerHTML = arr_sliders[str_tag].images[int_index].courtesy;
		document.getElementById(CURRENT_IMAGE_ID).value = arr_sliders[str_tag].images[int_index].img_id;
	}

	return;
} /* }}} */

// increment slider one image
function NextImage(str_tag) { /* {{{ */

	// this function call is in photo_gallery_editor.js
	if (CancelEditor) {
	    CancelEditor();
	}

	// hide left-most image
	document.getElementById(arr_sliders[str_tag].images[arr_sliders[str_tag].int_start_image].id).parentNode.className = 'photo-gallery-slide-image-container-hidden';

	// increment counters
	arr_sliders[str_tag].int_start_image++;
	arr_sliders[str_tag].int_stop_image++;

	// check of overflow
	if (arr_sliders[str_tag].int_start_image >= arr_sliders[str_tag].images.length) {
		arr_sliders[str_tag].int_start_image = 0;
	}
	if (arr_sliders[str_tag].int_stop_image >= arr_sliders[str_tag].images.length) {
		arr_sliders[str_tag].int_stop_image = 0;
	}

	// move other images into correct position
	UpdateImagePositions(str_tag);

	return;
} /* }}} */

// decrement slider one image
function PreviousImage(str_tag) { /* {{{ */

	// this function call is in photo_gallery_editor.js
	if (CancelEditor) {
		CancelEditor();
	}

	// hide right-most image
	document.getElementById(arr_sliders[str_tag].images[arr_sliders[str_tag].int_stop_image].id).parentNode.className = 'photo-gallery-slide-image-container-hidden';

	// decrement counters
	arr_sliders[str_tag].int_start_image--;
	arr_sliders[str_tag].int_stop_image--;

	// check of overflow
	if (arr_sliders[str_tag].int_start_image < 0) {
		arr_sliders[str_tag].int_start_image = arr_sliders[str_tag].images.length - 1;
	}
	if (arr_sliders[str_tag].int_stop_image < 0) {
		arr_sliders[str_tag].int_stop_image = arr_sliders[str_tag].images.length - 1;
	}

	// move other images into correct position
	UpdateImagePositions(str_tag);

	return;
} /* }}} */

// for building reference arrays of images
function GetImages() { /* {{{ */

	var loop_count = 0;
	if (MAX_PHOTO_GALLERY_SLIDER_IMAGES < arr_sliders['all'].images.length) {
		loop_count = MAX_PHOTO_GALLERY_SLIDER_IMAGES;
	}
	else {
		loop_count = arr_sliders['all'].images.length;
	}

	for (var i = 0; i < loop_count; i++) {
		var str_container_id = THUMBNAIL_IMAGE_CONTAINER_PREFIX + (i + 1);
		var elm_container = 
		LoadImage(arr_sliders['all'].images[i].id,
				  arr_sliders['all'].images[i].css_class,
				  arr_sliders['all'].images[i].src,
				  arr_sliders['all'].images[i].width,
				  arr_sliders['all'].images[i].height,
				  arr_sliders['all'].images[i].alt,
				  ThumbnailLoadingHandler,
				  document.getElementById(THUMBNAIL_IMAGE_CONTAINER_PREFIX + ParseThumbnailId(arr_sliders['all'].images[i].id)));
	}

	MakeCurrentImage('all', 4);

	return;
} /* }}} */

function MoveToCurrent(str_tag, int_index) { /* {{{ */

	var int_images_to_display = 0;

	// this function call is in photo_gallery_editor.js
	if (CancelEditor) {
		CancelEditor();
	}

	if (arr_sliders[str_tag].images.length < MAX_PHOTO_GALLERY_SLIDER_IMAGES) {
		int_images_to_display = arr_sliders[str_tag].images.length;
	}
	else {
		int_images_to_display = MAX_PHOTO_GALLERY_SLIDER_IMAGES;
	}

	// check variable existence
	if ((null != arr_sliders[str_tag]) &&
		(null != arr_sliders[str_tag].images) &&
		(int_index < arr_sliders[str_tag].images.length))
	{
		// hide current images
		HideCurrentImages(str_tag);

		// update start and stop indecies
		arr_sliders[str_tag].int_start_image = int_index - parseInt((int_images_to_display / 2), 10);
		arr_sliders[str_tag].int_stop_image = int_index + parseInt((int_images_to_display / 2), 10);

		// check for overflow
		if (0 > arr_sliders[str_tag].int_start_image) {
			arr_sliders[str_tag].int_start_image = arr_sliders[str_tag].int_start_image + arr_sliders[str_tag].images.length;
		}
		if (arr_sliders[str_tag].images.length <= arr_sliders[str_tag].int_stop_image) {
			arr_sliders[str_tag].int_stop_image = arr_sliders[str_tag].int_stop_image - arr_sliders[str_tag].images.length;
		}

		// update the image positions
		UpdateImagePositions(str_tag);
	}

	return;
} /* }}} */

function UpdateImagePositions(str_tag) { /* {{{ */

	var loop_count = 0;

	if (arr_sliders[str_tag].int_image_count < MAX_PHOTO_GALLERY_SLIDER_IMAGES) {
		loop_count = arr_sliders[str_tag].images.length;
	}
	else {
		loop_count = MAX_PHOTO_GALLERY_SLIDER_IMAGES;
	}

	for (var i = 0; i < loop_count; i++) {

		var int_index = arr_sliders[str_tag].int_start_image + i;

		// check for wrap around
		if (int_index >= arr_sliders[str_tag].images.length) {
			int_index -= arr_sliders[str_tag].images.length;
		}

		// check if image is already loaded
		if (document.getElementById(arr_sliders[str_tag].images[int_index].id)) {

			document.getElementById(arr_sliders[str_tag].images[int_index].id).parentNode.className = 'photo-gallery-slide-image-container-' + (i + 1);
			document.getElementById(arr_sliders[str_tag].images[int_index].id).className = '';
		}
		else {

			document.getElementById(THUMBNAIL_IMAGE_CONTAINER_PREFIX + ParseThumbnailId(arr_sliders[str_tag].images[int_index].id)).className = 'photo-gallery-slide-image-container-' + (i + 1);
			LoadImage(arr_sliders[str_tag].images[int_index].id,
				  arr_sliders[str_tag].images[int_index].css_class,
				  arr_sliders[str_tag].images[int_index].src,
				  arr_sliders[str_tag].images[int_index].width,
				  arr_sliders[str_tag].images[int_index].height,
				  arr_sliders[str_tag].images[int_index].alt,
				  ThumbnailLoadingHandler,
				  document.getElementById(THUMBNAIL_IMAGE_CONTAINER_PREFIX + ParseThumbnailId(arr_sliders[str_tag].images[int_index].id)));
		}
		

		// 5 being the middle index of 9 (the number of thumbnails)
		if (5 == (i + 1)) {
			MakeCurrentImage(str_tag, int_index);
		}
	}

	return;
} /* }}} */

function ParseThumbnailId(str_id) { /* {{{ */

	var img_id = -1;

	img_id = parseInt(str_id.substr(str_id.lastIndexOf('-') + 1), 10);

	return img_id;
} /* }}} */

function HideCurrentImages(str_tag) { /* {{{ */

	var loop_count = 0;

	if (arr_sliders[str_tag].int_image_count < MAX_PHOTO_GALLERY_SLIDER_IMAGES) {
		loop_count = arr_sliders[str_tag].images.length;
	}
	else {
		loop_count = MAX_PHOTO_GALLERY_SLIDER_IMAGES;
	}

	for (var i = 0; i < loop_count; i++) {

		var int_index = arr_sliders[str_tag].int_start_image + i;

		// check for wrap around
		if (int_index >= arr_sliders[str_tag].images.length) {
			int_index -= arr_sliders[str_tag].images.length;
		}


		// check for existance
		if (document.getElementById(THUMBNAIL_IMAGE_CONTAINER_PREFIX + ParseThumbnailId(arr_sliders[str_tag].images[int_index].id))) {
			document.getElementById(THUMBNAIL_IMAGE_CONTAINER_PREFIX + ParseThumbnailId(arr_sliders[str_tag].images[int_index].id)).className = 'photo-gallery-slide-image-container-hidden';
		}
	}

	return;
} /* }}} */

