//*******************************************************************
// Kappa Solutions Ltd. Javascript Library.
// (c) 2000-2009 Kappa Solutions Ltd. except where credited
// 
// Opacity and transition effects used in this class are credited to
// brainerror.net: 
//		http://brainerror.net/scripts/javascript/blendtrans/
//*******************************************************************

var KSL;
if (!KSL) KSL = {};
if (!KSL.Widget) KSL.Widget = {};

//*******************************************************************
// Slide Controller.
//
// This class controls slide movements.
//
//*******************************************************************
KSL.Widget.SlideController = function(element, button, indicator, me, opts) 
{
	this.element = element;			// Must be jQuery filter
	this.button = button;			//     ditto
	this.indicator = indicator;		//     ditto
	this.me = me;					// Variable name

	// Following values can be changed via the opts parameter.
	this.transitionTime = 5000;		// Default is one sec.
	this.resetTime 		= 500;		// Default is half sec.
	this.fadeTime 		= 2500;		// Default is quarter sec.
	this.slideClass = '.slide';
	this.fadeClass = '.fadeOut';
	
	KSL.Widget.SlideController.setOptions(this, opts);
	
	// Initialise slides and indicator
	var slides = $(this.element + ' ' + this.slideClass);
	for (var i = 0; i<slides.length; i++)
	{
		var slide = slides[i];
		if (i > 0)
		{
			$(slide).fadeOut(1);
		}
	} 
	
	this.lastSlide = slides.length-1;
	this.selection = 0;
	//this.moveComplete();
	
	// Add the next slide event handler
	//$(this.button).click(function() {
	//	eval(me + '.moveNext();');
	//});
};

KSL.Widget.SlideController.prototype.moveNext = function()
{
	this.showSlide(this.selection + 1);
};

KSL.Widget.SlideController.prototype.showSlide = function(slideNo)
{

// Show new slide and hide (all) visible ones
	var slides = $(this.element + ' ' + this.slideClass);
	if (slideNo >= slides.length || slideNo < 0) slideNo = 0;
	for (var i = 0; i<slides.length; i++)
	{
		var slide = slides[i];
		if (i == slideNo)
		{
			$(slide).fadeIn(this.fadeTime);
		}
		else
		{
			$(slide).fadeOut(this.fadeTime);
		}
	} 
	this.selection = slideNo;
}

KSL.Widget.SlideController.prototype.moveComplete = function()
{
	// Fade in messages
	$(this.element + ' .message').fadeIn();

	// Update slide indicator. Remove all "on" classes and set the ind for the active slide  
	$(this.indicator + ' li').removeClass('ind-on').addClass('ind-off');
	
	var allinds = $(this.indicator + ' li');
	$(allinds[this.selection]).removeClass('ind-off').addClass('ind-on');

}
KSL.Widget.SlideController.setOptions = function(obj, optionsObj, ignoreUndefinedProps)
{
	if (!optionsObj) return;
	for (var optionName in optionsObj)
	{
		if (ignoreUndefinedProps && optionsObj[optionName] == undefined)
			continue;
		obj[optionName] = optionsObj[optionName];
	}
};


