$(document).ready(function(){

  $('div#slideshow').each(function(index) {
	var theShow = $(this);

	  var currentPosition = 0;
	  var slideWidth = parseInt(($('#slideshow #slidesContainer').css('width') != undefined ? $('#slideshow #slidesContainer').css('width').replace('px','') : '650'));
	  var slides = $('.slide',theShow);
	  var numberOfSlides = slides.length;
	  

	  $('#slidesContainer',theShow).css('overflow', 'hidden');

	  slides
		.wrapAll('<div id="slideInner"></div>')
		.css({
		  'float' : 'left',
		  'width' : slideWidth
		});

	  $('#slideInner',theShow).css('width', slideWidth * numberOfSlides);

	  theShow
		.prepend('<span class="control" id="leftControl">Clicking moves left</span>')
		.append('<span class="control" id="rightControl">Clicking moves right</span>');

	  manageControls(currentPosition);

	  $('.control',theShow)
		.bind('click', function(){

		
		currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
		//ytp_1.pauseVideo();
		//ytp_2.pauseVideo();
		//ytp_3.pauseVideo();

		manageControls(currentPosition);
		$('#slideInner',theShow).animate({'marginLeft' : slideWidth*(-currentPosition)}, {duration: 420});
	  });

	  function manageControls(position){
		if(position==0){ $('#leftControl',theShow).hide() } else{ $('#leftControl',theShow).show() }
		if(position==numberOfSlides-1){ $('#rightControl',theShow).hide() } else{ $('#rightControl',theShow).show() }
	  }

   });

});
