﻿// Author: Kris
// Project: C:\code\RealosophyDotCom\RealosophyV2\
// Path: C:\code\RealosophyDotCom\RealosophyV2\javascripts
// Lines: 329
// Creation date: 8/30/2008 10:11 PM
// Last modified: 1/10/2009 10:32 PM

//////////////////////////////////////////////////////////////////////////////////////////////
// Author: Kris Kasias
// Date: January 26, 2007
// Purpose:  Google Maps Api interaction for Realosophy.com
//////////////////////////////////////////////////////////////////////////////////////////////


var HasPhotos = false;
var photoOverlay;
var HasWiki = false;
var wikiOverlay;

var pathToMarkerImages = 'http://' + RealSystem.get_currentHostName() + '/images/markers/';

function OnMapLoad(element, zoomLevel, supportDragEnd, showWiki, showPanoramio) {
  var myMapDiv = $get(element);
  if (myMapDiv == null)
    return;
    
  photoOverlay = new google.maps.Layer("com.panoramio.all");
  wikiOverlay = new google.maps.Layer("org.wikipedia.en");

  if (defaultCentrePoint == null) {
    defaultCentrePoint = new Array(43.69915480258559, -79.43458557128906);
    zoomLevel = 11;
  }
  
  var point = new google.maps.LatLng(defaultCentrePoint[0], defaultCentrePoint[1], false);

  if (GBrowserIsCompatible()) {
    var mapOptions = {
      googleBarOptions: {
        style: "new"
      }
    }

    map = new google.maps.Map2(myMapDiv, mapOptions);
    map.setUIToDefault();
    map.enableGoogleBar();
    
    map.addControl(new google.maps.LargeMapControl());
    map.addControl(new google.maps.MapTypeControl());
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    map.setCenter(point, zoomLevel);
    if (supportDragEnd) {
      google.maps.Event.addListener(map, "dragend",
        function() {
          if (lastSearchParam == null) return;
          ProcessPOI(lastSearchParam.get_Header(), lastSearchParam.get_ShowHeader());
        }
      );
    }

    if (showPanoramio == true)
      togglePhoto();

    if (showWiki == true)
      toggleWiki();
    
    $addHandler(window, "unload", RealUnload);
  }

  //Get the map markers setup
  setupIcons();

  searchKeywords = new Array(
    new Realosophy.Web.Client.POIOptions("Elementary", "SC_Elem", "Elementary", icons[Realosophy.Web.Client.IconType.School]),
    new Realosophy.Web.Client.POIOptions("Secondary", "SC_Second", "Secondary", icons[Realosophy.Web.Client.IconType.School]),
    new Realosophy.Web.Client.POIOptions("Private", "SC_Priv", "Private", icons[Realosophy.Web.Client.IconType.School]),
    new Realosophy.Web.Client.POIOptions("Day Care", "SC_DayCare", "Daycare", icons[Realosophy.Web.Client.IconType.School]),
    new Realosophy.Web.Client.POIOptions("Restaurant", "EN_Rest", "Restaurant", icons[Realosophy.Web.Client.IconType.Entertainment]),
    new Realosophy.Web.Client.POIOptions("Cafe", "EN_Cafe", "Cafe", icons[Realosophy.Web.Client.IconType.Entertainment]),
    new Realosophy.Web.Client.POIOptions("Theatre", "EN_Theatre", "category: Theatres", icons[Realosophy.Web.Client.IconType.Entertainment]),
    new Realosophy.Web.Client.POIOptions("Art Gallery", "EN_Arts", "category: Art Galleries, Dealers & Consultants", icons[Realosophy.Web.Client.IconType.Entertainment]),
    new Realosophy.Web.Client.POIOptions("Book Store", "EN_Book", "category: Book Dealers Retail", icons[Realosophy.Web.Client.IconType.Entertainment]),
    new Realosophy.Web.Client.POIOptions("Hospital", "HE_Hosp", "category: Hospitals -Animal", icons[Realosophy.Web.Client.IconType.Health]),
    new Realosophy.Web.Client.POIOptions("Doctor", "HE_Dr", "Dr.", icons[Realosophy.Web.Client.IconType.Health]),
    new Realosophy.Web.Client.POIOptions("Drug Store", "HE_Drug", "category: Pharmacies", icons[Realosophy.Web.Client.IconType.Health]),
    new Realosophy.Web.Client.POIOptions("Yoga", "HE_Yoga", "category: Yoga Instruction", icons[Realosophy.Web.Client.IconType.Health]),
    new Realosophy.Web.Client.POIOptions("Fitness", "HE_Gym", "Fitness", icons[Realosophy.Web.Client.IconType.Health]),
    new Realosophy.Web.Client.POIOptions("Grocery", "SH_Groc", "Groceries", icons[Realosophy.Web.Client.IconType.Shopping]),
    new Realosophy.Web.Client.POIOptions("Home Improvement", "SH_Home", "category: Building Materials", icons[Realosophy.Web.Client.IconType.Shopping]),
    new Realosophy.Web.Client.POIOptions("Salon", "SH_Salon", "category: Beauty Salons", icons[Realosophy.Web.Client.IconType.Shopping]),
    new Realosophy.Web.Client.POIOptions("Wine", "SH_Lcbo", "liquor control board ontario", icons[Realosophy.Web.Client.IconType.Shopping]),
    new Realosophy.Web.Client.POIOptions("Beer", "SH_Beer", "The Beer Store", icons[Realosophy.Web.Client.IconType.Shopping]),
    new Realosophy.Web.Client.POIOptions("Gas", "SH_Gas", "category: Service Stations Gasoline, Oil & Natural Gas", icons[Realosophy.Web.Client.IconType.Shopping]),
    new Realosophy.Web.Client.POIOptions("Starbucks", "DB_Star", "Starbucks", icons[Realosophy.Web.Client.IconType.Starbucks]),
    new Realosophy.Web.Client.POIOptions("Tim Hortons", "DB_Tim", "Tim Hortons", icons[Realosophy.Web.Client.IconType.TimHortons])
  );

}

var IsTransitLoaded = false;
var routes = null;
var zoomEndHandle = null;
var moveEndHandle = null;

function toggleTransit() {
  /*  
  Transit init
  */
  if (!IsTransitLoaded) {
    var baseurl = "http://" + RealSystem.get_currentHostName() + "/images/transit/";
    var markerIcons = [
          new DTransitMarkerIcon(baseurl, "stop", new GSize(14, 14),
                   new GPoint(7, 7), new GPoint(7, 7)),
          new DTransitMarkerIcon(baseurl, "stop-transfer", new GSize(14, 14),
                   new GPoint(7, 7), new GPoint(7, 7)),
          new DTransitMarkerIcon(baseurl, "interchange", new GSize(40, 40),
                   new GPoint(20, 20), new GPoint(20, 20)),
          new DTransitMarkerIcon(baseurl, "interchange-transfer", new GSize(40, 40),
                   new GPoint(20, 20), new GPoint(20, 20)),
          new DTransitMarkerIcon(baseurl, "gostop", new GSize(12, 12),
                   new GPoint(6, 6), new GPoint(6, 6))
      ];

    // Setup routes.
    routes = new DTransitRouteSet(map, baseurl,
          ["yonge", "bloor", "scarborough", "sheppard",
          "lakeshorewest", "lakeshoreeast", "milton", "georgetown", "bradford",
          "richmondhill", "stoufville"],
          markerIcons,
          ["elevator", "washroom", "cashparking", "metropassparking", "go", "ttc"]);

    zoomEndHandle = google.maps.Event.addListener(map, "zoomend", function(oldZoomLevel, newZoomLevel)
    { routes.onZoom(oldZoomLevel, newZoomLevel); });

    moveEndHandle = google.maps.Event.addListener(map, "moveend", function() { routes.onMoveEnd(); });
    IsTransitLoaded = true;
  } else {
    google.maps.Event.removeListener(zoomEndHandle);
    google.maps.Event.removeListener(moveEndHandle);

    routes.hide();
    for (var i = 0; i < this.routes.length; ++i) {
      if (this.routes[i] != null) {
        delete routes[i].polylines;
        delete routes[i];
      }
    }
    delete routes;
    IsTransitLoaded = false;
  }
}



/// <summary>
/// Toggle photo from Panaramio
/// </summary>
function togglePhoto() {
  if (HasPhotos) {
    map.removeOverlay(photoOverlay);
    HasPhotos = false;
  } else {
    map.addOverlay(photoOverlay);
    HasPhotos = true;
  }
} // togglePhoto()

/// <summary>
/// Toggle wiki
/// </summary>
function toggleWiki() {
  if (HasWiki) {
    map.removeOverlay(wikiOverlay);
    HasWiki = false;
  } else {
    map.addOverlay(wikiOverlay);
    HasWiki = true;
  }
} // toggleWiki()

Realosophy.Web.Client.RealIcon = function() {
  this.name = null;
  this.HotIcon = null;
  this.ColdIcon = null;
}
Realosophy.Web.Client.RealIcon.registerClass('Realosophy.Web.Client.RealIcon');

function setupIcons() {
  var icon = new Realosophy.Web.Client.RealIcon();
  icon.name = 'Entertainment';
  icon.HotIcon = createGIcon(pathToMarkerImages + 'entertainment_on.png');
  icon.ColdIcon = createGIcon(pathToMarkerImages + 'entertainment.png');
  icons.push(icon);

  icon = new Realosophy.Web.Client.RealIcon();
  icon.name = 'Health';
  icon.HotIcon = createGIcon(pathToMarkerImages + 'health_on.png');
  icon.ColdIcon = createGIcon(pathToMarkerImages + 'health.png');
  icons.push(icon);

  icon = new Realosophy.Web.Client.RealIcon();
  icon.name = 'House';
  icon.HotIcon = createGIcon(pathToMarkerImages + 'house_on.png');
  icon.ColdIcon = createGIcon(pathToMarkerImages + 'house.png');
  icons.push(icon);

  icon = new Realosophy.Web.Client.RealIcon();
  icon.name = 'Neighbourhoods';
  icon.HotIcon = createGIcon(pathToMarkerImages + 'neighbourhoods_on.png');
  icon.ColdIcon = createGIcon(pathToMarkerImages + 'neighbourhoods.png');
  icons.push(icon);

  icon = new Realosophy.Web.Client.RealIcon();
  icon.name = 'School';
  icon.HotIcon = createGIcon(pathToMarkerImages + 'school_on.png');
  icon.ColdIcon = createGIcon(pathToMarkerImages + 'school.png');
  icons.push(icon);

  icon = new Realosophy.Web.Client.RealIcon();
  icon.name = 'Shopping';
  icon.HotIcon = createGIcon(pathToMarkerImages + 'shopping_on.png');
  icon.ColdIcon = createGIcon(pathToMarkerImages + 'shopping.png');
  icons.push(icon);

  icon = new Realosophy.Web.Client.RealIcon();
  icon.name = 'Starbucks';
  icon.HotIcon = createGIcon(pathToMarkerImages + 'starbucks_on.png');
  icon.ColdIcon = createGIcon(pathToMarkerImages + 'starbucks.png');
  icons.push(icon);

  icon = new Realosophy.Web.Client.RealIcon();
  icon.name = 'TimHortons';
  icon.HotIcon = createGIcon(pathToMarkerImages + 'timhortons_on.png');
  icon.ColdIcon = createGIcon(pathToMarkerImages + 'timhortons.png');
  icons.push(icon);
}

function createGIcon(imageURL) {
  var icon = new google.maps.Icon();
  icon.image = imageURL;
  icon.iconSize = new google.maps.Size(31, 51);
  icon.iconAnchor = new google.maps.Point(17, 52);
  icon.infoWindowAnchor = new google.maps.Point(9, 2);
  icon.infoShadowAnchor = new google.maps.Point(15, 22);
  return icon;
}

var visiblePolygon = null;
function drawPolygon(points) {
  if (visiblePolygon)
    map.removeOverlay(visiblePolygon);

  var llArray = [];
  for (var i = 0; i < points.length; i++) {
    llArray.push(new google.maps.LatLng(points[i].lat, points[i].lng));
  }
  var poly = new google.maps.Polygon(llArray, null, 2, 0.7, "#E3EAEB", 0.5, {clickable: false});
  map.addOverlay(poly);
  visiblePolygon = poly;
}

function createListMarker(lat, lng, RegionName) {
  var icon = new google.maps.Icon();
  icon.image = 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);

  var point = new google.maps.LatLng(lat, lng);
  var marker = new google.maps.Marker(point, icon);
  var content = formatListInfoWindowContent(RegionName);
  map.addOverlay(marker);
  google.maps.Event.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(content);
    showDistrictPolygon(RegionName);
    map.setCenter(marker.getPoint(), 13);
  });
  return marker;
}

function formatListInfoWindowContent(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;
}

function showListMarkers() {
  initMarkerCentrePoints();
}

function showNeighbourhoodListInfoWindow(regionIndex, regionName) {
  var marker = markerCentrePoints[regionIndex];
  showDistrictPolygon(regionName);
  marker.openInfoWindow(formatListInfoWindowContent(regionName));
  map.setCenter(marker.getPoint(), 13);
}

function showDistrictPolygon(regionName) {
  Realosophy.Web.Service.FindNearby.GetDistrictPolygon(regionName, drawPolygon);
}

/* Used to kickoff the POI search routines */
function ProcessPOI(tabText, showHeader) {
  var queryString;
  var searchParam = null;

  mySearchWell.clear();
  map.closeInfoWindow();

  // Find which Keywords have been matched to the tab  
  for (var i = 0, length = searchKeywords.length; i < length; i++) {
    if (searchKeywords[i].get_Header() == tabText) {
      searchParam = searchKeywords[i];
      break;
    }
  }

  if (searchParam == null) return; //On one of the main tabs so nothing to do

  searchParam.set_ShowHeader(showHeader);
  queryString = searchParam.get_QueryString(); //[Realosophy.Web.Client.SearchParam.QueryString];

  if (queryString.match(/^.*\b(Elementary|Secondary|Private)\b.*$/)) {
    // Schools
    searchParam.set_FormatterFunc(formatSchoolTabPOI);  // Assign the format function
    findNearbySchools(searchParam);
  } else if (queryString.match(/^.*\b(Residential)\b.*$/)) {
    // Neighbourhoods
    searchParam.set_FormatterFunc(FormatDistrictTabPOI);
    findNearbyNeighbourhoods(searchParam);
  } else {
    searchParam.set_FormatterFunc(formatLocalSearchResultOneLine);
    executeSearch(searchParam);
  }
}