var itemList = [], 
    mainCarousel;

function itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, itemList.length);
    carousel.add(i, getItemHTML(itemList[idx - 1]));
};

function itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

/**
 * Item html creation helper.
 */
function getItemHTML(item) {
    return item.html;
};

function buildItems(carousel, state) {
  mainCarousel = carousel;
  $(".carousel-item").each(function () {
    itemList.push({ html: $(this).get(0) });
  });
  bindMouseEvents(carousel);
};

function getSrcAttributes(image) {
  var src = $(image).attr("src"),
      filename = src.substring(0, src.indexOf(".jpg")),
      ext = src.substring(src.indexOf(".jpg"));
  return { src: src, filename: filename, ext: ext };
};

function bindMouseEvents(carousel) {
  $(".jcarousel-item img").livequery(function () {
    $(this).hover(function () {
    carousel.stopAuto();
  }, function () {
    carousel.startAuto();
  })});
};

var cache = [];
// Arguments are image paths relative to the current page.
$.preLoadImages = function() {
  var args_len = arguments.length;
  for (var i = args_len; i--;) {
    var cacheImage = document.createElement('img');
    cacheImage.src = arguments[i];
    cache.push(cacheImage);
  }
};

function fadeOutNav() {
  $(".jcarousel-prev").fadeOut("slow");
  $(".jcarousel-next").fadeOut("slow");
}

function fadeInNav() {
  $(".jcarousel-prev").fadeIn("slow");
  $(".jcarousel-next").fadeIn("slow");
}

$(document).ready(function() {  
  $.preLoadImages("/images/buttons/blue_off.jpg", "/images/buttons/blue_on.jpg", "/images/buttons/green_off.jpg", "/images/buttons/green_on.jpg",
      "/images/buttons/light_blue_off.jpg",
      "/images/buttons/light_blue_on.jpg",
      "/images/buttons/contact_off.jpg",
      "/images/buttons/contact_on.jpg",
      "/images/buttons/send_on.jpg",
      "/images/buttons/send_off.jpg",
      "/images/buttons/cancel_off.jpg",
      "/images/buttons/cancel_on.jpg");
  
  
  carousel = $('#carousel').jcarousel({
    auto: 60,
    wrap: "circular",
    scroll: 1,
    animation: "slow",
    initCallback: buildItems,
    itemVisibleInCallback: {onBeforeAnimation: itemVisibleInCallback},
    itemVisibleOutCallback: {onAfterAnimation: itemVisibleOutCallback}
  });
  
  // Rollovers for site buttons.
  $(".button").livequery(function () {
    $(this).find("img").hover(function () {
      this.src = this.src.replace("_off","_on");
     }, function () {
      this.src = this.src.replace("_on","_off");
     });
  });
  
  // Click activation for contact button.
  $("#contact-button").livequery(function () {
    $(this).find("img").mousedown(function () {
      this.src = this.src.replace("_off","_on");
    });
    $(this).find("img").mouseup(function () {
      this.src = this.src.replace("_on","_off");
    });
    $(this).click(function () {
      $("#contact-form").fadeIn("slow");
      fadeOutNav();
    });
  });
  
  // Click activation for cancel button.
  $("#cancel").livequery(function () {
    $(this).click(function () {
      $(this).parents(".hover-page").fadeOut("slow");
      if ($(".hover-page:visible").length == 1) {
        fadeInNav();
      }
    });
  });
  
  // Click activation for cancel button.
  $("#submit").livequery(function () {
    $(this).click(function () {
      $("form").submit();
    });
  });
  
  // Click activation for learn more button.
  $("#learn-more-button").livequery(function () {
    $(this).click(function () {
      $("#learn-more").fadeIn("slow");
      fadeOutNav();
    });
  });

  $("#wrapper").swipe({
    swipeLeft: function () { mainCarousel.next(); },
    swipeRight: function () { mainCarousel.prev(); }
  });
});
