﻿function MFBAirportMarker(latitude, longitude, name, code, type, fShowMarker)
{
    this.latitude = latitude;
    this.longitude = longitude;
    this.Name = name;
    this.Code = code;
    this.Type = type;
    this.fShowMarker = fShowMarker;
}

function MFBPhotoMarker(urlThumb, urlFull, latitude, longitude) 
{
    this.urlThumb = urlThumb;
    this.urlFull = urlFull;
    this.latitude = latitude;
    this.longitude = longitude;
}

function nll(lat, lng)
{
    return new google.maps.LatLng(lat,lng);
}

function MFBMarker()
{
    this.marker = null;
    this.bodyHTML = null;
    this.mfbMap = null;
}

function displayInfoWindow(mfbmarker)
{
    if (mfbmarker.mfbMap.infoWindow != null)
        mfbmarker.mfbMap.infoWindow.close();
        
    mfbmarker.mfbMap.infoWindow = new google.maps.InfoWindow({content: mfbmarker.bodyHTML});
    mfbmarker.mfbMap.infoWindow.open(mfbmarker.mfbMap.gmap, mfbmarker.marker);
}


function MFBMap()
{
    this.gmap = null;
    this.divContainer = "";
    this.id = "";
    this.rgAirports = new Array();
    this.rgImages = new Array();
    this.zoom = 0;
    this.fShowMap = 0;
    this.fShowMarkers = 1;
    this.fShowRoute = 1;
    this.fDisableUserManip = 0;
    this.defaultZoom = 1;
    this.fAutoZoom = 0;
    this.defaultLat = 47.90634167;
    this.defaultLong = -122.2815639;
    this.minLat = this.defaultLat;
    this.maxLat = this.defaultLat;
    this.minLong = this.defaultLong;
    this.maxLong = this.defaultLong;
    this.center = new google.maps.LatLng(this.defaultLat, this.defaultLong);
    this.pathArray = null;
    this.infoWindow = null;
    this.MapType = google.maps.MapTypeId.HYBRID;
    this.clickPositionMarker = null;
    
    this.NewMap = function()
    {
        this.center = new google.maps.LatLng(this.defaultLat, this.defaultLong);
        var options = {zoom:this.zoom, center:this.center, mapTypeId: this.MapType};
        this.gmap = new google.maps.Map(document.getElementById(this.divContainer), options);
        if (this.fDisableUserManip != 0)
        {
            options = {draggable:false, 
                disableDoubleClickZoom:true,
                mapTypecontrol: false,
                scrollwheel: false,
                navigationControl: false, 
                disableDefaultUI: true,
                scaleControl:false};
            this.gmap.setOptions(options);
        }
        else
        {
            options = {scrollwheel:true, disableDoubleClickZoom:false};
            this.gmap.setOptions(options);
        }
        
        this.ShowOverlays();
        this.ZoomOut();

        return this.gmap;
    }
    
    this.ZoomOut = function() 
    {
       var gBoundsMap = new google.maps.LatLngBounds(new google.maps.LatLng(this.minLat, this.minLong), 
                                                     new google.maps.LatLng(this.maxLat, this.maxLong));
                                                     
       if (this.fAutoZoom == 0)
            this.gmap.setZoom(this.defaultZoom);
       else
            this.gmap.fitBounds(gBoundsMap);
       this.gmap.setCenter(gBoundsMap.getCenter());
    }
    
    this.showAirport = function(i)
        {
	        if (this.gmap !== null)
	        {
	            this.gmap.setCenter(new google.maps.LatLng(this.rgAirports[i].latitude, this.rgAirports[i].longitude));
	            this.gmap.setZoom(14);
		    }
        }
    
    this.showImage = function(i)
    {
        if (this.gmap != null)
        {
            this.gmap.setCenter(new google.maps.LatLng(this.rgImages[i].latitude, this.rgImages[i].longitude));
            this.gmap.setZoom(14);
        }
    }
   
    this.addEventMarker = function(point, s)
    {
        this.createNavaidMarker(point, s, '', 0, 'pin', this.id);
    }
    
    this.addListenerFunction = function(s)
    {
        google.maps.event.addListener(this.gmap, 'click', s);
    }
    
    this.createMarker = function(point, name, szIcon, szHtml)
    {
      var mfbmarker = new MFBMarker();
      mfbmarker.marker = new google.maps.Marker({position:point, clickable:true, icon:szIcon, map:this.gmap, title:name});;
      mfbmarker.bodyHTML = szHtml;
      mfbmarker.mfbMap = this;
      
      google.maps.event.addListener(mfbmarker.marker, 'click', function() {displayInfoWindow(mfbmarker);});
      
      return mfbmarker.marker;
    }

    this.iconURLForType = function (sz) {
        if (sz == "pin")
            return "/logbook/images/pushpin.png";
        if (sz == "Airport" || sz == "Heliport" || sz == "Seaport" || sz == "A" || sz == "H" || sz == "S")
            return "http://myflightbook.com/logbook/images/airport.png";
        else if (sz.length > 0)
            return 'http://myflightbook.com/logbook/images/tower.png';
        else
            return '';
    }
    
    // Creates a marker at the given point with the given number label and adds a listener that pops an info-window.  Designed for airports and navaids.
    this.createNavaidMarker = function(point, name, code, i, type, mapID) 
    {
        var sz;
        sz = "<b>" + name + "</b><br />";
        if (type == "" || type == "pin")
            sz = name;
        else
            {
            if (type == "Airport")
                {
                sz += "<a href=\"http://map.aeroplanner.com/mapping/chart/aptrpt.cfm?typ=APT&txt=" + code + "\">Get Airport Info for " + code + "</a><br />";
                sz += "<a href=\"http://www.aopa.org/wx/#a=" + code + "\">Get Current weather at " + code + "</a><br />";
                }
            else
                sz += type + "<br />";

            sz += "<a href=\"javascript:" + mapID + ".showAirport(" + i + ")\">Zoom in</a>";
            }
      
      return this.createMarker(point, name, this.iconURLForType(type), sz);
    }
    
    this.createImageMarker = function(point, i, mapID)
    {
        var szImg = "<a href=\"" + this.rgImages[i].urlFull + "\" target=\"_blank\"><img src=\"" + this.rgImages[i].urlThumb + "\"></a>";
        var szZoom = "<a href=\"javascript:" + mapID + ".showImage(" + i + ")\">Zoom in</a>";
        var szDiv = "<div>" + szZoom + "<br />" + szImg + "</div>";
        return this.createMarker(point, "Photograph", 'http://myflightbook.com/logbook/images/cameramarker.png', szDiv);
    }
    
    // edit airports functionality    
    this.clickMarker = function(point, name, type, szHtml)
    {
        if (this.clickPositionMarker)
            this.clickPositionMarker.setMap(null);
        if (this.infoWindow)
            this.infoWindow.close();
        
        this.clickPositionMarker = this.createMarker(point, name, this.iconURLForType(type), szHtml);
    }

    // set up overlays
    this.ShowOverlays = function()
    {
        if (this.rgAirports.length > 0) 
        {
            var points = [];
    		
            if (this.fShowMarkers != 0)
            {
            for (var i = 0; i < this.rgAirports.length; i++)
                {
                var point = new google.maps.LatLng(this.rgAirports[i].latitude, this.rgAirports[i].longitude);
                this.createNavaidMarker(point, this.rgAirports[i].Name + "(" + this.rgAirports[i].Code + ")", this.rgAirports[i].Code, i, this.rgAirports[i].Type, this.id);
                points.push(point)
                }

            if (this.fShowRoute != 0)
		        var routeMap = new google.maps.Polyline({path:points, strokeColor: "#0000FF", strokeOpacity: 0.5, strokeWeight:5, map:this.gmap, geodesic:true});
            }
        }
        if (this.rgImages.length > 0 && this.fShowMarkers != 0)
        {
            for (var i = 0; i < this.rgImages.length; i++)
            {
                var point = new google.maps.LatLng(this.rgImages[i].latitude, this.rgImages[i].longitude);
                this.createImageMarker(point, i, this.id);
            }
        }

        if (this.pathArray != null)
            var pathMap = new google.maps.Polyline({path:this.pathArray, strokeColor: "#FF0000", strokeOpacity: 0.5, strokeWeight: 5, map:this.gmap});
	}
}

function MFBNewMapOptions(mfbMapOptions)
{
    var mfbNewMap = new MFBMap();
    
    if (mfbMapOptions.divContainer != null)
        mfbNewMap.divContainer = mfbMapOptions.divContainer;
    if (mfbMapOptions.rgAirports != null)
        mfbNewMap.rgAirports = mfbMapOptions.rgAirports;
    if (mfbMapOptions.rgImages != null)
        mfbNewMap.rgImages = mfbMapOptions.rgImages;
    if (mfbMapOptions.fZoom != null)
        mfbNewMap.zoom = mfbMapOptions.fZoom;
    if (mfbMapOptions.fShowMap != null)
        mfbNewMap.fShowMap = mfbMapOptions.fShowMap;
    if (mfbMapOptions.fShowMarkers != null)
        mfbNewMap.fShowMarkers = mfbMapOptions.fShowMarkers;
    if (mfbMapOptions.fShowRoute != null)
        mfbNewMap.fShowRoute = mfbMapOptions.fShowRoute;
    if (mfbMapOptions.fDisableUserManip != null)
        mfbNewMap.fDisableUserManip = mfbMapOptions.fDisableUserManip;
    if (mfbMapOptions.defaultZoom != null)
        mfbNewMap.defaultZoom = mfbMapOptions.defaultZoom;
    if (mfbMapOptions.fAutoZoom != null)
        mfbNewMap.fAutoZoom = mfbMapOptions.fAutoZoom;
    if (mfbMapOptions.defaultLat != null)
        mfbNewMap.defaultLat = mfbMapOptions.defaultLat;
    if (mfbMapOptions.defaultLong != null)
        mfbNewMap.defaultLong = mfbMapOptions.defaultLong;
    if (mfbMapOptions.defaultLat != null)
        mfbNewMap.defaultLat = mfbMapOptions.defaultLat;
    if (mfbMapOptions.minLat != null)
        mfbNewMap.minLat = mfbMapOptions.minLat;
    if (mfbMapOptions.maxLat != null)
        mfbNewMap.maxLat = mfbMapOptions.maxLat;
    if (mfbMapOptions.minLong != null)
        mfbNewMap.minLong = mfbMapOptions.minLong;
    if (mfbMapOptions.maxLong != null)
        mfbNewMap.maxLong = mfbMapOptions.maxLong;
    if (mfbMapOptions.pathArray != null)
        mfbNewMap.pathArray = mfbMapOptions.pathArray;
    if (mfbMapOptions.MapType != null)
        mfbNewMap.MapType = mfbMapOptions.MapType;
    if (mfbMapOptions.id != null)
        mfbNewMap.id = mfbMapOptions.id;
        
    return mfbNewMap;        
}        


function AddMap(mfbMapOptions) 
{
  if (mfbMapOptions.fShowMap) 
    {
    mfbMapOptions.NewMap();

    return mfbMapOptions;
    }
}

function unload()
{
GUnload();
}
    
