var displayingTweet = 0;
$(document).ready(init);

function init(){
  $('.container').fadeIn(1500);
  resetInputs();
  addFormEvents();
  setUpTweets();
  $("h1.video").fitText();
}

function setUpTweets() {
  if ($("div#feeder span.description").size() > 0) setInterval('slideTweets(' + $("div#feeder span.description").size() + ')', 7000);
}

function slideTweets(mod){
  displayingTweet = (displayingTweet + 1) % mod;
  $('.description').addClass('hide');
  $('.description').eq(displayingTweet).removeClass('hide');    
}

function resetInputs() {
  $("#fromname").val("What's your name?");
  $("#fromemail").val("And your email?");
  $("#message").text("Now write us a message...");
}

function cleanInputs() {
  if ($("#fromname").val()  === null || $("#fromname").val()  === '' || $("#fromname").val()  === ' ') $("#fromname").val("What's your name?");
  if ($("#fromemail").val() === null || $("#fromemail").val() === '' || $("#fromemail").val() === ' ') $("#fromemail").val("And your email?");
  if ($("#message").val()   === null || $("#message").val()   === '' || $("#message").val()   === ' ') $("#message").val("Now write us a message...");
}

function selectedPage(pageName) {
  $('a.' + pageName).addClass('top-menu-item-selected');
}

function addFormEvents() {
  $("#fromname").focus(handleFocusText);
  $("#fromname").blur(handleBlurText);
  $("#fromname").keypress(handleKeydownText);
  $("#fromemail").focus(handleFocusText);
  $("#fromemail").blur(handleBlurText);
  $("#fromemail").keypress(handleKeydownText);
  $("#message").focus(handleFocusText);
  $("#message").blur(handleBlurText);
  $("#message").keypress(handleKeydownText);
}

function handleFocusText() {
  if ($(this).val() !== null) {
    $(this).addClass('selected-form-highlight');
  }
}

function handleKeydownText() {
	if($(this).hasClass('selected-form-highlight')){  
		$(this).val('');
		$(this).removeClass('selected-form-highlight');
	}
}

function handleBlurText() {
    $(this).removeClass('selected-form-highlight');
    cleanInputs();
}

