var map = null;
var geocoder = null;
var markers = null;


function createMarker(point,id,paye) {
  var marker = new GMarker(point, { icon: getIcon(1+paye) });
  marker.id = id;
  //map.setCenter(point,10);
  map.setZoom(10);
  GEvent.addListener(marker, "click", function() {
    infoSociete(marker,id);
  });
  //alert("Ajout evenement list");
  var soc = document.getElementById("societe_"+id);
  if (soc != null) {
    GEvent.clearListeners(soc,"click");
    GEvent.addDomListener(soc,"click", function () {
      infoSociete(marker,id);
    });
  }
  //alert("Fin d'ajout evenement: "+soc);
  return marker;
}

function infoSociete(marker,id) {
  map.closeInfoWindow();
  hiddenOverview();
  desactivateRollOverMenu();
  desactivateRollOverMarker();
  var data = ADownloadUrl('./include/GoogleMap/info_ent.php?id_ent='+id);
  marker.openInfoWindowHtml(data);
  //map.panTo(new GLatLng(marker.getPoint().lat(),map.getCenter().lng()));
  GEvent.addListener(map, "infowindowclose", function() {
    GEvent.clearListeners(map,"infowindowclose");
    map.panTo(marker.getPoint());
    activateRollOverMenu();
    activateRollOverMarker();
    showOverview()
  });
  //Flash player, a cause de se putain d'IE qui lance pas le javascript contenu dans un flux recus par AJAX
  var node = document.getElementById('player2');
  if (node != null ) {
    var id_video = node.firstChild.id;
    var myflashvars = "file=swf/extras/php_mysql_playlist.php?id="+id_video;
    myflashvars += "&displayheight=240&repeat=false&scale=false&lightcolor=0xDE0016&backcolor=0x222222&frontcolor=0xFFFFFF&showdigits=true&autostart=true&showicons=true&thumbsinplaylist=true&linker=nolink&showfsbutton=true&fsreturnpage=chaine.php?id="+id_video+"&shuffle=false&callback=swf/extras/stat/statistics.php&full_screen=false";
    //alert("Creation variable flash");
    var FU = {
              movie:"swf/flvplayer.swf",
              width:"320",
              height:"280",
              majorversion:"8",
              build:"0",
              wmode:"transparent",
              embed:"transparent",
              bgcolor:"#FFFFFF",
              flashvars:myflashvars
            }
    //alert("Create UFO");
    UFO.create( FU, "player2");
  }
}

function miniInfoSociete(marker,id) {
  var data =  ADownloadUrl('./include/GoogleMap/minibule.php?id='+id);
  marker.openInfoWindowHtml(data);
}

function getIcon(isSociete) {
    var icon = new GIcon(isSociete);
    if ( isSociete >= 1 ) {
      if (isSociete > 1)
        icon.image = "./images/GoogleMap/punaise.png";
      else
        icon.image = "./images/GoogleMap/punaise_grise.png";
      icon.shadow = "./images/GoogleMap/ombre_punaise.png";
      icon.transparent = "./images/GoogleMap/transparent_ie.png";
      icon.iconSize = new GSize(36, 48);
      icon.shadowSize = new GSize(53,48);
      icon.iconAnchor = new GPoint(25, 44);
      icon.infoWindowAnchor = new GPoint(10, 8);
    } else {
      icon.image = "./images/GoogleMap/cible1.png";
      icon.iconSize = new GSize(36, 36);
      icon.iconAnchor = new GPoint(18, 18);
      icon.infoWindowAnchor = new GPoint(25, 7);
    }
  return icon;
}

function generate_markers() {
    map.clearOverlays();
    markers.clear();
    generate_label();
}

function zoom_has_changed(old_level, new_level) {
  if ((old_level <= 10 && new_level > 10) || (old_level > 10 && new_level <= 10)) {
    generate_markers();
  }
}

function desactivateRollOverMenu() {
  markers.each(function(marker) {
    GEvent.clearListeners(document.getElementById("societe_"+marker.id),"mouseover");
  });
}

function activateRollOverMenu() {
  markers.each(function(marker) {
    GEvent.clearListeners(document.getElementById("societe_"+marker.id),"mouseover");
    GEvent.addDomListener(document.getElementById("societe_"+marker.id),"mouseover", function () {
      miniInfoSociete(marker,marker.id);
    });
  });
}

function desactivateRollOverMarker() {
  markers.each(function(marker) {
    GEvent.clearListeners(marker,"mouseover");
    GEvent.clearListeners(marker,"mouseout");
  });
}

function activateRollOverMarker() {
  markers.each(function(marker) {
    GEvent.clearListeners(marker,"mouseover");
    GEvent.clearListeners(marker,"mouseout");
    GEvent.addListener(marker, "mouseover", function() {
      miniInfoSociete(marker,marker.id);
    });
    GEvent.addListener(marker, "mouseout", function() {
      map.closeInfoWindow();
    });
  });
}

function hiddenOverview() {
  if(ovmap.isHidden() != true)
    //setTimeout("ovmap.hide(true)",0);
    ovmap.hide(true);
}

function showOverview() {
  if(ovmap.isHidden() == true)
      //setTimeout("ovmap.show(true)",0);
      ovmap.show(true);
}
