var rotate_Pause = false;
var rotate_interval;
	
  function theRotator() {
	//Set the opacity of all images to 0
	$('div.rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div.rotator ul li:first').css({opacity: 1.0});
	//Call the rotator function to run the slideshow, 8000 = change to next image after 8 seconds

rotate_interval = setInterval('rotate()',8000);

}


function rotate() {	


	//Get the first image
	var current = ($('div.rotator ul li.show') ?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));

    if ( current.length == 0 ) current = $('div.rotator ul li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') :current.next()) : $('div.rotator ul li:first'));
	
	//Un-comment the 3 lines below to get the images in random order
	
	//var sibs = current.siblings();
        //var rndNum = Math.floor(Math.random() * sibs.length );
        //var next = $( sibs[ rndNum ] );
		
		var dot = ($('ul.simple-nav li.active') ?  $('ul.simple-nav li.active') : $('ul.simple-nav li:first'));

    if ( dot.length == 0 ) dot = $('ul.simple-nav li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next_dot = ((dot.next().length) ? ((dot.next().hasClass('active')) ? $('ul.simple-nav li:first') :dot.next()) : $('ul.simple-nav li:first'));
	
		
	var dot_2 = ($('ul.simple-nav li a.active') ?  $('ul.simple-nav li a.active') : $('ul.simple-nav li:first a'));

    if ( dot_2.length == 0 ) dot_2 = $('ul.simple-nav li:first a');

	//Get next image, when it reaches the end, rotate it back to the first image
	var child_tree =dot_2.parent().next('li').children('a');
	var next_dot_2 = ((child_tree.length) ? ((child_tree.hasClass('active')) ? $('ul.simple-nav li:first a') : child_tree) : $('ul.simple-nav li:first a'));
		
	
	
		//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');

	
	next_dot_2.addClass('active');
	dot_2.removeClass('active');
}





function startagain(){

rotate_interval = setInterval(rotate,8000);
rotate();

}






$(document).ready(function() {		

	//Load the slideshow
	
		
	theRotator();
	
	$('div.rotator').fadeIn(1000);
    $('div.rotator ul li').fadeIn(1000); // tweek for IE
	
$('#button_play').click(function() {
	if(rotate_Pause == false) {
		clearInterval(rotate_interval);
		rotate_Pause = true;
		$('#button_play').empty();		
$('#button_play').append("PLAY");	

		$("#button_play").removeClass("jqb_btn_pause");
		$("#button_play").addClass("jqb_btn_play");
	} else {
		startagain();
		rotate_Pause = false;
$('#button_play').empty();		
$('#button_play').append("PAUSE");	
		
		$("#button_play").removeClass("jqb_btn_play");
		$("#button_play").addClass("jqb_btn_pause");
	
	
	}
		
});


$('ul.simple-nav li a').click(function(){
	
	$('ul.simple-nav li a').removeClass('active');
	$(this).addClass('active');
	clearInterval(rotate_interval);
	rotate_Pause = true;
	$('#button_play').empty();
	$('#button_play').append("PLAY");	
	$("#button_play").removeClass("jqb_btn_pause");
	$("#button_play").addClass("jqb_btn_play");
	var gallery_image = $(this).attr('href');
	$('div.rotator ul li').removeClass('show').animate({opacity: 0.0}, 1000);
	$('div.rotator ul li'+gallery_image+'').addClass('show').animate({opacity: 1.0}, 1000);
	return(false);
});


});


$('#button_play').append("PLAY");
