// AjaxQueue - by Pat Nakajima
(function($) {
  var startNextRequest = function() {
    if ($.ajaxQueue.currentRequest) { return; }
    if (request = $.ajaxQueue.queue.shift()) {
      $.ajaxQueue.currentRequest = request;
      request.perform();
    }
  }
  
  var Request = function(url, type, options) {
    this.opts = options || { };
    this.opts.url = url;
    this.opts.type = type;
    var oldComplete = this.opts.complete || function() { }
    this.opts.complete = function(response) {
      oldComplete(response);
      $.ajaxQueue.currentRequest = null;
      startNextRequest();
    };
  }
  
  Request.prototype.perform = function() {
    $.ajax(this.opts);
  }
  
  var addRequest = function(url, type, options) {
    var request = new Request(url, type, options);
    $.ajaxQueue.queue.push(request);
    startNextRequest();
  }
  
  $.ajaxQueue = {
    queue: [],
    
    currentRequest: null,
    
    reset: function() {
      $.ajaxQueue.queue = [];
      $.ajaxQueue.currentRequest = null;
    },
    
    post: function(url, options) {
      addRequest(url, 'POST', options);
    },
    
    get: function(url, options) {
      addRequest(url, 'GET', options);
    },
    
    put: function(url, options) {
      addRequest(url, 'PUT', options);
    },
    
    del: function(url, options) {
      addRequest(url, 'DELETE', options);
    }
  }
})(jQuery)

var app = {
  initializeAjax: function () {
    // show "loading" div when AJAX call starts
    $('body').ajaxStart(function () {
      $('#ajax-loading').show();
    });
    // hide "loading" div when AJAX call stops
    $('body').ajaxStop(function () {
      $('#ajax-loading').fadeOut();
    });
    // show "error" div when AJAX call results in an error
    $('body').ajaxError(function () {
      $('#ajax-error').show().click(function () {
        $(this).fadeOut();
      })
    });
  },

  // add/remove focus class on field focus/blur; also sets focus on first field
  initializeGenericForms: function () {
    $('form.generic :input:visible').focus(function () {
      $(this).parents('div.item').addClass('focus hover');
    }).blur(function () {
      $(this).parents('div.item').removeClass('focus hover');
    }).first().focus();
  },

  // add clear filter functionality
  initializeSearchForms: function () {
    $('form#search #clear-filter').click(function () {
      $(this).parents('form').find(':input:not(:submit):visible').val('');
      // since we're not preventing default click, we don't need to submit
    });
  },

  // add/remove hover class on mouseover/mouseout of table rows
  initializeTables: function () {
    $('table.list:not(.no-hover) tbody tr').mouseover(function () {
      $(this).addClass('hover');
    }).mouseout(function () {
      $(this).removeClass('hover');
    });
  },

  // toggle element specified in HREF attribute of link when it is clicked
  initializeToggleLinks: function () {
    $('a.toggle-link').click(function () {
      $($(this).attr('href')).toggle(100, function () {
        $(this).find(':input:visible').first().focus();
      });
      return false;
    });
  },

  // show feedback messages
  loadFeedback: function (path) {
    $.ajaxQueue.get(path, {
      success: function (data) {
        // start by clearing earlier feedback messages
        $('#feedback').empty().hide();
        if (data.length > 0) {
          $('#feedback').append(data).show();
        }
      }
    });
  },

  // load side bar
  loadSidebar: function (path, initialize) {
    $.ajaxQueue.get(path, {
      success: function (data) {
        $('#sidebar').html(data);
        if (initialize) {
          $('div#main').show();
          extraInitialization();
          if ($('form.generic :input:visible').size() == 0) {
            $('input#authentication_email').focus();
          }
        }
      }
    });
  },

  // load extra sub menu options
  loadSubmenu: function (path) {
    $.ajaxQueue.get(path, {
      success: function (data) {
        $('#submenu').append(data);
      }
    });
  },

  loadWho: function (path) {
    $.ajaxQueue.get(path, {
      success: function (data) {
        $('#who').html(data);
      }
    });
  }
}