﻿/* Copyright (c) Hoseasons Holidays Limited
* Author: Chris O'Brien
* Date: 01 September 2009
*/

var DestinationMap = {
  MAP_CANVAS_ID: "destinationSubRegionMap",
  map: null,
  startingpoint: "",
  data: { 
    center: { lat: 0.0, lng: 0.0 },
    minZoom: 5,
    maxZoom: 14,
    zoom: 6,
    markers: []
  },
  overlays: [],
  
  mapLoad: function() {
    
    if (GBrowserIsCompatible()) {
      DestinationMap.localSearch = new GlocalSearch();
      DestinationMap.map = new GMap2(document.getElementById(this.MAP_CANVAS_ID));
      GEvent.addListener(DestinationMap.map, "load", DestinationMap.addLoadingReport);
      DestinationMap.map.addControl(new GMapTypeControl());
      DestinationMap.map.addControl(new GLargeMapControl());
      DestinationMap.map.addControl(new GScaleControl());
      G_PHYSICAL_MAP.getMinimumResolution = function() { return DestinationMap.data.minZoom };
      G_NORMAL_MAP.getMinimumResolution = function() { return DestinationMap.data.minZoom };
      G_SATELLITE_MAP.getMinimumResolution = function() { return DestinationMap.data.minZoom };
      G_HYBRID_MAP.getMinimumResolution = function() { return DestinationMap.data.minZoom };

      G_PHYSICAL_MAP.getMaximumResolution = function() { return DestinationMap.data.maxZoom };
      G_NORMAL_MAP.getMaximumResolution = function() { return DestinationMap.data.maxZoom };
      G_SATELLITE_MAP.getMaximumResolution = function() { return DestinationMap.data.maxZoom };
      G_HYBRID_MAP.getMaximumResolution = function() { return DestinationMap.data.maxZoom };

      DestinationMap.PlotPointOnMap();
    }
    GEvent.addListener(DestinationMap.map, "zoomend", DestinationMap.mapOnZoomEnd);
    GEvent.addListener(DestinationMap.map, "dragend", DestinationMap.mapOnDragEnd);
  },
  
  addLoadingReport: function() {
    // Text to display 
    var loadingText = 'Loading Map...';

    // Create a new element 
    var info = document.createElement('div');
    info.appendChild(document.createTextNode(loadingText));

    // Add an id to the element, in case you want to access it later 
    info.setAttribute('id', 'mapLoading');

    // Alternatively, you could apply the styles through a CSS stylesheet, using the #mapLoading selector 
    info.style.position = 'relative';
    info.style.padding = '2em';
    info.style.fontWeight = 'bold';
    info.style.fontSize = '12px';
    info.style.color = '#333';

    // Insert into map container
    this.getContainer().insertBefore(info, this.getContainer().firstChild);
  },
  
  mapOnZoomEnd: function(oldZoom, newZoom) {
    DestinationMap.zoom = newZoom;
    DestinationMap.drawOverlays();
  },

  mapOnDragEnd: function() {
    DestinationMap.drawOverlays();
  },
  
  PlotPointOnMap: function() {
    DestinationMap.startingpoint = new GLatLng(DestinationMap.data.center.lat, DestinationMap.data.center.lng);
    DestinationMap.map.setCenter(DestinationMap.startingpoint, parseInt(DestinationMap.data.zoom));
    DestinationMap.CallSuccess();
  },
  
  CallSuccess: function() {
    
    //Create Markers
    for(var k = 0; k < DestinationMap.data.markers.length; k++) {
      DestinationMap.data.markers[k].marker = DestinationMap.createMarker(parseFloat(DestinationMap.data.markers[k].lat),
                                                                          parseFloat(DestinationMap.data.markers[k].lng), k+1);
        
      var handler = DestinationMap.createMarkerClickHandler(DestinationMap.data.markers[k]);
      GEvent.addListener(DestinationMap.data.markers[k].marker, "click", handler);
      DestinationMap.data.markers[k].clickHandler = handler;
    }

    DestinationMap.drawOverlays(); //Uses DestinationMap.data.markers
  },
  
  drawOverlays: function() {
    try {
      //Clear overlays
      for (var k = 0; k < DestinationMap.overlays.length; k++) {
        DestinationMap.overlays[k].overlayOn = false;
        DestinationMap.map.removeOverlay(DestinationMap.overlays[k]);
      }
      DestinationMap.overlays = [];

      var latitude = 0.0;
      var longitude = 0.0;

      //Divide map into grid.
      var gLatLngBounds = DestinationMap.map.getBounds();
      var southWest = gLatLngBounds.getSouthWest();
      var northEast = gLatLngBounds.getNorthEast();
      var zoom = DestinationMap.map.getZoom();

      //Draw single markers.
      var singleSiteData = {};
      for (var k = 0; k < DestinationMap.data.markers.length; k++) {
        singleSiteData = DestinationMap.data.markers[k];
        if (singleSiteData.lng > southWest.lng() && singleSiteData.lng < northEast.lng() &&
          singleSiteData.lat > southWest.lat() && singleSiteData.lat < northEast.lat()) {
          DestinationMap.drawSingleOverlay(singleSiteData);
        }
      }
    } catch(Error) { }
  },
  
  drawSingleOverlay: function(markerData) {
    DestinationMap.map.addOverlay(markerData.marker);
    markerData.marker.overlayOn = true;
    DestinationMap.overlays.push(markerData.marker);
  },
  
  createMarker: function(lat, lng, index) {
    var point = new GLatLng(lat, lng);
    return new GMarker(point, DestinationMap.GetIcon(index));
  },
  
  createMarkerClickHandler: function(markerData) {
    return function() {
      if (!markerData.marker.overlayOn) {
        DestinationMap.map.addOverlay(site.marker);
        markerData.marker.overlayOn = true;
        DestinationMap.map.setCenter(site.marker.getLatLng(), DestinationMap.viewOnMapZoom);
      }
      markerData.marker.openInfoWindowHtml(
        '<table id="balloon" >\
          <tr>\
            <td>\
              <h1>' + markerData.name + '</h1>\
              <br/>\
              <a href="' + markerData.moreInfoURL + '">More info &gt;&gt;</a><br />\
              <a href="' + markerData.viewVillasURL + '">View villas &gt;&gt;</a>\
            </td>\
            <td>\
              <img src="' + markerData.imageURL + '">\
            </td>\
          </tr>\
        </table>'
      );
      return false;
    };
  },
  
  GetIcon: function(index) {
    var icon = new GIcon();
    icon.image = "../../Images/EUVilla/GoogleMapIcons/Region" + index + ".png";
    icon.iconAnchor = new GPoint(10, 34);
    icon.infoWindowAnchor = new GPoint(17, 0);
    icon.iconSize = new GSize(20, 34);
    return icon;
  }
};