$(document).ready(function(){
  //		$('#place').focus();

  $('#weatherForm').submit( function() {
    hideForm();
    $('#helpWrapper').fadeOut();
    $('#spinner').show();
  });

  $('.cityList a').click( function() {
    hideForm();
    $('#helpWrapper').fadeOut();
    $('#spinner').show();
  })

  // Ensure input text is selected on focus
  $('#place').focus( function(event) {
    if ($(this).val() == 'enter a city or town') $(this).val('').removeClass('example');
  })

  $('#place').blur( function(event) {
    if ($(this).val() == '') $(this).val('enter a city or town').addClass('example');
  })

  $('#lblToday').click( function() {
    $('#lblToday').addClass('selected');
    $('#lblTomorrow').removeClass('selected');
  });

  $('#lblTomorrow').click( function() {
    $('#lblToday').removeClass('selected');
    $('#lblTomorrow').addClass('selected');
  });

  // Show the example places
  $('#show-examples').click(function() {
    $('.cityList').show().animate({
      top: '+=180'
    }, 1000, 'easeOutBounce' );
    $('#show-examples').fadeOut('slow');
    return false;
  });

  // Show the help
  $('#show-help').click(function() {
    $('#help-overlay').fadeIn();
    return false;
  });

  $('#help-close').click(function() {
    $('#help-overlay').fadeOut();
    return false;
  });

  $('#help-overlay').click(function() {
    $('#help-overlay').fadeOut();
  });


  bounceUp();
});

function hideForm() {
  $('#weatherFormWrapper').animate({
    opacity: 0.25,
    top: '-=1000'
  }, 500 );

  $('#rainBg').animate({
    opacity: 0,
  }, 500 );

  $('#bouncer').stop().animate({
    opacity: 0,
  }, 500 );
}

function bounceUp() {
  var width = $(window).width();
  var height = $(window).height();

  // Animate the bouncer
  $('#bouncer').css('left', -150).removeClass('cloud').addClass('sun').animate({
    bottom: ['+=' + 400, 'easeOutSine'],
    left: ['+=' + ((Math.round(width / 2)) + 75), 'linear']
  },1500, function() {
    $('#bouncer').animate({
      bottom: ['-=400', 'easeInSine'],
      left: ['+=' + ((Math.round(width / 2)) + 75), 'linear']
    },1500, function() {
      bounceUpReverse();
    });
  });
}

function bounceUpReverse() {
  var width = $(window).width();
  var height = $(window).height();

  // Animate the bouncer
  $('#bouncer').css('left', width).removeClass('sun').addClass('cloud').animate({
    bottom: ['+=' + 400, 'easeOutSine'],
    left: ['-=' + ((Math.round(width / 2)) + 75), 'linear']
  },1500, function() {
    $('#bouncer').animate({
      bottom: ['-=400', 'easeInSine'],
      left: ['-=' + ((Math.round(width / 2)) + 75), 'linear']
    },1500, function() {
      bounceUp()
      });
  });
}
