var front;
var end;

function scroll_images()
{
  // time for each picture to move one space in milliseconds
  var speed = 1800;

  // distance each picture moves in that time (px)
  var dist = 95;

  // get the first element
  var first = $("#all_scroll_container").children().eq(0);

  // get margin value, used to continue animation after pause
  var marginLeft = first.css("margin-left");

  // remove the px from margin left
  marginLeft = parseFloat(marginLeft.substr(0, marginLeft.length - 2));
  if(isNaN(marginLeft)) marginLeft = 0;

  // work out how much time is left in the animation
  var animationTime = ((dist + (parseFloat(marginLeft))) / dist) * speed;

  first.animate({marginLeft: -dist}, animationTime, "linear", function()
  {
    $(this).css("margin-left", "0px");
    $(this).appendTo($("#all_scroll_container"));
    scroll_images();
  });
}


$(document).ready(function()
{
  scroll_images();
  
  $(".scroll_image").mouseover(function()
  {
    $("#all_scroll_container").children().stop();
  });
  
  $(".scroll_image").mouseout(function()
  {
    scroll_images();
  });
            
  $("#show-link").attr("href", "javascript:");
            
  // more details bit
  $("#show-link").click(function()
  {
    if($(this).text() == "More Details")
    {
      $("#more-details").css("height","auto");
      $(this).text("Hide Details");
    }
    else
    {
      $("#more-details").css("height","20px");
      $(this).text("More Details");
    }
  });
  
  // -- BMI form check -- \\
  $("#bmi-form").submit(function()
  {
    // check all input fields
    if($("#first").val().length == 0 || $("#last").val().length == 0)
    {
      alert("Please enter your full name");
      return false;
    }
    else
    {
      var email = $("#email").val();
      var dot = email.lastIndexOf(".");
      var at = email.indexOf("@");
      
      if(email.length == 0 || dot < 0 || at < 0 || dot < at)
      {
        alert("Please enter a valid email address");
        return false;
      }
      else
      {
        if($("#height").val() == 0)
        {
          alert("Please choose your height");
          return false;
        }
        else
        {
          var weight = $("#weight").val();
          if(weight == "" || weight <= 0)
          {
            alert("Pleaes enter your weight");
            return false;
          }
        }
      }
    }
  });
  
  // -- small BMI form check -- \\
  $("#small-bmi-form").submit(function()
  {
    // check all input fields
    if($("#small-first").val().length == 0 || $("#small-first").val() == "First Name" || $("#small-last").val().length == 0 || $("#small-last").val() == "Last Name")
    {
      alert("Please enter your full name");
      return false;
    }
    else
    {
      var email = $("#small-email").val();
      var dot = email.lastIndexOf(".");
      var at = email.indexOf("@");

      if(email.length == 0 || dot < 0 || at < 0 || dot < at)
      {
        alert("Please enter a valid email address");
        return false;
      }
      else
      {
        if($("#small-height").val() == 0)
        {
          alert("Please choose your height");
          return false;
        }
        else
        {
          var weight = $("#small-weight").val();
          if(weight == "" || weight <= 0)
          {
            alert("Pleaes enter your weight");
            return false;
          }
        }
      }
    }
  });
  
  // new home page - small form
  $("#small-bmi-form input").css({color: "#666"});
  
  $("#small-bmi-form input").click(function()
  {
    if(!$(this).hasClass("submit-button")) $(this).val("").css({color: "#000"});
  });
  
  $("#small-bmi-form input.submit-button").css({color: "#000"});
});
