﻿/// <reference name="AppGeo.Web.Extensions.js" assembly="AppGeo.Web"/>
/// <reference path="SearchAttributeView.js" />

function pageLoad(sender, args) {
  FacilityService.set_defaultFailedCallback(Svc_failed);

  BostonMashupData = new Sys.Data.DataService("Services/BostonMashupData.svc");
  BostonMashupData.set_defaultFailedCallback(Svc_failed);

  initializeComponents();
  processQueryString();

  if (AppContext.ShowSpanishLink) {
    $("#lnkSpanish").show();
  }

  /*
  if (appState.searchRadius)
  $("#ddlRadius").val(appState.searchRadius);
  else
  appState.setItem("searchRadius", 0.5);

  if (appState.searchAddress)
  $("#txtAddress").val(appState.searchAddress);
  else
  appState.setItem("searchAddress", "");
  */

  appState.clear();
  appState.setItem("searchRadius", 0.5);

  webRequests.queue(FacilityService, "GetSearchAttributes");
}

function initializeComponents() {
  appState = $create(Ag.AppState, { id: "appState" });

  $("#cmdGoToMap").val(ScriptResources.cmdGoToMap_Value);
  $("#cmdSearch").val(ScriptResources.cmdSearch_Value);
  $("#cmdSearchAddressCorrectCancel").val(ScriptResources.cmdAddressCorrectCancel_Value).click(cmdSearchAddressCorrectCancel_click);
  
  ddlFacilityName = $create(Ag.UI.SelectList, null, null, null, $get("ddlFacilityName"));
  $("#ddlFacilityType").change(ddlFacilityType_change);
  ddlNeighborhood = $create(Ag.UI.SelectList, null, null, null, $get("ddlNeighborhood"));

  gvAddressCorrect = $create(Ag.UI.GridView, { alternatingRowCssClass: "AlternatingRow" }, null, null, $get("gvAddressCorrect"));
  $("#gvAddressCorrect td").live("click", gvAddressCorrect_click);
  
  gvSearchAttributes = $create(SearchAttributeView, null, null, null, $get("gvSearchAttributes"));

  webRequests = $create(Ag.Net.WebRequestQueue, { id: "webRequests" }, { requestsCompleted: webRequests_requestsCompleted, requestsQueued: webRequests_resuestsQueued });
}

function processQueryString() {
  var queryString = new Ag.QueryString();
  var allKeys = queryString.get_allKeys();
}

// AJAX Events

function webRequests_requestsCompleted(sender, args) {
  $("#searchIndicator").hide();
}

function webRequests_resuestsQueued(sender, args) {
  $("#searchIndicator").show();
}

// DOM Events

function cmdChangeLanguage_click(e) {
  /// <summary>Hook this up within each page</summary>
  hl = (hl == "en" ? "es" : "en");
  window.location.href = "Search.aspx?hl=" + hl;
}

//function cmdGoToMap_click(e) {
//  window.location.href = String.format("Map.aspx?&hl={1}", hl);
//}

function cmdHideAdvanced_click(e) {
  $common.setVisible($get("pnlSearchAdvanced"), false);
  $common.setVisible($get("pnlShowAdvanced"), true);
}

function cmdSearchAddressCorrectCancel_click(e) {
  $("#pnlSearchAddressCorrect").hide();
}

function cmdShowAdvanced_click(e) {
  showAdvanced();
}

function ddlFacilityType_change(e) {
  webRequests.queue(BostonMashupData, "GetFacilityNames", { facilityType: $(e.target).val() });
}

function gvAddressCorrect_click(e) {
  appState.setItem("searchAddress", e.target.tag);
  window.location.href = "Results.aspx?&hl=" + hl;
}

function form_submit(e) {
  search();
  return false;
}

// Service Callbacks

function Svc_failed(error) {
  alert(error.get_message());
}

function BostonMashupData_GetFacilityNames_succeeded(result) {
  var anyText = ddlFacilityName.get_element().options[0].text;
  ddlFacilityName.set_dataSource(result);
  ddlFacilityName.dataBind();
  $(ddlFacilityName.get_element()).prepend("<option selected='selected' value=''>" + anyText + "</option>");
  ddlFacilityName.set_selectedIndex(0);
}

function FacilityService_GetSearchAttributes_succeeded(result) {
  gvSearchAttributes.set_dataSource(result);
  gvSearchAttributes.dataBind();
}

function FacilityService_VerifyAddress_succeeded(result) {
  if (result.length == 0) {
    alert(ScriptResources.alert_SearchAddress);
  } else if (result.length == 1) {
    appState.setItem("searchAddress", result[0].fullAddress);
    window.location.href = "Results.aspx?&hl=" + hl;
  } else {
    gvAddressCorrect.set_dataSource(result);
    gvAddressCorrect.dataBind();
    $("#pnlSearchAddressCorrect").show();
  }
}

// Public Methods

function search() {
  appState.setItem("hasCriteria", false);
  
  appState.setItem("searchRadius", parseFloat($("#ddlRadius").val()));

  if ($("#txtAddress").val()) {
    appState.setItem("searchAddress", $("#txtAddress").val());
    appState.setItem("hasCriteria", true);
  }

  if ($("#ddlNeighborhood").val()) {
    appState.setItem("searchNeighborhood", $("#ddlNeighborhood").val());
    appState.setItem("hasCriteria", true);
  }

  if ($("#ddlFacilityType").val()) {
    appState.setItem("searchFacilityType", $("#ddlFacilityType").val());
    appState.setItem("hasCriteria", true);
  }

  if ($("#ddlFacilityName").val()) {
    appState.setItem("searchFacilityName", $("#ddlFacilityName").val());
    appState.setItem("hasCriteria", true);
  }

  var searchAttributes = $("#gvSearchAttributes select");
  searchAttributes.each(function(i) {
    var jEl = $(this);
    if (jEl.val()) {
      appState.setItem(jEl.attr("tag"), jEl.val());
      appState.setItem("hasCriteria", true);
    }
  });

  if (!appState.hasCriteria) {
    alert(ScriptResources.error_NoCriteria);
  } else {
    if (appState.searchAddress)
      webRequests.queue(FacilityService, "VerifyAddress", { address: appState.searchAddress });
    else
      window.location.href = "Results.aspx?&hl=" + hl;
  }
}

function showAdvanced() {
  $common.setVisible($get("pnlShowAdvanced"), false);
  $common.setVisible($get("pnlSearchAdvanced"), true);
}

// Stored References

var BostonMashupData = null;

var appState = null;
var ddlFacilityName = null;
var ddlNeighborhood = null;
var ddlRadius = null;
var gvAddressCorrect = null;
var gvSearchAttributes = null;
var webRequests = null;

// Private Fields

if (typeof (Sys) !== "undefined") {
  Sys.Application.notifyScriptLoaded();
}
