var Marker = new GIcon();
var Marker = Object.extend(Marker, {
    image: '/markers/f72.png',
    shadow: '/markers/s.png',
    iconSize: new GSize(20,34),
    shadowSize: new GSize(36,34),
    iconAnchor: new GPoint(10,34),
    infoWindowAnchor: new GPoint(0,1)
});

var MarkerRoute = new GIcon();
var MarkerRoute = Object.extend(MarkerRoute, {
    image: '/markers/small/f72.png',
    shadow: '/markers/small/s.png',
    iconSize: new GSize(12,20),
    shadowSize: new GSize(22,20),
    iconAnchor: new GPoint(6,20),
    infoWindowAnchor: new GPoint(5,1)});

var Place = Class.create();
Place.prototype = {
    itemSelectedClass: 'listItem listItemSelected',
    itemClass: 'listItem',
    initialize: function(parent, name, description, sequence)
    {
        this.sequence = sequence;
        this.type = 1;
        this.selected = false;
        this.parent = parent;
        this.name = name;
        this.tags = '';
        this.description = description;
        this.createDiv();
        this.latlng = null;
        this.marker = null;
    },
    addMarkerCore: function(point)
    {
      var my_icon = this.parent.source_marker;
      this.latlng = point;
      this.marker = new GMarker(this.latlng, {draggable: false, icon: my_icon});
      this.marker.parent = this;
      this.parent.map.gMap.addOverlay(this.marker);
      var map = this.parent.map.gMap;
      var marker = this.marker;
      var parent = this;
      GEvent.addListener(this.marker, "click", function() {
          marker.parent.parent.selectItem(marker.parent);
      });
      GEvent.addListener(this.marker, "dragstart", function() {
          //marker.parent.parent.selectItem(marker.parent);
          if (!marker.parent.selected)
              marker.parent.parent.deselectAllItems();
      });	
      GEvent.addListener(this.marker, "dragend", function() {
          parent.latlng = marker.getPoint();
          //marker.parent.parent.selectItem(marker.parent);
      });
    },
    addMarker: function(point)
    {
        var my_icon = this.parent.source_marker;
        if (this.selected)
            my_icon = this.parent.source_marker_route;
        if (this.marker == null && point != null)
        {
            this.latlng = point;
        } else return;
        this.marker = new GMarker(this.latlng, {draggable: false, icon: my_icon});
        this.marker.parent = this;
        this.parent.map.gMap.addOverlay(this.marker);
        var map = this.parent.map.gMap;
        var marker = this.marker;
        var parent = this;
        GEvent.addListener(this.marker, "click", function() {
            marker.parent.parent.selectItem(marker.parent);
        });
    },
    removeOverlay: function()
    {
        if (this.marker != null)
            this.parent.map.gMap.removeOverlay(this.marker);
        this.marker = null;
    },
    refreshMarker: function()
    {
        this.removeOverlay();
        this.addMarker(this.latlng);
    },
    createDiv: function()
    {
        var div = document.createElement('div');
        this.header = document.createElement('div');
        this.header.className = this.itemClass;
        if (this.selected)
            this.header.className = this.itemSelectedClass;
        this.header.onclick = this.onclick.bind(this);
        this.headerName = document.createElement('div');
        this.headerName.innerHTML = this.name;
        
        this.header.appendChild(this.headerName);
        div.appendChild(this.header);

        this.div = div;
    },
    getDiv: function()
    {
        return this.div;
    },
    onclick: function()
    {
        if (this.selected)
            this.parent.deselectAllItems();
        else
        {
            this.parent.selectItem(this, true);
        }
    },
    select: function(state)
    {
        var oldstate = this.selected;
        if (state)
        {
            this.selected = state;
            this.header.className = this.itemSelectedClass;
            if (oldstate != state) this.refreshMarker();
            var html = "<div class='viewerInfoWindow'>";
            html += "<span class='orangeButton' style='padding-left:3px; padding-right:5px; float:right' onclick='infoWindowZoom(this, " + this.marker.getPoint().lat()   +  " , " + this.marker.getPoint().lng()  + ")'>" + lang[29] +"</span>";
            html += "<h2>" + this.name + "</h2>";
            if (this.description != "")
              html += "<hr>" + this.description;
            html += "</div>";
            this.marker.openInfoWindowHtml(html);
        }
        else
        {
            this.selected = state;
            this.header.className = this.itemClass;
            if (oldstate != state) this.refreshMarker();
            this.parent.map.gMap.closeInfoWindow();
        }
    },
    onNameInput: function()
    {
        this.name = this.nameInput.value;
        this.headerName.innerHTML = this.name;
    },
    onDescriptionInput: function()
    {
        this.description = this.descriptionInput.value;
    },
    onTagsInput: function()
    {
        this.tags = this.tagsInput.value;
    },
    onRemovePlace: function()
    {
        if (confirm(lang[9] + ' ' + this.name + '?'))
        {
            this.parent.deselectAllItems();
            this.remove();
            this.parent.deleteItem(this);
        }
    },
    remove: function()
    {
        this.removeOverlay();
    },
    onMoveUpPlace: function()
    {
        this.parent.moveItem(this, -1);
    },
    onMoveDownPlace: function()
    {
        this.parent.moveItem(this, 1);
    },
    centerMapOnIt: function()
    {
        if (this.marker != null)
            this.parent.map.gMap.panTo(this.latlng);
    },
    initialized: function()
    {
        return (this.marker != null);
    }
}

function infoWindowZoom(div, lat, lng)
{
  if (div.zoomState == 0)
  {
      map.gMap.returnToSavedPosition();
      if (map.gMap.getZoom() == 17)
        map.returnToBounds();
      div.innerHTML=lang[29];
      div.zoomState = 1;
  }
  else
  {
    map.gMap.savePosition();
    div.innerHTML=lang[30];
    map.gMap.setCenter(new GLatLng(lat, lng));
    map.gMap.setZoom(17);
    div.zoomState = 0;
  }
}
