
function EventLoader() {

  this.Events = [];
  this.AccessIdEventI = [];
  this.PasswordEventIs = {};

  this.WelcomeMessages = [];
  this.ImageCount = 0;

  this.Init = EventLoaderInit;
  this.TPImageIdToEPILoc = EventLoaderTPImageIdToEPILoc;
  this.TPImageIdToEventI = EventLoaderTPImageIdToEventI;
  this.ShowErrorDialog = EventLoaderShowErrorDialog;
}

function EventLoaderInit(password, cur_ivid, add_passwordless, success_callback, failure_callback) {
  var EL = this;
  var access_ids = [];
  for (var i in EL.AccessIdEventI) access_ids.push(i);

  var req_data = {
    access_ids: access_ids.join(',')
  };
  if (password != null) req_data['password'] = unescape(password);
  if (cur_ivid != null) req_data['cur_ivid'] = cur_ivid;
  if (add_passwordless) req_data['ap'] = 1;
  $.ajax({
    type: "POST",
    url: "/ecom/unlock_events_json.pl",
    data: req_data,
    dataType: "json",
    success: function(data, status) {
      if (data.Result == 'Success') {
	if (EL.PasswordEventIs[password] == null)
	  EL.PasswordEventIs[password] = [];
	for (var i = 0; i < data.Events.length; i++) {
	  var event_data = data.Events[i];
	  var event_i = EL.AccessIdEventI[event_data.AccessId];
	  var event = EL.Events[event_i];
	  if (event.Loaded) continue;
	  event.WelcomeMessageId = event_data.WelcomeMessageId;
	  event.EventDescription = event_data.EventDescription;
	  event.AllowZoom = event_data.AllowZoom;

	  if (event.Locked) {
	    event.Password = password;
	    EL.PasswordEventIs[password].push(event_i);
	    event.Locked = false;
	  }
	  if (event.ExampleImageRef != null)
	    event.ExampleImageRef = event.ExampleImageRef.replace(/1$/, '0');
	}
	for (var i in data.WelcomeMessages) {
	  EL.WelcomeMessages[i] = data.WelcomeMessages[i];
	}
	if (success_callback != null)
	  success_callback(data.CurTPImageId);
      }
      else if (data.Result == 'Failure') {
	EL.PasswordEventIs[password] = [];
	if (failure_callback != null)
	  failure_callback(data.ErrorTitle, data.ErrorMessage);
      }
      else {
	EL.ShowErrorDialog('Unknown error loading event overview', null);
      }
    }
  });
}

function EventLoaderShowErrorDialog(title, message) {
  document.getElementById("error_title").innerHTML = title == null ? '' : title;
  document.getElementById("error_message").innerHTML = message == null ? '' : message;
  if (this.Events.length == 0) {
    $('#errorbox_a').nyroModalManual({
      endRemove: function() { history.back(); }
    });
  }
  else {
    $('#errorbox_a').nyroModalManual();
  }
}

function EventLoaderTPImageIdToEPILoc(tp_image_id) {
  if (this.Events == null) return null;

  var e_idx, p_idx, i_idx;

  for (e_idx = 0; e_idx < this.Events.length; e_idx++) {
    var event = this.Events[e_idx];
    if (event.StartSeq + event.ImageCount <= tp_image_id) continue;
    if (! event.Loaded) return null;
    for (p_idx = 0; p_idx < event.PageOverviews.length; p_idx++) {
      var page_overview = event.PageOverviews[p_idx];
      var page_start_seq = page_overview.StartSeq;
      if (page_start_seq + page_overview.ImageCount <= tp_image_id) continue;
      i_idx = tp_image_id - page_start_seq;
      return { EventIndex: e_idx, PageIndex: p_idx, ImageIndex: i_idx };
    }
  }
}

function EventLoaderTPImageIdToEventI(tp_image_id) {
  if (this.Events == null) return null;

  var e_idx, p_idx, i_idx;
  for (e_idx = 0; e_idx < this.Events.length; e_idx++) {
    var event = this.Events[e_idx];
    if (event.StartSeq + event.ImageCount <= tp_image_id) continue;
    return e_idx;
  }
}

