/*** 
    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 :)
	controls added by Chris Arasin
***/

function slideSwitch() {
    var $active = $('#slideshow div.slide.active');

    if ( $active.length == 0 ) $active = $('#slideshow div.slide:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow div.slide:first');

   
	//if using controls includ these lines
		$("a.page.sel").removeClass("sel");
		var nextInd = $next.index();
		$("a.page").eq(nextInd).addClass("sel");
    
	
	$active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 300, function() {
            $active.removeClass('active last-active');
        });
}

function slideSwitchControl(hovered) {
    var $active = $('#slideshow div.slide.active');    
    var $next =  $('#slideshow div.slide').eq(hovered); 
	var activeInd = $active.index();	
	
	if (hovered != activeInd){	
		$("a.page.sel").removeClass("sel");
		$("a.page").eq(hovered).addClass('sel');
		$active.addClass('last-active');
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 300, function() {
				$active.removeClass('active last-active');
				
			});
	}
}

$(function() {
    var slideTimer = setInterval( "slideSwitch()", 5000 );
		
	
	$("#slidefooter").hover(
	function() {
		clearInterval(slideTimer);
	},
	function() {
		slideTimer = setInterval ( "slideSwitch()", 5000);
		}
	);
	$("a.page").click(function(e) {
		e.preventDefault();
		var indNext = $("a.page").index($(this));
		var next = $("#slideshow div.slide").eq(indNext);		
		slideSwitchControl(indNext); 
	});
	
	
	
	$("a.next").click(function(e) {
		e.preventDefault();
		var $active = $('#slideshow div.slide.active');

    if ( $active.length == 0 ) $active = $('#slideshow div.slide:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow div.slide:first');
		var indNext = $('#slideshow div.slide').index($next);

		slideSwitchControl(indNext);
	});
	
	$("a.prev").click(function(e) {
		e.preventDefault();
		var activeInd = $('#slideshow div.slide').index($('#slideshow div.slide.active'));
		var indNext = activeInd - 1;
		
		if (indNext == -1 ) {nextInd = $('#slideshow div.slide').length; }

    

		slideSwitchControl(indNext);
	});
	
	
	
});
