// JavaScript Document
// Slideshow by Mark White (c) 2009 Mark White/Creative Media Disciples
// Requires JQuery

// Assumed set is as follows

// div tag set to width/height of one slide - this will be the "window"
// div tag inside that holds all slides images, this will be set to the width of all slides and will be relatively positioned so we have a top/left. This will be repositioned to be the basis of the slide carousel
// img tags inside this div, each a slide, with alt text for caption
// div tag (wherever) that is the caption which will be set into a paragraph. It's default should be set to the 1st slide


// Variables required in setting document

// slideCount - number of slides
// slideWidth - width of slides (should be generic across all and in pixels)
// slideListName -  slides holding layer id
// currentSlide - current slide number
// slideButtonClass - buttons class for initial assignment of click action
// slideAlt - true/false whether we have a caption area
// slideAltName - slides alt text or caption areas text
// slidesNamePrefix - slides prefix i.e. xyzButton(#number) with xyz being the prefix. Also applies to alt/caption which should be just the slide prefix and relevant slide number and set in it's alt tag
// autoPlay - true/false whether the automatic slideshow is on
// timer - Seconds to display slide for before moving on

var slideTimer;	 // Slide Timer

function initSlides()
{
			// Initial onload calling of activity;
		$("."+slideButtonClass).click(function(e){
			moveSlide($(this));
			return false;
		});

		clearTimeout(slideTimer);

		// Autoplay Slides options
		if(autoPlay)
			 slideTimer = setTimeout(function() {moveSlide();}, (1000*timer));
}
	
	
function moveSlide(slideButtonName){

	var SlideNumber=null;
	var altText=null;

	// If no button name provided then attempt to move to next slide (unless is last slide and set to 1st slide)
	// We use the prefix option here to create the id of the relevant button then carry out the same functions as before
	if(slideButtonName==null)			
	{
		if(currentSlide+1>slideCount)
			slideButtonName=slidesNamePrefix+"Button1";
		else
			slideButtonName=slidesNamePrefix+"Button"+(currentSlide+1);
	}
	else
	{
		// slide was clicked for
		slideButtonName=$(slideButtonName).attr("id");
		autoPlay = false; // since it's clicked the users taking control so turn of autoplay slideshow
	}
			


	// Check the text
	if(IsNumeric($("#"+slideButtonName).text()))
		SlideNumber=$("#"+slideButtonName).text();

	// Check the alt text - gives us a second option
	if(SlideNumber==null && IsNumeric($("#"+slideButtonName).attr("alt")))
		SlideNumber=$("#"+slideButtonName).attr("alt");

	if(slideAlt)
		altText=$("#"+slidesNamePrefix+SlideNumber).attr("alt");

	//move right to left - Actual Movement - dependant on if the same slide is clicked
	if(currentSlide!=SlideNumber)
	{
		// If we have alt text
		if(altText!=null)
		{
			$("#"+slideAltName+" p").css({opacity : 0});
			$("#"+slideAltName).animate({"top": "-="+$("#"+slideAltName).css("height"),"opacity" : 0},"fast").animate({"top": "+="+$("#"+slideAltName).css("height"), "opacity" : 1},"slow");
			$("#"+slideAltName+" p").html(altText);
			$("#"+slideAltName+" p").animate({opacity : 1},"fast");
		}
		
		var newSlidePos=slideWidth-(SlideNumber*slideWidth); // Create new slide position
		$("#"+slideListName).animate({"left" : newSlidePos+"px"},"slow");

		currentSlide=parseInt(SlideNumber);
	}

	// Auto Slideshow
	clearTimeout(slideTimer);

	if(autoPlay)
	 slideTimer = setTimeout(function() {moveSlide();}, (1000*timer));
};	

// Checking for numeric value
function IsNumeric(sText)
{
 var ValidChars = "0123456789.";
 var IsNumber=true;
 var Char;

 for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			 {
			 IsNumber = false;
			 }
		}
 return IsNumber;
 
};

