﻿//Author: Santhosh Nair
//Date Created: 12/16/2009
//Description:  This script file includes functions used to for Google Maps mashup
//This script file is witten to be used with the MapsAndDirections/Default.aspx page.
//The file uses location information in XML format.  The xml is as below
/*

<locations>
  <location locationid="0" lat="43.0524841" lng="-88.2118126" title="Bielinski Homes" address="N16 W23377 Stone Ridge Drive" csz= "Waukesha WI 53188" phone="262.542.9494"/>
  <location .../>
  ...
</locations>
*/


//page_level variable delarations
var _map
var _side_bar_html = "";
var _markers = [];
var _htmls = [];
var _toHTMLs = [];
var _fromHTMLs = [];

var _ProjectID;
var _locFileName;



function GetDirectionsFrom(to, fromLat, fromLng,  cinfo)
{

    var directionsPanel;
    var directions;
    var destination;

    var point1 =   new GLatLng(fromLat,fromLng);

  /*  var pfHref = '"javascript:printDirections( ';
    pfHref = pfHref  + "'from: " + from + " to: " + point1.toUrlValue(5) + "'";
    pfHref = pfHref + ", '" + escape(toAddress) + "'";
    pfHref = pfHref + ' );"';
*/

     var pfHref = 'PrintDirections.aspx?dirs=';
    pfHref = pfHref  + escape("from: " + point1.toUrlValue(5) + " to: " + to );
    pfHref = pfHref + "&cinfo=" + escape(cinfo);

    directionsPanel = document.getElementById("left_panel");
    destination = '<a href="javascript:loadLocations();">Back To Locations</a> <br/><br/><a href='+ pfHref + ' target=_blank>Printer Friendly Version</a> <br/><br/> <strong>Directions From:</Strong><br/>' + cinfo + '<br/>';
    directionsPanel.innerHTML = destination;    

    _map.clearOverlays();
    _map.setCenter(point1, 13);  
    
    directions = new GDirections(_map, directionsPanel);  
    
   // directions.load("from: " + from + " to: " + toAddress + ", "  + toLat + ", " + toLng );
    directions.load("from: " + point1.toUrlValue(5) + " to: " + to  );
    
}




function GetDirectionsTo(from, toLat, toLng,  cinfo)
{

    var directionsPanel;
    var directions;
    var destination;

    var point1 =   new GLatLng(toLat,toLng);

  /*  var pfHref = '"javascript:printDirections( ';
    pfHref = pfHref  + "'from: " + from + " to: " + point1.toUrlValue(5) + "'";
    pfHref = pfHref + ", '" + escape(toAddress) + "'";
    pfHref = pfHref + ' );"';
*/

     var pfHref = 'PrintDirections.aspx?dirs=';
    pfHref = pfHref  + escape("from: " + from + " to: " + point1.toUrlValue(5) );
    pfHref = pfHref + "&cinfo=" + escape(cinfo);

    directionsPanel = document.getElementById("left_panel");
    destination = '<a href="javascript:loadLocations();">Back To Locations</a> <br/><br/><a href='+ pfHref + ' target=_blank>Printer Friendly Version</a> <br/><br/> <strong>Directions To:</Strong><br/>' + cinfo + '<br/>';
    directionsPanel.innerHTML = destination;    

    _map.clearOverlays();
    _map.setCenter(point1, 13);  
    
    directions = new GDirections(_map, directionsPanel);  
    
   // directions.load("from: " + from + " to: " + toAddress + ", "  + toLat + ", " + toLng );
    directions.load("from: " + from + " to: " + point1.toUrlValue(5) );
    
          
}



 
  // This function picks up the click and opens the corresponding info window
  function myclick(i) {
    _markers[i].openInfoWindowHtml(_htmls[i]);
  }

  // functions that open the directions forms
  function tohere(i) {
    _markers[i].openInfoWindowHtml(_toHTMLs[i]);

  }

  // functions that open the directions forms
  function fromHere(i) {
    _markers[i].openInfoWindowHtml(_fromHTMLs[i]);

  }

  
  
  function Centerhere(i) {
   _map.setCenter( _markers[i].getLatLng());
  }


  function highestZIndex(marker,b) 
  {
        return 1000000000;
  }



function processGo()
{
    if (event.keyCode == 13)
    {
        event.cancelBubble = true;
        event.returnValue = false;
        document.getElementById("defaultButton").click();
    }
}



function createMarker(point,name,address, csz, ord, phone) 
{

      var Icon = new GIcon();
      var markerImageSrc;
      
      if(ord==0) 
        markerImageSrc = "images/Yellow0.png";
      else
        markerImageSrc = "images/red" + ord + ".png";
         
      Icon.image = markerImageSrc;
      Icon.iconSize = new GSize(32, 32);
      Icon.iconAnchor = new GPoint(16, 32);
      Icon.infoWindowAnchor = new GPoint(16, 2);
      Icon.shadow = "images/shadow.png";
      Icon.shadowSize = new GSize(37, 34);

    
     if(ord==0) 
        markerOptions = { icon:Icon, zIndexProcess:highestZIndex };
      else
        markerOptions = { icon:Icon};

    
    var marker = new GMarker(point, markerOptions );

    address1 = "'" + address + ", " + csz +  ", Phone: " + phone +  "'";
    
    var html= "<div> <strong> " + name +  "  </Strong> <br/>" + address + "<br/>" + csz + "<br/> Phone: " + phone + "<div>";
    var side_bar_info = html;
    
    var i = _markers.length;
    
    divname = "'txtGetdirections'";

    _toHTMLs[i] = html + '<div id="divGetDirections" >' +
                        '<strong>Get&nbsp;Directions</strong> <br/>' + 
                        'Starting&nbsp;from: <input type="text" id="txtGetdirections" onkeypress="javascript:processGo();"/>' + 
                        '<input id="defaultButton" type="button" value="Go" onclick="javascript:GetDirectionsTo(document.getElementById(' + 
                        divname +
                        ').value, ' +  
                        point.lat() + 
                        ', ' + 
                        point.lng() + 
                        ' , ' +
                        address1 + 
                        ')"/>' + 
                        '<br/></div>';

    _fromHTMLs[i] = html + '<div id="divGetDirections" >' +
                        '<strong>Get&nbsp;Directions</strong> <br/>' + 
                        'Destination: <input type="text" id="txtGetdirections" onkeypress="javascript:processGo();"/>' + 
                        '<input id="defaultButton" type="button" value="Go" onclick="javascript:GetDirectionsFrom(document.getElementById(' + 
                        divname +
                        ').value, ' +  
                        point.lat() + 
                        ', ' + 
                        point.lng() + 
                        ' , ' +
                        address1 + 
                        ')"/>' + 
                        '<br/></div>';

                        
  
    html = html + '<a href="javascript:Centerhere(' + i + ')">Center Map Here<\/a> <br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <a href="javascript:fromHere(' + i + ')">From here<\/a>'

    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
    });
    // save the info we need to use later for the side_bar
    _markers.push(marker);
    _htmls[i] = html;
    // add a line to the side_bar html
    _side_bar_html += '<div style="text-align:left; width:280px; height:60px "><div style="float:left; width:45px; height:60px"><img src="'+ markerImageSrc + '"></div><div float:right; width:235px; height:60px><a href="javascript:myclick(' + i + ')"> <strong>' + name + '</strong> <br/>' + address + '<br/>' + csz +  ' <\/a><br/></div></div>' ;
    return marker;
}



function loadLocations()
{
    //clear everything just in case the page is being reloaded after a directions lookup
    _map.clearOverlays();
    
    //Initialize the left pane
    _side_bar_html = "<div>Click on the links or markers on the map for contact information and driving directions</div><br/>";
    
    //Center the map to corporate location
	_map.setCenter(new GLatLng(43.052489,-88.212447), 9);        
    if (window.XMLHttpRequest)
      {
      xhttp=new XMLHttpRequest();
      }
        else // Internet Explorer 5/6
      {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }      
      
      xhttp.open("get", _locFileName, true);      
      xhttp.onreadystatechange = function() 
      {
        if (xhttp.readyState == 4) 
        {
          var xmlDoc = xhttp.responseXML;
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("location");
          
          for (var i = 0; i < markers.length; i++) {
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var title = markers[i].getAttribute("title");
            var address = markers[i].getAttribute("address");
            var csz = markers[i].getAttribute("csz");
            var phone = markers[i].getAttribute("phone");
            var ProjectID = markers[i].getAttribute("locationid");
            if( ProjectID == _ProjectID)
            {
                var point = new GLatLng(lat,lng);            
                // create the marker
                var marker = createMarker(point,title,address, csz, 0, phone);
                _map.addOverlay(marker);
                marker.openInfoWindowHtml(_htmls[0]);
                _map.setCenter( point);
            }
          }
          
          var ord = 0;
          for (var i = 0; i < markers.length; i++) {
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var title = markers[i].getAttribute("title");
            var address = markers[i].getAttribute("address");
            var csz = markers[i].getAttribute("csz");
            var phone = markers[i].getAttribute("phone");
            var ProjectID = markers[i].getAttribute("locationid");
            if( ProjectID != _ProjectID)
            {
                ord = ord + 1;
                var point = new GLatLng(lat,lng);            
                // create the marker
                var marker = createMarker(point,title,address, csz, ord, phone);
                _map.addOverlay(marker);
            }
          }
          
          
          
          // put the assembled _side_bar_html contents into the side_bar div
          document.getElementById("left_panel").innerHTML = _side_bar_html;
          
        }
      }
      xhttp.send(null);
}




function initializeMap(pProjectID, locFileName) 
{      
    _ProjectID = pProjectID;
    
    _locFileName = locFileName;

	if (GBrowserIsCompatible()) 
	{        
		_map = new GMap2(document.getElementById("map_canvas"));        
		_map.setUIToDefault();    
        loadLocations();
		
	}    
}    

