
	/*
	How to use Google Maps?
	IMPORTANT : DO NOT call this script inside html <header> tag. This script writes some divs to the page, so should be used in html header of the page
            Called it anywhere after the <body> tag.
		
	STEP 1: Include following code on the page
	
	    <!-- include this JS file -->
	    <script src="/javascripts/GetScript.aspx?name=GoogleMaps" type="text/javascript"></script> 
	    
	    <!-- import screen postion code -->
        <script src="/javascripts/GetScript.aspx?name=ScreenPosition" type="text/javascript"></script> 
        
        <!-- import popupbox css -->
        <style type="text/css">    
            @import url(/stylesheets/getstylesheet.aspx?name=PopupBox);    
        </style>
	
	STEP 2: Call the ShwoGMap function like this:
		<a href="javascript://Show Map" onclick='ShowGMap("29 Newbury Street", "Boston, MA 02116", this);'>Map</a>
		
		1st parameter - street address
		2nd parameter - city state zip
		3rd parameter - just pass 'this'  --- anchor (href) object, so the position of the map is calculated.
		4th parameter -  xOffset - you can further offset X coordinate of the map popup
        5th parameter -  yOffset - you can further offset Y coordinate of the map popup
		
    STEP 3:		
        On the mouse click event you can poulate "_addnlInfoToPrintWithMaps" div with any text you want to print when 
        the user clicks print button on the maps. See alasks/destinations/controls/UCTopPicks.ascx.cs for example
    
	*/ 
   
   var gmapsAPIKey;
   
   //This API key was obtained from google.
   //Spanish site has a different key, so check if spanish and use correct key;
   
   if (window.location.href.indexOf('convertlanguage.com') > 0)
   {
    //Use key for SPANISH site
    gmapsAPIKey = "ABQIAAAAPrjTVRMqP81Eta69N9YfLBRHpijO6Gou9v18PesCMBU2GLobQxQKHcVz37Dj3dGNzOIFelyrYX65yw";
   }
   else
   {
    //Use key for generic alaskaair.com site
    gmapsAPIKey = "ABQIAAAAPrjTVRMqP81Eta69N9YfLBTCpqhqTZRRC_dudoiIl9AbxE4DUBTfWdo5beHQAjKWrM8c2CrISO8bbg";   
   }   
      
    document.write('<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' + gmapsAPIKey + '"  type="text/javascript"></script>');      
                  
    document.write('<div id="_mapHolder" style="position:absolute; display:none; z-index:1000;">');
    document.write('<div id="popupBox">');
    document.write('<div class="popup">');
    document.write('<table>');
    document.write('<tbody>'); 
    document.write('<tr>'); 
    document.write('<td class="tl"/><td class="t"/><td class="tr"/>'); 
    document.write('</tr>'); 
    document.write('<tr>');
    document.write('<td class="l"/>'); 
    document.write('<td class="body">'); 

    document.write('<div class="content">');
    document.write('<div id="_popupMapContent">');

    document.write('<div id="_printBlockForMap">');
    document.write('<div id="_mapCanvas" style="float:left;">');            
    document.write('<!-- -->');
    document.write('</div>');
    document.write('<div id="_drivingDirections" style="width: 320px; height: auto;float:left; overflow:auto; display:none;">');
    document.write('<!-- -->');
    document.write('</div>');
    document.write('</div>'); //end of print block   

    document.write('</div>'); 
    document.write('</div>'); 
    document.write('<div class="footer"  style="width:367px;margin-bottom:0;padding-bottom:0; float:right;">'); 

    document.write('<img src="/images/B_Print_Light.gif" alt="Print Map" onclick="javascript:PrintMap(); return false;" class="handcursor" style="border-width:0px;margin:2px;"/><br/>');	    	
    document.write('<img src="/images/B_Close_Dark.gif" alt="Close Map" onclick="javascript:CloseMap(); return false;" class="handcursor" style="border-width:0px;margin:2px;"/>');

    document.write('</div>');
    document.write('</td>');
    document.write('<td class="r"/>');
    document.write('</tr>');
    document.write('<tr>');
    document.write('<td class="bl"/><td class="b"/><td class="br"/>');
    document.write('</tr>');
    document.write('</tbody>');
    document.write('</table>');
    document.write('</div>'); 
    document.write('</div>');
    document.write('</div>');

    //This dummy iframe prevents IE6 to show slect boxes through the popup div
    document.write('<iframe id="_dummyIframeForMaps" src="javascript:false;" scrolling="no" frameborder="0" style="position:absolute;display:none;"></iframe>');
    //This is waiting spin wheel for slow browsers and machines
    document.write('<div id="_waitSpinWheel" style="background-color:white; border:solid 1px #ccc; display:none;z-index:1200; position:absolute; padding:10px;">');
	document.write('<center>');
    document.write('Processing...<br />');
    document.write('<img src="/images/spinwheel-waiting.gif" alt=""/>');
    document.write('</center>');
    document.write('</div>');
    
    document.write('<div id="_addnlInfoToPrintWithMaps" style="display:none;">');    
    document.write('</div>');
   
    
    var map = null;
    var geocoder = null;     
    var _mapRetryCount = 0;     
    var directions;      
	var _disableDiretions = "NO";
	var _currentAddress = "";
	var _showingDrivingDirections = "NO";
	var _directionsAddress;
	var _directionsFromMapAddr;	
    
    function initializeGmaps() {

        if (GBrowserIsCompatible()) {               
            map = new GMap2(document.getElementById("_mapCanvas"),{ size: new GSize(500,500) } );            
            //map.addControl(new GLargeMapControl()); 
            //var mapControl = new GMapTypeControl();
            //map.addControl(mapControl);                                     
            //map.setCenter(new GLatLng(37.4419, -122.1419), 13);            
            geocoder = new GClientGeocoder();
        }            
    }

function ShowGMap(streetAddress, cityStateZip, anchorItem, xOffset, yOffset) {

    _disableDiretions = "NO";
    GetMap(streetAddress, cityStateZip, anchorItem, xOffset, yOffset);

}
    
function GetMap(streetAddress, cityStateZip, anchorItem, xOffset, yOffset) {
    	
  ShowSpinWaitSpinWheel(anchorItem);  
  var address;  
  address =  streetAddress + " " +  cityStateZip;  
  if(!geocoder)
    initializeGmaps();
    
  geocoder.getLatLng(
    address,
    function(point) {      
      if (!point) {                
        if(_mapRetryCount  == 0){   //IE6 fix : Give another shot
            _mapRetryCount++;		            	
            GetMap(streetAddress, cityStateZip, anchorItem);			
        }
        else if(_mapRetryCount  == 1){ //MEXICO fix (or unknown street address fix : give a shot only with city state zip             
            _mapRetryCount++;
			_disableDiretions  = "YES";			
            GetMap("", cityStateZip, anchorItem);			         
        } 
        else
        {            
            SetMapNotFoundMessage(address, anchorItem);                                    
        }        
      } else {             
        DrawMap(point, address, anchorItem, xOffset, yOffset);                                    
      }
    }
  );            
}  

function SetMapNotFoundMessage(address, anchorItem)
{    
    _mapRetryCount = 0;
    var mapCanvas = document.getElementById("_mapCanvas");
    mapCanvas.innerHTML = "<div style=\"padding:20px\"><strong>Map for " + address + " not found.</strong></div>";        
   
    var mapHolder = document.getElementById("_mapHolder");   
    var mapBoxPosX = 20;
    var mapBoxPosY = GetObjectPosition(anchorItem).y - 500; 
   
    mapHolder.style.top = mapBoxPosY + "px"; 
    mapHolder.style.left = mapBoxPosX + "px"; 
    mapHolder.style.display = "block";
   
    var dummyIframe = document.getElementById("_dummyIframeForMaps");					
    dummyIframe.style.width =  mapHolder.offsetWidth;
	dummyIframe.style.height = mapHolder.offsetHeight;	
	dummyIframe.style.top = mapBoxPosY + "px";  
	dummyIframe.style.left = mapBoxPosX + "px"; 
	dummyIframe.style.zIndex = mapHolder.style.zIndex - 1; 
	dummyIframe.style.display = "block";
	
	_disableDiretions = "YES";
	HideSpinWaitSpinWheel();
}

function ShowDirectionsForm(directionType)
{
    var directionsFormDiv = document.getElementById("_directionsFormDiv");
    document.getElementById("directionsType").value = directionType;
    directionsFormDiv.style.display = "block";
    document.getElementById("addressInputDirections").focus();
    //IE throws runtime error so catch and supress
    try{
        if(directionType == "FROM")
        {
            document.getElementById("inputAddressTitle").innerHTML = "End Address: ";
        }
        else
        {
            document.getElementById("inputAddressTitle").innerHTML = "Start Address: ";
        }
    }
    catch(ex)
    {
    //Supress error
    }

}

function getDirections()
{ 
      if(document.getElementById("_mapHolder").style.display == "none"){ return; }
      
      var mappedAddress;
      var inputAddress;
      var directionsType;
            
      directionsType = document.getElementById("directionsType").value;
      mappedAddress = document.getElementById("mappedAddress").value;
      inputAddress = document.getElementById("addressInputDirections").value;
            
      if(inputAddress == ""){ return; }
      
      clearDirections();       
      clearMap();   
           
      document.getElementById("_popupMapContent").style.width = "790px";      
      document.getElementById("_drivingDirections").style.width = "260px";
      document.getElementById("_drivingDirections").style.display = "block";
      document.getElementById("_dummyIframeForMaps").style.width = "790px";	
      
      map = new GMap2(document.getElementById("_mapCanvas"),{ size: new GSize(500,500) } );
      map.addControl(new GLargeMapControl()); 
      var mapControl = new GMapTypeControl();
      map.addControl(mapControl);             
      directions = new GDirections(map, document.getElementById("_drivingDirections"));           
      GEvent.addListener(directions, "error", HandleDirectionErrors);
      
      if(directionsType == "FROM")
      {	  
        directions.load("from: " + mappedAddress + " to: " + inputAddress); 
        _directionsFromMapAddr = "YES";
        _directionsAddress = inputAddress;
      }
      else
      {
		directions.load("from: " + inputAddress + " to: " + mappedAddress);
		_directionsFromMapAddr = "NO";
		_directionsAddress = inputAddress;
	   }
      
      _showingDrivingDirections = "YES";
      return true;    
}

function HandleDirectionErrors()
{                       
    document.getElementById("_mapCanvas").innerHTML = "<center><br/><br/><strong>We could not get directions as requested. <br/><br/><br/>Data may not be available for the requested address. Please verify the address.</strong> <br/></center>"; 
    document.getElementById("_popupMapContent").style.width = "500px";                
    document.getElementById("_drivingDirections").style.display = "none";           
}

//Not handling map errors individually right now... 
function HandleMapErrors()
{               
    //G_GEO_UNKNOWN_ADDRESS 
    //G_GEO_SERVER_ERROR
    //G_GEO_MISSING_QUERY 
    //G_GEO_BAD_REQUEST 
    //G_GEO_BAD_KEY
    
    document.getElementById("_mapCanvas").innerHTML = "<br/><strong>We could not get map as requested. Please verify the input address.</strong> <br/><br/>Status Code: " +  directions.getStatus().code;    
    document.getElementById("_popupMapContent").style.width = "500px";                
    document.getElementById("_drivingDirections").style.display = "none";      
}

function clearDirections()
{
    document.getElementById("_drivingDirections").innerHTML = "";
}

function DrawMap(point, address, anchorItem, xOffset, yOffset)
{    
    clearMap();
    _mapRetryCount = 0;
    //clearMap();       
    var infoWindowText;
    map.setCenter(point, 14);               
    var marker = new GMarker(map.getCenter());
    map.addOverlay(marker);  
    
    _currentAddress = address;
    _showingDrivingDirections = "NO";    
    
    infoWindowText = address + "<div style='height:100px; width:300px;'><br/><br/>";	
	if(_disableDiretions == "NO"){
		infoWindowText = infoWindowText + "<strong>Directions:</strong> <a href='javascript:ShowDirectionsForm(&#34FROM&#34);'>From here</a> | <a href='javascript:ShowDirectionsForm(&#34TO&#34);'>To here</a><br/><br/>";              
	    infoWindowText = infoWindowText + "<div id='_directionsFormDiv' style='display:none;height:400px;'>";
	    infoWindowText = infoWindowText + "<form action='javascript://' onsubmit='getDirections();'>";
	    infoWindowText = infoWindowText + "<div id='inputAddressTitle'>&nbsp;</div><input type='text' id='addressInputDirections' style='width:200px;'/>";
	    infoWindowText = infoWindowText + "<input type='hidden' id='mappedAddress' value=\"" + address + "\"/>";
	    infoWindowText = infoWindowText + "<input type='hidden' id='directionsType'/> &nbsp; <input type='submit' value='Go'/></form><br/> (Tip: You can enter airport code as address)<br/></div>";
	}
    marker.openInfoWindowHtml(infoWindowText);  
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(infoWindowText);}); 

    var mapHolder = document.getElementById("_mapHolder");   
    var mapBoxPosX = 20;
    var mapBoxPosY = GetObjectPosition(anchorItem).y - 400;  
    
    if (isNaN(xOffset)){ xOffset = 0; }
    if (isNaN(yOffset)){ yOffset = 0; }
    
    mapBoxPosX = mapBoxPosX + xOffset;
    mapBoxPosY = mapBoxPosY + yOffset;    
      
    document.getElementById("_popupMapContent").style.width = "500px";               
    mapHolder.style.top = mapBoxPosY + "px"; 
    mapHolder.style.left = mapBoxPosX + "px"; 
    mapHolder.style.display = "block";
    document.getElementById("_drivingDirections").style.display = "none";
   
    var dummyIframe = document.getElementById("_dummyIframeForMaps");					
    dummyIframe.style.width =  mapHolder.offsetWidth;
	dummyIframe.style.height = mapHolder.offsetHeight;	
	dummyIframe.style.top = mapBoxPosY + "px";  
	dummyIframe.style.left = mapBoxPosX + "px"; 
	dummyIframe.style.zIndex = mapHolder.style.zIndex - 1; 
	dummyIframe.style.display = "block";   
			
	HideSpinWaitSpinWheel();
}

function clearMap()
{
    document.getElementById("_mapCanvas").innerHTML = "";
    map = new GMap2(document.getElementById("_mapCanvas"),{ size: new GSize(500,500) } );
    map.addControl(new GLargeMapControl()); 
    var mapControl = new GMapTypeControl();
    map.addControl(mapControl);  
}

function ShowSpinWaitSpinWheel(anchorItem)
{        
    var spinWheel = document.getElementById("_waitSpinWheel");   
    var spinWheelPos = GetObjectPosition(anchorItem);   
      
    spinWheel.style.top = spinWheelPos.y + "px"; 
    spinWheel.style.left = spinWheelPos.x + "px"; 
    spinWheel.style.display = "block";                  
}

function HideSpinWaitSpinWheel()
{
    var spinWheel = document.getElementById("_waitSpinWheel");   
    spinWheel.style.display = "none";  
}

function CloseMap()
{    
    var mapHolder = document.getElementById("_mapHolder"); 
    var dummyIframe = document.getElementById("_dummyIframeForMaps");	   
    dummyIframe.style.display = "none";
    mapHolder.style.display = "none";
    
    document.getElementById("_addnlInfoToPrintWithMaps").innerHTML = "";
} 	

function PrintMap()
{         
    var additionInfoForPrint
    var marker;
    var printWindow;
       
	var addnlInfo = document.getElementById("_addnlInfoToPrintWithMaps").innerHTML;
	var printUrl;
	var isCityGudes = document.getElementById("_isCityGudes").value;
	if(isCityGudes == "YES" && _disableDiretions == "NO") //Cityguides specific priting
	{
	    var currentCityCode = document.getElementById("FormUserControl__currentCityCode").value;
	    var idOfCuurentMapVenue = document.getElementById("_idOfCuurentMapVenue").value;	    
	    printUrl = '/Destinations/PrintDestinations.aspx?cityguides=' + isCityGudes + '&id=' + idOfCuurentMapVenue + '&citycode=' + currentCityCode;	    
	}
	else //Generic printing
	{
	    printUrl = '/Destinations/PrintDestinations.aspx?address=' + _currentAddress + '&addnlInfo=' + addnlInfo;	    
	}
	
	if(_showingDrivingDirections == "YES")
	{
	      printUrl = printUrl + "&directionsfrom" + _directionsFromMapAddr + "&directionsaddress=" + _directionsAddress;
	}
	printWindow = window.open(printUrl,'','');
	CloseMap();	
}