var RecipeSlideshow,
	slideshowLarge_first,
	slideshowThumb_first,
	slideshowThumb_last,
	slideshowThumb_total;


RecipeSlideshow = function (recipes) {
	
	var pos = 0,
		busy = false,
		running = false,
		interval = null,
		delay = 5000,
		count = recipes.length,
		firstAdjustmentMade = false;
	
	function populate() {
		var i,
			largeBelt,
			thumb,
			thumbBelt;
		largeBelt = $('.recipe-slideshow-large-carousel-belt');
		thumbBelt = $('.recipe-slideshow-thumbs-carousel-belt');
		for (i = 0; i < recipes.length; i++) {
			largeBelt.append(
				'<div class="recipe-slideshow-large-carousel-panel">'+
				'<div class="recipe-slideshow-carousel-image"><a href="'+recipes[i].url+'"><img src="/images/'+recipes[i].large+'" alt="" /></a></div>'+
				'</div>');
			thumb = $(
				'<div class="recipe-slideshow-thumbs-carousel-panel" onclick="slideshow.goTo('+(i+1)+');">'+
				'<div class="recipe-slideshow-carousel-image"><img src="/images/'+recipes[i].thumb+'" alt="" /></div>'+
				'<div>'+recipes[i].name+'</div>'+
				'</div>');
			recipes[i].trigger = thumb;
			thumbBelt.append(thumb);
		}
	}
	
	function carouselize() {
		// Thumbs
		stepcarousel.setup({
			galleryid: 'recipe-slideshow-thumbs-carousel', //id of carousel DIV
			beltclass: 'recipe-slideshow-thumbs-carousel-belt', //class of inner "belt" DIV containing all the panel DIVs
			panelclass: 'recipe-slideshow-thumbs-carousel-panel', //class of panel DIVs each holding content
			panelbehavior: {
				speed: 500,
				wraparound: false,
				persist: false
			},
			defaultbuttons: {
				enable: true,
				moveby: 4,
				leftnav: [
					'/images/recipe-slideshow-left.gif',
					-18, 25
				],
				rightnav: [
					'/images/recipe-slideshow-right.gif',
					2, 25
				]
			},
			statusvars: [
				'slideshowThumb_first',
				'slideshowThumb_last',
				'slideshowThumb_total'
			],
			contenttype: ['inline'] //content setting ['inline'] or ['external', 'path_to_external_file']
		});

		// Large
		stepcarousel.setup({
			galleryid: 'recipe-slideshow-large-carousel', //id of carousel DIV
			beltclass: 'recipe-slideshow-large-carousel-belt', //class of inner "belt" DIV containing all the panel DIVs
			panelclass: 'recipe-slideshow-large-carousel-panel', //class of panel DIVs each holding content
			panelbehavior: {
				speed: 500,
				wraparound: true,
				persist: false
			},
			defaultbuttons: {
				enable: false
			},
			statusvars: [
				'slideshowLarge_first'
			],
			onslide: function() {
				onGoTo(slideshowLarge_first);
			},
			contenttype: ['inline'] //content setting ['inline'] or ['external', 'path_to_external_file']
		});
		
	}


	function onGoTo(index) {
		var i;
		pos = index - 1;
		if (!running) {
			location.href = '#' + recipes[pos].fragment;
		}
		for (i = 0; i < recipes.length; i++) {
			if (pos == i) {
				if (recipes[pos].alternate) {
					$(recipes[pos].alternate).css({display: 'block'});
					$('#recipe-slideshow-item-content').css({display: 'none'});
				} else {
					$('.recipe-slideshow-item-alternate').css({display: 'none'});
					$('#recipe-slideshow-item-content').css({display: 'block'});

					$('#recipe-slideshow-index').html(index+'/'+count);
					$('#recipe-slideshow-name').html(
						'<a href="'+recipes[i].url+'"><span><span>'+
						recipes[i].name+'</span></span></a>');
					$('#recipe-slideshow-description').html(recipes[i].description);
					$('#recipe-slideshow-next-name').html(
						recipes[(i+1)%recipes.length].name);
				}
				recipes[i].trigger.addClass('current');
			} else {
				recipes[i].trigger.removeClass('current');
			}
		}
		busy = false;
		if (!firstAdjustmentMade) {
			adjustThumbs(index);
			firstAdjustmentMade = true;
		}
	}
	
	function change(offset, func) {
		
		var destination, thumb;
		if (!busy) {
			busy = true;

			// where are we trying to end up?
			destination = slideshowLarge_first + offset;
			
			// is that out of bounds?
			if (destination < 1) {
				destination += slideshowThumb_total;
			}
			if (destination > slideshowThumb_total) {
				destination -= slideshowThumb_total;
			}

			adjustThumbs(destination);
			func();
		}
	}
	
	function adjustThumbs(center) {
	
		var first, visible;
		
		visible = slideshowThumb_last - slideshowThumb_first + 1;
		
		// to center the current thumb, start with the one that is the half the length of the
		// visible part of the thumb carousel, from the one we're trying to center.
		first = Math.ceil(center - visible / 2);
		
		if (first < 1) {
			first = 1;
		}
		if (first > slideshowThumb_total - visible + 1) {
			first = slideshowThumb_total - visible + 1;
		}
		stepcarousel.stepTo('recipe-slideshow-thumbs-carousel', first);
	}

	function next() {
		change(1, function(){
			stepcarousel.stepBy('recipe-slideshow-large-carousel', 1);
		});
	}
	
	function previous() {
		change(-1, function(){
			stepcarousel.stepBy('recipe-slideshow-large-carousel', -1);
		});
	}
	
	function goTo(index) {
		stop();
		change(index - slideshowLarge_first, function(){
			stepcarousel.stepTo('recipe-slideshow-large-carousel', index);
		});
	}
	
	function goToName(hash) {
		var i;
		if (hash === 'play') {
			play();
		} else {
			for (i = 0; i < recipes.length; i += 1) {
				if (recipes[i].fragment === hash) {
					goTo(i + 1);
					return;
				}
			}
		}
	}

	function play() {
		stop();
		running = true;
		next();
		location.href = '#play';
		interval = setInterval(next, delay);
		$('#recipe-slideshow-controls-stop')[0].style.display = 'block';
		$('#recipe-slideshow-controls-start')[0].style.display = 'none';
	}
	
	function stop() {
		running = false;
		location.href = '#' + recipes[pos].fragment;
		if (interval) {
			clearInterval(interval);
			interval = null;
		}
		$('#recipe-slideshow-controls-stop')[0].style.display = 'none';
		$('#recipe-slideshow-controls-start')[0].style.display = 'block';
	}
	
	function toggle() {
		if (running) {
			stop();
		} else {
			play();
		}
	}
	
	function init(options) {
		var hash, i;

		hash = (window.location.href.split('#')[1] || '').toLowerCase();
		
		for (i = 0; i < recipes.length; i += 1) {
			if (recipes[i].exclude) {
				count -= 1;
			}
		}

		if (options) {
			if (typeof options.delay !== 'undefined') {
				delay = options.delay;
			}
		}
		populate();
		carouselize();
		stop();
		setTimeout(function () {
			jQuery(document).ready(function ($) {
				goToName(hash);
			});
		}, 0);
	}
	
	this.next = next;
	this.previous = previous;
	this.goTo = goTo;
	this.goToName = goToName;
	this.play = play;
	this.stop = stop;
	this.toggle = toggle;
	this.init = init;
	
};

