var settings = {
	slideshowSpeed: 5000
};

fnSlideshow = function(){

	//options
	slideShowPause=6000;

	//ensure the slides are hidden
	$("#whatson .item").hide();
			
	//show the first slide, add on state
	$("#whatson .item:eq(0)").addClass("on").show();
			
	//set time interval
	runSlideshow = setInterval( "fnSlideSwitch()", slideShowPause );
	
	$("#whatson").mouseover( function(){
		clearInterval(runSlideshow);
	}).mouseout( function(){
		runSlideshow = setInterval( "fnSlideSwitch()", slideShowPause );
	});

};
fnSlideSwitch = function(){
	// hide current and show next
	$("#whatson .item.on").removeClass("on").fadeOut(1500).next(".item").addClass("on").fadeIn(1500);
	//check if that was the last item
	if ($("#whatson .item.on").length==0){
		$("#whatson .item:eq(0)").addClass("on").fadeIn(1500);
	};
};
function initSlideshows()
{
	$("div.slideshow").each(function()
	{
		var $slideshow = $(this);
		var selectedImage = 0;
		var maxSelectedImage = $slideshow.find("img").length - 1;

		$slideshow.css("position", "relative");

		$slideshow.find("img")
				.css("position", "absolute")
				.css("top", 0)
				.css("left", 0)
				.hide();

		$slideshow.find("img:eq("+selectedImage+")").show();
        var height = new Array();
        var width = new Array();
        $slideshow.find("img").each(function(i){
            var currentImg = $(this);
            height[i] = currentImg.height();
            width[i] = currentImg.width();
            i++;
        });

        $slideshow.css('height', height.max());
        $slideshow.css('width', width.max());
        
		var advanceSlideshow = function()
		{
			var newSelectedImage = selectedImage + 1;
			if(newSelectedImage > maxSelectedImage)
				newSelectedImage = 0;

			$slideshow.find("img:eq("+selectedImage+")").fadeOut();
			$slideshow.find("img:eq("+newSelectedImage+")").fadeIn();

			selectedImage = newSelectedImage;

			setTimeout(advanceSlideshow, settings.slideshowSpeed);
		};
		setTimeout(advanceSlideshow, settings.slideshowSpeed);
	});
}   

Array.prototype.max = function() {
    return Math.max.apply(null, this);
};
$(document).ready(function(){

	// add class to body to show we have javascript
	$("body").addClass("js");

	// homepage events box
	$('#eventHighlights ul li').append('<p class="more">More</p>').append('<p class="less">Less</p>').find('.less').hide();
	$('#eventHighlights ul li').find('.details').hide();
	$('#eventHighlights ul li .more').click(function() {

		currentLI = $(this).parent();

		$(currentLI).find('.more').hide();
		$(currentLI).find('.less').show();
		$(currentLI).addClass('on').find('.details').slideDown('slow');
		$(currentLI).parent().find('li:not(.on)').slideUp('slow');

		$('#eventHighlights ul li .less').click(function() {

			theUL = $(this).parent().parent();
		
			$(theUL).find('.more').show();
			$(theUL).find('.less').hide();
			$(theUL).find('li').removeClass('on').slideDown('slow');
			$(theUL).find('.details').slideUp('slow');

		});

	});

	// run events slideshow, there is more than one
	noOfSlideshows = $('#whatson div.item').size();
	if(noOfSlideshows != 1) {
		fnSlideshow();
	};
    initSlideshows();
});