$(document).ready(function(){


// GALLERY

$(function() {
	//update the URL with the slide number
	var index = 0, theNext = 0, thePrev = 0, hash = window.location.hash;

	if (hash) {
		index = /\d+/.exec(hash)[0];
		index = (parseInt(index) || 1) - 1; // slides are zero-based
		var last = $('#carousel-prev').children().length - 1;
		theNext = (index < last) ? (index+1) : 0;
		thePrev = (index == 0) ? last : (index-1);	
		}
	else {
		index = 0;
		var last = $('#carousel-prev').children().length - 1;
		theNext = (index < last) ? (index+1) : 0;
		thePrev = (index == 0) ? last : (index-1);
		}

	$('#carousel-current').cycle({ 
		fx:  'fade', 
		speed:  '20', 
		startingSlide: index, 
		timeout: 0,
		after: onAfter,
		next:'.next', 
		prev:'.prev'
	 	});

	$('#carousel-next').cycle({ 
		 fx:  'fade', 
		 speed:  '20', 
		 startingSlide: theNext, 
		 timeout: 0,
		 next:'.next', 
		 prev:'.prev'
		 });	

	$('#carousel-prev').cycle({ 
		fx:  'fade', 
		speed:  '20',
		startingSlide: thePrev, 
		timeout: 0,
		next:'.next', 
		prev:'.prev'
		});

	// put the slide number and total slide count in the #num
	function onAfter(curr,next,opts) {
		var num = (opts.currSlide + 1) + ' of ' + opts.slideCount;
		$('#num').html(num);
		window.location.hash = opts.currSlide + 1;
		}

	});	  

	
// KEY BINDINGS

 $(document).keydown(function (k) {
	// NEXT BINDINGS: right arrow key is 39, j key is 74 
	
	if (k.keyCode == 39 || k.charCode == 39 || 
		 k.keyCode == 74  || k.charCode == 74) { 
		$("#forward").click();
		return false;
		}

	// PREV BINDINGS: left arrow key is 37, k key is 75
			
	if (k.keyCode == 37 || k.charCode == 37 || 
		 k.keyCode == 75  || k.charCode == 75) { 
		$("#backward").click();
		return false;
		}
		
	});

});
