
var NO_RESPONSE = 'Could not complete request at this time.';
var map_loaded = false;

function toggleNavText(switchTabs) {
  var nav_list = "Cidades";
  var close_list = "Fechar Cidades";
  var nav_map = "Mapa";
  var close_map = "Fechar Mapa";

  if(switchTabs == "switchHoodToMap") {
    $("#toggle-nav-map").text(close_map);
    $("#toggle-nav-map").parent("li").addClass("active");
    $("#toggle-neighborhood-info").text(nav_list);
    $("#toggle-neighborhood-info").parent("li").removeClass("active");
    if($("#safari").length)
      $("#safari").show();
    $(".neighborhood-pane").removeClass("hide-border");
  } else if(switchTabs == "switchMapToHood") {
    $("#toggle-nav-map").text(nav_map);
    $("#toggle-nav-map").parent("li").removeClass("active");
    $("#toggle-neighborhood-info").text(close_list);
    $("#toggle-neighborhood-info").parent("li").addClass("active");
    $(".neighborhood-pane").removeClass("hide-border");
  } else if(switchTabs == "openedHood") {
    $("#toggle-neighborhood-info").text(nav_list);
    $("#toggle-neighborhood-info").parent("li").removeClass("active");
    $("#toggle-nav-map").text(nav_map);
    $("#toggle-nav-map").parent("li").removeClass("active");
    $(".neighborhood-pane").addClass("hide-border");
  } else if(switchTabs == "closedHood") {
    $("#toggle-neighborhood-info").text(close_list);
    $("#toggle-neighborhood-info").parent("li").addClass("active");
    $("#toggle-nav-map").text(nav_map);
    $("#toggle-nav-map").parent("li").removeClass("active");
    $(".neighborhood-pane").removeClass("hide-border");
  } else if(switchTabs == "openedMap") {
    $("#toggle-neighborhood-info").text(nav_list);
    $("#toggle-neighborhood-info").parent("li").removeClass("active");
    $("#toggle-nav-map").text(nav_map);
    $("#toggle-nav-map").parent("li").removeClass("active");
    $(".neighborhood-pane").addClass("hide-border");
  } else if(switchTabs == "closedMap") {
    $("#toggle-neighborhood-info").text(nav_list);
    $("#toggle-neighborhood-info").parent("li").removeClass("active");
    $("#toggle-nav-map").text(close_map);
    $("#toggle-nav-map").parent("li").addClass("active");
    $(".neighborhood-pane").removeClass("hide-border");
    if($("#safari").length)
      $("#safari").show();
  }
}

function toggleNavigation(type) {
  var hood_status = $("#neighborhood-info").css("display");
  var map_status = $("#navigation-map").css("display");
  if(type == "hood") {
    if(map_status != "none") {
      $("#navigation-map").hide();
      $("#neighborhood-info").show();
      toggleNavText("switchMapToHood");
    } else {
      if(hood_status == "none") {
        toggleNavText("closedHood");
      } else {
        toggleNavText("openedHood");
      }
      $("#neighborhood-info").slideToggle("fast");
    }
  } else if(type == "map") {
    if(hood_status != "none") {
      $("#neighborhood-info").hide();
      $("#navigation-map").show();
      toggleNavText("switchHoodToMap");
    } else {
      if(map_status == "none") {
        toggleNavText("closedMap");
      } else {
        toggleNavText("openedMap");
      }
      $("#navigation-map").slideToggle("fast");
    }
  }
}

var Try = {
  these: function() {
    var returnValue;

    for (var i = 0; i < arguments.length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }

    return returnValue;
  }
}

$(document).ready(function() {

  $("#toggle-cities").click(function() {
    $("#more-cities").toggle(50);
    if($("#toggle-cities").text().substring(0,4) == "mais") {
      $("#toggle-cities").html("&laquo; fechar");
    } else {
      $("#toggle-cities").html("mais &raquo;");
    }
  });

  $("#toggle-cities-nav").click(function() {
    $("#more-cities-nav-container").toggle(0);
    if($("#toggle-cities-nav").text().substring(0,4) == "mais") {
      $("#toggle-cities-nav").html("&laquo; fechar");
    } else {
      $("#toggle-cities-nav").html("mais &raquo;");
    }
  });

  $(".nav-close-bubble").click(function() {
    $("#more-cities-nav-container").hide(0);
    if($("#toggle-cities-nav").text().substring(0,4) == "mais") {
      $("#toggle-cities-nav").html("&laquo; fechar");
    } else {
      $("#toggle-cities-nav").html("mais &raquo;");
    }
  });

  $("#toggle-nav-map").click(function (){
    toggleNavigation("map");
    if(map_loaded == false) {
      activateNavMap();
      map_loaded = true;
    }
  });

  $("#toggle-neighborhood-info").click(function() {
    toggleNavigation("hood");
    /*$("#neighborhood-info").slideToggle("fast");
    if($("#toggle-neighborhood-info").text() == "close") {
      $("#toggle-neighborhood-info").text("choose neighborhood");
      $("#loc-bar").removeClass("loc-bar-close");
      $("#loc-bar").addClass("loc-bar-open");
    } else {
      $("#toggle-neighborhood-info").text("close");
      $("#loc-bar").removeClass("loc-bar-open");
      $("#loc-bar").addClass("loc-bar-close");

    }*/
  });

});
