//////////////////////////////////////////////
// Site related objects and functions.
//////////////////////////////////////////////

/**
 * Initalizer class
 */
function Initializer() {
  this._callbacks = new Array();
}

Initializer.prototype._callbacks;

Initializer.prototype.addCallback = function(aCallback) {
  this._callbacks.push(aCallback);
}

Initializer.prototype.load = function() {
  for (i = 0 ; i < this._callbacks.length ; i++) {
    aCallback = this._callbacks[i];
    aCallback.load();
  }
}

Initializer.prototype.unload = function() {
  for (i = 0 ; i < this._callbacks.length ; i++) {
    aCallback = this._callbacks[i];
    aCallback.unload();
  }
}

/////////////////////////////////////////////////////////////

/**
 * InitializerCallback class
 */
function InitializerCallback() {
}

InitializerCallback.prototype.load = function() {
}

InitializerCallback.prototype.unload = function() {
}

/////////////////////////////////////////////////////////////

/**
 * LocationPerCategoriesFinder class
 */
function LocationPerCategoriesFinder(formId) {
  this._formId = formId;
}

LocationPerCategoriesFinder.prototype._formId;

LocationPerCategoriesFinder.prototype.find = function() {
  //validate at least a category was selected
  categoriesParams = $(this._formId).serialize();
  if (categoriesParams.empty()) {
    alert("Por favor seleccione al menos una categoría");
    return;
  }

  //build url
  mapAttributes = new MapAttributes();
  url = "/search.location/category/format/json?";
  url = url.concat(categoriesParams);
  url = url.concat("&", "bounds=", mapAttributes.getBounds());

  //call server and start indicator
  new Ajax.Request(url, {
      method: 'get',
      onCreate: this.startIndicator,
      onSuccess: this.onSuccess,
      onFailure: this.onFailure,
      onComplete: this.stopIndicator
    }
  );
}

LocationPerCategoriesFinder.prototype.startIndicator = function () {
  new SearchIndicator("category_search_indicator").activate();
}

LocationPerCategoriesFinder.prototype.onSuccess = function(result) {
  //create markers on map
  new LocationMarkersCreator(result.responseJSON);

  //show results
  new ShowSearchResult(result.responseJSON, "search_result");
}

LocationPerCategoriesFinder.prototype.onFailure = function(result) {
  alert("ups! Ocurrió un error no esperado :S");
}

LocationPerCategoriesFinder.prototype.stopIndicator = function () {
  new SearchIndicator("category_search_indicator").deactivate();
}

/////////////////////////////////////////////////////////////
/**
 * SearchIndicator class
 */
function SearchIndicator(indicatorId) {
  this._indicatorId = indicatorId;
}

SearchIndicator.prototype._indicatorId;

SearchIndicator.prototype.activate = function() {
  $(this._indicatorId).show();
}

SearchIndicator.prototype.deactivate = function() {
  $(this._indicatorId).hide();
}

/////////////////////////////////////////////////////////////

/**
 * ShowSearchResult class
 */
function ShowSearchResult(data, divId) {
  if (data.length == 0) {
    $(divId).update("No se encontraron sitios de interés.");
  } else if (data.length == 1) {
    $(divId).update("Se encontró un sitio de interés.");
  } else {
    $(divId).update("Se encontraron <b>".concat(data.length).concat("</b> sitios de interés."));
  }

  //highlight result
  new Effect.Highlight($(divId));
}

/////////////////////////////////////////////////////////////

/**
 * LocationPerNameFinder class
 */
function LocationPerNameFinder(formId) {
  this._formId = formId;
}

LocationPerNameFinder.prototype._formId;

LocationPerNameFinder.prototype.find = function() {
  //build url
  url = "/search.location/name/format/html?";
  url = url.concat( $(this._formId).serialize() );

  //call server and start indicator
  new Ajax.Updater('search_result', url, {
      method: 'get',
      onCreate: this.startIndicator,
      onSuccess: this.onSuccess,
      onFailure: this.onFailure,
      onComplete: this.stopIndicator
    }
  );
}

LocationPerNameFinder.prototype.startIndicator = function () {
  new SearchIndicator("name_search_indicator").activate();
}

LocationPerNameFinder.prototype.onSuccess = function(result) {
  //highlight result
  new Effect.Highlight('search_result');
}

LocationPerNameFinder.prototype.onFailure = function(result) {
  alert("ups! Ocurrió un error no esperado :S");
}

LocationPerNameFinder.prototype.stopIndicator = function () {
  new SearchIndicator("name_search_indicator").deactivate();
}

/////////////////////////////////////////////////////////////

/**
 * ExpanderCollaider class
 */
function ExpanderCollaider(elemId) {
  if ($(elemId).visible()) {
    //hide
    Effect.BlindUp(elemId, 2);
  } else {
    //show
    Effect.BlindDown(elemId, 2);
  }
}