var slideshow = {
	opts: {},
	images: [],
	slider: {},
	index: 0,
	timer: 0,
	working: false,
	
	setup: function(opts) {
		if (!opts.container) opts.container = '#slideshow';
		if (!opts.image_width) opts.image_width = 500;
		if (!opts.visible_width) opts.visible_width = 500;
		if (!opts.visible_height) opts.visible_height = 300;
		if (!opts.padding) opts.padding = 0;
		if (!opts.delay) opts.delay = 8000;
		if (!opts.speed) opts.speed = 1000;
		if (!opts.button_left_style) opts.button_left_style = {};
		if (!opts.button_right_style) opts.button_right_style = {};
		if (!opts.z_index) opts.z_index = 10;
		
		this.opts = opts;
		if (this.setup_html()) {
			this.index = Math.floor(Math.random() * this.images.length);
			this.slider.css({left: -this.get_position(this.index)});
			this.restart_timer();
		}
	},
	
	restart_timer: function() {
		clearTimeout(this.timer);
		this.timer = setTimeout("slideshow.tick()", this.opts.delay);
	},
	
	tick: function() {
		this.move(1);
	},
	
	move: function(dir) {
		if (!this.working) {
			this.working = true;
			this.index += dir;
			
			this.slider
				.stop()
				.animate({opacity: 0.6},250)
				.animate({left: -this.get_position(this.index)}, this.opts.speed, 
					function() {
						jQuery(this).animate({opacity: 1.0},250);
						slideshow.move_complete(); 
					}
				);
			
			this.restart_timer();
		}
	},
	
	move_complete: function() {
		if (this.index >= this.images.length) {
			this.index = 0;
			this.slider.css( {left: -this.get_position(this.index) });
		} else if (this.index < 0) {
			this.index = this.images.length - 1;
			this.slider.css( {left: -this.get_position(this.index) });
		}
		this.working = false;
	},
	
	get_position: function(index) {
		return index * (this.opts.image_width + this.opts.padding);
	},
	
	setup_html: function() {
		var container = jQuery(this.opts.container);
		if (container.length > 0) {
			//first find all images
			this.images = [];
			container.find('li').each(function() {
				slideshow.images.push(jQuery(this).html());
			});
			
			container.css({
				width: this.opts.visible_width, 
				height: this.opts.visible_height,
				position: 'relative',
				overflow: 'hidden'
			});
			
			this.slider = jQuery('<div />');
			this.slider.css({
				position: 'absolute',
				top: 0,
				left: 0,
				width: this.get_position(this.images.length),
				height: this.opts.visible_height,
				'white-space' : 'nowrap'
			});
			
			for (var i=0; i<this.images.length; i++) {
				this.create_item( this.images[i], this.get_position(i));
			}
			for (var i=0; i<2; i++) {
				this.create_item( this.images[i], this.get_position(this.images.length + i));
			}
			this.create_item( this.images[this.images.length - 1], this.get_position(-1) );			
			
			
			var button_left = jQuery('<div />');
			button_left.css(this.opts.button_left_style);
			button_left.css({'z-index' : this.opts.z_index + 1});
			button_left.click(function() {
				slideshow.move(-1);
			});
			
			var button_right = jQuery('<div />');
			button_right.css(this.opts.button_right_style);
			button_right.css({'z-index' : this.opts.z_index + 1});
			button_right.click(function() {
				slideshow.move(1);
			});
			
			container.html(this.slider);
			container.append(button_left);
			container.append(button_right);

			return true;
		}
		return false;
	},
	
	create_item: function(html, left) {
		var item = jQuery('<div />');
		item.css({
			position: 'absolute',
			top: 0,
			left: left,
			width: this.opts.image_width,
			height: this.opts.image_height,
			'z-index': this.opts.z_index
			
		});
		item.html(html);
		this.slider.append(item);
	}
};

jQuery(document).ready(function() {
	var opts = {
		container: '#slideshow',
		image_width: 558,
		visible_width: 732,
		visible_height: 300,
		padding: 4,
		delay: 9000,
		speed: 1000,
		button_left_style: { cursor: 'pointer', width: 29, height: 56, position: 'absolute', top: 97, left: 9, 'background-image' : "url('/images/slideshow-button-left.png')" },
		button_right_style: { cursor: 'pointer', width: 29, height: 56, position: 'absolute', top: 97, right: 9, 'background-image' : "url('/images/slideshow-button-right.png')" },
		z_index: 10
	};
	slideshow.setup(opts);
});

