
var idhcTour = {

	init : function () {
		$('ol.slideShow').
			attr('id', function(idx) {
				return this.id || ('slideShow'+idx);
			}).
			addClass('slideShowActive').
			each(function () {
				$(this).after('<div id="'+this.id+'_Controls" class="slideShowControls"><a id="'+this.id+'_Previous" class="prev" href="#prev">Previous</a><span></span><a id="'+this.id+'_Next" class="next" href="#next">Next</a></div>');
				$(this).height($(this).height());
				$(this).children().fadeOut(1);
				idhcTour.slide(this,0);
			});
		
		$('div.slideShowControls a.prev').
			click(idhcTour.prev);
		$('div.slideShowControls a.next').
			click(idhcTour.next);
	},

	prev : function () {
		if ( ! $(this).hasClass('inactive'))
			idhcTour.slide($(this).parent().prev()[0],-1);
		return false;
	},
	
	next : function () {
		if ( ! $(this).hasClass('inactive'))
			idhcTour.slide($(this).parent().prev()[0], 1);
		return false;
	},
	
	slide : function (tour, prevnext) {
		tour = $(tour);
		var currActive = tour.data('active');
		if (typeof currActive != 'number') {
			currActive = 0;
		} else {
			tour.data('prevActive', currActive);
			tour.children().eq(currActive).
				removeClass('incoming');
			currActive += prevnext;
		}
		
		if (currActive >= tour.children().length -1) {
			currActive = tour.children().length -1;
			tour.next().children().eq(2).addClass('inactive');
		} else {
			tour.next().children().eq(2).removeClass('inactive');
		}

		if (currActive <= 0) {
			currActive = 0;
			tour.next().children().eq(0).addClass('inactive');
		} else {
			tour.next().children().eq(0).removeClass('inactive');
		}

		tour.next().children().eq(1).html((currActive +1) +' of '+ tour.children().length);

		tour.data('active', currActive);
		tour.children().eq(currActive).
			addClass('active').
			addClass('incoming').
			fadeIn('fast', function() {
				var prevActive = $(this).parent().data('prevActive');
				if (typeof prevActive == 'number') {
					tour.children().eq(prevActive).
						fadeOut(1).
						removeClass('active');
				}
			});
	},
	
	complete : 1
};


$(idhcTour.init);

