// ----------------------------------
// Simple Hash extension that refs
// objects by ID (adding an item
// automatically sets the key ->
// value association)
// ----------------------------------
var IDHash = Class.create(KeyValueCodedHash, {
  initialize: function($super) {
    $super();
    this.allItems = $([]);
  },
  
  addItem: function(item) {
    this[item.id] = item;
    this.allItems.push(item);
  },
  
  removeItem: function(item) {
    this.unset(item.id);
    this.allItems = this.allItems.without(item);
  }
})


// -------------------------------------------------------
// Simple Enumeration class allowing references by index
// and name. e.g new Enumeration('one', 'two')
// => {0: 'one', 1: 'two', 'one': 0, 'two': 1}
// -------------------------------------------------------
function Enumeration() {
  this.allItems = [];
  for(var i = 0; i < arguments.length; i++) {
    name = arguments[i];
    this[name] = i;
    this[i] = name;
    this.allItems.push(name)
  }
}


// -------------------------------------------------------
// Convenience function to set the content of a select
// input to the values in an Enumeration object
// -------------------------------------------------------
function displayEnumerationInSelectInput(enumeration, select) {
  options = "";
  option  = new Template("<option value='#{id}'>#{value}</option>");
  for(var i = 0; i < enumeration.allItems.length; i++)
    options += option.evaluate({id: i, value: enumeration.allItems[i]});
  $(select).innerHTML = options;
}


// -------------------------------------------------------
// Respond to an AJAX request using a standard forumla -
// run a function only when a certain key exists, fail
// otherwise with an error message.
// -------------------------------------------------------
function respondToAJAXRequest(options, transport) {
  if (transport.responseJSON) {
    if (transport.responseJSON.success) {
      if (options.key) {
        if(transport.responseJSON[options.key]) {
          options.fn(transport.responseJSON[options.key]);
          return;
        }
      } 
      else {
        options.fn();
        return;
      }
    }
  }
    
  // fall through to here in case of an error
  if (transport.responseJSON && transport.responseJSON.reason) {
    alert(transport.responseJSON.reason);
  } 
  else {
    alert(options.error);
  }
}


function ajaxWrapper(url, requestParams, successPredicate, successFunction, failureFunction) {
  var onSuccess = function(transport) {
    var x = transport.responseJSON;
    if (x.success && (successPredicate == null || successPredicate(x)))
      successFunction(x);
    else {
      if (x.reason)
        failureFunction(transport, x.reason);
      else
        failureFunction(transport);
    }
  };
  new Ajax.Request(url, {parameters: requestParams, onSuccess: onSuccess, onFailure: failureFunction});
}


function msgDivClear(id) {
  var x = $(id);
  x.update();
  x.hide();
}

function msgDivDisplay(id, msg, success) {
  var x = $(id);
  x.update();
  x.removeClassName('success_msg');
  x.removeClassName('failure_msg');
  x.addClassName(success ? 'success_msg' : 'failure_msg');
  x.insert(msg);
  x.appear({duration: 0.5, queue: 'end'});
}


function spinnerShow(id) {
  $(id).appear({duration: 0.5, queue: 'end'});
}

function spinnerHide(id) {
  $(id).fade({duration: 0.5, queue: 'end'});
}

var USER_FOOTER_TEMPLATE = new Template("<span class='name #{role_colour}'>#{author}</span>. #{role_name} at <span class='name'>#{author_school}</span>. Posted at #{timestamp}.");
function userFooter(json) {
  json.role_name = roleEnum[json.author_role];
  if (json.author_role == roleEnum['Student'] && json.author_year)
    json.role_name += ' in Year ' + json.author_year;
  if (roleEnum[json.author_role] == 'Admin')
    json.role_colour = 'admin';
  else if (roleEnum[json.author_role] == 'Lecturer')
    json.role_colour = 'lecturer';
  else if (roleEnum[json.author_role] == 'Tutor')
    json.role_colour = 'tutor';
  return USER_FOOTER_TEMPLATE.evaluate(json);
}

function generateRoleString(json) {
  role = roleEnum[json.role];
  if (json.role == roleEnum['Student'] && json.year)
    role += ' in Year ' + json.year;
  return role;
}


