/**
 * Class that handles the main content area slideshow
 *
 * @todo Cache the jQuery objects
 */
function SlideShow(aInitObj)
{
	/**
	 * Time between slides
	 */
	this.SLIDESHOW_PAUSE_DURATION = 2000;
	
	/**
	 * Transition speed
	 */
	this.SLIDESHOW_TRANSITION_SPEED = 100;
	
	/**
	 * Class members
	 */
	this.objInit = typeof aInitObj == "object" ? aInitObj : {};
	this.bPaused = false;
	this.jqSelContainer = '';
	this.jqControlsContainer = '';
	this.objAnimOptions = {};
	this.jqSelFieldToUpdate = '';
	this.jqSelPausePlay = '';
	
	/**
	 * Initialize the slideshow
	 */
	this.init = function ()
	{
		this.jqSelContainer = aInitObj.jqSelContainer;
		this.jqControlsContainer = aInitObj.jqControlsContainer;
		this.jqSelFieldToUpdate = aInitObj.jqSelFieldToUpdate;
		this.jqSelPauseInputs = aInitObj.jqSelPauseInputs;
		this.jqSelPausePlay = aInitObj.jqSelPausePlay;
		
		this.objAnimOptions = this.objInit;
		
		if (typeof this.objAnimOptions.timeout == "undefined") {
			this.objAnimOptions.timeout = this.SLIDESHOW_PAUSE_DURATION;
		}
		if (typeof this.objAnimOptions.speed == "undefined") {
			this.objAnimOptions.speed = this.SLIDESHOW_PAUSE_DURATION;
		}
		
                if (!($(this.jqSelContainer).get().length)) {
                        return false;
                }

		this.setPauseInputs();
		
                //Show the Controls
                $(this.jqControlsContainer).show();

		//add a click event handler to for the pause/play button
		var self = this;
		$(this.jqSelPausePlay).click(function () {
			self.pausePlay();
			return false;
		});
	};
	
	/**
	 * Starts/resumes(if paused) the slideshow
	 */
	this.start = function ()
	{
		$(this.jqSelContainer).cycle(this.objAnimOptions);
		this.play();
	};
	
	/**
	 * Plays (if paused) or pauses (if playing) the slideshow
	 */
	this.pausePlay = function ()
	{
		//pause/resume the slideshow
		$(this.jqSelContainer).cycle(this.bPaused ? "resume" : "pause");
		
		//update the flag
		this.bPaused = !this.bPaused;
		
		//update the text to indicate the next available action
		if (this.bPaused) {
			$(this.jqSelPausePlay).addClass('paused');
		} else {
			$(this.jqSelPausePlay).removeClass('paused');
		}
	};
	
	/**
	 * Resumes the slideshow
	 */
	this.play = function ()
	{
		//pause/resume the slideshow
		$(this.jqSelContainer).cycle("resume");
		
		//update the flag
		this.bPaused = false;
		
		//update the css class to indicate the next available action
		$(this.jqSelPausePlay).removeClass('paused');
	};
	
	/**
	 * Pauses the slideshow
	 */
	this.pause = function ()
	{
		//pause/resume the slideshow
		$(this.jqSelContainer).cycle("pause");
		
		this.bPaused = true;	//update the flag
		
		//update the text to indicate the next available action
		$(this.jqSelPausePlay).addClass('paused');
	};
	
	/**
	 * Flips to the previous image and pauses the slideshow
	 */
	this.prev = function ()
	{
		$('#' + this.objAnimOptions.prev).click();
	};
	
	/**
	 * Flips to the next image and pauses the slideshow
	 */
	this.next = function ()
	{
		$('#' + this.objAnimOptions.next).click();
	};
	
	/**
	 * Accepts a number of fields and sets their onfocus handlers to pause the slideshow
	 */
	this.setPauseInputs = function ()
	{
		var self = this;
		$(this.jqSelPauseInputs).focus(function () { self.pause(); });
	};
	
	/**
	 * update a hidden field on a form on the same page (with the name of image) anytime the image changes
	 */
	this.updateHiddenField = function (newValue)
	{
		$(this.jqSelFieldToUpdate).val(newValue);
	};
}

