if (!BENMOORE) { BENMOORE = { cfyh:{} }; }
if (!BENMOORE.cfyh) { BENMOOR.cfyh = {}; }

BENMOORE.cfyh.MoreImages = function(options) {
	var current = 0; // <Number> - currently selected image's index
	var firstVisible = 0; // <Number> - index of the first visible thumb
	var lastVisible; // <Number> - index of the last possible visible thumb
	var maxThumbs = 4; // <Number> - the maximum number of visible thumbnails
	var numImages; // <Number> - total number of thumbnails
	var previousButton; // <jQuery> - the previous button for the sliding images
	var nextButton; // <jQuery> - the next button for the sliding images
	// store this for later reference
	var that = this;
	// let the CSS know to apply the image lightbox styles
	jQuery('body').addClass('moreImages');
	// function to check and see if the content has been inserted into the DOM yet
	this.checkLoaded = function() {
		if (!!jQuery('#moreImages').length) {
			that.initialize();
		} else {
			setTimeout(that.checkLoaded, 25);
		}
	};

	this.initialize = function() {
		var moreImages = jQuery('#moreImages');
		jQuery('#moreImages .picker ul').css('width',jQuery('#moreImages .picker li').length * 72);
		var lis = jQuery('#moreImages li').append('<div class="png selectedOverlay"></div>');
		lis.click(clickThumb) // bind the click event
				.bind('mouseenter mouseleave',function() { jQuery(this).toggleClass('hover'); })
				.filter(':first-child').click(); // call the click event for the first child
		numImages = lis.length;
		jQuery("#moreImages .png").pngfix();
		previousButton = disableButton(jQuery('#moreImages .picker .previous').click(previousImage));
		nextButton = jQuery('#moreImages .picker .next').click(nextImage);
		if (numImages === 1) {
			disableButton(nextButton);
		}
		lastVisible = numImages < maxThumbs ? numImages - 1 : maxThumbs - 1;
		jQuery('#moreImages .description .max').html(numImages);
	};

	function selectImage(index) {
		jQuery('#moreImages .mainImage img.active').fadeOut('normal',function() {
				var child = index + 1;
				jQuery('#moreImages .mainImage img').removeClass('active') // remove active class from all images
						.filter(':nth-child(' + child + ')') // filter down to just the image indicated by the index
						.addClass('active') // add the active class
						.fadeIn('normal'); // show it
				jQuery('#moreImages li').removeClass('active') // remove active class from all thumbs
						.filter(':nth-child(' + child + ')') // filter down to just the thumb indicated by the index
						.addClass('active'); // add the active class
				jQuery('#moreImages .description .current').html(child);
				var left, lastVis, firstVis;
				if (index > lastVisible) { // if the image selected is off to the right
					left = -( (index - lastVisible) * 73 );
					lastVis = index;
					firstVis = index - 3;
				} else if (index === lastVisible && index < (numImages - 1)) { // if the image selected is the last visible
					left = -( (index + 1 - lastVisible) * 73 );
					lastVis = index + 1;
					firstVis = index - 2;
				} else if (index < firstVisible) { // if the image selected is off to the left
					left = -( index * 73 );
					lastVis = index + 3;
					firstVis = index;
				} else if (index === firstVisible && index > 0) { // if the image selected is the first visible and not the first
					left = -( (index - 1) * 73 );
					lastVis = index + 2;
					firstVis = index - 1;
				}
				if (typeof left !== 'undefined') {
					jQuery('#moreImages .picker ul').animate({'left': left},'normal','linear',function() {
						lastVisible = lastVis;
						firstVisible = firstVis;
					});
				}
				if (current === 0) {
					disableButton(previousButton);
				} else if (current === (numImages - 1)) {
					disableButton(nextButton);
				} else {
					enableButton(nextButton);
					enableButton(previousButton);
				}
		});
	}

	function clickThumb() {
		var index = jQuery.inArray(this,jQuery('#moreImages li'));
		if (current === index) { return; }
		current = index;
		selectImage(current);
	}

	function nextImage(e) {
		if (e.preventDefault) { e.preventDefault(); }
		current = current + 1;
		if (current === numImages) {
			current = numImages - 1;
			return;
		}
		selectImage(current);
	}

	function previousImage(e) {
		if (e.preventDefault) { e.preventDefault(); }
		current = current - 1;
		if (current < 0) {
			current = 0;
			return;
		}
		selectImage(current);
	}

	function disableButton(button) {
		return button.addClass('disabled').addClass(button.hasClass('next') ? 'disabledNext' : 'disabledPrevious');
	}

	function enableButton(button) {
		return button.removeClass('disabled').removeClass(button.hasClass('next') ? 'disabledNext' : 'disabledPrevious');
	}

	// check if the content is inserted
	this.checkLoaded();
};

