var slideshow = {
	pic: new Array(),
	slider: false,
	slider_content: false,
	current_holder: 0,
	current_image: 0,
	slider_timer: false,
	viewing_container: false,
	viewing_text: false,
	viewing_boxes: false,
	timer: false,
	timer_duration: 7000,
	fade_duration: 500,
	createSlideshow: function(vars) {
		var images_count = vars.images.length;
		
		this.vars = vars;
		
		if (images_count < 2) {
			////
			// Create the 2 pic holders
			this.pic[0] = $jQuery('<div></div>').attr('id','pic0').css({ 'z-index': '1' });
			this.setupImage(0, 0, false);
			$jQuery('#pics').html('');
			$jQuery('#pics').append(this.pic[0]).append(this.pic[1]);
			return;
		}
		
		////
		// Create the 2 pic holders
		this.pic[0] = $jQuery('<div></div>').attr('id','pic0').css({ 'z-index': '1' });
		this.pic[1] = $jQuery('<div></div>').attr('id','pic1').css({ 'z-index': '0' }).fadeOut();
		
		////
		// Create the content slider
		this.slider = $jQuery('<div></div>').attr('id','slider').css({ 'z-index': '2' });
		this.slider_content = $jQuery('<div></div>').attr('id','slider-content').css({ 'z-index': '2' });
		this.slider.append(this.slider_content);
		
		this.viewing_container = $jQuery('<div></div>').attr('id','viewing-container').css({ 'z-index': '3' });
		this.viewing_text = $jQuery('<div></div>').attr('id', 'viewing-text').html('Viewing: 1 of '+images_count);
		this.viewing_boxes = $jQuery('<div></div>').attr('id', 'viewing-boxes');
		
		for(var i = 0; i < images_count;i++) {
			var tmp = $jQuery('<a></a>').attr('href', '#').bind('click', {image:i}, slideshow.changeImage);
			if (i==0) {
				tmp.addClass('current');
			}
			this.viewing_boxes.append(tmp);
		}
		
		this.viewing_container.append(this.viewing_text).append(this.viewing_boxes);
		
		this.setupImage(0, 0, false);
		this.setupImage(1, 1, false);
				
		slideshow.slider_content.html(slideshow.vars.images[0].content);
				
		$jQuery('#pics').html('');
		$jQuery('#pics').append(this.pic[0]).append(this.pic[1]).append(this.slider).append(this.viewing_container);
						
		$jQuery('#pics').mouseenter(function() {
			if (slideshow.slider_timer != false) {
				clearTimeout(slideshow.slider_timer);
			}
			slideshow.stopTimer();
			slideshow.slider.animate({
				right: '0px'
			}, 300);
		}).mouseleave(function() {
			slideshow.slider_timer = setTimeout(function() {
				slideshow.setTimer();
				slideshow.slider.animate({
					right: '-268px'
				}, 300);
			}, 500);
		});
		
		slideshow.setTimer();
		
	},
	changeImage: function(e) {
		
		if (slideshow.current_image == e.data.image) return;
		slideshow.stopTimer();
		slideshow.loadImage(e.data.image);
		return false;
		
	},
	loadImage: function(image) {
		
		slideshow.current_image = image;
		
		if (slideshow.pic[0].css('display') == 'none') {
			slideshow.setupImage(image, 0);
		} else {
			slideshow.setupImage(image, 1);
		}
		slideshow.changeHolder();
		
		slideshow.resetTimer();
		
	},
	loadNextImage: function() {
		
		var next_image = slideshow.current_image + 1;	
		if (next_image == slideshow.vars.images.length) {
			next_image = 0;
		}
		slideshow.loadImage(next_image);
		
	},
	setupImage: function(image, holder) {
				
		$jQuery(slideshow.pic[holder]).html('');
		$jQuery(slideshow.pic[holder]).addClass('loading');
						
		var image_src = slideshow.vars.images[image].src;
		var image_link = $jQuery('<a></a>').attr('href',slideshow.vars.images[image].href).attr('target',slideshow.vars.images[image].target);
				
		var image = new Image();
		image_link.append(image);
		
		$jQuery(image)
			.load(function() {
				$jQuery(slideshow.pic[holder]).removeClass('loading');
				$jQuery(slideshow.pic[holder]).append(image_link);
			})
			.attr('src', image_src);			
	},
	changeHolder: function() {
		
		$jQuery('a', slideshow.viewing_boxes).removeClass('current');
		$jQuery('a:eq('+(slideshow.current_image)+')', slideshow.viewing_boxes).addClass('current');;
		
		slideshow.viewing_text.html('Viewing: '+(slideshow.current_image+1)+' of '+slideshow.vars.images.length);
		
		if (slideshow.pic[0].css('display') == 'none') {
			
			slideshow.slider_content.html(slideshow.vars.images[slideshow.current_image].content);
			
			slideshow.pic[1].fadeOut(slideshow.fade_duration);
			slideshow.pic[0].fadeIn(slideshow.fade_duration);
			
			slideshow.current_holder = 1;
			
		} else {
		
			slideshow.slider_content.html(slideshow.vars.images[slideshow.current_image].content);
		
			slideshow.pic[0].fadeOut(slideshow.fade_duration);
			slideshow.pic[1].fadeIn(slideshow.fade_duration);
			
			slideshow.current_holder = 0;
			
		}
				
	},
	setTimer: function() {
		slideshow.timer = setTimeout(slideshow.loadNextImage, slideshow.timer_duration);
	},
	resetTimer: function() {
		clearTimeout(slideshow.timer);
		slideshow.setTimer();
	},
	stopTimer: function() {
		clearTimeout(slideshow.timer);
	}
}



