<h1><%= @title %></h1>

<%= error_messages_for 'diary_entry' %>

<% form_for :diary_entry do |f| %>
  <table>
    <tr valign="top">
      <td class="fieldName"><%= t 'diary_entry.edit.subject' -%></td>
      <td><%= f.text_field :title, :size => 60 %></td>
    </tr>
    <tr valign="top">
      <td class="fieldName"><%= t 'diary_entry.edit.body' -%></td>
      <td><%= f.text_area :body, :cols => 80 %></td>
    </tr>
    <tr valign="top">
      <td class="fieldName"><%= t 'diary_entry.edit.language' -%></td>
      <td><%= f.collection_select :language_code, Language.find(:all, :order => :english_name), :code, :name %></td>
    </tr>
    <tr valign="top">
      <td class="fieldName"><%= t 'diary_entry.edit.location' -%></td>
      <td>
        <div id="map" style="border: 1px solid black; position: relative; width : 90%; height : 400px; display: none;"></div>
        <span class="location"><%= t 'diary_entry.edit.latitude' -%> <%= f.text_field :latitude, :size => 20, :id => "latitude" %> <%= t 'diary_entry.edit.longitude' -%> <%= f.text_field :longitude, :size => 20, :id => "longitude" %></span>
        <a href="javascript:openMap()" id="usemap"><%= t 'diary_entry.edit.use_map_link' -%></a>
        <br/><br/>
      </td>
    </tr>
    <tr>
      <td></td>
      <td>
	  <%= submit_tag t('diary_entry.edit.save_button') %>
	  <%# TODO: button should say 'publish' or 'save changes' depending on new/edit state %>
      </td>
    </tr>
  </table>
<% end %>

<% if @user.home_lat.nil? or @user.home_lon.nil? %>
  <% lon = h(params['lon'] || '-0.1') %>
  <% lat = h(params['lat'] || '51.5') %>
  <% zoom = h(params['zoom'] || '4') %> 
<% else %>
  <% lon = @user.home_lon %>
  <% lat = @user.home_lat %>
  <% zoom = '12' %>
<% end %>

<%= javascript_include_tag '/openlayers/OpenLayers.js' %>
<%= javascript_include_tag '/openlayers/OpenStreetMap.js' %>
<%= javascript_include_tag 'map.js' %>

<script type="text/javascript">
  <!--
  var marker;

  OpenLayers.Lang.setCode("<%= I18n.locale.to_s %>");

  function init() {
    var centre = new OpenLayers.LonLat(<%= lon %>, <%= lat %>);
    var zoom = <%= zoom %>;

    var map = createMap("map");

    setMapCenter(centre, zoom);

    map.events.register("click", map, setLocation);
  }        

  function setLocation( e ) { 
    closeMapPopup();

    var lonlat = getEventPosition(e);

    document.getElementById('latitude').value = lonlat.lat;
    document.getElementById('longitude').value = lonlat.lon;

    if (marker) {
      removeMarkerFromMap(marker);
    }

    marker = addMarkerToMap(lonlat, null, "<%= t 'diary_entry.edit.marker_text' -%>");
  }

  function openMap() {
    $("map").style.display = "block";
    $("usemap").style.display = "none";
  }

  window.onload = init;
// -->
</script>
