<iframe id="linkloader" style="display: none">
</iframe>

<div id="browse_map">
  <% if map.instance_of? Changeset or (map.instance_of? Node and map.version > 1) or map.visible %>
  <div id="small_map">
  </div>
  <span id="loading"><%= t 'browse.map.loading' %></span>
  <%= link_to(t("browse.map.larger.area"), { :controller => :site, :action => :index, :box => "yes" }, { :id => "area_larger_map", :class => "geolink bbox" }) %>
  <br />
  <%= link_to(h(t("browse.map.edit.area")) + content_tag(:span, "▾", :class => "menuicon"), { :controller => :site, :action => :edit }, { :id => "area_edit", :class => "geolink bbox" }) %>
  <% unless map.instance_of? Changeset %>
    <br />
    <%= link_to(t("browse.map.larger." + map.class.to_s.downcase), { :controller => :site, :action => :index }, { :id => "object_larger_map", :class => "geolink object" }) %>
    <br />
    <%= link_to(h(t("browse.map.edit." + map.class.to_s.downcase)) + content_tag(:span, "▾", :class => "menuicon"), { :controller => :site, :action => :edit }, { :id => "object_edit", :class => "geolink object" }) %>
  <% end %>
  <% else %>
    <%= t 'browse.map.deleted' %>
  <% end %>
</div>

<div id="area_edit_menu" class="menu">
  <ul>
    <% Editors::ALL_EDITORS.each do |editor| %>
      <li><%= link_to t('layouts.edit_with', :editor => t("editor.#{editor}.description")), { :controller => :site, :action => :edit, :editor => editor }, { :id => "#{editor}_area_edit", :class => "geolink bbox" } %></li>
    <% end %>
  </ul>
</div>

<div id="object_edit_menu" class="menu">
  <ul>
    <% Editors::ALL_EDITORS.each do |editor| %>
      <li><%= link_to t('layouts.edit_with', :editor => t("editor.#{editor}.description")), { :controller => :site, :action => :edit, :editor => editor }, { :id => "#{editor}_object_edit", :class => "geolink object" } %></li>
    <% end %>
  </ul>
</div>

<% if map.instance_of? Changeset or (map.instance_of? Node and map.version > 1) or map.visible %>
  <script type="text/javascript">
    function remoteEditHandler(event, bbox, select) {
      var left = bbox.left - 0.0001;
      var top = bbox.top + 0.0001;
      var right = bbox.right + 0.0001;
      var bottom = bbox.bottom - 0.0001;
      var loaded = false;

      $("#linkloader").load(function () { loaded = true; });

      if (select) {
        $("#linkloader").attr("src", "http://127.0.0.1:8111/load_and_zoom?left=" + left + "&top=" + top + "&right=" + right + "&bottom=" + bottom + "&select=" + select);
      } else {
        $("#linkloader").attr("src", "http://127.0.0.1:8111/load_and_zoom?left=" + left + "&top=" + top + "&right=" + right + "&bottom=" + bottom);
      }

      setTimeout(function () {
        if (!loaded) alert("<%=j t('site.index.remote_failed') %>");
      }, 1000);

      return false;
    }

    function init() {
      var map = createMap("small_map", {
        controls: [ new OpenLayers.Control.Navigation() ]
      });

      <% if map.instance_of? Changeset -%>
        <% bbox = map.bbox.to_unscaled %>
        var minlon = <%= bbox.min_lon %>;
        var minlat = <%= bbox.min_lat %>;
        var maxlon = <%= bbox.max_lon %>;
        var maxlat = <%= bbox.max_lat %>;
        var bbox = new OpenLayers.Bounds(minlon, minlat, maxlon, maxlat);
        var centre = bbox.getCenterLonLat();

        map.zoomToExtent(proj(bbox));
        addBoxToMap(bbox);

        $("#loading").hide();
        $("#browse_map .geolink").show();

        $("#remote_area_edit").click(function (event) {
          return remoteEditHandler(event, bbox);
        });

        <% if preferred_editor == "remote" -%>
        $("#area_edit").click(function (event) {
          return remoteEditHandler(event, bbox);
        });
        <% end -%>

        updatelinks(centre.lon, centre.lat, 16, null, minlon, minlat, maxlon, maxlat);
      <% else -%>
        var obj_type = "<%= map.class.name.downcase %>";
        var obj_id = <%= map.id %>;
        var obj_version = <%= map.version %>;
        var obj_visible = <%= map.visible %>;
        var url = "/api/<%= API_VERSION %>/<%= map.class.name.downcase %>/<%= map.id %>";

        if (obj_type != "node") {
          url += "/full";
        } else if (!obj_visible) {
          var previous_version = obj_version - 1;
          url += "/" + previous_version;
        }

        $("#object_larger_map").hide();
        $("#object_edit").hide();

        addObjectToMap(url, true, function(extent) {
          $("#loading").hide();
          $("#browse_map .geolink").show();

          if (extent) {
            extent.transform(map.getProjectionObject(), map.displayProjection);

            var centre = extent.getCenterLonLat();

            $("#remote_area_edit").click(function (event) {
              return remoteEditHandler(event, extent);
            });

            <% if preferred_editor == "remote" -%>
            $("#area_edit").click(function (event) {
              return remoteEditHandler(event, extent);
            });
            <% end -%>

            $("#remote_object_edit").click(function (event) {
              return remoteEditHandler(event, extent, "<%= map.class.to_s.downcase + map.id.to_s %>");
            });

            <% if preferred_editor == "remote" -%>
            $("#object_edit").click(function (event) {
              return remoteEditHandler(event, extent, "<%= map.class.to_s.downcase + map.id.to_s %>");
            });
            <% end -%>

            $("#object_larger_map").show();
            $("#object_edit").show();

            updatelinks(centre.lon, centre.lat, 16, null, extent.left, extent.bottom, extent.right, extent.top, "<%= map.class.to_s.downcase %>", <%= map.id %>);
          } else {
            $("#small_map").hide();
          }
        });
      <% end -%>

      createMenu("area_edit", "area_edit_menu", "right");
      createMenu("object_edit", "object_edit_menu", "right");
    }

    window.onload = init;
  </script>
<% end %>
