// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} })
// When I say html I really mean script for rails
// $.ajaxSettings.accepts.html = $.ajaxSettings.accepts.script;

function _ajax_request(url, data, callback, type, method) {
    if (jQuery.isFunction(data)) {
        callback = data;
        data = {};
    }
    return jQuery.ajax({
        type: method,
        url: url,
        data: data,
        success: callback,
        dataType: type
        });
}

jQuery.extend({
    put: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'PUT');
    },
    delete_: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'DELETE');
    }
});

$(document).ready(function() {
  //default click event for all links
  $.ajaxSettings.accepts.html = $.ajaxSettings.accepts.script;

  $(document).ajaxSend(function(event, request, settings) {
       if (typeof(window.AUTH_TOKEN) == "undefined") return;
       // <acronym title="Internet Explorer 6">IE6</acronym> fix for http://dev.jquery.com/ticket/3155
       if (settings.type == 'GET' || settings.type == 'get') return;
       settings.data = settings.data || "";
       settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN);
     });
     
  $(document).ajaxSend(function(event, request, settings) {
    if ( settings.type == 'post' ) {
        settings.data = (settings.data ? settings.data + "&" : "")
            + "authenticity_token=" + encodeURIComponent( AUTH_TOKEN );
    }
  });
  $(document).bind('keyup', 'Ctrl+h', function(){ self.location = "/home"; } );
  $(document).bind('keyup', 'Ctrl+l', function(){ self.location = "/login"; } );
  $(document).bind('keyup', 'Ctrl+r', function(){ self.location = "/register"; } );
  $(document).bind('keyup', 'Ctrl+e', function(){ self.location = "/entries"; } );
  $(document).bind('keyup', 'Ctrl+o', function(){ self.location = "/logout"; } );
  $(document).bind('keyup', 'Ctrl+t', function(){ self.location = "/timesheets"; } );
  $(document).bind('keyup', 'Ctrl+p', function(){ self.location = "/companies"; } );
  setupMenus();
  $('.imagebutton').hoverbox();
});

function setupMenus()
{
    $('#menu1').ptMenu();
    $('#menu2').ptMenu();
}

function displayResults(request, errorType) {
  //var msg = '<div class="error">'+request.responseText+'(click to close)</div>'
  //$('#bookmarks-list').append(msg)
  //$('.error').click(function(){$(this).hide()})
  alert(msg);
}

jQuery.fn.flash = function( color, backColor, duration )
{
    var current = this.css( 'color' );
    var current_back = this.css('backgroundColor');
    this.animate( {color: 'rgb(' + color + ')', backgroundColor: 'rgb('+backColor+')' }, duration / 2 );
    this.animate( {color: current, backgroundColor: current_back }, duration / 2 );
}