function initHomePage(){
  //arenasearchbox
  $(".arenasearchbox").focus();
}
function initMapV3(addr){
  var location = null;
  var map = null;
  var directionsDisplay = new google.maps.DirectionsRenderer();
  var directionsService = new google.maps.DirectionsService();
  
  geocoder = new google.maps.Geocoder();
  geocoder.geocode( { 'address': addr}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      location = results[0].geometry.location;
      createMapOnLocation(location);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
  function createMapOnLocation(location){
    //var latlng = new google.maps.LatLng(location.lat, location.lng);
    var myOptions = {
      zoom: 16,
      center: location,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map"), myOptions);
    var marker = new google.maps.Marker({
        map: map,
        position: location
    });    
  }
  if(startlocation){
    directionsPanel = document.getElementById("route");
    directions = new GDirections(map, directionsPanel);
    directions.load("from: "+startlocation+" to: "+addr);
  }
  $("#startlocform").submit(function(){
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("route"));
    calcRoute();
    return false;
  });
  
  function calcRoute() {
    var start = $("#startlocfield").val();
    var end = location;
    var request = {
      origin:start,
      destination:end,
      travelMode: google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }else{
        alert("Il y a eu une erreur dans le calcule de l'itinéraire.");
      }
    });
  }
  
}

function initMapV2(addr){
  map = new GMap2(document.getElementById('map'));
  geocoder = new GClientGeocoder();

  //var burnsvilleMN = new GLatLng(44.797916,-93.278046);
  //map.setCenter(burnsvilleMN, 8);
  //showAddress("866 boul. Gérard Cadieux Beauharnois, Québec  J6N 2G2")
  showAddress(addr );
  $("#map").show();
  if(startlocation){
    directionsPanel = document.getElementById("route");
    directions = new GDirections(map, directionsPanel);
    directions.load("from: "+startlocation+" to: "+addr);
  }
  $("#startlocform").submit(function(){
    if(!directions){
      directionsPanel = document.getElementById("route");
      directions = new GDirections(map, directionsPanel);
    }
    document.getElementById("route").innerHTML = "";
    directions.load("from: "+$("#startlocfield").val()+" to: "+addr,{"locale":"fr_CA"});
    //GEvent.addListener(directions, "load", function() {
    //}
    GEvent.addListener(directions, "error", function() {
      document.getElementById("route").innerHTML = "<span style='color: brown'>Malheureusement, l'adresse que vous avez entrée n'a pas pu être trouvée.</span>";
      s = directions.getStatus();
      try{
        if(s['code'] == 200){
          alert("200 baby");
        }
      }catch(e){
        alert("Il y a eu une erreur attrappée");
      }
    });
    
    return false;
  });
}
function initArenaPage(){
  addr = $(".street-address").html() +" "+ $(".locality").html() +" "+ $(".region").html() +" "+ $(".postal-code").html();
  initMapV3(addr);
  //initMapV2(addr);
}
function initArenasPage(){
}
function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
      if (!point) {
        //alert(address + " not found");
        document.getElementById("map").innerHTML = "Couldn't find this address on a map.";
      } else {
        map.setCenter(point,13);
        //map.addControl(new GSmallMapControl());
        //map.setMapType(G_HYBRID_MAP);
        map.setZoom(16);
        map.setUIToDefault();
        
        var marker = new GMarker(point);
        marker.title = "Click to get directions from Google maps";
        map.addOverlay(marker);
        var durl = 'http://maps.google.com/?q='+escape(address);
        GEvent.addListener(marker, "click", function (){
          infowin = map.getInfoWindow();
          infowin.setLatLng(point);
          infowin.show();
          //window.open('http://maps.google.com/?q='+escape(address),'mywindow','');
        });
        
        
        //marker.openInfoWindowHtml("<h1>Hello</h1>"+address);
        //marker.openInfoWindowHtml(document.createTextNode("Hello, world"));
      }
      }
    );
  }
}
