$(document).ready(function() {
  init_events();
  init_window_height();
  init_external_windows();
  init_form_validation();
  init_lightbox();
});

function init_events() {
  $("button, a").each(function() {
    if ($(this).attr("data-expose") == "click") {
      $(this).click(function() {
        $($(this).attr("data-lead")).fadeIn("slow");
        if($(this).attr("data-lead") == "#gallery") {
          $('ul#carousel').roundabout();
        }
        return false;
      });
    } else if ($(this).attr("data-expose") == "move") {
      $(this).mouseover(function() {
        $($(this).attr("data-lead")).fadeIn("slow");
      });
    }
    if ($(this).hasClass("close")) {
      $(this).click(function() {
        $(this).parents(".modal").fadeOut();
        if($(this).attr("data-lead") == "#contact") {
          setTimeout("focus_input()", 1000);
        }
        return false;
      });
    }
  });
}
function init_window_height() {
  $("#site_wrap").height($(document).height());
}

/* Scans the DOM for links with the
* rel="external" attribute and
* sets a click event to open said
* link in a new window. */
function init_external_windows() {
  $("a[rel='external']").click(function() {
    if ($(this).parent().hasClass("roundabout-in-focus")) {
      window.open($(this).attr('href'), "new_window", "resizable=1,width="+ $(window).width()-20 + ",height=620,location=no,menubar=no,status=no,titlebar=np,toolbar=no");
      return false;
    }
  });
}
function focus_input() {
  $("input[name='realname']").focus();
}

function init_form_validation() {
  $("form input").keypress(function(event) {                                                                                        
    var code = (event.keyCode ? event.keyCode : event.which);
    if(code == 13) {
      event.preventDefault();
      $("button").last().trigger("click");
    }   
  }); 

  $("form button").click(function() {
    var errors = false;
    $("form input").each(function() {
      if ($(this).is(":visible")) {
        if($(this).attr("name") == "email") {
          if(!valid_email($(this).val())) {
            $("#errors").html("<p>This is not a valid email address.");
            errors = true;
            return false;
          }
        } else {
          if($(this).val() == "") {
            $("#errors").html("<p>The form is missing something.</p>");
            errors = true;
            return false;
          }
        }
      }
    });
    if (!errors) {
      $("form").submit();
    } else {
      $("#errors").slideDown();
      setTimeout('$("#errors").slideUp("slow")', 1500);
    }
  });
}

function valid_email(emailAddress) {
  var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
  return pattern.test(emailAddress);
}

function init_lightbox() {
  $(".colorbox").colorbox({width:"700px", height:"500px"});
}

