var iSlideShowAutoscrollTimeout = 5000; // the time to wait until the slides transition by themselves (1000 = 1 second)
var iSlideshowSlidingSpeed      = 750; // the time it takes for the slides to make a transition (1000 = 1 second)

// the opacity and fading speed for the shadow that darkens the slideshow while sliding...
var iSlideshowShadowOpacity       = 0.2;
var iSlideshowShadowFadeInSpeed   = 100;
var iSlideshowShadowFadeOutSpeed  = 200;
var iSlideshowShadowJumpFadeSpeed = 300;

// the opacity and fading speed for the next + previous buttons...
var iSlideshowNextPreviousButtonOpacity      = 0.2;
var iSlideshowNextPreviousButtonHoverOpacity = 0.8;
var iSlideshowNextPreviousButtonFadeOutSpeed = 50;
var iSlideshowNextPreviousButtonFadeInSpeed  = 500;

// the opacity of the slide counter...
var iSlideshowCounterOpacity = 1;

// only show the next + previous buttons + slide counter when the mouse hovers over the slideshow?
var bSlideshowShowNavOnMouseOver = false;

var bShowNumbersInSlideCounter = true;

// declare some global variables...
var bSlideshowSliding;
var iSlideshowTimeout;
var iSlideshowCurrentSlide;

function initialise_slideshow( objSlideshowContainer ) {

	bSlideshowSliding = false;
	iSlideshowCurrentSlide = 1;

	objSlideshowScroller = $(objSlideshowContainer).children('.scroller');

	// get the size of the slideshow...
	iSingleSlideWidth = $(objSlideshowScroller).width();
	iNumberOfSlides   = $(objSlideshowScroller).children('ul').children('li').length;
	
	initialise_slideshow_links();
	
	// only initialise this if there is more than one slide
	if ( iNumberOfSlides>1 ) {

		// set the width of the slider based on how many slides there are...
		$(objSlideshowScroller).children('ul').css({'width': iSingleSlideWidth * iNumberOfSlides});

		// set the click events of the next and previous buttons...
		$(objSlideshowContainer).children('.next-button').click( function() {
		  slideshow_slide( objSlideshowScroller, 'next');
		});
		$(objSlideshowContainer).children('.previous-button').click( function() {
		  slideshow_slide( objSlideshowScroller, 'previous');
		});

		// fill out the counter depending on how many slides there are...
		if (bShowNumbersInSlideCounter) {
			sCounterListItems = '<ul><li class="on">1</li>';
		} else {
			sCounterListItems = '<ul><li class="on"></li>';
		}
		for (i=2; i<=iNumberOfSlides; i++) {
			if (bShowNumbersInSlideCounter) {
				sCounterListItems += '<li>' + i + '</li>';
			} else {
				sCounterListItems += '<li></li>';
			}
		}
		sCounterListItems += '</ul>';
		$(objSlideshowContainer).children('.slide-counter').append( sCounterListItems );	

		// set the click events of the counter list items...
		// (please note that the list items need to have a background set in css for them to be clickable in IE)
		var objListItems = $(objSlideshowContainer).children('.slide-counter').children('ul').children('li').each( function( index ) {
			$(this).click( function(event) {
				slideshow_jump_to_slide( $(this).parent().parent().siblings('.scroller'), objListItems.index(this)+1 );	
			});
		});

		if ( bSlideshowShowNavOnMouseOver ) {
			// show the navigation buttons and slide counter on mouse over, hide them on house out...
			$(objSlideshowContainer).hover(
				function () {
					// show controls on mouse over...
					$(this).children('.previous-button').fadeTo(iSlideshowNextPreviousButtonFadeInSpeed, iSlideshowNextPreviousButtonOpacity);
					$(this).children('.next-button'    ).fadeTo(iSlideshowNextPreviousButtonFadeInSpeed, iSlideshowNextPreviousButtonOpacity);		
					$(this).children('.slide-counter').children('ul').children('li').fadeTo(iSlideshowNextPreviousButtonFadeInSpeed, iSlideshowCounterOpacity);
				}, 
				function () {
					// hide controls on mouse out...
					$(this).children('.previous-button').fadeOut(iSlideshowNextPreviousButtonFadeOutSpeed);
					$(this).children('.next-button'    ).fadeOut(iSlideshowNextPreviousButtonFadeOutSpeed);		
					$(this).children('.slide-counter').children('ul').children('li').fadeOut(iSlideshowNextPreviousButtonFadeOutSpeed);
				}
			);
		} else {
			// fade in the slideshow controls and counter...
			$(objSlideshowContainer).children('.previous-button').fadeTo(500, iSlideshowNextPreviousButtonOpacity);
			$(objSlideshowContainer).children('.next-button'    ).fadeTo(500, iSlideshowNextPreviousButtonOpacity);		
			$(objSlideshowContainer).children('.slide-counter').children('ul').children('li').fadeTo(750, iSlideshowCounterOpacity);
		}

		// set the hover states of the navigation buttons...
		$(objSlideshowContainer).children('.previous-button, .next-button').hover(
			function() {
				$(this).fadeTo(200, iSlideshowNextPreviousButtonHoverOpacity);		
			},
			function() {
				$(this).fadeTo(200, iSlideshowNextPreviousButtonOpacity);		
			}
		);

		// schedule showing the next slide...
		iSlideshowTimeout = setTimeout(function() { slideshow_slide( objSlideshowScroller, 'next' ); }, iSlideShowAutoscrollTimeout );
		
	}
	
}

function initialise_slideshow_links() {

	$('.scroller li a[rel="video"]').click( function() {
		
		sHref = $(this).attr('href');
		
		if ( sHref.indexOf('youtube.com') > -1 ) {
			
			sVideoCode = sHref.substr(  sHref.indexOf('v=')+2 )
			if ( sVideoCode.indexOf('&') > -1 ) {
				sVideoCode = sVideoCode.substr( 0, sVideoCode.indexOf('&') )
			}			
			
			var sYoutubeEmbedCode = '<object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/' + sVideoCode + '?fs=1&amp;hl=en_US&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + sVideoCode + '?fs=1&amp;hl=en_US&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>';		
			
			if ( $('#youtube-overlay').length > 0 ) {
				
				objOverlayDiv = $('#youtube-overlay');
				objOverlayShadow = $('#youtube-overlay-shadow');
				
			} else {
				var objOverlayDiv = $('<div></div>');
				objOverlayDiv.attr('id', 'youtube-overlay');
				$('body').append(objOverlayDiv);

				var objOverlayShadow = $('<div></div>');
				objOverlayShadow.attr('id', 'youtube-overlay-shadow');
				objOverlayShadow.width( $(document).width() );
				objOverlayShadow.height( $(document).height() );			

				$('body').append(objOverlayShadow);

			}
			
			objOverlayDiv.html(sYoutubeEmbedCode + '<div id="close-button">Close</div>');
			
			objOverlayShadow.fadeTo(200, 0.5, function() {
				objOverlayDiv.css('display', 'block');
			});
			
			objOverlayShadow.click( function() {

				$('#youtube-overlay').remove();
				
				$(this).fadeOut(200, function() {
					$('#youtube-overlay-shadow').remove();
				});
				
			});

			$('#youtube-overlay #close-button').click( function() {

				$('#youtube-overlay').remove();
				
				$(this).fadeOut(200, function() {
					$('#youtube-overlay-shadow').remove();
				});
				
			});
			
		}
		
		return false
	});
	
}

function slideshow_slide( objSlideshowScroller, sDirection ) {

	if ( bSlideshowSliding==false ) {

		bSlideshowSliding = true;

		// cancel any scheduled slide...
		clearTimeout( iSlideshowTimeout );

		if ( bSlideshowShowNavOnMouseOver==false ) {
			// fade out the next + previous buttons...
			objSlideshowScroller.siblings('.previous-button').fadeOut(iSlideshowNextPreviousButtonFadeOutSpeed);
			objSlideshowScroller.siblings('.next-button').fadeOut(iSlideshowNextPreviousButtonFadeOutSpeed);
			//objSlideshowScroller.siblings('.slide-counter').children('ul').children('li').fadeOut(iSlideshowNextPreviousButtonFadeOutSpeed);
		}

		// fade in the shadow...
		if( bIsIE6!=true ) {
			$(objSlideshowScroller).siblings('.shadow').fadeTo(iSlideshowShadowFadeInSpeed, iSlideshowShadowOpacity);
		}

		// update the counter so that nothing is highlighted...
		$(objSlideshowScroller).siblings('.slide-counter').children('ul').children('li').removeClass('on');

		// get some info on where the slideshow currently is...
		iSlideWidth        = objSlideshowScroller.width();
		iCurrentScrollLeft = objSlideshowScroller.scrollLeft();
		iNumberOfSlides    = $(objSlideshowScroller).children('ul').children('li').length;

		// update the global iSlideshowCurrentSlide variable...
		if (sDirection=='next') {
			iNewScrollLeft = iCurrentScrollLeft+iSlideWidth;
			iSlideshowCurrentSlide+=1;
		} else {
			iNewScrollLeft = iCurrentScrollLeft-iSlideWidth;
			iSlideshowCurrentSlide-=1;
		}
		if (iSlideshowCurrentSlide>iNumberOfSlides) {
			iSlideshowCurrentSlide = 1;
		} else if ( iSlideshowCurrentSlide<1 ) {
			iSlideshowCurrentSlide = iNumberOfSlides;
		}

		// shuffle slides around if we've reached the end...
		if( iNewScrollLeft >= iNumberOfSlides * iSlideWidth ) {

			// we're scrolling right and we've reached the end of the slideshow, so the slides are looking like this...
			// 	slide1|slide2|slide3
			// ... with slide3 being shown
			//
			// the following few lines shuffle things around so that the slides look like this...
			// 	slide2|slide3|slide1
			// ... with slide3 still being shown, but with some room to scroll to the right and see slide 1

			var sSlideContents = ""

			// get the second till the last list items...
			for(i=2; i<=iNumberOfSlides; i++) {
				sSlideContents += '<li>';
				sSlideContents += $(objSlideshowScroller).children('ul').children('li:nth-child(' + i + ')').html();
				sSlideContents += '</li>';
			}

			// add the first list item...
			sSlideContents += '<li>';
			sSlideContents += $(objSlideshowScroller).children('ul').children('li:nth-child(1)').html();
			sSlideContents += '</li>';

			// update the list with the newly ordered list items...
			$(objSlideshowScroller).children('ul').html(
				sSlideContents
			);

			// move the slideshow back one slide...
			$(objSlideshowScroller).scrollLeft(iCurrentScrollLeft-iSlideWidth);
			iNewScrollLeft = iCurrentScrollLeft;
			
		} else if( iNewScrollLeft < 0 ) {

			// we're scrolling left and we've reached the start of the slideshow, so the slides are looking like this...
			// 	slide1|slide2|slide3
			// ... with slide1 being shown
			//
			// the following few lines shuffle things around so that the slides look like this...
			// 	slide3|slide1|slide2
			// ... with slide1 still being shown, but with some room to scroll to the left and see slide 3

			var sSlideContents = ""

			// get the last list item...
			sSlideContents += '<li>';
			sSlideContents += $(objSlideshowScroller).children('ul').children('li:nth-child(' + iNumberOfSlides + ')').html();
			sSlideContents += '</li>';

			// add the first till second to last list items...
			for(i=1; i<iNumberOfSlides; i++) {
				sSlideContents += '<li>';
				sSlideContents += $(objSlideshowScroller).children('ul').children('li:nth-child(' + i + ')').html();
				sSlideContents += '</li>';
			}

			// update the list with the newly ordered list items...
			$(objSlideshowScroller).children('ul').html(
				sSlideContents
			);

			// move the slideshow forward one slide...
			$(objSlideshowScroller).scrollLeft(iCurrentScrollLeft+iSlideWidth);
			iNewScrollLeft = iCurrentScrollLeft;	
			
		}

		initialise_slideshow_links();

		// slide the slideshow...
		$(objSlideshowScroller).animate({scrollLeft: iNewScrollLeft}, iSlideshowSlidingSpeed, function() {
			bSlideshowSliding = false;
			objSlideshowScroller = $(this);

			// get the order number of the current slide...
			iNewCurrentSlideNumber = parseInt( $(objSlideshowScroller).scrollLeft() / $(objSlideshowScroller).width() ) + 1;

			// update the counter to highlight the current slide number...
			$(objSlideshowScroller).siblings('.slide-counter').children('ul').children('li:nth-child(' + iSlideshowCurrentSlide + ')').addClass('on');

			// fade out the shadow...
			if( bIsIE6!=true ) {
				$(objSlideshowScroller).siblings('.shadow').fadeOut(iSlideshowShadowFadeOutSpeed);
			}

			if ( bSlideshowShowNavOnMouseOver==false ) {
				// fade in the navigation buttons...
				objSlideshowScroller.siblings('.previous-button').fadeTo(iSlideshowNextPreviousButtonFadeInSpeed, iSlideshowNextPreviousButtonOpacity);
				objSlideshowScroller.siblings('.next-button').fadeTo(iSlideshowNextPreviousButtonFadeInSpeed, iSlideshowNextPreviousButtonOpacity);
				objSlideshowScroller.siblings('.slide-counter').children('ul').children('li').fadeTo(iSlideshowNextPreviousButtonFadeInSpeed, iSlideshowCounterOpacity);
			}

			// schedule another slide...
			iSlideshowTimeout = setTimeout(function() { slideshow_slide( objSlideshowScroller, 'next' ); }, iSlideShowAutoscrollTimeout );
		});
	}		

}

function slideshow_jump_to_slide( objSlideshowScroller, iNewSlideNumber ) {

	if ( bSlideshowSliding==false && iNewSlideNumber!=iSlideshowCurrentSlide ) {

		if ( iNewSlideNumber == iSlideshowCurrentSlide+1 ) {

			// slide to the next slide...
			slideshow_slide( objSlideshowScroller, 'next' );

		} else if ( iNewSlideNumber == iSlideshowCurrentSlide-1 ) {

			// slide to the previous slide...
			slideshow_slide( objSlideshowScroller, 'previous' );

		} else {

			// we're jumping more than one slide away, so we'll fade out - do the scrolling - and fade back in

			bSlideshowSliding=true;

			// cancel any scheduled slide...
			clearTimeout( iSlideshowTimeout );			

			// update the counter so that nothing is highlighted...
			$(objSlideshowScroller).siblings('.slide-counter').children('ul').children('li').removeClass('on');

			// get some info on where the slideshow currently is...
			iSlideWidth          = objSlideshowScroller.width();
			iCurrentScrollLeft   = objSlideshowScroller.scrollLeft();
			iNumberOfSlides      = $(objSlideshowScroller).children('ul').children('li').length;
			iTotalSlideShowWidth = iSlideWidth*iNumberOfSlides;

			// work out how far we have to scroll...
			var iScrollDistance = (iNewSlideNumber - iSlideshowCurrentSlide) * iSlideWidth;			

			// get the new scrollLeft number...
			var iNewScrollLeft = iCurrentScrollLeft + iScrollDistance;

			// fix scrollLeft if it's too high or too low...
			if ( iNewScrollLeft < 0 ) {
				iNewScrollLeft+=iTotalSlideShowWidth
			} else if ( iNewScrollLeft>=iTotalSlideShowWidth ) {
				iNewScrollLeft-=iTotalSlideShowWidth
			}

			// update the global current slide variable...
			iSlideshowCurrentSlide = iNewSlideNumber

			// fade in the shadow...
			$(objSlideshowScroller).siblings('.shadow').fadeTo(iSlideshowShadowJumpFadeSpeed, 1, function() {

				// scroll to the chosen slide...
				$(objSlideshowScroller).scrollLeft(iNewScrollLeft);

				// update the counter to highlight the current slide number...
				$(objSlideshowScroller).siblings('.slide-counter').children('ul').children('li:nth-child(' + iSlideshowCurrentSlide + ')').addClass('on');

				if ( bSlideshowShowNavOnMouseOver==false ) {
					// fade out the next + previous buttons...
					objSlideshowScroller.siblings('.previous-button').fadeTo(0, 0);
					objSlideshowScroller.siblings('.next-button').fadeTo(0, 0);
				}					

				// fade out the shadow
				$(this).fadeOut(iSlideshowShadowJumpFadeSpeed, function() {

					bSlideshowSliding = false;

					if ( bSlideshowShowNavOnMouseOver==false ) {
						// fade in the navigation buttons...
						objSlideshowScroller.siblings('.previous-button').fadeTo(iSlideshowNextPreviousButtonFadeInSpeed, iSlideshowNextPreviousButtonOpacity);
						objSlideshowScroller.siblings('.next-button').fadeTo(iSlideshowNextPreviousButtonFadeInSpeed, iSlideshowNextPreviousButtonOpacity);
					}

					// schedule another slide...
					iSlideshowTimeout = setTimeout(function() { slideshow_slide( objSlideshowScroller, 'next' ); }, iSlideShowAutoscrollTimeout );
				});

			});

		}

	}

}
