var timeoutID = 0;
var working = false;

timeoutID = setTimeout( "slideSwitch()", 4000 );

/*** 
    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() {
    var $active = $('#slideshow DIV.active');
	var $active_selected = $('#slideshow_selected a.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
	if ( $active_selected.length == 0 ) $active_selected = $('#slideshow_selected a:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');
	var $next_selected =  $active_selected.next().length ? $active_selected.next()
        : $('#slideshow_selected a:first');	

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

	working = true;
    $active.addClass('last-active');
	
	$next_selected.addClass('active');
    $active_selected.removeClass('active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
			working = false;
			var timeoutAmount = 4000;
			if ($('#slideshow div').index($next) == 1)
				timeoutAmount = 10000;
			if ($('#slideshow div').index($next) == 2)
				timeoutAmount = 5000;	
			timeoutID = setTimeout( "slideSwitch()", timeoutAmount );
				
        });
		
		
}


$(document).ready( function(){
$active = $('#slideshow DIV.active');													

$("#slideshow_selected a").click(function(e){
	e.preventDefault();									  
										  
	var $active2 = $('#slideshow DIV.active');
	var $active_selected2 = $('#slideshow_selected a.active');
		
		if (!working){
			working = true;
			clearTimeout(timeoutID);
			clickedid = $(this).attr('id');
			var clicked = 0;
			switch(clickedid){
				case "slide0":
				clicked = 0;
				break;
				case "slide1":
				clicked = 1;
				break;
				case "slide2":
				clicked = 2;
				break;
			}
			
			if ("slide"+clicked.toString() == $('#slideshow_selected a.active').attr("id")){
				working = false;
				return;
			}
			
			var $jumpto = $('#slideshow div:eq('+clicked+')');
			$active2.addClass('last-active');
			
			$('#'+clickedid).addClass('active');
			$active_selected2.removeClass('active');
	
			$jumpto.css({opacity: 0.0})
				.addClass('active')
				.animate({opacity: 1.0}, 1000, function() {
					$active2.removeClass('active last-active');
					working = false;
				});
		}
	});
});
