//based on Slideshow Code by John Raasch
//http://jonraasch.com/blog/a-simple-jquery-slideshow

function prepareSlideshow () {
	//wrap images in divs
	$('#slideshow img').wrap('<div>');
	
	//make all divs transparent
	$('#slideshow div').css({opacity: 0.0});
	
	//make first div active
	$('#slideshow div:first').addClass('active').css({opacity: 1.0});
	
	//transform the image to background-image of the div, then delete the image
	$('#slideshow div img').each(function(){
		//selctor performance!
		var currentImg = $(this);
		
		//get path
		var path = 'url(\''+currentImg.attr('src')+'\')';
		
		//make background image, then delete all child nodes
		currentImg.parent().css('background-image', path).empty();
		
	});
}

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

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

    var $next =  $active.next().length ? $active.next() : $('#slideshow div:first');
	
	$active.animate({opacity: 0.0}, 1500, 'easeOutQuad');
	
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1500, 'easeInQuad', function() {
            $active.removeClass('active');
        });
}

function slideSwitchNews() {
    var $active = $('#news-latest-container div.active');

    if ( $active.length == 0 ) $active = $('#news-latest-container div:last');

    var $next =  $active.next().length ? $active.next() : $('#news-latest-container div:first');
	
	$active.animate({opacity: 0.0}, 1500, 'easeOutQuad', function(){
		$next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1500, 'easeInQuad', function() {
            $active.removeClass('active');
        });
	});
	

}

$(document).ready(function(){
	//DOM manipulation
	prepareSlideshow();
	
	$('#news-latest-container div').css({opacity: 0.0});
	$('#news-latest-container div:first').addClass('active').css({opacity: 1.0});
	
	
	//call switch function periodically
	setInterval( "slideSwitch()", 5000 );
	setInterval( "slideSwitchNews()", 10000 );
});