/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

function slideSwitch(slideTo) {
    var $active = $('#slideshow div.active');
    if ( $active.length == 0 ) {
    	$active = $('#slideshow div:last');
    }
    var $next =  $active.next().length ? $active.next() : $('#slideshow div:first');
    $active.addClass('last-active');
    
    var slideTo = ( slideTo+1 ) ? slideTo : null;
    if ( slideTo != null ) {
    	$next = $('#slideshow div').eq(slideTo);
    }
    
    $active.animate({opacity: 0.0}, 500);
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
        	$active.removeClass('active last-active');
//            $active.removeClass('active last-active').animate({opacity: 0.0}, 250);
        });
}


$(function() {
	$('div.joel').addClass('active');
	$('div.joel').css('opacity', '1.0');

	//setInterval( "slideSwitch()", 5000 );
    var playSlideshow = setInterval( "slideSwitch()", 5000 );
    // create buttons to move to specific slide
    var $slideButtons = $("#slideShowButtons a.slideButton");
    $slideButtons.click(function(){
    	var that = $(this);
    	var direction = that.attr('rel');
    	var $active = $('#slideshow div.active');
    	var activeIndex = $active.index();
    	var numberOfSlides = $('#slideshow div.slide').length;
//    	console.log('active: ' + activeIndex);
    	
    	if ( $active.length == 0 ) { 
    		$active = $('#slideshow div:last');
    	}
    	
    	
    	
    	if(direction == 'prev') {
    		if (activeIndex == 0) {
    			var moveToSlide = (numberOfSlides - 1);
    		} else {
    			var moveToSlide = (activeIndex - 1);
    		}
    		
    	} else if (direction == 'next') {
    		if (activeIndex == (numberOfSlides - 1)) { // adjusting for array values
    			var moveToSlide = 0;
    		} else {
	    		var moveToSlide = (activeIndex + 1);
	    	}
    	}
    	
//    	console.log('move to slide: ' + moveToSlide);
    	  	
    	// stop the slideshow, to keep it from trying to overlap our transition
    	clearInterval(playSlideshow);
    	// call the function using the index of the clicked button
    	slideSwitch( moveToSlide );
    	// restart the slideshow
//    	 setTimeout( 'playSlideshow = setInterval( "slideSwitch()", 5000 )', 5000);
    });
});


function disableEnterKey(e) {
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}


