slideshow = {
	//properties
	currentImage : 0,
	ulId : '',
	linkId : '',
	showBuyNow : false,
	images : [],
	html : '<div id="slideshow"><a id="slideshow-close-btn" href="">Close</a><div id="slideshow-top"></div><div id="slideshow-content"><img src="" /></div><div id="slideshow-menu"><a id="slideshow-left" href="">Left</a><a id="slideshow-right" href="">Right</a><a id="slideshow-buy-now" href="compare.php">Buy Now!</a></div><div id="slideshow-bottom"></div><div id="slideshow-overlay"></div></div>',
	
	//methods
	init : function(_ulId, _linkId, _showBuyNow) {
		$('body').append( slideshow.html );
		
		//set initial parameters of this slideshow
		this.ulId=_ulId;
		this.linkId=_linkId;
		this.showBuyNow=_showBuyNow;
		
		//link click does reveal
		$('#'+this.linkId).click(function() { return slideshow.birth(); });
		
		//establish image key value pairs
		var $img_lines = $('#'+this.ulId+' li a');
		var _images = [];
		$img_lines.each(function(i, e) {
			_images[i] = $(this).attr('href');
		});
		this.images = _images;
		
		//position the thumbs for lightbox items
		var max_width = 480 - 40;
		for(i in this.images) {
			var _left = max_width / this.images.length * i + 84;
			var $bullet = $('<a class="slideshow-bullet" href="">' + i + '</a>');
			$bullet.insertBefore('#slideshow-right');
			$bullet.css({'left' : _left});
			$bullet.data('index', i);
			$bullet.click(function() {
				var i = $(this).data('index');
				return slideshow.handleBulletClick(i); 
			});
		}
		
		$('#slideshow-left').click(function() {
			return slideshow.handleDirClick('L'); 
		});
		$('#slideshow-right').click(function() {
			return slideshow.handleDirClick('R'); 
		});
		
		//close button and overlay click closes
		$('#slideshow-close-btn, #slideshow-overlay').click(function() { 
			return slideshow.death(); 
		});
	},
	
	birth : function() {
		var i = this.currentImage = 0;
		
		$('#slideshow').show();
		
		//fadeoutimage, tick on load, and set source
		$img = $('#slideshow-content img');
		$img.fadeOut(0);
		$img.load(function() {
			$(this).fadeIn(150);
		});
		
		slideshow.bindKeys();
		
		//don't follow through link click
		return slideshow.updateImage();
	},
	
	bindKeys : function() {
		$('body').keydown(function(e) {
			switch(e.keyCode) {
				//escape 27
				case 27:
				$('#slideshow-close-btn').click();
				break;
				//right and space 39 32, doubled up for ie
				case 39:
				$('#slideshow-right').click();
				break;
				case 32:
				$('#slideshow-right').click();
				break;
				//right 37
				case 37:
				$('#slideshow-left').click();
				break;
			}
		});
	},
	
	handleBulletClick : function (i) {
		slideshow.currentImage = i;
		
		return slideshow.updateImage();
	},
	
	handleDirClick : function (str) {
		//move the direction
		if(str == 'L') 
			slideshow.currentImage--;
		else
			slideshow.currentImage++;
			
		return slideshow.updateImage();
	},
	
	updateImage : function () {
		//wrap around the numbers	
		if(slideshow.currentImage >= slideshow.images.length)
			slideshow.currentImage = 0;
			
		if(slideshow.currentImage < 0)
			slideshow.currentImage = slideshow.images.length-1;
			
		//handle bullets grx
		$('.slideshow-bullet').each(function() {
			$(this).removeClass('slideshow-bullet-current');
		});
		$('.slideshow-bullet:eq(' + slideshow.currentImage + ')').addClass('slideshow-bullet-current');
		
		var imgUrl = slideshow.images[slideshow.currentImage];
		
		//fadeoutimage, tick on load, and set source
		$img = $('#slideshow-content img');
		$img.fadeOut(0);
		$img.attr('src', imgUrl);
		
		//don't follow through link click
		return false;
	},
	
	death : function() {
		$('#slideshow').hide();
		
		//don't follow through link click
		return false;
	}
}

$(function() {
	if( $('#gallery').length !== 0 &&
		$('#start_tour').length !== 0 )
		slideshow.init('gallery', 'start_tour', true);
});

/*

HTML: 

<div id="slideshow">
	<a id="slideshow-close-btn" href="">Close</a>
	<div id="slideshow-top"></div>
	<div id="slideshow-content">
		<img src="" />
	</div><!-- END #slideshow-content -->
	<div id="slideshow-menu">
		<a id="slideshow-left" href="">Left</a>
		
		<a id="slideshow-right" href="">Right</a>
		<a id="slideshow-buy-now" href="compare.php">Buy Now!</a>
	</div><!-- END #slideshow-menu -->
	<div id="slideshow-bottom"></div>
	<div id="slideshow-overlay"></div>
</div><!-- END #slideshow -->

*/
