/**
##############################################################################################
#
#		Title: Ouch! Slideshow Script
#
#		Description: Dynamic Behaviour specific to slideshow pages
#
##############################################################################################
#   $Id: slideshow.js 386 2008-10-15 10:14:42Z Dave $
##############################################################################################
*/
Slideshow = {
	
	currentSlide: 1, // Current slide being displayed
	totalSlides: null, // Total number of slides
	gotos: null, // Array of goto slides link elements
	
	// Max number of slides to show either side of current slide
	slideRange: 5,
	
	/**
	 * Function: init
	 * 
	 * Description: Performs onLoad initialisation of behaviour
	 */
	init : function(){
						
		// Show first slide
		var first = 1;
		if (location.hash)
			first = location.hash.split('-')[1];
		
		$('slide-' + first).style.display = 'block';
		
		if ($('slide-controls')) {
			bbcjs.dom.addEventListener($('sc-prev-l'), 'click', Slideshow.clickPrev);
			bbcjs.dom.addEventListener($('sc-next-l'), 'click', Slideshow.clickNext);
			
			Slideshow.gotos = $('gotos').getElementsByTagName('a');
			for (var i = 0; i < Slideshow.gotos.length; i++) {
				bbcjs.dom.addEventListener(Slideshow.gotos[i], 'click', Slideshow.clickGoTo);
			}
			Slideshow.totalSlides = Slideshow.gotos.length;
			
			// check if there is a strong element and if so add another slide to the total
			if ($('gotos').getElementsByTagName('strong').length == 1)
				Slideshow.totalSlides++;
				
			Slideshow.checkSlideControls();
		}
		
		Common.updateBuffer();
	},
	
	/**
	 * Function: clickPrev
	 * 
	 * Description: Handles clicking previous slide control	
	 * 
	 * @param {Object} e - the click event
	 */
	clickPrev: function(e){
		bbcjs.dom.stopEvent(e); // stop following the link
		
		Slideshow.hideSlide(Slideshow.currentSlide);
		Slideshow.showSlide(--Slideshow.currentSlide);			
		
		Slideshow.checkSlideControls();
	},
	
	/**
	 * Function: clickNext
	 * 
	 * Description: Handles clicking next slide control	
	 * 
	 * @param {Object} e - the click event
	 */
	clickNext: function(e){
		bbcjs.dom.stopEvent(e); // stop following the link
		
		Slideshow.hideSlide(Slideshow.currentSlide);
		Slideshow.showSlide(++Slideshow.currentSlide);
		
		Slideshow.checkSlideControls();		
	},
	
	/**
	 * Function: checkSlideControls
	 * 
	 * Description: Shows/hides slide controls based on current slide number
	 */
	checkSlideControls: function(){
		
		if (Slideshow.currentSlide == 1){
		$('sc-prev').style.visibility = 'hidden';
		}else{
			$('sc-prev').style.visibility = 'visible';
		}
		
		if (Slideshow.currentSlide == Slideshow.totalSlides){
			$('sc-next').style.visibility = 'hidden';
		}else{
			$('sc-next').style.visibility = 'visible';
		}
		
		if (Slideshow.totalSlides > (Slideshow.slideRange * 2) + 1){
			for (var i=0; i<Slideshow.gotos.length; i++){
				var posComp; (parseInt(Slideshow.currentSlide) + Slideshow.slideRange + 1) - Slideshow.totalSlides > 0 ? posComp = (parseInt(Slideshow.currentSlide) + Slideshow.slideRange + 1) - Slideshow.totalSlides : posComp =1;
				var negComp; parseInt(Slideshow.currentSlide) - Slideshow.slideRange - 2 < 0 ? negComp = Math.abs(parseInt(Slideshow.currentSlide) - Slideshow.slideRange - 2) : negComp = 1;
				if (parseInt(Slideshow.gotos[i].innerHTML) > (parseInt(Slideshow.currentSlide) - (Slideshow.slideRange + posComp)) && parseInt(Slideshow.gotos[i].innerHTML) < (parseInt(Slideshow.currentSlide) + (Slideshow.slideRange + negComp))){
					Slideshow.gotos[i].style.display = 'inline';
				}else{
					Slideshow.gotos[i].style.display = 'none';
				}
			}
		}
		
		Common.updateBuffer();		
	},
	
	/**
	 * Function: clickGoTo
	 * 
	 * Description: Handles clicking go to slide control
	 * 
	 * @param {Object} e - the click event
	 */
	clickGoTo: function(e){
		bbcjs.dom.stopEvent(e); // stop following the link	
		var number = e.target.innerHTML;
		
		Slideshow.hideSlide(Slideshow.currentSlide);
		Slideshow.currentSlide = number;
		Slideshow.showSlide(Slideshow.currentSlide);
		
		Slideshow.checkSlideControls();
	},
	
	/**
	 * Function: showSlide
	 * 
	 * Description: Shows the specified slide
	 * 
	 * @param {Integer} number - the slide number to show
	 */
	showSlide: function(number){
		$('slide-' + number).style.display = 'block';
		var oldBut = $('scp-' + number);
		var newBut = bbcjs.dom.elem('strong',{id:'scp-' + number},bbcjs.dom.text(oldBut));
		bbcjs.dom.attr(newBut,'class','sc');
		bbcjs.dom.before(oldBut,newBut);
		bbcjs.dom.remove(oldBut);
	},
	
	/**
	 * Function: hideSlide
	 * 
	 * Description: Hides the specified slide
	 * 
	 * @param {Integer} number - the slide number to hide
	 */
	hideSlide: function(number){
		$('slide-' + number).style.display = 'none';
		var oldBut = $('scp-' + number);
		var newBut = bbcjs.dom.elem('a',{id:'scp-' + number,href:"#"},bbcjs.dom.text(oldBut));
		bbcjs.dom.attr(newBut,'class','sc');
		bbcjs.dom.before(oldBut,newBut);
		bbcjs.dom.remove(oldBut);
		bbcjs.dom.addEventListener(Slideshow.gotos[number-1], 'click', Slideshow.clickGoTo);
	}
	
	

};


/**
 * Compile-time executed code
 */
bbcjs.addOnLoadItem(Slideshow.init);
/**
  * END
  */
