var slideshow = {
	// Default settings
	container_id		:	'',
	mode				:	'fade',
	time_pause			:	5000,
	time_anim			:	500,
	
	// Other variables
	elm_container		:	null,
	current				:	0,
	items				:	0,
	timer				:	false,
	container_width		:	0,
	container_height	:	0,
	animating			:	false,
	
	// 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('div.slideshow_item').length;
		
		// Set current item
		this.current = 1;
		
		// Check if any items are found
		if (this.items < 1){
			return false;
		}
		
		// Update controls
		this.update_controls();
		
		// Set text
		this.set_text();
		
		// Start the timer
		this.set_timer();
	},
	
	// Next slide
	next				:	function(){
		if (this.animating){
			return false;
		}
		
		var from = this.current;
		
		if (this.current == this.items){
			this.current = 1;
		}
		else{
			this.current = (this.current + 1);
		}
		
		switch (this.mode){
			case 'fade':
				this.fade(from, this.current);
			break;
			case 'slide':
			default:
				this.slide(from, this.current, 'right');
			break;
		}
		
		this.update_controls();
		
		this.set_text();
	},
	
	// Previous slide
	prev				:	function(){
		if (this.animating){
			return false;
		}
		
		var from = this.current;
		
		if (this.current == 1){
			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(from, this.current, 'left');
			break;
		}
		
		this.update_controls();
		
		this.set_text();
	},
	
	// Goto slide
	goto				:	function(goto_id){
		if (this.animating || this.current == goto_id){
			return false;
		}
		
		var from = this.current;
		
		this.current = goto_id;
		
		switch (this.mode){
			case 'fade':
				this.fade(from, this.current);
			break;
			case 'slide':
			default:
				this.slide(from, this.current, (this.current > from ? 'right' : 'left'));
			break;
		}
		
		this.update_controls();
		
		this.set_text();
	},
	
	// Slide
	slide				:	function(current_id, next_id, left_or_right){
		this.clear_timer();
		
		this.animating = true;
		
		var current = $('#slideshow_item_'+current_id);
		var next = $('#slideshow_item_'+next_id);
		
		current.css('left', 0);
		current.css('z-index', 1);
		current.animate({ left : (left_or_right == 'left' ? this.container_width : -this.container_width) }, this.time_anim, function(){ current.css('display', 'none'); });
		
		next.css('left', (left_or_right == 'left' ? -this.container_width : this.container_width));
		next.css('z-index', 2);
		next.css('display', 'block');
		next.animate({ left : 0 }, this.time_anim, function(){ slideshow.animating = false; slideshow.set_timer(); });
	},
	
	// Fade
	fade				:	function(current_id, next_id){
		this.clear_timer();
		
		this.animating = true;
		
		var current = $('#slideshow_item_'+current_id);
		var next = $('#slideshow_item_'+next_id);
		
		current.css('opacity', 1);
		current.css('z-index', 1);
		
		next.css('opacity', 0);
		next.css('z-index', 2);
		next.css('display', 'block');
		next.animate({ opacity : 1 }, this.time_anim, function(){ current.css('display', 'none'); slideshow.animating = false; slideshow.set_timer(); });
	},
	
	// Update controls
	update_controls		:	function(){
		var elm = jQuery(this.elm_container.children('div.slideshow_controls')[0]);
		
		if (!elm.length){
			return false;
		}
		
		var i, html = '';
		for (i = 1; i <= this.items; i++){
			if (i > 1){
				html+= ' ';
			}
			
			if (i == this.current){
				html+= i;
			}
			else{
				html+= '<a href="javascript:void(0);" onclick="slideshow.goto('+i+');">'+i+'</a>';
			}
		}
		
		elm.html(html);
	},
	
	// Set text
	set_text			:	function(){
		var elm = jQuery(this.elm_container.children('div.slideshow_text')[0]);
		
		if (!elm.length){
			return false;
		}
		
		var current = $($('#slideshow_item_'+this.current).children('img')[0]);
		
		if (typeof(current.attr('slideshow-title')) == 'undefined'){
			current.attr('slideshow-title', current.attr('title'));
			current.attr('title', '');
		}
		
		elm.html(current.attr('slideshow-title'));
	},
	
	// Set timer to show next slide
	set_timer			:	function(){
		this.clear_timer();
		this.timer = setTimeout("slideshow.next();", this.time_pause);
	},
	
	// Clear timer to show next slide
	clear_timer			:	function(){
		clearTimeout(this.timer);
		this.timer = false;
	}
};
