var map;
var centerLongitude = 2.9;
var centerLatitude = 46.9;
var startZoom = 5;
var markerHash={};
var currentFocus=false;

function focusPoint(id){
  if (currentFocus) {
    Element.removeClassName("sidebar-item-"+currentFocus,"current");
  }
  Element.addClassName("sidebar-item-"+id,"current");
  if (markerHash[id].zoom > 0) {
    markerHash[id].marker.openInfoWindowHtml("<span style='color:#000;font-weight:bold;'>"+markerHash[id].pseudo+"</span> <span style='color:#000;'> - Level "+markerHash[id].level+"</span><br /><span style='color:#000;'>"+markerHash[id].city+"</span><br /><div style='color:#000;padding-top:10px;'><i>"+markerHash[id].comment+"</i></div><p></p><span style='color:#000;float:right;'><a href='#' onclick=\"zoomToSatUserView("+markerHash[id].lat+","+markerHash[id].lng+","+markerHash[id].zoom+"); return false;\">Satellite view</a></span>", {maxWidth:350});
  } else {
    markerHash[id].marker.openInfoWindowHtml("<span style='color:#000;font-weight:bold;'>"+markerHash[id].pseudo+"</span> <span style='color:#000;'> - Level "+markerHash[id].level+"</span><br /><span style='color:#000;'>"+markerHash[id].city+"</span><br /><div style='color:#000;padding-top:10px;'><i>"+markerHash[id].comment+"</i></div>", {maxWidth:350});
  }
  currentFocus=id;
}

function zoomToSatUserView(latsat, lngsat, zoomsat) {
  var latlng = new GLatLng(latsat, lngsat);
  map.setCenter(latlng,zoomsat,eval('G_SATELLITE_MAP'));
}

function filter(type){
  map.closeInfoWindow();
  var val = 0;
  if (type == 'Level') { type = parseInt(document.getElementById('chooselevel').value); val = 1;}
    for(i=0;i<markers.length;i++) {
      var current=markers[i];
      if (type == 'Chan') {
        current.type = current.irc?'Chan':'';
      } else if (type == 'Comment') {
        current.type = (current.comment == null || current.comment == "")?'':'Comment';
      } else if (type == 'Sat') {
        current.type = (current.typemapsettings == 'G_SATELLITE_MAP')?'Sat':'';
      }
      if (val == 1) {
        current.type = parseInt(current.level);
      }
      if (current.type == type || 'All' == type) {
        if ((document.getElementById("sidebar-item-"+markers[i].id)) != null) {
          Element.show("sidebar-item-"+markers[i].id)
        }
        if (!markerHash[current.id].visible) {
          map.addOverlay(markerHash[current.id].marker);
          markerHash[current.id].visible=true;
        }
      } else {
        if (markerHash[current.id].visible) {
          map.removeOverlay(markerHash[current.id].marker);
          markerHash[current.id].visible=false;
        }
        if ((document.getElementById("sidebar-item-"+markers[i].id)) != null) {
          Element.hide("sidebar-item-"+markers[i].id)
        }
      }
    }
}

function sortByLevel(a, b) {
  var x = parseInt(a.level);
  var y = parseInt(b.level);
  return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function addMarker(latitude, longitude, id) {
  var marker = new GMarker(new GLatLng(latitude, longitude), icon);
  GEvent.addListener(marker, 'click',
    function() {
      focusPoint(id);
    });
  map.addOverlay(marker);
  return marker;
}

function addPlayerToMap(element, selectedItem) {
  if (selectedItem.id != 0) {
    var IdPlayer = selectedItem.id;
    new Ajax.Request( 'account/search_player',
      { method: 'post',
        parameters: 'player='+IdPlayer,
        onComplete: function(request){
          res=eval( "(" + request.responseText + ")" );
          if(!res.success) {
            alert(res.content);
          } else {
            markers.push(res.content);
            if (markerHash[res.content.id] == null) {
              icon = new GIcon();
              icon.image = "/images/of2.png";
              icon.iconSize = new GSize(20, 20);
              icon.iconAnchor = new GPoint(20, 20);
              icon.infoWindowAnchor = new GPoint(5, 1);
              marker=addMarker(res.content.lat, res.content.lng, res.content.id);
              markerHash[res.content.id] ={marker:marker, pseudo:res.content.pseudo, city:res.content.city, level:res.content.level, comment:(res.content.comment == null || res.content.comment == '')?'':res.content.comment,zoom:(res.content.typemapsettings == 'G_SATELLITE_MAP')?parseInt(res.content.zoomsettings):'',lat:parseFloat(res.content.lat),lng:parseFloat(res.content.lng),visible:true};
            }
            if (!markerHash[res.content.id].visible) {
              map.addOverlay(markerHash[res.content.id].marker);
              markerHash[res.content.id].visible=true;
            }
            map.panTo(new GLatLng(parseFloat(res.content.lat),parseFloat(res.content.lng)));
            focusPoint(res.content.id);
	  }
	}
      });
  }
}

function init() {
  handleResize();
  icon = new GIcon();
  icon.image = "/images/of.png";
  icon.iconSize = new GSize(20, 20);
  icon.iconAnchor = new GPoint(20, 20);
  icon.infoWindowAnchor = new GPoint(10, 0);
	
  map = new GMap2(document.getElementById('map'));
  map.addControl(new GMapTypeControl());
  map.addControl(new GLargeMapControl());
  map.enableDoubleClickZoom();
  map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	
  for(i=0;i<markers.length; i++) {
    var current =markers[i];
    marker=addMarker(parseFloat(current.lat), parseFloat(current.lng), parseInt(current.id));
    markerHash[current.id]={marker:marker,pseudo:current.pseudo,city:current.city,level:current.level,comment:(current.comment == null)?'':current.comment,zoom:(current.typemapsettings == 'G_SATELLITE_MAP')?parseInt(current.zoomsettings):'',lat:parseFloat(current.lat),lng:parseFloat(current.lng),visible:true};
  }
}

Event.observe(window, 'load', init, false);
Event.observe(window, 'resize', handleResize, false);
