$(document).ready( function(){

  // INITIALIZE DATE AND TIME PICKERS
  //$('#start_time').timepickr({convention:12});
  //$('#start_time').next().addClass('dark');
  $('#happens_on').datepicker();
  
  // ROUND ALL CORNERS
  $(':input').addClass("ui-corner-all");
  $('.submit').addClass("ui-corner-all");
  $('#admin_nav').addClass("ui-corner-all")
  $('.rounded').addClass("ui-corner-all")
  $('#admin_nav').children("li").addClass("ui-corner-all").addClass("ui-state-default")
  $('.restful_control').addClass("ui-corner-all").addClass("ui-state-default")
  $('#calendar').addClass("ui-corner-all");
  $('#calendar .today .day_number').addClass("ui-corner-all");
  $('#calendar .event').addClass("ui-corner-all");
  $('.player-card, .event').addClass("ui-corner-all");
  //$('#admin_nav').children("li.nav_description").removeClass("ui-state-default")
  
  // SET THE STYLE FOR FOCUSED INPUTS
  $(':input').focus(function() {  
    $(this).addClass("focused_field");  
  });
  
  // REMOVE THE STYLE FOR NON-FOCUSED INPUTS
  $(':input').blur(function() {  
    $(this).removeClass("focused_field");
  });
  
  // SETUP FORM SUBMISSION BUTTONS
  $('.submit, .remote-submit').addClass("ui-state-hover")
  $('.submit').click(function() {
    $(this).parents("form:first").submit();
  });
  
  // FADING ELEMENTS
  $('#notice').ready( function() {
    setTimeout('fadeAway("#notice");', 5000);
  });
  
  // LOGIN DIALOG BOX
  $('#login_box').dialog({
    autoOpen: false,
    width: 400,
    modal: true,
    buttons: {
      "Login": function() {
        $(this).children("form:first").submit();
      }
    }
  });
  
  $('#login').click(function() {  
    $('#login_box').dialog('open');  
  });
  
  
  
  // NEW LOCATION DIALOG BOX
  $('#event_location_id option:first').after("<option value='new'>New Location...</option>")
  $('#event_location_id').change(function(){
    if($(this).val() == "new"){
      $('#remote-location-form').fadeIn();
      $('#remote-location-form .remote-submit').click( function(){
        //console.log($("#remote-location-form #location_name").val());
        //console.log($("#remote-location-form input").serialize());
        $.post("/locations", $("#remote-location-form input").serialize(), function(data){
          //console.log(newLocation[0]);
          if( typeof(newLocation) != "undefined"){
            $('#event_location_id').append("<option value='"+ newLocation[1] +"'>"+ newLocation[0] +"</option>");
            $('#event_location_id').val(newLocation[1]);
            $('#remote-location-form').fadeOut();
          } else {
            var error = newLocationError[0] +" "+ newLocationError[1];
            $('#remote-location-form .column.half:first').append('<p class="remote-error clearfix ui-state-error ui-corner-all">'+ error +'</p>');
            $('.remote-error').delay(3000).fadeOut(1000);
            //console.log(newLocationError)
          }
          
        }, 'script');
      });
      
      
      
      
      
      
    } else {
      $('#remote-location-form:visible').fadeOut();
    }
  })
  
});



function fadeAway(e) {
  $(e).animate({ opacity: 0 }, 3000 );
};

function updateWithRubyTime(time){
  $('#calendar .day.today .day_number').attr('title', time);
}