/******************************************************************************
* Handles image swapping.
******************************************************************************/
var ImageSwap =
{
  MouseOver: "_lit",
  MouseDown: "_pressed",
  CachedImages: [],

  /****************************************************************************
  * Preloads images for all elements with the 'ImageSwap' CSS class.
  ****************************************************************************/
  _init: function()
  {
    $(".ImageSwap").each(function()
    {
      var targetImage = $(this);
      if (targetImage.prop("tagName").toLowerCase() != "img")
        targetImage = targetImage.find("img");

      //Make sure an image is assigned to the element
      var imageURL = targetImage.prop("src");
      if (imageURL.length < 1)
        return;

      //Get the extension of the image  
      var imageExtension = imageURL.substr(imageURL.lastIndexOf("."));

      //Preload the other images
      var mouseOverImage = new Image();
      mouseOverImage.src = imageURL.substr(0, imageURL.lastIndexOf(".")) + ImageSwap.MouseOver + imageExtension;
      ImageSwap.CachedImages.push(mouseOverImage);

      var mouseDownImage = new Image();
      mouseDownImage.src = imageURL.substr(0, imageURL.lastIndexOf(".")) + ImageSwap.MouseDown + imageExtension;
      ImageSwap.CachedImages.push(mouseDownImage);
    });

    this.wireEvents();
  },

  /****************************************************************************
  * Swaps the images for the four different mouse events to fully simulate a
  * button being pressed.
  ****************************************************************************/
  swapImage: function (e)
  {
    var targetImage = $(this);
    if (targetImage.prop("tagName").toLowerCase() != "img")
      targetImage = targetImage.find("img");

    //Get the extension of the image  
    var imageURL = targetImage.prop("src");
    var imageExtension = imageURL.substr(imageURL.lastIndexOf("."));

    switch (e.type)
    {
      case "mouseover":
        imageURL = imageURL.replace(ImageSwap.MouseOver, "");
        imageURL = imageURL.replace(ImageSwap.MouseDown, "");
        targetImage.prop("src", imageURL.substr(0, imageURL.lastIndexOf(".")) + ImageSwap.MouseOver + imageExtension);
        break;
      case "mousedown":
        targetImage.prop("src", imageURL.replace(ImageSwap.MouseOver, ImageSwap.MouseDown));
        break;
      case "mouseup":
        targetImage.prop("src", imageURL.replace(ImageSwap.MouseDown, ImageSwap.MouseOver));
        break;
      case "mouseout":
        targetImage.prop("src", imageURL.replace(ImageSwap.MouseOver, ""));
        break;
    }
  },

  /****************************************************************************
  * Wire up event-handlers
  ****************************************************************************/
  wireEvents: function()
  {
    $(".ImageSwap").mouseover(this.swapImage);
    $(".ImageSwap").mousedown(this.swapImage);
    $(".ImageSwap").mouseup(this.swapImage);
    $(".ImageSwap").mouseout(this.swapImage);
  },
};

$(document).ready(function ()
{
  ImageSwap._init();
});

