$(document).ready(function() {
  //$('#items > .item').setupItems();
  $('#items > .item').equalHeights();
  $('#items > .item.print, #home .small-item.print').fancybox({
		'overlayShow'    : false
		//'zoomSpeedIn'  : 500,
		//'zoomSpeedOut' : 500
	});
  $('#contact form').contactForm();
});

$.fn.equalHeights = function(minHeight, maxHeight) {
  var tallest = (minHeight) ? minHeight : 0;
  $(this).each(function() {
    if($(this).height() > tallest) {
      tallest = $(this).height();
    }
  });
  if ((maxHeight) && tallest > maxHeight) tallest = maxHeight;
  return $(this).each(function() {
    $(this).height(tallest);
    //$(this).height(tallest).css("overflow","auto");
  });
}

$.fn.setupItems = function() {
  $(this).each(function() {
    if ($.browser.msie && $.browser.version == 6.0) {
      $(this).bind("mouseover", function(e) {
        $(this).addClass("item-hover");
      });
      $(this).bind("mouseout", function(e) {
        $(this).removeClass("item-hover");
      });
    }
    $(this).bind("click", function(e) {
      window.location = $(this).attr("href");
    });
  });
}

$.fn.contactForm = function() {
  $(this).submit(function() {
    if (validate(this)) {
      this.removeChild(this.required);
      this.removeChild(this.validate);
      this.submit();
    }
    return false;
  });
  var validate = function(form) {
    var formLabels = {};
    var requiredFields = {};
    var validateFields = {};
    $(form).find("label").each(function() {
      formLabels[this.htmlFor] = $(this).text();
    });
    $.each(form.required.value.split(","), function() {
      requiredFields[form[this].id] = form[this];
    });
    $.each(form.validate.value.split(","), function() {
      validateFields[form[this].id] = form[this];
    });
    var isBlank = false;
    var blankTxt = "Please fill in the following required fields:\n";
    var isInvalid = false;
    var invalidTxt = "";
    $.each(requiredFields, function(id, input) { //for (var i in requiredFields) {
      if (!validBlank(input.value)) {
        blankTxt += formLabels[id] + "\n";
        isBlank = true;
      }
    });
    $.each(validateFields, function(id, input) { //for (var i in validateFields) {
      if (validBlank(input.value)) {
        if (id.indexOf("email") > -1) {
          if (!validEmail(input.value)) {
           invalidTxt += "Please enter in a valid email address.";
           isInvalid = true;   
          }
        }
        if (id.indexOf("phone") > -1) {
          if (!validPhone(input.value)) {
           invalidTxt += "Please enter in a valid US phone number.";
           isInvalid = true;   
          }
        }
      }
    });
    var errorTxt = "";
    if (isBlank) {
      errorTxt += blankTxt + "\n";
    }
    if (isInvalid) {
      errorTxt += invalidTxt;
    }
    if (isBlank || isInvalid) {
      alert(errorTxt);
      return false;
    }
    return true;
  }
  var validBlank = function(input) {
    if (input == "" || input.match(/^[\n\f\r\t\v\s]+$/)) {
      return false;
    }
    else {
      return true;
    }
  }
  var validEmail = function(input) {
    var validaddress = /^((\w+).?(\w+))+@[\w\-]+\.\w+/i;
    var result = input.match(validaddress);
    if (result == null) {
      return false;
    }
    else {
      return true;
    }
  }
  var validPhone = function(input) {
    var validphone1 = /^\d{3}(-|\.|\s)\d{3}(-|\.|\s)\d{4}$/i;
    var validphone2 = /^\d{10}$/i;
    var result1 = input.match(validphone1);
    var result2 = input.match(validphone2);
    if (result1 == null && result2 == null) {
      return false;
    }
    else {
      return true;
    }
  }
  return false;
}
