$(document).ready(function(){
  
  // Front Page
  function setup_home_buttons() {
    $(".button_previous.bhome").click(function(e) {
      e.preventDefault();
      var prev = current_video-1;
      if(prev < 0) {
        prev = videos.length-1;
      }
      $('#video').html(videos[prev]);
      current_video = prev;
      setup_home_buttons();
    });
    
    $(".button_next.bhome").click(function(e) {
      e.preventDefault();
      var next = current_video+1;
      if(next >= videos.length) {
        next = 0;
      }
      $("#video").html(videos[next]);
      current_video = next;
      setup_home_buttons();
    });
  }
  setup_home_buttons();
  
  // Upload Video Form
	$('form#uploadVideo').submit(function(e){
		// check fields
		var emailRegEx = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var errors = new Array();
		
		if(!$('#uploadVideo input[name=title]').val()) {
			errors.push('You must provide a title.');
		}
		
		if($('#uploadVideo input[name=email]').val().search(emailRegEx) == -1) {
			errors.push('You must provide a valid email address.');
		}
		
		if(!$('#uploadVideo input[name=CheckBox_terms]').attr('checked')) {
			errors.push('You must accept the Terms of Service');
		}
			
		if(errors.length > 0) {
			alert(errors.join("\n"));
			e.preventDefault();
		} else {
			$('form#uploadVideo input.submit').hide();
			$('form#uploadVideo .loader').show();
		}
	});
});