var gMap = null;
var gBaseIcon = null;

var gInfoWindow = null;
var gMarkers = new Array();

var gIcons = new Array();
var gCentreFrance = null;
var gDefaultZoom = 5;
var gCircle = null;
var gStreetViewEnabled = false;
var gMaxZoom = 14;


function gmap_initialize(div_id) {

  gCentreFrance = new google.maps.LatLng(46.7, 1.47);

  var options = {
    zoom: gDefaultZoom,
    center: gCentreFrance,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    scrollwheel: false
  };

  gMap = new google.maps.Map(document.getElementById(div_id), options);

  gIcons[0] = "/images/googlemaps_1.png";
  gIcons[1] = "/images/googlemaps_2.png";

  gInfoWindow = new google.maps.InfoWindow();
}

function resizeMap() {
  google.maps.event.trigger(gMap, "resize");
  refitMarkers();
}

function enableStreetView() {
  gStreetViewEnabled = true;
}

function setMaxZoom(zoom) {
  gMaxZoom = zoom;
}

function initStreetView(position) {
  // streetview

  var panoramaOptions = {
      position: position,
      pov: {
        heading: 0,
        pitch: 10,
        zoom: 0
      }
  };
  var panorama = new google.maps.StreetViewPanorama(document.getElementById("pano"), panoramaOptions);
  // TODO if (panorama == null)
  //  alert("pas de visualisation streetview");
  gMap.setStreetView(panorama);
}


function addMarker(id, point, iconCode) {

  var marker = new google.maps.Marker({
      position: point,
      map: gMap,
      icon: gIcons[iconCode]
  });

  gMarkers[id] = marker;

  google.maps.event.addListener(marker, 'click', function() {
    //openInfoWindow(id);
    gotoProgramme(id);
  });
}


function openInfoWindow(id) {
  if(gMarkers[id] != null) {
    gInfoWindow.close();

    var request = new Request({url: '/lib/make_google_maps_infowindow.cfm', onSuccess: function(response){
       gInfoWindow.setContent(response);
       gInfoWindow.open(gMap, gMarkers[id]);
    }}).get('i=' + id);

  }
}


function gotoProgramme(id) {
  if(gMarkers[id] != null) {
    gInfoWindow.close();

    var request = new Request({url: '/lib/make_google_maps_programme_url.cfm', onSuccess: function(response){
       //gInfoWindow.setContent(response);
       //gInfoWindow.open(gMap, gMarkers[id]);
       if (response != "")
        document.location.href = response;
    }}).get('i=' + id);

  }
}


function createMarkers(data) {
  var bounds = new google.maps.LatLngBounds();

  for (var i = 0; i < data.length; i++) {
	var point = new google.maps.LatLng(data[i][1], data[i][2]);
	var icon = 0;
	if (data[i].length == 4)
	  icon = data[i][3];
	  addMarker(data[i][0], point, icon);
    bounds.extend(point);
    if (i == 0) {
        streeViewPosition = point;
    }
  }

  if (bounds.isEmpty()){
    gMap.setCenter(gCentreFrance);
    gMap.setZoom(gDefaultZoom);
  } else {
    gMap.fitBounds(bounds);

    var listener = google.maps.event.addListener(gMap, "idle", function() {

      if (gMap.getZoom() > gMaxZoom)
        gMap.setZoom(gMaxZoom);
      else if (gMap.getZoom() < 4)
        gMap.setZoom(4);

      if (gStreetViewEnabled) {
          initStreetView(streeViewPosition);
      }
      google.maps.event.removeListener(listener);
    });
  }

}


function refitMarkers(data) {
  var bounds = new google.maps.LatLngBounds();
  s = "";
  for (i in gMarkers) {
	  if (!isNaN(i)) {
      bounds.extend(gMarkers[i].getPosition());
    }
  }

  if (bounds.isEmpty()){
    gMap.setCenter(gCentreFrance);
    gMap.setZoom(gDefaultZoom);
  } else {
    gMap.fitBounds(bounds);

    var listener = google.maps.event.addListener(gMap, "idle", function() {

      if (gMap.getZoom() > gMaxZoom)
        gMap.setZoom(gMaxZoom);
      else if (gMap.getZoom() < 4)
        gMap.setZoom(4);

      google.maps.event.removeListener(listener);
    });
  }
}


