var slideshow = {
	// Default settings
	container_id		:	'',
	mode				:	'fade',
	time_pause			:	5000,
	time_anim			:	500,
	time_anim_controls	:	250,
	use_controls		:	true,
	
	// Other variables
	elm_container		:	null,
	elm_prev			:	null,
	elm_next			:	null,
	current				:	0,
	items				:	0,
	timer				:	false,
	container_width		:	0,
	container_height	:	0,
	
	// Initialize the slideshow
	initialize			:	function(settings){
		// Check if settings is set, and container ID is given
		if (typeof(settings) != 'object' || typeof(settings.container_id) == 'undefined'){
			return false;
		}
		
		// Assign settings
		for (var key in settings){
			this[key] = settings[key];
		}
		
		// Get container element
		this.elm_container = jQuery('#'+this.container_id);
		
		// Check if container element is found
		if (!this.elm_container.length){
			return false;
		}
		
		// Set mouse enter and leave functions on the container
		this.elm_container.mouseenter(function(e){
			slideshow.clear_timer();
		});
		this.elm_container.mouseleave(function(e){
			slideshow.set_timer();
		});
		
		// Get container width and height
		this.container_width = this.elm_container.width();
		this.container_height = this.elm_container.height();
		
		// Get number of items
		this.items = this.elm_container.children('img').length - 1;
		
		// Check if any items are found
		if (this.items < 1){
			return false;
		}
		
		if (this.use_controls){
			// Create previous element
			this.elm_prev = jQuery('<div id="slideshow_prev" style="display:none;"></div>');
			this.elm_prev.click(function(e){
				slideshow.prev(true);
			});
			this.elm_prev.mouseenter(function(e){
				slideshow.clear_timer();
			});
			this.elm_prev.mouseleave(function(e){
				slideshow.set_timer();
			});
			this.elm_container.append(this.elm_prev);
			
			// Create next element
			this.elm_next = jQuery('<div id="slideshow_next" style="display:none;"></div>');
			this.elm_next.click(function(e){
				slideshow.next(true);
			});
			this.elm_next.mouseenter(function(e){
				slideshow.clear_timer();
			});
			this.elm_next.mouseleave(function(e){
				slideshow.set_timer();
			});
			this.elm_container.append(this.elm_next);
			
			// Show controls
			this.show_controls();
		}
		
		// Start the timer
		this.set_timer();
	},
	
	// Next image
	next				:	function(manual){
		this.clear_timer();
		
		var from = this.current;
		
		if (this.current == this.items){
			this.current = 0;
		}
		else{
			this.current = (this.current + 1);
		}
		
		switch (this.mode){
			case 'fade':
				this.fade(from, this.current);
			break;
			case 'slide':
			default:
				this.slide_right(from, this.current);
			break;
		}
		
		if (!manual){
			this.set_timer();
		}
	},
	
	// Previous image
	prev				:	function(manual){
		this.clear_timer();
		
		var from = this.current;
		
		if (this.current == 0){
			this.current = this.items;
		}
		else{
			this.current = (this.current - 1);
		}
		
		switch (this.mode){
			case 'fade':
				this.fade(from, this.current);
			break;
			case 'slide':
			default:
				this.slide_left(from, this.current);
			break;
		}
		
		if (!manual){
			this.set_timer();
		}
	},
	
	// Slide images right
	slide_right			:	function(current_id, next_id){
		var current = jQuery('#slideshow_image_'+current_id);
		var next = jQuery('#slideshow_image_'+next_id);
		
		this.hide_controls();
		
		current.css('left', 0);
		current.css('display', 'inline');
		current.animate({ left : -this.container_width }, this.time_anim);
		
		next.css('left', this.container_width);
		next.css('display', 'inline');
		next.animate({ left : 0 }, this.time_anim, function(){ slideshow.show_controls(); });
	},
	
	// Slide images left
	slide_left			:	function(current_id, next_id){
		var current = jQuery('#slideshow_image_'+current_id);
		var next = jQuery('#slideshow_image_'+next_id);
		
		this.hide_controls();
		
		current.css('left', 0);
		current.css('display', 'inline');
		current.animate({ left : this.container_width }, this.time_anim);
		
		next.css('left', -this.container_width);
		next.css('display', 'inline');
		next.animate({ left : 0 }, this.time_anim, function(){ slideshow.show_controls(); });
	},
	
	// Fade images
	fade				:	function(current_id, next_id){
		var current = jQuery('#slideshow_image_'+current_id);
		var next = jQuery('#slideshow_image_'+next_id);
		
		this.hide_controls();
		
		current.css('left', 0);
		current.css('opacity', 1);
		current.css('z-index', 1);
		current.css('display', 'inline');
		current.animate({ opacity : 0 }, this.time_anim);
		
		next.css('left', 0);
		next.css('opacity', 0);
		next.css('z-index', 2);
		next.css('display', 'inline');
		next.animate({ opacity : 1 }, this.time_anim, function(){ slideshow.show_controls(); });
	},
	
	// Hide controls
	hide_controls		:	function(){
		if (this.use_controls){
			this.elm_prev.animate({ opacity : 0.0 }, this.time_anim_controls, function(){ slideshow.elm_prev.css('display', 'none'); });
			this.elm_next.animate({ opacity : 0.0 }, this.time_anim_controls, function(){ slideshow.elm_next.css('display', 'none'); });
		}
	},
	
	// Show controls
	show_controls		:	function(){
		if (this.use_controls){
			this.elm_prev.css('opacity', '0');
			this.elm_prev.css('display', 'block');
			this.elm_prev.animate({ opacity : 0.5 }, this.time_anim_controls);
			
			this.elm_next.css('opacity', '0');
			this.elm_next.css('display', 'block');
			this.elm_next.animate({ opacity : 0.5 }, this.time_anim_controls);
		}
	},
	
	// Set timer to show next image
	set_timer			:	function(){
		this.clear_timer();
		this.timer = setTimeout("slideshow.next();", this.time_pause);
	},
	
	// Clear timer to show next image
	clear_timer			:	function(){
		clearTimeout(this.timer);
		this.timer = false;
	}
};
