var FMainSlideDuration;
var FMS_CurrentSlot = 0; // This is the slot that the current slide is in, could be different if we have wrapped around the end
var FMS_CurrentSlide = 0;
var FMS_Width;
var FMS_IndWidth = 16; // Please include margins and padding in this number
var FMS_TransitionDuration = 1500;
var FMS_GoForward = true;
var FMS_ShowButtons = true;
var FMS_ShowIndicators = false;
var FMS_Periodical;
var FMS_NumSlidesInView = 1;
var FMS_DoCannon = true;
var FMS_TotalNumSlides = 1;
var FMS_Slides;
var FMS_ReposPeriodical;

var FMS_Cannon = function() 
{
	if(FMS_GoForward == true) 
	{
		FMS_CurrentSlide = FMS_SlideNumberWrap(FMS_CurrentSlide + 1);
		FMS_CurrentSlot++;
	}
	else 
	{
		FMS_CurrentSlide = FMS_SlideNumberWrap(FMS_CurrentSlide - 1);
		FMS_CurrentSlot--;
	}	
	
	FMS_DoTransition();
}

var FMS_Next = function(event) 
{
	$clear(FMS_Periodical);
	FMS_RemoveButtonEvents();
	
	FMS_GoForward = true;
	
	FMS_CurrentSlide = FMS_SlideNumberWrap(FMS_CurrentSlide + 1);
	FMS_CurrentSlot++;		
	FMS_DoTransition();

	event.stop();
}

var FMS_Previous = function(event) 
{
	$clear(FMS_Periodical);
	FMS_RemoveButtonEvents();
	
	FMS_GoForward = false;
	
	FMS_CurrentSlide = FMS_SlideNumberWrap(FMS_CurrentSlide - 1);
	FMS_CurrentSlot--;		
	FMS_DoTransition();

	event.stop();
}

var FMS_DoTransition = function()
{
	// Get the slides that matter
	aNextSlideNumber = FMS_SlideNumberWrap(FMS_CurrentSlide + 1);
	aPrevSlideNumber = FMS_SlideNumberWrap(FMS_CurrentSlide - 1);
	
	// Slide all the active slides to the right positionss
	FMS_Slides.each(function(aSlide, aSlideIndex){
		aSlide.set('tween', null);
		if (   (aSlideIndex == aNextSlideNumber) 
			|| (aSlideIndex == aPrevSlideNumber)
			|| (aSlideIndex == FMS_CurrentSlide))
		{			
			aCurrentLeftPos = aSlide.getStyle('left').replace('px','').toInt();
			
			var aTween;			
			aTween = new Fx.Tween(aSlide, {duration: FMS_TransitionDuration});
			
			if (FMS_GoForward) aTween.start('left', (aCurrentLeftPos.toInt() - FMS_Width.toInt()) + 'px');
			else aTween.start('left', (aCurrentLeftPos.toInt() + FMS_Width.toInt()) + 'px');
		}
	});
	
	// Trigger repositioning of slides once the transition has completed (definitely!)
	FMS_ReposPeriodical = FMS_RepositionSlides.periodical(FMS_TransitionDuration + 50);
	
	if(FMS_ShowIndicators)
	{
		$$('#indicators li').set('class', 'inactive');
		$$('#indicators li')[FMS_CurrentSlide].set('class', 'active');
	}
	
	aCurrentSlideElement = FMS_Slides[FMS_CurrentSlide];
	FMS_HideCaption(aCurrentSlideElement);
}

function FMS_SlideNumberWrap(aSlideNumber)
{
	if (aSlideNumber < 0) aSlideNumber = FMS_TotalNumSlides - 1;
	else if (aSlideNumber > FMS_TotalNumSlides - 1) aSlideNumber = 0;
	
	return aSlideNumber;
}

function FMS_RepositionSlides()
{
	$clear(FMS_ReposPeriodical);
	
	// Reassign button events as they may have been disabled
	FMS_AssignButtonEvents();
	//EnableSlideEvents();
	
	// Get the slides that matter
	aNextSlideNumber = FMS_SlideNumberWrap(FMS_CurrentSlide + 1);
	aPrevSlideNumber = FMS_SlideNumberWrap(FMS_CurrentSlide - 1);
	
	// Position them either side of the current slide and display: block;
	FMS_Slides[FMS_CurrentSlide].setStyle('left', '0px');
	FMS_Slides[FMS_CurrentSlide].setStyle('display', 'block');
	FMS_Slides[aPrevSlideNumber].setStyle('left', '-960px');
	FMS_Slides[aPrevSlideNumber].setStyle('display', 'block');
	FMS_Slides[aNextSlideNumber].setStyle('left', '960px');
	FMS_Slides[aNextSlideNumber].setStyle('display', 'block');	
	
	// Hide all the other slides (display: none) and set position behind current slide
	FMS_Slides.each(function(aSlide, aSlideIndex){
		if (   (aSlideIndex != aNextSlideNumber) 
			&& (aSlideIndex != aPrevSlideNumber)
			&& (aSlideIndex != FMS_CurrentSlide))
		{
			aSlide.setStyle('left', FMS_CurrentSlot * FMS_Width + 'px');
			aSlide.setStyle('display', 'none');	
		}
	});
}

function FMS_ShowCaption(aCurrentSlideElementID)
{
	aCaptionElement = '';
	if ($$('#' + aCurrentSlideElementID + ' p.caption').length > 0) aCaptionElement = $(aCurrentSlideElementID).getLast('p.caption');
	
	if (aCaptionElement)
	{
		aCaptionElement.set('tween').tween('bottom', '0px');
	}
}

function FMS_HideCaption(aCurrentSlideElementID)
{
	aCaptionElement = '';
	if ($$('#' + aCurrentSlideElementID + ' p.caption').length > 0) aCaptionElement = $(aCurrentSlideElementID).getLast('p.caption');
	
	if (aCaptionElement)
	{
		aCaptionHeight = aCaptionElement.getStyle('height').replace('px','').toInt();
		aStartBottom = (0-(aCaptionHeight+50)*1) + 'px';
		
		aCaptionElement.set('tween').tween('bottom', aStartBottom);
	}
}

function FMS_DisplayButtons()
{
	$('leftslidebutton').fade(1);
	$('rightslidebutton').fade(1);
}

function FMS_HideButtons()
{
	$('leftslidebutton').fade(0);
	$('rightslidebutton').fade(0);
}

function FMS_InitialiseCaptions()
{
	if ($('leftslidebutton')) $('leftslidebutton').fade(0);
	if ($('rightslidebutton')) $('rightslidebutton').fade(0);
	
	$$('p.caption').each(function(aSlideElem) {
		aCaptionHeight = aSlideElem.getStyle('height').replace('px','').toInt();
		aStartBottom = (0-(aCaptionHeight+50)*1) + 'px';
		
		aSlideElem.setStyle('bottom', aStartBottom);
		aSlideElem.set('opacity', 0.7);
	});
}

function FMS_AssignButtonEvents()
{
	if(FMS_ShowButtons == true)
    {
    	// Remove events first!
    	FMS_RemoveButtonEvents();
    	
		// Assign button event - right
		$('rightslidebutton').addEvent('click', function(event) {
			aCurrentSlideElement = FMS_Slides[FMS_CurrentSlide];
			FMS_HideCaption(aCurrentSlideElement);
			
			FMS_Next(event)
		});
		
		// Assign button event - left
		$('leftslidebutton').addEvent('click', function(event) {
			aCurrentSlideElement = FMS_Slides[FMS_CurrentSlide];
			FMS_HideCaption(aCurrentSlideElement);
			
			FMS_Previous(event);
		});
	}
}

function FMS_RemoveButtonEvents()
{
	if(FMS_ShowButtons == true)
    {
		// Remove button event - right
		$('rightslidebutton').removeEvents('click');
		
		// Remove button event - left
		$('leftslidebutton').removeEvents('click');
	}
}

window.addEvent('domready', function() 
{
	// Initialise a few variables	
	FMS_Slides = $$('#imageslider div.slide');
	FMS_TotalNumSlides = FMS_Slides.length;
	FMS_Width = FMS_Slides[0].getStyle('width').substring(0, FMS_Slides[0].getStyle('width').length - 2);	
	
	// Have we more than one slide to show?
    if(FMS_TotalNumSlides > 1)
    {
    	// do we need to shwo the buttons?
    	if(FMS_ShowButtons == true)
    	{
    		// Build and insert left button
    		aSlideButtonLeftEl = new Element('div', {id: 'leftslidebutton'});
    		aSlideButtonLeftEl.inject($('imagesliderwrapper'), 'top');
			
			// Build and insert right button
    		aSlideButtonRightEl = new Element('div', {id: 'rightslidebutton'});
    		aSlideButtonRightEl.inject($('imagesliderwrapper'), 'top');
    		
    		// Add button behaviours
    		FMS_AssignButtonEvents();
		}
		
		// Do we need to insert the indicators?
    	if(FMS_ShowIndicators == true)
    	{
    		// Build the indicators
    		aIndicatorsListEl = new Element('ul', {id: 'indicators'});
    		aIndicatorsListEl.inject($('imageslider'), 'top');
			
			// Insert an indicator for each slide
	    	FMS_Slides.each(function(aItem, aIndex){
				aIndicatorEl = new Element('li');
				aIndicatorEl.set('class', 'inactive');
				aIndicatorEl.inject(aIndicatorsListEl, 'top');
			});
			$('indicators').getElement('li').set('class', 'active');
			
			// Work out positioning
			aListWidth = FMS_TotalNumSlides * FMS_IndWidth;
    		aTotalWidth = (FMS_Width / 2) - aListWidth;
    		aIndicatorsListEl.setStyle('width', aListWidth);
    		aIndicatorsListEl.setStyle('left', aTotalWidth);
		}
		
		// Initialise everything!
		EnableSlideEvents();	
		FMS_RepositionSlides();
    	if (FMS_DoCannon) FMS_Periodical = FMS_Cannon.periodical(FMainSlideDuration);
		FMS_InitialiseCaptions();
    }
});

function EnableSlideEvents()
{		
	// Add events to the indicators
	if(FMS_ShowIndicators == true)
	{			
		$$('#indicators li').addEvent('click', FMS_JumpTo);
	}
		
	// Add events to each of the slides to show the captions when the mouse hovers over them 
	$('imagesliderwrapper').addEvents({
		mouseover: function(e){
			aTargetClass = e.target.get('class');
			
			if (e.target.getParent().get('tag') == 'a') aCurrentSlideElement = e.target.getParent().getParent().get('id');
			else aCurrentSlideElement = e.target.getParent().get('id');
			
			FMS_ShowCaption(aCurrentSlideElement);
			FMS_DisplayButtons();
		},
		mouseleave: function(e){
			if (e.target.getParent().get('tag') == 'a') aCurrentSlideElement = e.target.getParent().getParent().get('id');
			else aCurrentSlideElement = e.target.getParent().get('id');
			
			FMS_HideCaption(aCurrentSlideElement);
			FMS_HideButtons();
		}
	});
	
	if($$('.videoslide'))
	{
		$$('.videoslide').addEvent('click', function(){
			$clear(FMS_Periodical);
		});
	}
}

function DisableSlideEvents()
{
	if (FMS_ShowIndicators == true) $$('#indicators li').removeEvents('click');

	// Add events to each of the slides to show the captions when the mouse hovers over them 
	$('imagesliderwrapper').removeEvents('mouseover');
	$('imagesliderwrapper').removeEvents('mouseleave');
	
	if ($$('.videoslide')) $$('.videoslide').removeEvents('click');
}
