require("/o2www/js/util/urlMod.js");

var infoPage_currentOptionElement;

addLoadEvent(initProximityNumbers);

function initProximityNumbers(e) {
  // Default is 10km
  var form   = document.getElementById("proximitySearchForm");
  if (!form) {
    return;
  }
  var select = form.radius;
  var option;
  for (var i = 0; i < select.childNodes.length; i++) {
    var elm = select.childNodes[i];
    if (elm.nodeType == 1 && elm.value == 10000) {
      option = elm;
      break;
    }
  }
  if (!option) {
    alert("Didn't find <option> with value=10000 in radius-<select>");
  }
  option.selected = true;
  var infoPageId = form.infoPageId.value;
  updateProximityNumbers(select, infoPageId);
}

var INFOPAGE_GLOBAL_SELECT;
function updateProximityNumbers(select, infoPageId, wasError) {
  if (wasError && o2UrlMod.getParam("preview")) {
    return; // Don't try more than once in preview mode
  }
  // Find clicked option element
  select = select || INFOPAGE_GLOBAL_SELECT;
  select.form.radius.value = select.value;
  var option;
  for (var i = 0; i < select.childNodes.length; i++) {
    var elm = select.childNodes[i];
    if (elm.nodeType == 1 && elm.value == select.value) {
      option = elm;
      break;
    }
  }
  var numSameCategoryWithinXKm = option.getAttribute("numSameCategoryWithin" + option.value + "Km");
  if (numSameCategoryWithinXKm) {
    document.getElementById("numSameCategoryWithinXKm").innerHTML    = numSameCategoryWithinXKm;
    document.getElementById("numOtherCategoriesWithinXKm").innerHTML = option.getAttribute("numOtherCategoriesWithin" + option.value + "Km");
  }
  else {
    INFOPAGE_GLOBAL_SELECT = select;
    infoPage_currentOptionElement = option;
    var url = o2UrlMod.urlMod({
      setDispatcherPath : "o2",
      setClass          : "InfoPage-InfoPageViewer",
      setMethod         : "getNumSameAndOtherCategoryGeoLinksWithinXKm",
      setParam          : "km=" + (select.value / 1000) + ",infoPageId=" + infoPageId
    });
    ajaxCall({
      "serverScript" : url,
      "handler"      : "_receiveNumCategories",
      "onError"      : "updateProximityNumbers(null, '" + infoPageId + "', true);"
    });
  }
}
function _receiveNumCategories(params) {
  var elm = infoPage_currentOptionElement;
  elm.setAttribute("numSameCategoryWithin"    + params["km"] + "Km", params["numGeoLinksInSameCategory"]);
  elm.setAttribute("numOtherCategoriesWithin" + params["km"] + "Km", params["numGeoLinksInOtherCategories"]);
  document.getElementById("numSameCategoryWithinXKm").innerHTML    = params["numGeoLinksInSameCategory"];
  document.getElementById("numOtherCategoriesWithinXKm").innerHTML = params["numGeoLinksInOtherCategories"];
}

function submitProximitySearchForm(inputName, inputValue) { // inputName is either "category" or "notCategory"
  var form = document.getElementById('proximitySearchForm');
  form[inputName].value = inputValue;
  var notInputName = inputName == "category" ? "notCategory" : "category";
  form[notInputName].value = "";
  form.submit();
}
