Hootenanny = window.Hootenanny || {};

Hootenanny.ImageGallery = function() {
	var _images = [];
	var _batch_size = 11;
	var _total_batches = 0;
	var _current_batch = 0;
	var _current_index = 0;
	var _update_thumb = false;
	
	function _preload_images() {
		for (var i = 0; i < _images.length; i++) {
			$.preloadImages(_images[i].file);
		}
	}
	
	function _init_navigation() {
		$('#previous-button a:first').click(function() {
			Hootenanny.ImageGallery.prev();
			return false;
		});
		
		$('#next-button a:first').click(function() {
			Hootenanny.ImageGallery.next();
			return false;
		});
	}
	
	function _init_thumbs() {
		$('#next_thumb').click(function() {
			_current_batch++;
			_load_batch();
			return false;
		});
		
		$('#prev_thumb').click(function() {
			_current_batch--;
			_load_batch();
			return false;
		});
		
		$('#image-list a:not(#next_thumb, #prev_thumb)').each(function(i) {
			$(this).click(function() {
				// don't take action if the thumb is already selected
				if (!$(this).hasClass('selected')) {
					// update current index
					for (var j = 0; j < _images.length; j++) {
						if (_images[j].file == $(this).attr('href')) {
							_current_index = j;
							break;
						}
					}
					
					var img_src = $(this).attr('href');
				
					$('#current-image img:first').attr('src', img_src);
					
					$('#title').html(_images[_current_index].title);
				
					// set selected style
					$('#image-list a').removeClass('selected');
					$(this).addClass('selected');
				}
				
				return false;
			})
		});
		
		_select_thumb();
	}
	
	function _select_thumb() {
		// de-select all
		$('#image-list a').removeClass('selected');
		
		$('#image-list a:not(#next_thumb, #prev_thumb)').each(function() {
			if ($(this).attr('href') == _images[_current_index].file) {
				$(this).addClass('selected');
				
				var img_src = _images[_current_index].file;
				img_src = img_src.replace(/thumbs\//gi, '');

				$('#current-image img:first').attr('src', img_src);
				
				$('#title').html(_images[_current_index].title);
			}
		});
	}
	
	function _load_batch() {
		var start_index = (_current_batch * _batch_size);
		var limit_modifier = null;
		
		if (_images.length < _batch_size) {
			limit_modifier = _images.length - _batch_size;
		} else {
			if (_images.length - start_index > _batch_size) {
				limit_modifier = 0;
			} else {
				limit_modifier = _images.length - start_index;
			}
		}
		
		var batch_item = 1;
		var li, anchor, img;
		
		var image_list = $('#image-list')[0];
		
		$(image_list).empty();
		
		// do we need to add the 'previous' thumbnail?
		if (_current_batch > 0) {
			li = document.createElement('li');
			
			anchor = document.createElement('a');
			$(anchor).attr('id', 'prev_thumb');
			$(anchor).attr('href', '');
			
			img = document.createElement('img');
			$(img).attr('src', '/images/prev_thumb.png');
			
			anchor.appendChild(img);
			li.appendChild(anchor);
			image_list.appendChild(li);
			
			batch_item++;
			
			// only decrement limit if the batch is full
			if ((_batch_size + limit_modifier) - start_index == _batch_size) {
				//alert('shortening batch');
				limit_modifier--;
			}
		}
		
		for (var i = start_index; i < (_batch_size + limit_modifier); i++, batch_item++) {
			if( _images[i] != undefined ) { // KT added this check 6-9-09
				li = document.createElement('li');
				
				// add 'alt' class to every third element
				if (batch_item > 1 && (batch_item % 3) == 0) {
					$(li).addClass('alt');
				}
				
				anchor = document.createElement('a');
				$(anchor).attr('href', _images[i].file);
				
				if (i == _current_index) {
					$(anchor).attr('class', 'selected');
				}
				
				img = document.createElement('img');

				var img_src = _images[i].file.replace("482_371_", "63_42_");
				
				$(img).attr('src', img_src);
				
				anchor.appendChild(img);
				li.appendChild(anchor);
				image_list.appendChild(li);
			}
		}
		
		// do we need to add the 'more' image?
		if (i < _images.length) {
			li = document.createElement('li');
			
			// add 'alt' class to every third element
			if (batch_item > 1 && (batch_item % 3) == 0) {
				$(li).addClass('alt');
			}
			
			anchor = document.createElement('a');
			$(anchor).attr('id', 'next_thumb');
			$(anchor).attr('href', '');
			
			img = document.createElement('img');
			$(img).attr('src', '/images/next_thumb.png');
			
			anchor.appendChild(img);
			li.appendChild(anchor);
			image_list.appendChild(li);
			
			batch_item++;
		}
		
		// adjust index, if needed
		if (_current_index < start_index) {
			// select the first item in the next set
			_current_index = start_index;
		}
		
		if (_current_index > (i - 1)) {
			// select the last item in the previous set
			_current_index = i - 1;
		}
		
		_init_thumbs();
	}
	
	return {
		next: function() {
			// make sure we're not at the last image
			if (_current_index + 1 < _images.length) {
				_current_index++;
				
				// do we need to load the next batch?
				if (_current_index >= ((_current_batch * _batch_size) + _batch_size)) {
					_current_batch++;
					_load_batch();
				}
				
				_select_thumb();
			}
		},
		
		prev: function() {
			if (_current_index - 1 >= 0) {
				_current_index--;
				
				// do we need to load the previous batch?
				if (_current_index < (_current_batch * _batch_size)) {
					_current_batch--;
					_load_batch();
				}
				
				_select_thumb();
			}
		},
		
		init: function(image_array) {
			_images = image_array;
			_total_batches = (Math.ceil(_images.length/_batch_size));
			_preload_images();
			_load_batch();
			_init_navigation();
		}
	}
}();