﻿// Author: Kris
// Project: C:\code\RealosophyDotCom\RealosophyV2\
// Path: C:\code\RealosophyDotCom\RealosophyV2\javascripts\RealFramework
// Lines: 313
// Creation date: 8/30/2008 10:11 PM
// Last modified: 11/29/2009 9:25 PM

/**
 * Copyright Realosophy.com 2007
 * Author: Kris Kasias
 * Purpose: Implements the IService Interface to provide a Google Maps Service
*/

// Load up the Google API
google.load("maps", "2.x" /*, { "callback": mapsLoaded }*/);

Type.registerNamespace("Realosophy.Web.Client.Map");

Realosophy.Web.Client.Map.RMarker = function (lat, lng, regionName, icon) {
  this._regionName = regionName;
  this._gPoint = new google.maps.LatLng(lat, lng, false);
  this._map = null;
  this._pathToMarkerImages = 'http://' + RealSystem.get_currentHostName() + '/images/markers/';

  /* setup the marker */
  if (!icon) {
    icon = new google.maps.Icon();

    icon.image = this._pathToMarkerImages + "iconb.png";
    icon.iconSize = new google.maps.Size(20, 34);
    icon.iconAnchor = new google.maps.Point(10, 34);
    icon.infoWindowAnchor = new google.maps.Point(9, 2);
    icon.infoShadowAnchor = new google.maps.Point(15, 22);
  }
  this._gMarker = new google.maps.Marker(this._gPoint, icon);
}

Realosophy.Web.Client.Map.RMarker.prototype = {
  get_regionName: function() {
    return this._regionName;
  },

  addToMap: function(map, clickHandler) {
    this._map = map;
    this._map.get_map().addOverlay(this._gMarker);
    google.maps.Event.addListener(this._gMarker, "click", clickHandler);
  },

  removeFromMap: function() {
    this._map.removeOverlay(this._gMarker);
    this._map = null;
  },

  get_Marker: function() {
    return this._gMarker;
  },

  getPoint: function() {
    return this.get_Marker().getPoint();
  },

  openInfoWindowHtml: function(html) {
    this.get_Marker().openInfoWindowHtml(html);
  },

  dispose: function() {
    this._gPoint = null;
    this._gMarker = null;
  }
}
Realosophy.Web.Client.Map.RMarker.registerClass('Realosophy.Web.Client.Map.RMarker', Sys.Component, Sys.IDisposable);

Realosophy.Web.Client.Map.RMap = function(element, zoomLevel, supportDragEnd, enableSearchBar) {
  this._mapElement              = $get(element);
  this._map                     = null;
  this._currentLocation         = null;
  this._centrePoint             = null;
  this._defaultCentrePoint      = new google.maps.LatLng(43.69915480258559, -79.43458557128906, false);
  this._zoomLevel               = zoomLevel;
  this._defaultZoomLevel        = 11;
  this._zoomControl             = null;
  this._mapTypeControl          = null; 
  this._enableDoubleClickZoom   = null;    
  this._enableContinuousZoom    = null;
  this._visiblePolygon          = null;
  this._supportDragEnd          = supportDragEnd;
  this._enableSearchBar         = enableSearchBar;
  this._pathToMarkerImages      = 'http://' + RealSystem.get_currentHostName() + '/images/markers/';
  this._markers                 = null;
}

Realosophy.Web.Client.Map.RMap.prototype = {
  /* Start Properties */
  get_serviceNameKey: function () {
    return "RealServiceMap";
  },

  get_serviceName: function () {
    return "Realosophy.Web.Client.Map.RMap";
  },

  get_serviceDescription: function () {
    return "Provides a service interface to the Realosophy Map Provider (Google).";
  },

  /**
  * Obtain a reference to the internal map object
  * @return GMap2
  */
  get_map: function () {
    return this._map;
  },

  get_mapElement: function () {
    return this._mapElement;
  },

  /**
  Obtain a reference to all markers
  */
  get_Markers: function () {
    return this._markers;
  },

  get_Marker: function (markerIndex) {
    return this._markers[markerIndex];
  },

  add_Marker: function (marker) {
    var map = this.get_map();
    this._markers.push(marker);
    map.addOverlay(marker);
  },

  add_Markers: function (markers) {
    for (var i = 0; i < markers.length; i++) {
      this.add_Marker(markers[i]);
    }
  },

  remove_Marker: function (markerIndex) {
    var map = this.get_map();
    var marker = this.get_Marker(markerIndex);
    map.removeOverlay(marker);
  },

  /**
  * Sets the current Centre Point of the map.
  * @param lat Represents the Latitude of the new Centre Point
  * @param lng Represents the Longitude of the new Centre Point
  */
  set_centrePoint: function (latLng) {
    this._centrePoint = latLng;
    this.get_map().setCenter(latLng, this.get_zoomLevel());
    return this._centrePoint;
  },

  get_centrePoint: function () {
    return this._centrePoint == null ? this._defaultCentrePoint : this.get_map().getCenter();
  },

  set_zoomLevel: function (zoomLevel) {
    this._zoomLevel = zoomLevel;
    this.get_map().setZoom(zoomLevel);
  },
  get_zoomLevel: function () {
    return this._zoomLevel == null ? this._defaultZoomLevel : this._zoomLevel;
  },

  get_supportDragEnd: function () {
    return this._supportDragEnd;
  },

  set_dragEnd: function (eventHandler) {
    this._supportDragEnd = true;
    google.maps.Event.addListener(this.get_map(), "dragend", eventHandler);
  },

  get_enableDoubleClickZoom: function () {
    return this._enableDoubleClickZoom;
  },
  set_enableDoubleClickZoom: function (value) {
    this._enableDoubleClickZoom = value;
  },

  get_enableContinuousZoom: function () {
    return this._enableContinuousZoom;
  },

  set_enableContinuousZoom: function (value) {
    this._enableContinuousZoom = value;
  },

  get_enableScrollWheelZoom: function () {
    return this._enableScrollWheelZoom;
  },
  set_enableScrollWheelZoom: function (value) {
    this._enableScrollWheelZoom = value;
  },

  formatListInfoWindowContent: function (RegionName) {
    var content = document.createElement("div");
    var url = "NeighbourhoodProfile.aspx?city=Toronto&district=" + RealSystem.URLEncode(RegionName);
    content.innerHTML = "<span class='bold'>" + RegionName + "</span><br /><p><a href=\"" + url + "\"><img src='images/profile.jpg' style='border:none'><span style='padding-left:10px' class='MediumHeader'>go to profile</span></a>";
    return content;
  },

  /* End Properties */

  /* Start Methods */
  //  initialize: function() {
  //    RealSystem.trace("Initialize of Realosophy.Web.Client.Map.RMap called.");
  //    this._defaultCentrePoint = new GLatLng(43.69915480258559, -79.43458557128906);
  //  },

  showMap: function () {
    if (GBrowserIsCompatible() && (this.get_mapElement() != null)) {
      if (this._enableSearchBar) {
        var mapOptions = {
          googleBarOptions: {
            style: "new"
          }
        }
        this._map = new google.maps.Map2(this.get_mapElement(), mapOptions);
        this._map.setUIToDefault();
        this._map.enableGoogleBar();
      } else {
        this._map = new google.maps.Map2(this.get_mapElement());
      }
      if (this._enableDoubleClickZoom)
        this._map.enableDoubleClickZoom();

      if (this._enableContinuousZoom)
        this._map.enableContinuousZoom();

      if (this._enableScrollWheelZoom)
        this._map.enableScrollWheelZoom();

      this._map.addControl(new google.maps.LargeMapControl());
      this._map.addControl(new google.maps.MapTypeControl());
      this.set_centrePoint(this.get_centrePoint());
    }
  },

  /**
  * Draws a polygon on the map from the Array of points provided
  * @param points A multi-dimensional array of points
  */
  drawPolygon: function (points, context) {
    if (context._visiblePolygon)
      context.get_map().removeOverlay(context._visiblePolygon);

    var llArray = [];
    for (var i = 0; i < points.length; i++) {
      llArray.push(new GLatLng(points[i].lat, points[i].lng));
    }
    var poly = new GPolygon(llArray, null, 2, 0.7, "#E3EAEB", 0.5, { clickable: false });
    context.get_map().addOverlay(poly);
    context._visiblePolygon = poly;
  },

  /**
  * Adds a polygon to the map from the Array of points provided
  * will not clear other overlays first.  Use drawPolygon to 1st clear old polygons
  * @param points A multi-dimensional array of points
  * @param context A reference to the Map object to be used
  */
  addPolygon: function (points, context) {
    var mapObj;
    if (context)
      mapObj = context;
    else
      mapObj = this;

    var llArray = [];
    for (var i = 0; i < points.length; i++) {
      llArray.push(new GLatLng(points[i].lat, points[i].lng));
    }
    var poly = new GPolygon(llArray, null, 2, 0.7, "#E3EAEB", 0.5, { clickable: false });
    mapObj.get_map().addOverlay(poly);
    mapObj._visiblePolygon = true;
    return poly;
  },

  showNeighbourhoodListInfoWindow: function (regionIndex) {
    var map = this.get_map();
    var _this = this;
    var marker = this._markers[regionIndex];
    var regionName = marker.get_regionName();
    var content = this.formatListInfoWindowContent(regionName);
    marker.get_Marker().openInfoWindow(content);
    Realosophy.Web.Service.FindNearby.GetDistrictPolygon(regionName, _this.drawPolygon, null, _this);
    map.setCenter(marker.getPoint(), 13);
  },

  showMarker: function (marker) {
    var _this = this;
    var regionName = marker.get_regionName();
    var content = this.formatListInfoWindowContent(regionName);
    marker.addToMap(this, function () {
      marker.get_Marker().openInfoWindow(content);
      Realosophy.Web.Service.FindNearby.GetDistrictPolygon(regionName, _this.drawPolygon, null, _this);
      _this.get_map().setCenter(marker.getPoint());
    });
  },

  showMarkers: function (markers) {
    for (var i = 0; i < markers.length; i++) {
      this.showMarker(markers[i]);
    }
  },

  createMarkers: function (markerInfo) {
    var markers = new Array();
    for (var i = 0; i < markerInfo.length; i++) {
      markers.push(new Realosophy.Web.Client.Map.RMarker(markerInfo[i].lat, markerInfo[i].lng, markerInfo[i].n))
      //this.createMarker(i, markerInfo[i])
    } // for
    return markers;
  },

  plotRegionMarkers: function (markers) {
    this._markers = this.createMarkers(markers);
    this.showMarkers(this._markers);
  },

  dispose: function () {
    RealSystem.trace("Dispose of Realosophy.Web.Client.Map.RMap called.");
    GUnload();
  }
  /* End Methods */

  /* Start Event Delegates */
  /* End Event Delegates */
}
Realosophy.Web.Client.Map.RMap.registerClass('Realosophy.Web.Client.Map.RMap', Sys.Component, Realosophy.Web.Client.IService, Sys.IDisposable);

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
