$(document).ready(function() {

  $('.text-field').each(function() {
      var the_default_value = this.value;
      $(this).focus(function() {
          if(this.value == the_default_value) {
              this.value = '';
          }
      });
      $(this).blur(function() {
          if(this.value == '') {
              this.value = the_default_value;
          }
      });
  });


  $('.sample-video-wrapper li .video-link').click(function() {
    $('#video-player-wrapper').slideDown();
    $('#videos').cycle('stop');
    var slideTarget = $(this).attr("id");
    creatSlideshow(slideTarget.slice(-1));
  });


  $('span.close-button').click(function(){
    $('#videos').cycle('stop');
    $('#video-player-wrapper').slideUp();
  });


  $("#contact-form .button").click(function() {
    clearErrors();
    validateContactForm();
//    successfulProcessing();
    return false;
  });


});



function creatSlideshow(x) {

// Instatiate a new slideshow at "#videos".
  $('#videos').cycle({
    fx: 'fade',
    timeout: 0,
    speed: 300,
    startingSlide: x,
    next: '.video-nav span.next-vid',
    prev: '.video-nav span.prev-vid'
  });

}


function validateContactForm() {

  var name = $("input#full-name").val();
  var company = $("input#company").val();
  var email = $("input#email").val();
  var phone1 = $("input#phone1").val(); 
  var phone2 = $("input#phone2").val(); 
  var phone3 = $("input#phone3").val(); 
  var phone = phone1 + phone2 + phone3;
  var contactPref = $('input[name=response-time-request]:checked').val();
  var contactSource = $('input#email-source').val();


  if (name == "Full Name") {
    showError("Please enter your name.", "name");
    return false;
  } else if (company == "Company") {
    showError("Please enter your company name.", "company");
    return false;
  } else if (email == "Email") {
    showError("Please enter your email.", "email");
    return false;
  } else if (phone == "Phone #") {
    showError("Please enter your phone number.", "phone1");
    return false;
  } else if (contactPref == "" || !contactPref) {
    showError("Please select your preferred contact time.", "contactPref");
    return false;
  } else {

  var dataString = 'full-name='+ name + '&company='+ company + '&email=' + email + '&phone1=' + phone1 + '&phone2=' + phone2 + '&phone3=' + phone3 + '&response-time-request=' + contactPref + '&email-source=' + contactSource;

    $.ajax({
      type: "POST",
      url: "http://pixelfish.com/SendMail/NewForm.aspx",
      data: dataString,
      success: successfulProcessing()
    });

  }

}


function clearErrors() {
  $('.error-label').hide();
  $('.error-label').empty();
}

function showError(errorText, errorTarget) {
  $('.error-label').fadeIn(200).append(errorText);
  $("input#" + errorTarget).focus();
}

function successfulProcessing() {
  $("#contact-form-box .contact-wrapper").fadeOut(200);
  $("#successful-send").fadeIn(200);
  $("body").append('<img src="http://aaa.tradatracker.net/track/conv.gif?advertiser=00e26af6ac3b1c1c49d7c3d79c60d000&action=LEADSUBMIT" />');
}
