/*
* Animated slideshow using jQuery

http://docs.jquery.com/Using_jQuery_with_Other_Libraries#Overriding_the_.24-function
*/
 
//Called on load to start the slideshow
slidetime = null;
slideshow();

function slideshow() {
	if (!slidetime) {
		//The interval between changing images(divs)
		slidetime = setInterval( "switchImage(1)", 4000 );
		//$("#controls DIV.playpause").css("background","url(img/control_pause.png) no-repeat center");
	} else {
		//Turn the timer off, slidetime is nulled so I can check if it's on or off next time
		clearInterval(slidetime);
		slidetime = null;
		//$("#controls DIV.playpause").css("background","url(img/control_play.png) no-repeat center"); 
	}
}

function switchImage($direction) {

	var $jq = jQuery.noConflict();

	//Gets div.current
    var $current = $jq('#slideshow DIV.current');
	
	if ( $current.length == 0 ) $current = $jq('#slideshow IMG:last');
	
	//Determine which direction we're changing and get the previous or next image(div)
	if ( $direction == 1 ) { //Next image
		var $next =  $current.next().length ? $current.next()
			: $jq('#slideshow DIV:first');
	} else { //Previous image
		var $next =  $current.prev().length ? $current.prev()
			: $jq('#slideshow DIV:last');
	}
	
	//Set the current div class as previmg
    $current.addClass('previmg');

	//Fade to the next image(div) and set the next image(div) as current
    $next.css({opacity: 0.0})
        .addClass('current')
        .animate({opacity: 1.0}, 1000, function() {
            $current.removeClass('current previmg');
        });
}
