// JavaScript Document

//this function switches adverts and provides a nice animation effect
function slideSwitch(name,hide) {
	var $active = $(name+' span.active');
	
	//if we don't have an active class make the last in sequence active
	if ( $active.length == 0 ) $active = $(name+' span:last');
	
	//find out what the next one is and save it into the $next jquery object (if we don't have one then the first will be next)
	var $next =  $active.next().length ? $active.next() : $(name+' span:first');
	
	//now change the active one to the last active one
	$active.addClass('last-active')
	if(hide)$active.hide();
	
	//change the opacity of the next and add the active class, then remove from the currently active
	$next.css({opacity: 0.0}).show().addClass('active').animate({opacity: 1.0}, 1000, function() {
	
	$active.removeClass('active last-active');
	var $next =  $active.next().length ? $active.next() : $(name+' span:first');

});
}
function goToSlide(advert) {
	
	//find the one that is shown at the moment
	var $active = $("#slideshow span.active");
	
	//find out what the next one is and save it into the $next jquery object
	var $next =  $("#slideshow span#" + advert);
	
	//now change the active one to the last active one
	$active.addClass('last-active')
	
	//change the opacity of the next and add the active class, then remove from the currently active
	$next.css({opacity: 0.0}).show().addClass('active').animate({opacity: 1.0}, 200, function() {
	
	$active.removeClass('active last-active');
	var $next =  $active.next().length ? $active.next() : $(name+' span:first');

});

	
	}
function stopShow(event) {
					//stop the slide show
					clearInterval(showtime);
					//change the button to have a play rather than pause
					$("#pause").hide();
					$("#play").show();						
}
function startShow() {
				  //start the slide show
				  showtime = setInterval( "slideSwitch('#slideshow')", 5000 );
				  
				  //change the button to have a pause rather than play
				  $("#play").hide();
				  $("#pause").show();	
	}
function goToAdvert(event) {
				   //stop the slideshow
				   stopShow(showtime);
				   
				   //show the right advert and hide the two that are wrong				   
				   goToSlide(event.data.advert);	   
				   }
$(document).ready(function(event) {
						   
					//set the interval
					showtime = setInterval( "slideSwitch('#slideshow')", 5000 );
					
					$("#pause").bind("click",{interval:showtime},stopShow);
					$("#play").click(startShow);
					
					//set the other three buttons

					$("#advertOneButton").bind("click",{advert:'advertOne'},goToAdvert);
					$("#advertTwoButton").bind("click",{advert:'advertTwo'},goToAdvert);
					$("#advertThreeButton").bind("click",{advert:'advertThree'},goToAdvert);


						   });
$(document).ready(function() {         
         
    // initialize scrollable  
    $("div.scrollable").scrollable();  
	
     
});