// Change the image to the assign number
function change_image(desired_image) {
  // Get the current image number
  var current_image = parseInt(jQuery('.main_image img').attr('name'));

  // Change to the new image
  jQuery('.main_image img').attr('src', 'images/image_' + desired_image + '.jpg');
  jQuery('.main_image img').attr('name', desired_image);

  // Change the current class on the span
  jQuery('.bar span').removeClass('current');
  jQuery('.bar span.image_' + desired_image).addClass('current');
}

// Go to the previous image
function previous(total_images) {
  // Get the current image number
  var current_image = parseInt(jQuery('.main_image img').attr('name'));
  var previous_image = ((current_image - 1) <= 0) ? total_images : (current_image - 1);

  // Change to the new image
  jQuery('.main_image img').attr('src', 'images/image_' + previous_image + '.jpg');
  jQuery('.main_image img').attr('name', previous_image);

  // Change the current class on the span
  jQuery('.bar span').removeClass('current');
  jQuery('.bar span.image_' + previous_image).addClass('current');
}

// Go to the next image
function next(total_images) {
  // Get the current image number
  var current_image = parseInt(jQuery('.main_image img').attr('name'));
  var next_image = ((current_image + 1) > total_images) ? 1 : (current_image + 1);

  // Change to the new image
  jQuery('.main_image img').attr('src', 'images/image_' + next_image + '.jpg');
  jQuery('.main_image img').attr('name', next_image);

  // Change the current class on the span
  jQuery('.bar span').removeClass('current');
  jQuery('.bar span.image_' + next_image).addClass('current');
}

// Pull in a custom image
function custom_change_image(desired_image) {
  // Change to the new image
  jQuery('.main_image img').attr('src', desired_image);
  jQuery('.main_image img').attr('name', 0);

  // Change the current class on the span
  jQuery('.bar span').removeClass('current');
}

// Show all points on the map
function showAll() {
  jQuery('.categories li').removeClass('current');
  jQuery('.categories li:first').addClass('current');
  jQuery('.locations ul').show();
  jQuery('.locations ul li').removeClass('current');
  jQuery('.points ul').show();
  jQuery('.points ul li').removeClass('current');
}

// Check and display the correct content in the input boxes
function check(field, value) {
  var checked = false;

  // Clear out the value if it matches the load value
  if(jQuery('input.' + field).val() == value && !checked) {
    jQuery('input.' + field).val('');
    checked = true;
  }

  // Clear out the value if it is blank
  if(jQuery('input.' + field).val() == '' && !checked) {
    jQuery('input.' + field).val(value);
    jQuery('input.' + field).css('color', '#c00');
    checked = true;
  }
}

// Check and display the correct content in the input boxes
function check_textarea(field, value) {
  var checked = false;

  var error_color = '#c00';
  var text_color = '#58585A';

  // Clear out the value if it matches the load value
  if(jQuery('textarea.' + field).text() == value && !checked) {
    jQuery('textarea.' + field).text('');
    checked = true;
  }

  // Clear out the value if it is blank
  if(jQuery('textarea.' + field).val() == '' && !checked) {
    jQuery('textarea.' + field).text(value);
    jQuery('textarea.' + field).css('color', error_color);
    jQuery('textarea.' + field).css('border-color', error_color);
    checked = true;
  }
}

// Validate form
function validate_form() {

  var return_value = true;

  var error_color = '#c00';
  var text_color = '#58585A';

  if(jQuery('input.name').val() == 'Name') {
    jQuery('input.name').css('color', error_color);
    jQuery('input.name').css('border-color', error_color);
    return_value = false;
  } else {
    jQuery('input.name').css('color', text_color);
    jQuery('input.name').css('border-color', text_color);
  }

  if(jQuery('input.email').val() == 'email') {
    jQuery('input.email').css('color', error_color);
    jQuery('input.email').css('border-color', error_color);
    return_value = false;
  } else {
    jQuery('input.email').css('color', text_color);
    jQuery('input.email').css('border-color', text_color);
  }

  if(jQuery('input.phone').val() == 'Phone Number') {
    jQuery('input.phone').css('color', error_color);
    jQuery('input.phone').css('border-color', error_color);
    return_value = false;
  } else {
    jQuery('input.phone').css('color', text_color);
    jQuery('input.phone').css('border-color', text_color);
  }

  if(jQuery('input.school').val() == 'Subject') {
    jQuery('input.school').css('color', error_color);
    jQuery('input.school').css('border-color', error_color);
    return_value = false;
  } else {
    jQuery('input.school').css('color', text_color);
    jQuery('input.school').css('border-color', text_color);
  }

  if(jQuery('textarea.message')) {
    if(jQuery('textarea.message').text() == 'Message') {
      jQuery('textarea.message').css('color', error_color);
      jQuery('textarea.message').css('border-color', error_color);
      return_value = false;
    } else {
      jQuery('textarea.message').css('color', text_color);
      jQuery('textarea.message').css('border-color', text_color);
    }
  }

  return return_value;
}

// DOM Ready functions
jQuery(document).ready(function() {

  // Add hover abilities to the submit buttom
  jQuery('.submit input').hover(function() {
    jQuery('.submit input').css('background-position', 'left bottom');
  },function() {
    jQuery('.submit input').css('background-position', 'left top');
  });
})