//Allow rails to detect ajax requests
jQuery.ajaxSetup({
    'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript")},
    error:function(x,e){
        if(x.status==0){
        alert('You are offline!!\n Please Check Your Network.');
        }else if(x.status==404){
        alert('Requested URL not found.');
        }else if(x.status==500){
        alert('Internel Server Error.');
        }else if(e=='parsererror'){
        alert('Error.\nParsing JSON Request failed.');
        }else if(e=='timeout'){
        alert('Request Time out.');
        }else {
        alert('Unknow Error.\n'+x.responseText);
        }
    }
});
jQuery.fn.submitWithAjax = function(){
   this.submit(function(){
      var submitButton = $('input[type=submit]', this);
      var origText = $(submitButton).attr("value");
      $(submitButton).attr("value", "Please wait...");
      $(submitButton).attr("disabled", "disabled");
      $.post(
        $(this).attr("action"),
        $(this).serialize(),
        function(){
          $(submitButton).removeAttr("disabled");
          $(submitButton).attr("value", origText);
        },
        "script"
      );
      return false;
   });
};
$(function(){
    //Setup date and time pickers
    $('.datetimepicker').datepicker({
        dateFormat: 'mm-dd-yy',
        onSelect: function(dateText, inst) {
             var dt = new Date();
             var time = dt.getHours() +':'+ dt.getMinutes() +':'+ dt.getSeconds();
             $(this).val(dateText + ' ' + time);
        }
    });
    //all hover logic for tab navigation
    $(".ui-state-default:not(.ui-state-disabled)")
        .hover(
            function(){
                    $(this).addClass("ui-state-hover");
            },
            function(){
                    $(this).removeClass("ui-state-hover");
            }
    )
    //all hover and click logic for buttons
    $(".fg-button:not(.ui-state-disabled)")
        .hover(
            function(){
                    $(this).addClass("ui-state-hover");
            },
            function(){
                    $(this).removeClass("ui-state-hover");
            }
    )
    .mousedown(function(){
        $(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
        if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
        else { $(this).addClass("ui-state-active"); }
    })
    .mouseup(function(){
        if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
                $(this).removeClass("ui-state-active");
        }
    });
});