/** <!---
	$Id: dsp_create_button_controls_bar.js 5731 2010-12-21 15:03:53Z mlesko $
	$HeadURL: http://vm-seprojects/repos/siteexecutive/branches/dev/2011-ep1/server/www/se/js/mod/imagerotator/dsp_create_button_controls_bar.js $

	@desc(
	  Javascript functions used to handle button events
	)@

	@parameters(

	)@

	@notes (

	)@

	@author	( Bryn Davies )@
	@date	( 12/11/2007 )@
	@revisions (
		21 July 2004 - Scott Springer: modified call to startOrChangeSlideshow()
		so that it's passing 2 values instead of 1.
		12/11/2007 - bdavies: moved from dsp_button_control_script.cfm
	)@
 ---> **/

/**<!---
 *  buttonControl
 *     parameters:
 *         buttonElement (object): reference to a button object
 *         thisCommand (string): a string containing the command represented by the button (i.e. "First", "Last", "Auto", etc.)
 *         instance (string): the string representation of the module ID that we're operating on
 *     returns:
 *         none
 *     purpose:
 *         event handler for the buttons in the button control bar on the manually controlled slideshows
 *  --->
 */
function buttonControl(buttonElement, thisCommand, thisInstance) {
  var controlArray = slideshows[thisInstance];

  var firstButton = document.getElementById("FirstBtn" + thisInstance);
  var prevButton = document.getElementById("PrevBtn" + thisInstance);
  var autoButton = document.getElementById("AutoBtn" + thisInstance);
  var stopButton = document.getElementById("StopBtn" + thisInstance);
  var nextButton = document.getElementById("NextBtn" + thisInstance);
  var lastButton = document.getElementById("LastBtn" + thisInstance);

  // Handle all of the buttons EXCEPT the "Auto" button
  if (thisCommand != "Auto") {

    // Handle the "Stop" command and turn the buttons back on depending on where the image counter is for this instance
    if (thisCommand == "Stop") {
      clearTimeout(controlArray.timerObject);
      buttonHandlers["Stop" + thisInstance].disable(stopButton, "Stop", thisInstance);
      buttonHandlers["Auto" + thisInstance].enable(autoButton, "Auto", thisInstance);
    }

    // Determine which button was pressed
    switch (thisCommand) {
      case "First":
        controlArray.counter = 0;
        break;
      case "Last":
        controlArray.counter = controlArray.images.length - 1;
        break;
      case "Prev":
        controlArray.counter -= 1;
        if (controlArray.counter < 0) {
          controlArray.counter = 0;
        }
        break;
      case "Next":
        controlArray.counter += 1;
        if (controlArray.counter >= controlArray.images.length) {
          controlArray.counter = controlArray.images.length - 1;
        }
        break;
    }

    // Handle the next button's activity status
    if (controlArray.counter == (controlArray.images.length - 1)) {
      buttonHandlers["Next" + thisInstance].disable(nextButton, "Next", thisInstance);
    } else {
      buttonHandlers["Next" + thisInstance].enable(nextButton, "Next", thisInstance);
    }

    // Handle the previous button's activity status
    if (controlArray.counter > 0) {
      buttonHandlers["Prev" + thisInstance].enable(prevButton, "Prev", thisInstance);
    } else {
      buttonHandlers["Prev" + thisInstance].disable(prevButton, "Prev", thisInstance);
    }

    // Handle the first button's activity status
    if (controlArray.counter > 0) {
      buttonHandlers["First" + thisInstance].enable(firstButton, "First", thisInstance);
    } else {
      buttonHandlers["First" + thisInstance].disable(firstButton, "First", thisInstance);
    }

    // Handle the last button's activity status
    if (controlArray.counter == (controlArray.images.length - 1)) {
      buttonHandlers["Last" + thisInstance].disable(lastButton, "Last", thisInstance);
    } else {
      buttonHandlers["Last" + thisInstance].enable(lastButton, "Last", thisInstance);
    }

    // replace the image being displayed
    if (thisCommand != "Stop") {
      switchImage(thisInstance);
    }
  } else {
    // Turn on the slideshow
    buttonHandlers["Next" + thisInstance].disable(nextButton, "Next", thisInstance);
    buttonHandlers["Prev" + thisInstance].disable(prevButton, "Prev", thisInstance);
    buttonHandlers["First" + thisInstance].disable(firstButton, "First", thisInstance);
    buttonHandlers["Last" + thisInstance].disable(lastButton, "Last", thisInstance);
    buttonHandlers["Auto" + thisInstance].disable(autoButton, "Auto", thisInstance);
    buttonHandlers["Stop" + thisInstance].enable(stopButton, "Stop", thisInstance);
    startOrChangeSlideshow(thisInstance, false);
  }
}



/**<!---
 *  setSrc
 *     parameters:
 *         ImgElement (object): reference to a button object
 *         buttonFunction (string): a string containing the command represented by the button (i.e. "First", "Last", "Auto", etc.)
 *         thisInstance (string): the string representation of the module ID that we're operating on
 *     returns:
 *         none
 *     purpose:
 *         "turns off" the image rollover effect by using the base image as the rollover image
 *  --->
 */
function setSrc(ImgElement, buttonFunction, thisInstance) {
  buttonInstance = buttonFunction + thisInstance;
  buttonHandler = buttonHandlers[buttonInstance];

  ImgElement.src = buttonHandler.currentRolloverImage;
}



/**<!---
 *  resetSrc
 *     parameters:
 *         ImgElement (object): reference to a button object
 *         buttonFunction (string): a string containing the command represented by the button (i.e. "First", "Last", "Auto", etc.)
 *         thisInstance (string): the string representation of the module ID that we're operating on
 *     returns:
 *         none
 *     purpose:
 *         turns the image rollover effect back on
 *  --->
 */
function resetSrc(ImgElement, buttonFunction, thisInstance) {
  buttonInstance = buttonFunction + thisInstance;
  buttonHandler = buttonHandlers[buttonInstance];

  ImgElement.src = buttonHandler.baseImage;
}


/**<!---
 *  enableButton
 *     parameters:
 *         thisButton (object): reference to an HTML form button object
 *         buttonFunction (string): a string containing the command represented by the button (i.e. "First", "Last", "Auto", etc.)
 *         thisInstance (string): the string representation of the module ID that we're operating on
 *     returns:
 *         none
 *     purpose:
 *         enables an HTML button
 *  --->
 */
function enableButton(thisButton, buttonFunction, thisInstance) {
  thisButton.disabled = false;
}


/**<!---
 *  enableImageButton
 *     parameters:
 *         thisButton (object): reference to an image element that we are using as a button in the button control bar
 *         buttonFunction (string): a string containing the command represented by the button (i.e. "First", "Last", "Auto", etc.)
 *         thisInstance (string): the string representation of the module ID that we're operating on
 *     returns:
 *         none
 *     purpose:
 *         enables the image button by resetting the onclick function to the event handler and enabling the over image
 *  --->
 */
function enableImageButton(thisButton, buttonFunction, thisInstance) {
  buttonInstance = buttonFunction + thisInstance;
  buttonHandler = buttonHandlers[buttonInstance];

  thisButton.onclick = function() { buttonHandler.onclick(thisButton, buttonFunction, thisInstance); }
  if (buttonHandler.rollover != null) {
    buttonHandler.currentRolloverImage = buttonHandler.rolloverImage;
  }
}


/**<!---
 *  disableButton
 *     parameters:
 *         thisButton (object): reference to an HTML form button object
 *         buttonFunction (string): a string containing the command represented by the button (i.e. "First", "Last", "Auto", etc.)
 *         thisInstance (string): the string representation of the module ID that we're operating on
 *     returns:
 *         none
 *     purpose:
 *         disables an HTML button
 *  --->
 */
function disableButton(thisButton, buttonFunction, thisInstance) {
  thisButton.disabled = true;
}


/**<!---
 *  disableImageButton
 *     parameters:
 *         thisButton (object): reference to an image element that we are using as a button in the button control bar
 *         buttonFunction (string): a string containing the command represented by the button (i.e. "First", "Last", "Auto", etc.)
 *         thisInstance (string): the string representation of the module ID that we're operating on
 *     returns:
 *         none
 *     purpose:
 *         disables the image button by setting the onclick function to null and disabling the over image
 *  --->
 */
function disableImageButton(thisButton, buttonFunction, thisInstance) {
  buttonInstance = buttonFunction + thisInstance;
  buttonHandler = buttonHandlers[buttonInstance];

  thisButton.onclick = null;
  if (buttonHandler.rollover != null) {
    buttonHandler.currentRolloverImage = buttonHandler.baseImage;
  }
}

