// **************************************************
// BBC Homepage Cycler
// C.Cross 2009
// **************************************************

var current_frame=1; // Global function which holds the current frame of the slideshow
var autorotation=1; // Only auto rotate if the user hasn't provided their own input and stop auto rotation if we've already done it once

// Function to override auto rotation and allow the user to make a choice
function flash_switch(frame) {
	// Load in two variables, the current frame and the desired frame
	autorotation = 0; // User has selected to disable autorotation
	
	// Only switch slides if it is not the current slide
	if (current_frame!=frame) {
	
	// Handle switching of slides for the user
		new Effect.Fade('flash_text' + current_frame, {queue: 'front', duration: 0.3, afterFinish: function() { $('flash_navigation').hide(); } }); // Fade out current slide text
		new Effect.SlideUp('flash_box' + current_frame, {queue: 'end', duration: 0.3 }); // Slide up current slide box
  	new Effect.Fade('flash_slide' + current_frame, {queue: 'end', duration: 0.2 }); // Fade out current slide image
		
		// Remove the class from current selected navigation
		$('carousel' + current_frame).removeClassName('ly');
		
		current_frame = frame; // Update the current frame counter
		
		// Update with the new selected slide
		$('carousel' + frame).addClassName('ly');
		
		new Effect.Appear('flash_slide' + frame, {queue: 'end', to: 1.0000001 }); // Fade in the new slide image
	  new Effect.SlideDown('flash_box' + frame, {queue: 'end', duration: 0.3 } ); // Slide down the new slide box
		
		// If this is IE then text has to fade to opacity of 1, if this is Firefox then text has to fade to opacity of 1.0000001 due to Cleartype bug
		if (Prototype.Browser.IE) { 
		new Effect.Appear('flash_text' + frame, {queue: 'end', duration: 0.2, to: 1, afterFinish: function() { $('flash_navigation').show(); } }); // Fade out current slide text
		} else {
		new Effect.Appear('flash_text' + frame, {queue: 'end', duration: 0.2, to: 1.000001, afterFinish: function() { $('flash_navigation').show(); } }); // Fade out current slide text
		};
	
	}
}

// Function called on page load
function flash_cycler(end_frame) {
	
	// Only start processing the slides if there is more than one frame
	if (end_frame>1){
	
	// Set up a loop to write out links for each slide in cycler
  for(i = 0; i < end_frame; i++)
	{
		// Start appending list items in to the UL for slide selection
		$('flash_selector').innerHTML += "<li id=carousel" + (i+1) + "><a href=# onClick=flash_switch(" + (i+1) + ");><span>Go to " + (i+1) + "</span></a></li>";
  }
	
	// Update the first selector icon because the first slide is now showing
		$('carousel1').addClassName('ly');

	setTimeout(flash_frame(1, end_frame), 8000); //Function only called when the page is first loaded and we are on frame 1
	}
}

// Function to provide provide slide autorotation
function flash_frame(frame, end_frame) {
	return (function() {

  // Only cycle the frames while autorotation is enabled
  if (autorotation==1){

  new Effect.Fade('flash_text' + frame, {queue: 'front', duration: 0.3, afterFinish: function() { $('flash_navigation').hide(); } }); // Fade out current slide text
	new Effect.SlideUp('flash_box' +frame, {queue: 'end', duration: 0.3 }); // Slide up out the old slide box
  new Effect.Fade('flash_slide' + frame, {queue: 'end' }); // Fade out the old slide image
	
	// Reset the frame counter if necessary, otherwise just incrememnt it by one, also we've looped through the slides so reset the autorotation
	if (frame == end_frame) { frame = 1; autorotation = 0; } else { frame = frame + 1; };
	
	// Remove the class from current selected navigation
	$('carousel' + current_frame).removeClassName('ly');
	
	// Asign the current frame number to a global variable so it is accessible by other functions
	current_frame = frame;
	
	// Update with the new selected slide
	$('carousel' + frame).addClassName('ly');
	
  new Effect.Appear('flash_slide' + frame, {queue: 'end', to: 1.0000001 }); // Fade in the new slide image
  new Effect.SlideDown('flash_box' + frame, {queue: 'end', duration: 0.3 } ); // Slide down the new slide box
	// If this is IE then text has to fade to opacity of 1, if this is Firefox then text has to fade to opacity of 1.0000001 due to Cleartype bug
		if (Prototype.Browser.IE) { 
	new Effect.Appear('flash_text' + frame, {queue: 'end', to: 1, afterFinish: function() { $('flash_navigation').show(); setTimeout(flash_frame(frame, end_frame), 8000);}}); // Fade in the new slide text specifically for IE
		} else {
			new Effect.Appear('flash_text' + frame, {queue: 'end', to: 1.0000001, afterFinish: function() { $('flash_navigation').show(); setTimeout(flash_frame(frame, end_frame), 8000);}}); // Fade in the new slide text for other browsers
		};

	}

})
}