function showImage(item){
  split = $(item).attr('rel').split('|');
  file = "/images/gallery-large/" + split[0];
  itemCaption = split[1];
  if(split[2]) {
    altText = split[2];
  } else {
    altText = split[1];
  }
  $("#gallery-large").html("<img src=\"" + file + "\" alt=\"" + itemCaption + "\" title=\"" + altText + "\" />");
  $("#caption").html(itemCaption);
  $("#gallery-selector ul li.selected").removeClass("selected");
  $(item).parent().addClass("selected");
}

function showRequest(){
  $("#spinner").show();
}

function processResult(data){
  $("#spinner").hide();
  if(data == "true"){
    $("#email-signup-form").html("<p>Thank you for signing up!</p>");
  } else {
    alert("Your name and a valid email address are required.");
  }
}

$(document).ready(function(){
  $("input#name").focus(function(){
    if(this.value == "NAME"){
      this.value = '';
    }
  });
  $("input#name").blur(function(){
    if(this.value == ""){
      this.value = "NAME";
    }
  });
  $("input#email").focus(function(){
    if(this.value == "EMAIL"){
      this.value = '';
    }
  });
  $("input#email").blur(function(){
    if(this.value == ""){
      this.value = "EMAIL";
    }
  });
  $("#gallery-selector ul li img").click(function(){
    showImage(this);
    $(document).stopTime();
  });
  $(document).everyTime(5000, function(){
    selections = $("#gallery-selector ul li");
    count = selections.length;
    activeIndex = selections.index($("#gallery-selector ul li.selected"));
    if(activeIndex == -1 || activeIndex == (count - 1)){
      showImage($(selections[0]).children("img")[0]);
    } else if(activeIndex >= 0 && activeIndex <= (count - 1)){
      newIndex = activeIndex + 1;
      showImage($(selections[newIndex]).children("img")[0]);
    }
  });
  $("#gallery-selector ul li img").each(function(){
    altText = $(this).attr('rel').split('|')[1];
    $(this).attr('alt',altText);
    $(this).attr('title',altText);
  });
  showImage($("#gallery-selector ul li img")[0]);
  $("#email-signup-form").ajaxForm({
    beforeSubmit:showRequest,
    success:processResult
  });
});
