function addSwipeListener(el, listener)
{
 var startX;
 var dx;
 var direction;
 
 function cancelTouch()
 {
  el.removeEventListener('touchmove', onTouchMove);
  el.removeEventListener('touchend', onTouchEnd);
  startX = null;
  startY = null;
  direction = null;
 }
 
 function onTouchMove(e)
 {
  if (e.touches.length > 1)
  {
   cancelTouch();
  }
  else
  {
   dx = e.touches[0].pageX - startX;
   var dy = e.touches[0].pageY - startY;
   if (direction == null)
   {
    direction = dx;
   // if next line is uncommented, it will prevent scrolling if touchmove is started on element e
   // e.preventDefault();
   }
   else if ((direction < 0 && dx > 0) || (direction > 0 && dx < 0) || Math.abs(dy) > 15)
   {
   	//alert("Touch Canceled");
    cancelTouch();
   }
  }
 }

 function onTouchEnd(e)
 {
  cancelTouch();
  if (Math.abs(dx) > 50)
  {
   listener({ target: el, direction: dx > 0 ? 'right' : 'left' });
  }
 }
 
 function onTouchStart(e)
 {
  if (e.touches.length == 1)
  {
   startX = e.touches[0].pageX;
   startY = e.touches[0].pageY;
   el.addEventListener('touchmove', onTouchMove, false);
   el.addEventListener('touchend', onTouchEnd, false);
  }
 }
 
 el.addEventListener('touchstart', onTouchStart, false);
}


$(document).ready(function()
{
	if ('ontouchmove' in document.body) // check for touch event support
	{
		addSwipeListener(document.getElementById('slide-show'), function(e) { 
			//alert(e.direction); 
			if ( e.direction = 'right'){
				slideshow.showPrevious();
			}
			else if ( e.direction = 'left'){
				slideshow.showNext();
			}
		});
		//$('#slide-show').after('<p class="notopmargin" style="font-size: smaller; font-style: italic;">Swipe for prev. / next image.</p>');
	}
});

