﻿// JScript File

var gMap;
var polygonPoints = new Array(0);   //To hold all the polygon points.

var fillColor = "#0000FF"; // blue fill
var lineColor = "#000000"; // black line
var opacity = .5;
var lineWeight = 2;
var JMarker;
var polyShape;
var polyLine;
var i = 0;          //Variable used to show the GMarker
var GMarkers = [];  //Variable used to show the GMarker

var Counter;

var allowedBounds = new GLatLngBounds(new GLatLng(40.35910267579199, -74.542236328125), new GLatLng(42.62183364891663, -70.9716796875));

var SMMapArrows = null;
var zoomLevel;

function loadMap()
{
    
    zoomLevel = 8;
    var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
    
    //Initialize the map to the center of CT.
    gMap = new GMap2(document.getElementById("map"));
    gMap.setCenter(new GLatLng(41.500753, -72.757385), zoomLevel);
    //gMap.addControl(new GMapTypeControl(), topRight);

    var arrowControlPosition = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10));
    if (SMMapArrows == null)
        SMMapArrows = document.getElementById("smMapArrows");
    arrowControlPosition.apply(SMMapArrows);
    gMap.getContainer().appendChild(SMMapArrows);
    
    //gMap.addControl(new GSmallMapControl());
    i = 0;
    
    drawPolygonOnMap();
    
    outlineCT();
    
    gMap.continuousZoomEnabled();
        
    GEvent.addListener(gMap, "move", function() { checkBounds(); });
    GEvent.addListener(gMap, "zoomend", function() { OnZoomEnd(); });
}

function OnZoomEnd()
{
    zoomLevel = gMap.getZoom();
}

// If the map position is out of range, move it back
function checkBounds() {
    // Perform the check and return if OK
    if (allowedBounds.contains(gMap.getCenter())) {
      return;
    }
    
    var C = gMap.getCenter();
    var X = C.lng();
    var Y = C.lat();

    var AmaxX = allowedBounds.getNorthEast().lng();
    var AmaxY = allowedBounds.getNorthEast().lat();
    var AminX = allowedBounds.getSouthWest().lng();
    var AminY = allowedBounds.getSouthWest().lat();

    if (X < AminX) {X = AminX;}
    if (X > AmaxX) {X = AmaxX;}
    if (Y < AminY) {Y = AminY;}
    if (Y > AmaxY) {Y = AmaxY;}
    gMap.setCenter(new GLatLng(Y,X));
}

function createOfficeMarker(latlng, name, address, phone, fax)
{
    var marker = new GMarker(latlng);
    var html = "<table border='0' cellpadding='0' cellspacing='0'><tr><td class='txt_10B'>" + name + "</td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td height='5'></td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td class='txt_10G'>" + address + "</td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td height='5'></td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td class='txt_10G'>" + phone + "</td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td height='5'></td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td class='txt_10G'>" + fax + "</td></tr></table>";
    
    GEvent.addListener(marker, "mouseover", function() { marker.openInfoWindowHtml(html); });
    if(i == -1) 
    {
        i=0; 
    }
    
    GMarkers[i] = marker;
    i++;
    
    gMap.addOverlay(marker);
}

function createAgentMarker(latlng, agentName, officeName, address, phone, email)
{
    var marker = new GMarker(latlng);
    var html = "<table border='0' cellpadding='0' cellspacing='0'><tr><td class='txt_10B'>" + agentName + "</td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td height='5'></td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td class='txt_10B'>" + officeName + "</td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td height='5'></td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td class='txt_10G'>" + address + "</td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td height='5'></td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td class='txt_10G'>" + phone + "</td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td height='5'></td></tr></table><table border='0' cellpadding='0' cellspacing='0'><tr><td class='txt_10G'>" + email + "</td></tr></table>";
    
    GEvent.addListener(marker, "mouseover", function() { marker.openInfoWindowHtml(html); });
    if(i == -1) 
    {   i=0; 
    }
    
    GMarkers[i] = marker;
    i++;

    gMap.addOverlay(marker);
}

function searchPointsOfInterest(){}

function ClearResultsOnMap()
{
    var canIDraw = document.getElementById("chkDraw");
    if(canIDraw.checked == true)
    {
        clearZipCode();
        if(GMarkers.length > 0)
        {
            var answer = confirm("Results on the map will be cleared.");
            if(answer)
                gMap.clearOverlays();
            else
                canIDraw.checked = false;
        }
    }
}

function drawPolygonOnMap()
{
      var canIDraw = document.getElementById("chkDraw");
      var mapElement = document.getElementById("map");
      
      //Array to hold all the points.
      var linePoints = new Array(0);
      
      GEvent.addListener(gMap, "click", function (PMarker, clickLatLng)
      {
        if (canIDraw.checked == true)   //If I can draw.
          {
            if(clickLatLng)  //If the map is clicked and not a marker.
            {
              //Push the next latlng into the array.
              polygonPoints.push(clickLatLng);
              
              //Remove all the markers on the map.
              //gMap.clearOverlays();
              if(polyShape != null)
                gMap.removeOverlay(polyShape);
            
              polyShape = new GPolygon(polygonPoints, lineColor,lineWeight,opacity,fillColor);
                           
              //Always display the first marker. This is the marker which will be used to close the polygon.
              PMarker = new GMarker(polygonPoints[0]);
              
              GEvent.addListener(PMarker, "mouseover", function() { PMarker.openInfoWindowHtml("<span class='txt_10B'>Double click this marker<br/>to close the shape.</span>"); });
              
              //Add the marker and the polygon.
              gMap.addOverlay(PMarker);
              gMap.addOverlay(polyShape);  
              
              //The polygon can only be closed if there are more then two points on the map.
              if (polygonPoints.length > 2)
              {
                GEvent.addListener(PMarker, "dblclick", closePolygon);
              }
            }
          }
      });
      
      //Used to change the style of the map element (div element). 
      //Changes the cursor.
      GEvent.addDomListener(canIDraw, "click", function ()
      {
        if(canIDraw.checked == true)
          mapElement.className = mapElement.className.replace("view", "edit");
        else
          mapElement.className = mapElement.className.replace("edit", "view");
      });
}

function closePolygon()
{
    //push the first point in the points array.
    polygonPoints.push(polygonPoints[0]);
      
    //clear everything.
    gMap.clearOverlays();
      
    //Draw the polygon again.
    polyShape = new GPolygon(polygonPoints, lineColor,lineWeight,opacity,fillColor,opacity);
      
    gMap.addOverlay(polyShape);      
      
    var bounds = polyShape.getBounds();
      
    gMap.panTo(bounds.getCenter());
      
    var middleMarker = new GMarker(bounds.getCenter());
    gMap.addOverlay(middleMarker);
      
    //Set all the coordiantes to the hidden variable.
    SetCoordinates();

    //Empty the points array.
    polygonPoints = [];
    
    var btnMapSubmit = document.getElementById("btnMapSubmit");
    btnMapSubmit.click();
    
}

function SetCoordinates()
{
    var hPolygonArea = document.getElementById('hPolygonArea');
    hPolygonArea.value = '';
    
    for(var i = 0; i < polygonPoints.length; i++)
    {
        if( i == polygonPoints.length - 1)
            hPolygonArea.value = hPolygonArea.value + polygonPoints[i];
        else
            hPolygonArea.value = hPolygonArea.value + polygonPoints[i] + ';';
    }
}


function outlineCT()
    {

	var BPoints = new Array();
    BPoints.push(new GLatLng('41.012520','-73.654152'));
    BPoints.push(new GLatLng('41.100700','-73.727700'));
    BPoints.push(new GLatLng('41.212750','-73.481954'));
    BPoints.push(new GLatLng('41.295100','-73.551360'));
    BPoints.push(new GLatLng('41.365204','-73.543460'));
    BPoints.push(new GLatLng('41.522652','-73.531150')); 
    BPoints.push(new GLatLng('41.522652','-73.530823')); 
    BPoints.push(new GLatLng('41.665592','-73.517578'));
    BPoints.push(new GLatLng('42.047337','-73.484665'));
	BPoints.push(new GLatLng('42.036217','-73.046082')); 
    BPoints.push(new GLatLng('42.035915','-73.006546'));
    BPoints.push(new GLatLng('42.033413','-72.816910')); 
    BPoints.push(new GLatLng('41.997089','-72.818138'));
    BPoints.push(new GLatLng('42.002071','-72.768036'));
    BPoints.push(new GLatLng('42.033752','-72.756355')); 
    BPoints.push(new GLatLng('42.030441','-72.609993')); 
    BPoints.push(new GLatLng('42.022705','-72.608292'));
    BPoints.push(new GLatLng('42.021511','-72.582375'));
    BPoints.push(new GLatLng('42.030029','-72.571693'));
    BPoints.push(new GLatLng('42.030670','-72.508041'));
    BPoints.push(new GLatLng('42.026306','-72.136826'));
    BPoints.push(new GLatLng('42.025703','-72.095451'));
    BPoints.push(new GLatLng('42.023500','-71.800617')); 
    BPoints.push(new GLatLng('42.008000','-71.799117')); 
    BPoints.push(new GLatLng('41.721504','-71.788734'));	
    BPoints.push(new GLatLng('41.641659','-71.793091'));
    BPoints.push(new GLatLng('41.601208','-71.790680'));
    BPoints.push(new GLatLng('41.415730','-71.803230'));
	BPoints.push(new GLatLng('41.403755','-71.846481'));
	BPoints.push(new GLatLng('41.341862','-71.837357'));
	BPoints.push(new GLatLng('41.325249','-71.848259')); 
   
   
    var BShape = new GPolyline(BPoints, lineColor,lineWeight,opacity,fillColor,opacity);
    
    gMap.addOverlay(BShape);    
      
      
    }

function showGMarker(i)
{
    var marker = GMarkers[i];
    if(marker != null)
        GEvent.trigger(marker, "mouseover");
}

function PanMap(pan)
{
    if (pan == "top")
        gMap.panDirection(0, 1);
    else if(pan == "left")
        gMap.panDirection(1, 0);
    else if(pan =="right")
        gMap.panDirection(-1, 0);
    else
        gMap.panDirection(0, -1);
}

function ZoomMap(dir)
{
    if(dir == "in")
        zoomLevel = zoomLevel + 1;
    else
        zoomLevel = zoomLevel - 1;
    gMap.setZoom(zoomLevel);
}