function gotoSlide(pos, toSlide){
		if ( !$(toSlide + ' .ipad_slides').is(":animated") ) {
			$(toSlide + ' .slide_ctrl li a').removeClass();
			$(toSlide + ' .slide_ctrl li:eq(' + pos + ') a').addClass('act');
			var shiftX = pos * $('.ipad_slides div').width();
			$(toSlide + ' .ipad_slides').stop().animate({marginLeft:-shiftX+'px'},450);
		}
	}

$(function(){

$('.ipad_slideshow').each(function() {
	var totWidth=0;
	var positions = new Array();

	$('.ipad_slides div', this).each(function(i){
		/* Loop through all the slides and store their accumulative widths in totWidth */
		positions[i]= totWidth;
		totWidth += $(this).width();

		/* The positions array contains each slide's commulutative offset from the left part of the container */

		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
	});

	$('.ipad_slides', this).width(totWidth);

	/* Change the container div's width to the exact width of all the slides combined */
	
	});
	
	$('.ipad_slides a').click(function(){
	
		var toSlide = '#' + $(this).closest('.ipad_slideshow').attr('id');
		
		var pos = Math.abs(parseInt($(toSlide + ' .ipad_slides').css('margin-left')) / $('.ipad_slides div').width());
		if (pos < $(toSlide + ' .ipad_slides div').length-1) {
			gotoSlide(pos+1, toSlide);
		} else {
			gotoSlide(0, toSlide);
		}
		return false;
	});
	
	$('.slide_ctrl li a:not([class*="slide"])').click(function(e){
		var toSlide = '#' + $(this).parent().parent().attr('rel');
		var pos = $(this).parent().prevAll().length;
		gotoSlide(pos, toSlide);
		return false;
		/* Prevent the default action of the link */
	});
	
	$('.slide_left').click(function(){
		var toSlide = '#' + $(this).parent().attr('rel');
		var pos = Math.abs(parseInt($(toSlide + ' .ipad_slides').css('margin-left')) / $('.ipad_slides div').width());
		if (pos > 0) {
			gotoSlide(pos-1, toSlide);
		} else {
			gotoSlide(($(toSlide + ' .ipad_slides div').length-1), toSlide)
		}
		return false;
	});
	
	$('.slide_right').click(function(){
		var toSlide = '#' + $(this).parent().attr('rel');
		var pos = Math.abs(parseInt($(toSlide + ' .ipad_slides').css('margin-left')) / $('.ipad_slides div').width());
		if (pos < $(toSlide + ' .ipad_slides div').length-1) {
			gotoSlide(pos+1, toSlide);
		} else {
			gotoSlide(0, toSlide);
		}
		return false;
	});

});
