﻿// JScript File
    var map;
    var directionsPanel;
    var directions;
    var gdir;
    var homelatlng;
    var okaloosalatlng;
    var panamalatlng;
    var okaloosamarker;
    var panamamarker;
    var mileLayer;
    
    function initialize() {
      if (GBrowserIsCompatible()) {      

        map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GSmallMapControl());
        directionsPanel = document.getElementById("route");
        directions = new GDirections(map, directionsPanel);

        map.setCenter(new GLatLng(30.280000, -86.015000), 6);
        //GEvent.addListener(directions, "load", onGDirectionsLoad);
        //GEvent.addListener(directions, "error", handleErrors);
        //setDirections("Okaloosa Regional Airport", "South Barrett Square, Rosemary Beach, FL 32461", "en_US");
        var boundaries = new GLatLngBounds(new GLatLng(25.820529092773226,-91.165537109375), new GLatLng(34.61466147177172,-80.87134765625));
        mileLayer = new GGroundOverlay("Assets/GoogleMaps/markers/300mile.png", boundaries);
        
        map.addOverlay(mileLayer); 
        homelatlng = new GLatLng(30.28093510972301, -86.01580917835235);
        okaloosalatlng = new GLatLng(30.48000725736013, -86.53535028076172);
        panamalatlng = new GLatLng(30.30095, -85.828521);
        okaloosamarker = createMarker(okaloosalatlng, "", "Okaloosa Regional Airport, United States", "http://www.rosemarybeach.com/assets/googlemaps/plane.png", "http://www.rosemarybeach.com/assets/googlemaps/plane_shadow.png");
        panamamarker = createMarker(panamalatlng, "", "982 State Highway 388, Panama City Beach, FL 32413", "http://www.rosemarybeach.com/assets/googlemaps/plane.png", "http://www.rosemarybeach.com/assets/googlemaps/plane_shadow.png");
        map.addOverlay(createMarker(homelatlng, "http://www.rosemarybeach.com/Images/eCards/card7_sm.jpg", "Rosemary Beach", "http://www.rosemarybeach.com/assets/googlemaps/POI.png", "http://www.rosemarybeach.com/assets/googlemaps/POI_shadow.png")); 
        map.addOverlay(okaloosamarker); 
        map.addOverlay(panamamarker); 
        okaloosamarker.hide();
        panamamarker.hide();
        toggleSubNav(0);
                /*     
                GEvent.addListener(map,"click", function(overlay,latlng) {
                  if (overlay) {
                    // ignore if we click on the info window
                    return;
                  }
                  var tileCoordinate = new GPoint();
                  var tilePoint = new GPoint();
                  var currentProjection = G_NORMAL_MAP.getProjection();
                  tilePoint = currentProjection.fromLatLngToPixel(latlng, map.getZoom());
                  tileCoordinate.x = Math.floor(tilePoint.x / 256);
                  tileCoordinate.y = Math.floor(tilePoint.y / 256);
                  var myHtml = "Latitude: " + latlng.lat() + "<br/>Longitude: " + latlng.lng() + 
                    "<br/>The Tile Coordinate is:<br/> x: " + tileCoordinate.x + 
                    "<br/> y: " + tileCoordinate.y + "<br/> at zoom level " + map.getZoom();	
                  map.openInfoWindow(latlng, myHtml);
                });*/

      }
    }
    function initdirections()
    {
        directionsPanel = document.getElementById("route");
        directions = new GDirections(map, directionsPanel);

    }

    function onGDirectionsLoad(){ 
          // Use this function to access information about the latest load()
          // results.

          // e.g.
          // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	      // and yada yada yada...
    }
    
    function createMarker(point, img, name, iconimg, iconshadow ) 
    {
            // Create a lettered icon for this point using our icon class
            //var letter = String.fromCharCode("A".charCodeAt(0) + index);
        var baseIcon = new GIcon();
        
        baseIcon.iconSize = new GSize(32, 32);
        baseIcon.shadowSize = new GSize(59, 32);
        baseIcon.iconAnchor = new GPoint(24, 24);
        baseIcon.infoWindowAnchor = new GPoint(24, 24);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);

        var specialIcon = new GIcon(baseIcon);
        if(iconimg.length==0)
        {
            specialIcon.image = "http://maps.google.com/mapfiles/ms/micons/homegardenbusiness.png";
            specialIcon.shadow = "http://maps.google.com/mapfiles/ms/micons/homegardenbusiness.shadow.png";
        }
        else
        {
            specialIcon.image = iconimg;
            specialIcon.shadow = iconshadow;
        }
            

        // Set up our GMarkerOptions object
        markerOptions = { icon:specialIcon };
        var marker = new GMarker(point, markerOptions);
        GEvent.addListener(marker, "click", function() {
            //marker.openInfoWindowHtml("Marker <b>" + letter + "</b>");
            marker.openInfoWindowHtml("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td></td><td height='10px'></td><td valign='top'></td></tr><tr><td><img src='" + img + "'/></td><td width='10px'></td><td valign='top'>" + name + "</td></tr></table>"); 
        });
        return marker;
    }
    
      
    function handleErrors()
    {
	    if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);
	    else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
	        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);
    	   
	    else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
	        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);

	        //   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	        //     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
    	     
	    else if (directions.getStatus().code == G_GEO_BAD_KEY)
	        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code);

	    else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
	        alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
    	    
	    else alert("An unknown error occurred.");
    }  
	
    function setDirections(fromAddress, toAddress, locale) {
        okaloosamarker.hide();
        panamamarker.hide();
        mileLayer.hide();
        var Airlines = document.getElementById("airlinenotes");
        Airlines.innerHTML = '';
        Airlines.style.display='none';
        if(fromAddress=='Okaloosa Regional Airport, United States')
        {
           okaloosamarker.show(); 
           Airlines.style.display='block';
           Airlines.innerHTML = '<br><hr><br><strong>***NOTE:<br>Okaloosa Air Terminal</strong> in Fort Walton Beach offers daily service by Delta (Atlantic Southeast Airlines), Northwest and American airlines.';
        }
        if(fromAddress=='982 State Highway 388, Panama City Beach, FL 32413')
        {
           panamamarker.show(); 
           Airlines.style.display='block';
           Airlines.innerHTML = '<br><hr><br><strong>***NOTE:</strong><br>Panama City Beach Airport (ECP) is located at: 982 State Highway 388, Panama City Beach, FL 32413. <br>For more information visit the <a href="http://newpcairport.com/directions-to-airport/" target="_blank">official website</a>.';
        }
        
      var fromAddressTxt = document.getElementById("fromAddress");
      fromAddressTxt.value =  fromAddress;
      directions.load("from: " + fromAddress + " to: " + toAddress);
        //var fromdiv = document.getElementById("fromform");
        //fromdiv.style.display='none';
        //var todiv = document.getElementById("toform");
        //todiv.style.display='none';
        var divroute = document.getElementById("route");
        if((fromAddress.length>0)||(toAddress.length>0))
        {
            
            divroute.style.display='block';
        }
        else
        {
            divroute.style.display='none';
        }
                
    }
    
    function showradius()
    {
        mileLayer.show();
        okaloosamarker.hide();
        panamamarker.hide();
        var fromAddressTxt = document.getElementById("fromAddress");
        fromAddressTxt.value =  '';
        var toAddressTxt = document.getElementById("toAddress");
        toAddressTxt.value =  '';
        var Airlines = document.getElementById("airlinenotes");
        Airlines.innerHTML = '';
        Airlines.style.display='none';
        directions.load("from:  to: ");
                    if(map.getZoom()!=6)
                    {
                        map.setZoom(6)
                    }
                    map.panTo(new GLatLng(30.280000, -86.015000));
    }
        
    function showDirectionForm()
    {
        toggleSubNav(1);
        var fromAddressTxt = document.getElementById("fromAddress");
        fromAddressTxt.value =  '';
        var toAddressTxt = document.getElementById("toAddress");
        toAddressTxt.value =  '';
        if(map.getZoom()!=12)
        {
            map.setZoom(12)
        }
        map.panTo(new GLatLng(30.280000, -86.015000));
        setDirectionFrom();
    }
    
    function setDirectionFrom()
    {
        toggleSubNav(1);
        okaloosamarker.hide();
        panamamarker.hide();
        mileLayer.hide();
        var fromAddress = document.getElementById("fromAddress").value;
        var divroute = document.getElementById("route");
        var Airlines = document.getElementById("airlinenotes");
        Airlines.innerHTML = '';
        Airlines.style.display='none';
        if(fromAddress.length>0)
        {
            
            divroute.style.display='block';
        }
        else
        {
            divroute.style.display='none';
        }
        directions.load("from: " + fromAddress + " to: 78 North Barrett Square, Rosemary Beach, FL 32461");
        return false;
    }
    
    function setDirectionTo()
    {
        okaloosamarker.hide();
        panamamarker.hide();
        mileLayer.hide();
        var toAddress = document.getElementById("toAddress").value;
        var divroute = document.getElementById("route");
        var Airlines = document.getElementById("airlinenotes");
        Airlines.innerHTML = '';
        Airlines.style.display='none';
        if(toAddress.length>0)
        {
            
            divroute.style.display='inline';
        }
        else
        {
            divroute.style.display='none';
        }      
        directions.load("from: 78 North Barrett Square, Rosemary Beach, FL 32461 to: " + toAddress);  
        return false;
    }
    
    function checkEnter(e, from)
    { 
        //e is event object passed from function invocation
        var characterCode 

        if(e && e.which)
        { 
            //if which property of event object is supported (NN4)
            e = e
            characterCode = e.which //character code is contained in NN4's which property
        }
        else
        {
            e = event
            characterCode = e.keyCode //character code is contained in IE's keyCode property
        }
        //alert(characterCode);
        if(characterCode == 13)
        { 
            //if generated character code is equal to ascii 13 (if enter key)
            if(from)
            {
                setDirectionFrom();
            }
            else
            {
                setDirectionTo();
            }
            return false 
        }
        else
        {
            return true 
        }

    }
                