ImageSwitcher.prototype = {};

function ImageSwitcher(config)
{
	var element = jQuery(config.selector),
		currentImage = element.find("IMG:first"),
		originalImageUrl = currentImage.attr("src"),
		transition = config.transition,
		delay = config.delay ? config.delay : 1000,
		enabled = config.enabled ? config.enabled : true,
		images = config.images,
		imageIdx = 0,
		prevBtn = jQuery(config.prevBtnSelector),
		nextBtn = jQuery(config.nextBtnSelector),
		viewLink = jQuery(config.viewLinkSelector),
		imageLinksCtr = jQuery(config.imageLinksSelector),
		imageLinks = [],
		tickTimeoutId = -1,
		interactionTimeoutId = -1,
		lockUI = false,
		self = this;
		
	initialize();
	
	function initialize()
	{
		//currentImage.css("z-index", 20);
		var idx = 0;
		
		// Wire up buttons
		prevBtn.css("cursor", "pointer").click(function(){self.prev();});
		nextBtn.css("cursor", "pointer").click(function(){self.next();});
		
		for(idx = 0; idx < images.length; idx++)
		{
			var link = jQuery("<a>");
			
			link.html(idx + 1).attr("rel", idx).css("cursor", "pointer").click(function(){ 
				var link = jQuery(this);
				
				//link.addClass("highlighted").siblings("A").removeClass("highlighted");
				
				switchTo(jQuery(this).attr("rel"));
				
				pauseCycling();
			});
			
			imageLinksCtr.append(link);
			
			imageLinks.push(link);
		}
		
		switchTo(0);
		
		resumeCycling();
	}
	
	function highlightLink(idx)
	{
		imageLinksCtr.find("A").removeClass("highlighted");
		
		imageLinksCtr.find("A:eq(" + idx + ")").addClass("highlighted");
	}
	
	function resumeCycling()
	{
		tickIntervalId = setTimeout(tick, ImageSwitcher.CYCLE_INTERVAL);
	}
	
	function pauseCycling()
	{
		// Pause cycling here always assumes that we'll want to resume it after
		// a delay
		clearInterval(tickIntervalId);
		
		clearInterval(interactionTimeoutId);
		interactionTimeoutId = setTimeout(resumeCycling, ImageSwitcher.INTERACTION_TIMEOUT);
	}
	
	function tick()
	{
		self.next();
	}
	
	this.prev = function()
	{
		if (!lockUI)
		{
			imageIdx--;
			
			if (imageIdx < 0)
			{
				imageIdx = images.length - 1;
			}
			
			switchTo(imageIdx);
			
			lockUI = true;
		}
	};
	
	this.next = function()
	{
		if (!lockUI)
		{
			imageIdx++;
			imageIdx %= images.length;
			
			switchTo(imageIdx);
			
			lockUI = true;
		}
	};
	
	this.getImageCount = function()
	{
		return images.length;
	};
	
	function switchTo(imageIdx)
	{
		if (enabled)
		{
			var imageInfo = images[imageIdx],
				newImage = jQuery("<img>");
			
			highlightLink(imageIdx);
			viewLink.attr("href", imageInfo.url);
			
			newImage.attr({
							src: imageInfo.image
						  });
			
			element.prepend(newImage);
			
			if (transition == ImageSwitcher.FADE_FIRST)
			{
				newImage.fadeTo(0, 0);
			}
			
			newImage.load(function()
			{
				newImage.nextAll("IMG").fadeTo(delay, 0, function()
				{
					jQuery(this).remove();
					
					if (transition == ImageSwitcher.FADE_FIRST)
					{
						newImage.fadeTo(delay, 1);
					}
					
					currentImage = newImage;
					
					lockUI = false;
					
					tickIntervalId = setTimeout(tick, ImageSwitcher.CYCLE_INTERVAL);
				});
			});
		}
	};
}

ImageSwitcher.FADE_FIRST = 0;
ImageSwitcher.FADE_THROUGH = 1;

ImageSwitcher.CYCLE_INTERVAL = 5000;
ImageSwitcher.INTERACTION_TIMEOUT = 15000;
