var Slideshow = function(slideshow_div, thumbnail_ul, start_index, set)
{
	var me = this;
	
	var WIDTH                 = 500;
	var HEIGHT                = 410;
	var CHANGE_TIMEOUT        = 5000;
	var BASE_PATH             = "/images/photo_sets/" + set + "/scaled/";
	var THUMB_PATH            = "/images/photo_sets/" + set + "/thumbs/";

	var _slideshow_div        = slideshow_div;
	var _thumbnail_links      = jQuery(thumbnail_ul).find("a");
	var _paths 				  = _getPaths();
	
	
	var _images               = _get_images(paths);
	var _current_index        = start_index;
	
	var _current_slide        = jQuery(slideshow_div).find('.container')[0];
	_current_slide.image      = jQuery(_current_slide).find("img")[0];
	_current_slide.onclick    = _slide_click;

	var _status_indicator     = jQuery(slideshow_div).find('#status')[0];
	_status_indicator.onclick = _slide_click;

	var _play_interval        = null;

	me.getCurrentSlide = function() { return _current_slide; }
	me.getCurrentIndex = function() { return _current_index; }

	me.setSlide = function (index)
	{		
		if (index != _current_index)
			_load_slide(index);
	};
	
	// Stop the slideshow. If timeout is not null, stop if for the amount of time
	// specified by timeout
	me.stop = function(timeout)
	{
		clearInterval(_play_interval);
		
		if (timeout)
			setTimeout(me.start, timeout);
		
		me.playing = false;
		_set_indicator("paused");
	};
	
	// Start playing the slideshow. Immediately is true if the show should immediately start.
	me.start = function(immediately)
	{
		_play_interval = setInterval(_load_next, CHANGE_TIMEOUT);
		
		if (immediately)
			_load_next();
		
		me.playing = true;	
		_set_indicator("playing");
	};
	
	(function init()
	{
		_slideshow_div.style.position = "relative";
		
		_slideshow_div.onmouseover = function () { jQuery(_status_indicator).show(); };
		_slideshow_div.onmouseout = function () { jQuery(_status_indicator).hide(); };
		_center_indicator();
		
		_load_thumbs();
		
		me.start();
	})();
	
	// Returns an array of image paths
	function _getPaths()
	{
		var paths = [];
		for(var i = 0; i < _thumbnail_links.length; i++)
		{
			var segments = _thumbnail_links[i].href.split('/');
			paths.push(segments[segments.length - 1]);
		}
		
		return paths;
	}
	
	// Loads the thumbnail images that weren't loaded initially
	function _load_thumbs()
	{
		for(var i = 0; i < _thumbnail_links.length; i++)
			if (jQuery(_thumbnail_links[i]).find("img").length < 1)
				_thumbnail_links[i].appendChild(_get_thumbnail(paths[i]));
	}
	
	// Preloads the larger images that haven't been loaded yet
	function _preload_images()
	{
		for(var i = 0; i < _paths.length; i++)
			_preload_image(i);
	}
	
	function _preload_image(index)
	{
		var image = new Image();
		image.src = BASE_PATH + _paths[index];
		image.onLoad = function () { image = null; };
	}
	
	// Returns a new thumbnail img for the path
	function _get_thumbnail(path)
	{
		var img = document.createElement("img");
		img.src = THUMB_PATH + path;
		
		return img;
	}
		
	// Load the next slide
	function _load_next() { _load_slide(_get_next_index()); }
	
	// Changes the image displayed on the pause/play indicator
	function _set_indicator(status) { _status_indicator.className = status; }
		
	// Load a slide for the index
	function _load_slide(index)
	{
		if (_current_slide)
			_unload(_current_slide);
				
		_current_index = index;
		_current_slide = _get_slide(index);
		_slideshow_div.appendChild(_current_slide);
		jQuery(_current_slide).fadeIn(1000);
	}
	
	// Fade the slide out and remove it from the DOM
	function _unload(slide) { jQuery(slide).fadeOut(1000, function () { jQuery(slide).remove() }); }
	
	// Get the next index that should be displayed (returns to zero)
	function _get_next_index() { return _current_index + 1 < _images.length ? _current_index + 1 : 0; }
	
	// Gets an array of images from an array of image paths	
	function _get_images(paths)
	{
		var images = [];
		for(var i = 0; i < paths.length; i++)
			images.push(_get_image(paths[i]));
		
		return images;
	}
	
	// Get an image for the image path
	function _get_image(path)
	{
		var image = new Image();
		image.src = BASE_PATH + path;
		return image
	}
	
	// Set the position of the image so it is centered in the slide
	function _center_image(image)
	{		
		image.style.position = "absolute";
		image.style.top  = _get_top(image);
		image.style.left = _get_left(image);
		return image;
	}
	
	// Centers the pause/play indicator
	function _center_indicator()
	{
		_status_indicator.style.top = _get_top(_status_indicator);
		_status_indicator.style.left = _get_left(_status_indicator);
	}
	
	// Get the left position based on the image width
	function _get_left(image)
	{
		var position_left = (WIDTH - (image.width || jQuery(image).width())) / 2;
		return position_left + "px";
	}
	
	// Get the top position based on the image height
	function _get_top(image)
	{
		var position_top = (HEIGHT - (image.height || jQuery(image).height())) / 2;
		return position_top + "px";
	}
	
	// Handles the onclick event for the slide 
	function _slide_click()
	{
		if (me.playing) 
			me.stop();
		else
			me.start(true);
	}
	
	// Get the slide for the index
	function _get_slide(index)
	{
		var slide = document.createElement("div");
		slide.style.display = "none";
		slide.style.height = HEIGHT + "px";
		slide.style.width  = WIDTH + "px";
		slide.style.position = "absolute";
		slide.style.cursor = "pointer";
		slide.image = _center_image(_images[index]);
		slide.appendChild(slide.image);
		slide.onclick = _slide_click;
		
		return slide;
	}
}