/*
    Variables: Map
    GMap2 Object
    
    Variables: newBounds
    Used in <drawCircle> function
    
    Variables: PointDoubleClick
    GLatLng object used in <GetAptInRadius>
    
    Variables: Markers
    An associative array of GMarker objects. Objects are accessed in a Markers[AptCode] manner
    
    Variables: ad
    An HTML string containing an Iframe of the Banman Pro ad
    
    Variables: basicFBOTemplate
    An HTML string of the basic FBO bubble
    
    Variables: platFBOTemplate
    An HTML string of the platinum FBO bubble
    
    Variables: fuelFBOTemplate
    An HTML string of the fuel container contained within the platinum FBO bubble

    Variables: Airports
    An array of all the airports currently on the map. It is set from the DisplayAirports function.
    
    Variables: openedInfoWindow
    Contains the AptIndex of the currently open info window. Used to stop trying to reopen an already opened bubble.
*/
var Map;
var newBounds = new GLatLngBounds();
var PointDoubleClick;
var Markers = [];
var ad = "<div align='center'>" + banmanPro() + "</div>";
var Airports = [];
var openedInfoWindow;
var searchingIcon = false;
var sHTML = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

var basicFBOTemplate = new Template("<div id='fuelContainer'><div id='fuelTop'></div><div id='fuelAptIdentContainer'><b>#{AptCode}</b>" + sHTML + sHTML + sHTML + sHTML + "#{Approach}</div><div style='padding: 5px; border-left: 1px #555555 solid; border-right: 1px #555555 solid;'><b>#{AptName}</b><br><b>Longest Runway:</b> #{RunwayLength} ft<br></div><div style='clear: both;'></div><div style='padding: 5px; border-left: 1px #555555 solid; border-right: 1px #555555 solid;'>#{FuelBrandLogos}</div><div id='fuelLinksContainer'><div id='fuelLinksLeft'><a target='_blank' href='http://www.globalair.com/airport/airport.aspx?AptCode=#{AptCode}'>Airport Information</a><br>#{ExtraLinksLeft}</div><div id='fuelLinksRight'>#{ExtraLinksRight}</div><div style='clear: both;'></div></div><div id='fuelBottom'></div></div>");
var platFBOTemplate = new Template("<div id='fuelContainer'><div id='fuelTop'></div><div id='fuelAptIdentContainer'><b>#{AptCode}</b>" + sHTML + sHTML + sHTML + sHTML + "#{Approach}</div><div style='border-left: 1px #555555 solid; border-right: 1px #555555 solid;'><div style='width: 100px; padding: 2px; float: left;'><a target='_blank' href='http://www.globalair.com/airport/airport.aspx?AptCode=#{AptCode}'><img class='bubbleLogo' src='#{FBOLogo}' border='0' style='float: left; width: 100px;'/></a></div><div id='fuelFBOInfo'><b>FBO:</b> #{FBOName}<br/><b>Arinc:</b> #{FBOArinc}<br/><b>Phone:</b> #{FBOPhone}<br/><b>Runway:</b> #{RunwayLength} ft</div><div style='clear: both;'></div></div><div style='clear: both;'><div style='padding: 5px; border-left: 1px #555555 solid; border-right: 1px #555555 solid;'>#{FuelContainer}<div style='clear: both; font-size: 8pt;'>All Prices Include Taxes</div></div><div style='padding: 5px; border-left: 1px #555555 solid; border-right: 1px #555555 solid;'>#{FuelBrandLogos}</div><div id='fuelLinksContainer'><div id='fuelLinksLeft'><a target='_blank' href='http://www.globalair.com/airport/airport.aspx?AptCode=#{AptCode}'>Airport Information</a><br>#{ExtraLinksLeft}</div><div id='fuelLinksRight'>#{ExtraLinksRight}</div><div style='clear: both;'></div></div></div><div id='fuelBottom'></div></div>");
var fuelFBOTemplate = new Template("<div id='fuelTypeContainer'><div id='fuelType'><center>#{FuelName}</center></div><div id='fuelPrice'><center>#{FuelPrice}</center></div></div>");

/*
   Function: GetAptsAroundApt

   Function displays airports around a certain airport, specified by the user, that also meet the users specified requirements.

   Parameters:

      AptCode - The Airport Identifier.
      Range - The nautical mile distance from the airport. Accepted Values Are *(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)*
      Private - Show only private airports - Accepted Values Are *(True or False)*
      Public - Show only public airports - Accepted Values Are *(True or False)*
      Fac - Show only the selected facility type. Accepted Values Are *(Airport, Balloonport, Gliderport, Heliport, Seaplane Base, Stolport, Ultralight)*
      Rwy - Show only airports with runway lengths of a certain length. Accepted Values Are *(any, gt5000(Greater Than 5000 FT in Length), lt5000(Less Than 5000 FT in Length))*
      Jeta - Show only airports reporting Jeta. Accepted Values Are *(True or False)*
      ll100 - Show only airports reporting 100LL. Accepted Values Are *(True or False)*
      Mogas - Show only airports reporting Mogas. Accepted Values Are *(True or False)*
      G8087 - Show only airports reporting 8087. Accepted Values Are *(True or False)*
      FuelBrand - Show only airports with FBOs selling a particular brand of fuel

   Returns:

      Null
   
   See:
   
      <DisplayAirports>
*/
var AptsInRadius;
function GetAptsAroundApt(AptCode, Range, Private, Public, Fac, Rwy, Approach, Jeta, ll100, Mogas, g8087, FuelBrand) {
    showSearchingIcon("Searching for Airports...");
    var first_apt;
    var temp_1 = new Ajax.Request('/airport/_includes/ajax/getapt.aspx?', {
          method:'get',
          parameters: {'aptcode': AptCode},
          onSuccess: function(e){
            e.responseText = '[' + e.responseText + ']';
            first_apt = eval(e.responseText);
            if(first_apt[0] == null) {
                alert("Your fuel mapping search did not return any matching airports. Please try different search parameters.");
                hideSearchingIcon();
            }
          }
    });
    var temp_2 = new Ajax.Request('/airport/_includes/ajax/getaptinrange.aspx?', {
      method:'get',
      parameters: {'aptcode': AptCode, 'range': Range, 'pr': Private, 'pu': Public, 'fac': Fac, 'rwy': Rwy, 'approach': Approach, 'jeta': Jeta, '100ll': ll100, 'mogas': Mogas, '8087': g8087, 'fuelbrand': FuelBrand},
      onSuccess: function(e){
        AptsInRadius = eval(e.responseText);
        if(AptsInRadius[0] != undefined) {
	        AptsInRadius.unshift(first_apt[0]);
	        DisplayAirports(AptsInRadius);
	    } else if(first_apt[0] != null) {
	    	DisplayAirports(first_apt);
	    }
      }
    });
    PointDoubleClick = null;
}
/*
   Function: GetAptInRadius

   Function takes in the passed variables and does an ajax call to the getaptinrange.aspx page located in
   _includes/ajax/ folder. The information recieved is then passed on to the DisplayAirports function.

   Parameters:

      Lat - Latitude of the point to find airports around.
      Lng - Longitude of the point to find airports around.
      Range - The nautical mile distance from the airport. Accepted Values Are *(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)*
      Private - Show only private airports - Accepted Values Are *(True or False)*
      Public - Show only public airports - Accepted Values Are *(True or False)*
      Fac - Show only the selected Facility type. Accepted Values Are *(Airport, Balloonport, Gliderport, Heliport, Seaplane Base, Stolport, Ultralight)*
      Rwy - Show only airports with runway lengths of a certain length. Accepted Values Are *(any, gt5000(Greater Than 5000 FT in Length), lt5000(Less Than 5000 FT in Length))*
      Jeta - Show only airports reporting Jeta. Accepted Values Are *(True or False)*
      ll100 - Show only airports reporting 100LL. Accepted Values Are *(True or False)*
      Mogas - Show only airports reporting Mogas. Accepted Values Are *(True or False)*
      G8087 - Show only airports reporting 8087. Accepted Values Are *(True or False)*
      FuelBrand - Show only airports with FBOs selling a particular brand of fuel

   Returns:

      Null
   
   See:
   
      <DisplayAirports>
*/
function GetAptInRadius(Lat, Lng, Range, Private, Public, Fac, Rwy, Approach, Jeta, ll100, Mogas, g8087, FuelBrand) {
    showSearchingIcon("Searching for Airports...");
    PointDoubleClick = new GLatLng(Lat, Lng);
    var temp = new Ajax.Request('/airport/_includes/ajax/getaptinrange.aspx?', {
      method:'get',
      parameters: {'lat': Lat, 'lng': Lng, 'range': Range, 'pr': Private, 'pu': Public, 'fac': Fac, 'rwy': Rwy, 'approach': Approach, 'jeta': Jeta, '100ll': ll100, 'mogas': Mogas, '8087': g8087, 'fuelbrand': FuelBrand},
      onSuccess: function(e){
        var AptsInRadius = eval(e.responseText);
        if(AptsInRadius[0] == null) {
                alert("Your fuel mapping search did not return any matching airports. Please try different search parameters.");
                hideSearchingIcon();
        } else {
        	DisplayAirports(AptsInRadius);
        }
        }
    });

}
/*
   Function: DoubleClick

   Function pulls the information from the fields the user provides input into. 
   The information gathered is then passed on to the GetAptInRadius function.

   Parameters:

      overlay - Overlay object given but not used in this function.
      point - GLatLng object provided by Google Event Listener. Access variables as such: point.lat() point.lng()

   Returns:

      Null
   
   See:
   
      <GetAptInRadius>
*/
function DoubleClick(overlay, point) {

    var url_vars = getUrlVars();

    var priv = "false";
    var pub = "false";
    
    if (url_vars.av == "pu")
    {
    pub = "true";
    }
    else if (url_vars.av == "pr")
    {
    priv = "true";
    }
    else
    {
    pub = "true";
    priv = "true";
    }

    var dist = "";
    if (url_vars.rad != null && url_vars.rad != "")
    {
    dist = url_vars.rad;
    }
    
    var fac = "";
    if (url_vars.FacType != null && url_vars.FacType != "")
    {
    fac = url_vars.FacType;
    }
    
    var rwy = "";
    if (url_vars.rwy != null && url_vars.rwy != "")
    {
    rwy = url_vars.rwy;
    }
    
    var approach = "";
    if (url_vars.app != null && url_vars.app != "")
    {
    approach = url_vars.app;
    }
    
    var jeta = "false";
    if (url_vars.fueljeta != null && url_vars.fueljeta != "")
    {
    jeta = "true";
    }
    var ll100 = "false";
    if (url_vars.fuel100ll != null && url_vars.fuel100ll != "")
    {
    ll100 = "true";
    }
    var g8087 = "false";
    if (url_vars.fuel8087 != null && url_vars.fuel8087 != "")
    {
    g8087 = "true";
    }
    var mogas = "false";
    if (url_vars.fuelmogas != null && url_vars.fuelmogas != "")
    {
    mogas = "true";
    }
    
    var fb = "";
    if (url_vars.fuelbrand != null && url_vars.fuelbrand != "")
    {
    fb = url_vars.fuelbrand;
    }
    
    GetAptInRadius(point.lat(), point.lng(), dist, priv, pub, fac, rwy, approach, jeta, ll100, mogas, g8087, fb);
}
/*
   Function: FindAirports

   Function pulls the information from the fields the user provides input into. 
   The information gathered is then used to either call the 
   GetAptsAroundApt function of the GetAptInRadius function depending on
   if the user provided an airport indentifer.

   Parameters:

      Null

   Returns:

      Null
   
   See:
   
      <GetAptInRadius> | <GetAptsAroundApt>
*/
function FindAirports() {

    var url_vars = getUrlVars();

    var apt = url_vars.aptcode;
    
    var priv = "false";
    var pub = "false";
    
    if (url_vars.av == "pu")
    {
    pub = "true";
    }
    else if (url_vars.av == "pr")
    {
    priv = "true";
    }
    else
    {
    pub = "true";
    priv = "true";
    }

    var dist = "";
    if (url_vars.rad != null && url_vars.rad != "")
    {
    dist = url_vars.rad;
    }
    
    var fac = "";
    if (url_vars.FacType != null && url_vars.FacType != "")
    {
    fac = url_vars.FacType;
    }
    
    var rwy = "";
    if (url_vars.rwy != null && url_vars.rwy != "")
    {
    rwy = url_vars.rwy;
    }
    
    var approach = "";
    if (url_vars.app != null && url_vars.app != "")
    {
    approach = url_vars.app;
    }
    
    var jeta = "false";
    if (url_vars.fueljeta != null && url_vars.fueljeta != "")
    {
    jeta = "true";
    }
    var ll100 = "false";
    if (url_vars.fuel100ll != null && url_vars.fuel100ll != "")
    {
    ll100 = "true";
    }
    var g8087 = "false";
    if (url_vars.fuel8087 != null && url_vars.fuel8087 != "")
    {
    g8087 = "true";
    }
    var mogas = "false";
    if (url_vars.fuelmogas != null && url_vars.fuelmogas != "")
    {
    mogas = "true";
    }
    
    var fb = "";
    if (url_vars.fuelbrand != null && url_vars.fuelbrand != "")
    {
    fb = url_vars.fuelbrand;
    }
    
    if(apt != '') {
        GetAptsAroundApt(apt, dist, priv, pub, fac, rwy, approach, jeta, ll100, mogas, g8087, fb);
    } else {
        GetAptInRadius(Map.getCenter().lat(), Map.getCenter().lng(), dist, priv, pub, fac, rwy, approach, jeta, ll100, mogas, g8087, fb);
    }
}
/*
   Function: SetZoomLevel

   Function pulls the range distance from the field the user provides input into. 
   The information gathered is then used to set the zoom level of the map based on the range
   the user provides in the dropdown menu.
   
   Parameters:
   
      Null
   
   Returns:
     
      Null
*/
function SetZoomLevel() {

    var url_vars = getUrlVars();
    var dist = "50";
    if (url_vars.rad != null && url_vars.rad != "")
    {
    dist = url_vars.rad;
    }

    ZoomDistance = dist;
    
    switch(parseInt(ZoomDistance)) {
        case 10:
            Map.setZoom(11);
            break;
        case 20:
            Map.setZoom(10);
            break;
        case 30:
            Map.setZoom(9);
            break;
        case 40:
            Map.setZoom(9);
            break;
        case 50:
            Map.setZoom(8);
            break;
        case 60:
            Map.setZoom(8);
            break;
        case 70:
            Map.setZoom(8);
            break;
        case 80:
            Map.setZoom(8);
            break;
        case 90:
            Map.setZoom(8);
            break;
        case 100:
            Map.setZoom(7);
            break;
        default:
            Map.setZoom(7);
            break;
    }
}
/*
   Function: DisplayAirports

   Function pulls the information from the fields the user provides input into. 
   The information gathered is then passed on to the GetAptInRadius function.

   Parameters:

      Apts - An Airport Array recieved from the aspx pages that were called. It contains
      the following elements for each airport: 

   Airport Array:
   
      lat - Latitude
      lng - Longitude
      apt - Airport Identifer
      fac - Airport Name
      dist - Distance
      rwy - A *Runway Array*
      fbo - A *FBO Array*
   
   Runway Array:
      
      id - Id of the longest runway on the airport.
      length - Length of the longest runway on the airport in feet.
      
   FBO Array:
      
      compid - FBO Id in the database
      comp - FBO Name
      servlevel - FBO subscription level. Plat: 1, Gold: 2, Silver: 3, Basic: 4
      complogo - File name of FBO's logo. Image can be found in the following folder: http://www.globalair.com/airport/images/complogo/sm/
      facpic - An array of image file names that the FBO has uploaded
      unicom - FBO Unicom id
      arinc - FBO arinc id
      manager - FBO manager's name
      phone - FBO phone number
      fax - FBO fax number
      truck - FBO *Truck Array*
      ss - FBO *Self Service Array*
   
   Truck Array:
      jeta - Price of Jeta fuel
      ll - Price of 100LL fuel
      eight - Price of 8087 fuel
      mogas - Price of mogas
      add - Price of Jeta Prist
   
   Self Service Array:
      jeta - Price of Jeta fuel
      ll - Price of 100LL fuel
      eight - Price of 8087 fuel
      mogas - Price of mogas
      add - Price of Jeta Prist
      
   Returns:

      Null
*/
function DisplayAirports(Apts) {
    Airports = [];
    Airports = Apts;
    $('FuelAptList').replace('<div id=\"FuelAptList\"></div>');
    Map.clearOverlays();
    Markers = [];
    
    var url_vars = getUrlVars();
    
    if(PointDoubleClick == null) {
        Map.setCenter(new GLatLng(Apts[0].lat, Apts[0].lng));
    } else {
        Map.setCenter(PointDoubleClick);
    }
    SetZoomLevel();
    
    var dist = "50";
    if (url_vars.rad != null && url_vars.rad != "")
    {
    dist = url_vars.rad;
    }
    
    drawCircle(Map.getCenter(), dist);
    for(i = 0; i < Apts.length; i++) {
        var lat = Apts[i].lat;
        var lng = Apts[i].lng;
        var gmrker;
        var opts;
        var HasFuel;
        if(Apts[i].fbo[0] == undefined) {
           HasFuel = false;
        } else {
           HasFuel = true;
        }
        if(HasFuel) {
            opts = {
                "icon": FuelIconLabeled,
                "clickable": true,
                "labelText": "<font style=\"color: white; font-size: 10px;\">" + Apts[i].apt + "</font>",
                "labelOffset": new GSize(-9, -12)
            };
        } else {
            opts = {
                "icon": noFuelIconLabeled,
                "clickable": true,
                "labelText": "<font style=\"color: white; font-size: 10px;\">" + Apts[i].apt + "</font>",
                "labelOffset": new GSize(-9, -12)
            };
        }
                
        Map.addOverlay(DisplayMarker(new GLatLng(lat, lng), opts, Apts[i].apt, i));
        if(Apts[i].dist == '|distance|') {
            $('FuelAptList').insert('<a href=\"javascript:openInfoWindowLinked(\'' + Apts[i].apt + '\', \'' + i + '\')\">(' + Apts[i].apt + ') ' + Apts[i].fac + '</a><br />');
        } else {
            $('FuelAptList').insert('<a href=\"javascript:openInfoWindowLinked(\'' + Apts[i].apt + '\', \'' + i + '\')\">(' + Apts[i].apt + ') ' + Apts[i].fac + '</a> - ' + parseInt(Apts[i].dist).toFixed(0) + ' mi<br />');
        }
    }
    hideSearchingIcon();
    $('FuelAptList').insert('<br />' + ad);
    
    var aptcode = "";
    if (url_vars.aptcode != null && url_vars.aptcode != "")
    {
    aptcode = url_vars.aptcode;
    }
    
    if(aptcode != "") {
    
    	openInfoWindowLinked(Apts[0].apt, 0);
    }
}
/*
   Function: DisplayMarker

   Function creates the marker that will be placed onto the map. This was done so the browser
   will remember the airport identifer within the click event.

   Parameters:

      point - GLatLng object provided by Google Event Listener. Access variables as such: point.lat() point.lng()
      opts - Contains an array of options about display marker. Contains which icon to display at that point, if the marker is clickable, what to display on the marker, and the offset of the label from the point of the marker
      AptCode - The airport identifer of the airport
      AptIndex - Location of the airport in the *Airports* array

   Returns:

      gmrker - GMarker object
*/
function DisplayMarker(point, opts, AptCode, AptIndex) {
    var gmrker;
    gmrker = new LabeledMarker(point, opts);
    GEvent.addListener(gmrker, "click", function() {

        openInfoWindow(gmrker, AptIndex, AptCode);
    });
    GEvent.addListener(Map, "infowindowclose", function() {
        openedInfoWindow = null;
    });
    Markers[AptCode] = gmrker;
    return gmrker
}
/*
   Function: openInfoWindowLinked

   Function used to open the info window on the marker when the user clicks the link from the list of airports that are
   currently displayed on the map. Takes in an airport identifer, which if found within the Markers array
   is then used to call the openInfoWindow function.

   Parameters:

      AptCode - Airport identifer string
      AptIndex - Location of the airport in the *Airports* array
   
   Returns:
     
      Null

   See:

      <openInfoWindow>
*/

function openInfoWindowLinked(AptCode, AptIndex) {
    if(Markers[AptCode] != undefined) {
 
        openInfoWindow(Markers[AptCode], AptIndex, AptCode);
    }
}
/*
   Function: openInfoWindow

   Function takes in the passed variables and the constructs the information to fill the bubble using the templates
   found in the begining of the code. It will use the basic fbo template or the platinum fbo template based on if the 
   FBO is a basic, silver, gold, or plat.

   Parameters:

      gmrker - GMarker object
      AptIndex - Location of the airport in the *Airports* array
   
   Returns:
   
      Null
      
   Airport Array:
   
      lat - Latitude
      lng - Longitude
      apt - Airport Identifer
      fac - Airport Name
      dist - Distance
      rwy - A *Runway Array*
      fbo - A *FBO Array*
   
   Runway Array:
      
      id - Id of the longest runway on the airport.
      length - Length of the longest runway on the airport in feet.
      
   FBO Array:
      
      compid - FBO Id in the database
      comp - FBO Name
      servlevel - FBO subscription level. Plat: 1, Gold: 2, Silver: 3, Basic: 4
      complogo - File name of FBO's logo. Image can be found in the following folder: http://www.globalair.com/airport/images/complogo/sm/
      facpic - An array of image file names that the FBO has uploaded
      unicom - FBO Unicom id
      arinc - FBO arinc id
      manager - FBO manager's name
      phone - FBO phone number
      fax - FBO fax number
      truck - FBO *Truck Array*
      ss - FBO *Self Service Array*
   
   Truck Array:
      jeta - Price of Jeta fuel
      ll - Price of 100LL fuel
      eight - Price of 8087 fuel
      mogas - Price of mogas
      add - Price of Jeta Prist
   
   Self Service Array:
      jeta - Price of Jeta fuel
      ll - Price of 100LL fuel
      eight - Price of 8087 fuel
      mogas - Price of mogas
      add - Price of Jeta Prist
*/
function openInfoWindow(gmrker, AptIndex, airportCode) {

    if(openedInfoWindow != AptIndex) {
	    var bubble;
	    var plat_found = 0;

	    if(Airports[AptIndex].fbo[0] == undefined) {
	        var basicVars = {AptCode: Airports[AptIndex].apt, Approach: Airports[AptIndex].approach, AptName: Airports[AptIndex].fac, RunwayLength: Airports[AptIndex].rwy.length, StopNum: 0, FuelBrandLogos: Airports[AptIndex].fuelbrandlogos, ExtraLinksLeft: '', ExtraLinksRight: ''};
	        bubble = basicFBOTemplate.evaluate(basicVars);
	        gmrker.openInfoWindowHtml(bubble);
	    } else {
	        for(i = 0; i < Airports[AptIndex].fbo.length; i++) {
	            if(Airports[AptIndex].fbo[i].servlevel == 1) {
	                if(Airports[AptIndex].fbo[i].truck.jeta != 0) {
	                    var temp = {FuelName: 'JETA', FuelPrice: Airports[AptIndex].fbo[i].truck.jeta};
		                var JETA = fuelFBOTemplate.evaluate(temp);
			        } else {
				        var JETA = '';
			        }
			        if(Airports[AptIndex].fbo[i].ss.jeta != 0) {
				        var temp = {FuelName: 'SS JETA', FuelPrice: Airports[AptIndex].fbo[i].ss.jeta};
		                var SSJETA = fuelFBOTemplate.evaluate(temp);
	                } else {
			            var SSJETA = '';
			        }
	                if(Airports[AptIndex].fbo[i].truck.ll != 0) {
	                    var temp = {FuelName: '100LL', FuelPrice: Airports[AptIndex].fbo[i].truck.ll};
		                var Fuel100LL = fuelFBOTemplate.evaluate(temp);
	                } else {
			            var Fuel100LL = '';
			        }
			        if(Airports[AptIndex].fbo[i].ss.ll != 0) {
			            var temp = {FuelName: 'SS 100LL', FuelPrice: Airports[AptIndex].fbo[i].ss.ll};
		                var SS100LL = fuelFBOTemplate.evaluate(temp);
	                } else {
			            var SS100LL = '';
			        }
			        if(Airports[AptIndex].fbo[i].truck.add != 0) {
			            var temp = {FuelName: 'JETA+', FuelPrice: Airports[AptIndex].fbo[i].truck.add};
		                var JETAPRIST = fuelFBOTemplate.evaluate(temp);
		            } else {
		            	var JETAPRIST = '';
		            }
		            if(Airports[AptIndex].fbo[i].ss.add != 0) {
			            var temp = {FuelName: 'SS JETA+', FuelPrice: Airports[AptIndex].fbo[i].ss.add};
		                var SSJETAPRIST = fuelFBOTemplate.evaluate(temp);
		            } else {
		            	var SSJETAPRIST = '';
		            }
		            if(Airports[AptIndex].fbo[i].ss.eight != 0) {
			            var temp = {FuelName: '80/87', FuelPrice: Airports[AptIndex].fbo[i].ss.eight};
		                var Fuel8087 = fuelFBOTemplate.evaluate(temp);
		            } else {
		            	var Fuel8087 = '';
		            }
		            if(Airports[AptIndex].fbo[i].ss.mogas != 0) {
			            var temp = {FuelName: 'Mogas', FuelPrice: Airports[AptIndex].fbo[i].ss.mogas};
		                var Mogas = fuelFBOTemplate.evaluate(temp);
		            } else {
		            	var Mogas = '';
		            }
		            
		            var platLogo = "";
		            if (Airports[AptIndex].fbo[i].complogo == null || Airports[AptIndex].fbo[i].complogo == "")
		  {
		  platLogo = "http://www.globalair.com/airport/images/fbo_no_logo.gif";
		  }
		  else
		  {
		  platLogo = "http://www.globalair.com/airport/images/complogo/sm/" + Airports[AptIndex].fbo[i].complogo;
		  }
          
		            var Fuel = Fuel100LL + SS100LL + JETA + SSJETA + '<div style="clear:left;"></div>' + JETAPRIST + SSJETAPRIST + Fuel8087 + Mogas;
			        var platVars = {AptCode: Airports[AptIndex].apt, Approach: Airports[AptIndex].approach, AptName: Airports[AptIndex].fac, RunwayLength: Airports[AptIndex].rwy.length, StopNum: 0, FBOName: Airports[AptIndex].fbo[i].comp, FBOPhone: Airports[AptIndex].fbo[i].phone, FBOLogo: platLogo, FBOArinc: Airports[AptIndex].fbo[i].arinc, FuelContainer: Fuel, FuelBrandLogos: Airports[AptIndex].fuelbrandlogos, ExtraLinksLeft: '', ExtraLinksRight: '<a href=\"http://www.globalair.com/airport/airport.aspx?AptCode=' + Airports[AptIndex].apt + '#fuel\" target=\"_blank\">Fuel Prices</a>'};
	                bubble = platFBOTemplate.evaluate(platVars);
	                gmrker.openInfoWindowHtml(bubble);
	                plat_found++;
	                break;
	            }
	        }
	        if(plat_found == 0) {
	        
	            var basicVars = {AptCode: Airports[AptIndex].apt, Approach: Airports[AptIndex].approach, AptName: Airports[AptIndex].fac, RunwayLength: Airports[AptIndex].rwy.length, StopNum: 0, FuelBrandLogos: Airports[AptIndex].fuelbrandlogos, ExtraLinksLeft: '', ExtraLinksRight: '<a href=\"http://www.globalair.com/airport/airport.aspx?AptCode=' + Airports[AptIndex].apt + '#fuel\" target=\"_blank\" style=\"padding-left: 5px;\">Fuel Prices</a>'};
	            bubble = basicFBOTemplate.evaluate(basicVars);
	            gmrker.openInfoWindowHtml(bubble);
	        }
	    }
	    openedInfoWindow = AptIndex;
	}
}

/*
   Function: drawCircle

   Function draws a circle around the given point at the given radius.

   Parameters:

      point - GLatLng object provided by Google Event Listener. Access variables as such: point.lat() point.lng()
      radius - Radius int
   
   Returns:
   
      Null
*/
function drawCircle(point, radius) {
    var cColor = "#3366ff";
    var cWidth = 5;
    var Cradius = radius;
    var d2r = Math.PI / 180;
    var r2d = 180 / Math.PI;
    var Clat = (Cradius / 3444) * r2d;  // Earth's radius in Nautical miles 
    var Clng = Clat / Math.cos(point.lat() * d2r);
    var Cpoints = [];
    for (var i = 0; i < 33; i++) {
        var theta = Math.PI * (i / 16);
        var CPlng = point.lng() + (Clng * Math.cos(theta));
        var CPlat = point.lat() + (Clat * Math.sin(theta));
        var P = new GLatLng(CPlat, CPlng);
        newBounds.extend(P);
        Cpoints.push(P);
    }
    Map.addOverlay(new GPolyline(Cpoints, cColor, cWidth));
}
/*
   Function: Init_Map

   Function initializes the Map object and sets different map settings. It also initializes the icons.
   It then performs a check to see if the url is passing any variables from the main ARC page and calls the appropriate functions
   depending on what information was passed, specificly if an airport code or a city and state were passed. If a city and
   state are passed, the function then calls Google's geocoder to find the latitude and longitude of the given city and state.

   Parameters:

      Null
   
   Returns:
   
      Null
      
   See:
      <GetAptsAroundApt> | <GetAptInRadius>
*/
function Init_Map() {
    Map = new GMap2(document.getElementById('map'));
    Map.setCenter(new GLatLng(37.71859, -98.701172), 4);
    
    Map.addControl(new GSmallMapControl());
    Map.addControl(new GMapTypeControl());
    Map.enableContinuousZoom();
    //Map.enableScrollWheelZoom();
    Map.disableDoubleClickZoom();
    Map.enableGoogleBar();
    
    GEvent.addListener(Map, "dblclick", DoubleClick);
    
    noFuelIconLabeled = new GIcon();
    noFuelIconLabeled.image = './images/fuelmap/no_fuel_png.png';
    noFuelIconLabeled.transparent = './images/fuelmap/transparent.png';
    noFuelIconLabeled.iconSize = new GSize(45, 13);
    noFuelIconLabeled.iconAnchor = new GPoint(20, 12);
    noFuelIconLabeled.infoWindowAnchor = new GPoint(25, 7);

    FuelIconLabeled = new GIcon();
    FuelIconLabeled.image = './images/fuelmap/has_fuel_png.png';
    FuelIconLabeled.transparent = './images/fuelmap/transparent.png';
    FuelIconLabeled.iconSize = new GSize(45, 13);
    FuelIconLabeled.iconAnchor = new GPoint(20, 12);
    FuelIconLabeled.infoWindowAnchor = new GPoint(25, 7);
       
    var url_vars = getUrlVars();
    if(url_vars.aptcode != null && url_vars.aptcode != '') {
        var priv;
        var pub;
        var factype;
        var range;
        var runwayLength;
        var aptApproach;
        var fuelJETA;
        var fuel100LL;
        var fuelMOGAS;
        var fuel8087;
        var fuelBrand;
        if(url_vars.av == 'all') {
            priv = true;
            pub = true;
        } else if(url_vars.av == 'pu') {
            priv = false;
            pub = true;           
        } else if(url_vars.av == 'pr') {
            priv = true;
            pub = false;
        } else {
            priv = false;
            pub = true;
        }
	if(url_vars.rad == null) {
		range = 50;
	} else {
		range = getUrlRad(url_vars.rad);
	}
        if(url_vars.FacType == null) {
            factype = 'Airport';
        } else {
            factype = url_vars.FacType;
        }
        if (url_vars.rwy == null)
        {
        runwayLength = 'Any';
        }
        if(url_vars.rwy == 'lt5000')
        {
        runwayLength = 'lt5000';
        }
        else if(url_vars.rwy == 'gt5000')
        {
        runwayLength = 'gt5000';
        }
        else
        {
        runwayLength = 'Any';
        }
        if (url_vars.app == null)
        {
        aptApproach = "";
        }
        if(url_vars.app == 'ils')
        {
        aptApproach = 'ils';
        }
        else if(url_vars.app == 'instrument')
        {
        aptApproach = 'instrument';
        }
        else
        {
        aptApproach = '';
        }
        if (url_vars.fueljeta == 'true')
        {
        fuelJETA = 'true';
        }
        else
        {
        fuelJETA = 'false';
        }
        if (url_vars.fuel100ll == 'true')
        {
        fuel100LL = 'true';
        }
        else
        {
        fuel100LL = 'false';
        }
        if (url_vars.fuelmogas == 'true')
        {
        fuelMOGAS = 'true';
        }
        else
        {
        fuelMOGAS = 'false';
        }
        if (url_vars.fuel8087 == 'true')
        {
        fuel8087 = 'true';
        }
        else
        {
        fuel8087 = 'false';
        }
        if (url_vars.fuelbrand == 'AirBP')
        {
        fuelBrand = 'AirBP';
        }
        else if(url_vars.fuelbrand == 'AvFuel')
        {
        fuelBrand = 'AvFuel';
        }
        else if(url_vars.fuelbrand == 'Chevron')
        {
        fuelBrand = 'Chevron';
        }
        else if(url_vars.fuelbrand == 'ExxonMobil')
        {
        fuelBrand = 'ExxonMobil';
        }
        else if(url_vars.fuelbrand == 'Phillips66')
        {
        fuelBrand = 'Phillips66';
        }
        else if(url_vars.fuelbrand == 'Shell')
        {
        fuelBrand = 'Shell';
        }
        else if(url_vars.fuelbrand == 'Texaco')
        {
        fuelBrand = 'Texaco';
        }
        else if(url_vars.fuelbrand == 'Independent')
        {
        fuelBrand = 'Independent';
        }
        else
        {
        fuelBrand = '';
        }
        GetAptsAroundApt(url_vars.aptcode, range, priv, pub, factype, runwayLength, aptApproach, fuelJETA, fuel100LL, fuelMOGAS, fuel8087, fuelBrand);
    }
    if(url_vars.city != null && url_vars.state != null) {
        geocoder = new GClientGeocoder();
        geocoder.getLatLng(url_vars.city + ', ' + url_vars.state, function(point) {
            var priv;
            var pub;
            var factype;
            var range;
            var runwayLength;
            var aptApproach;
            var fuelJETA;
            var fuel100LL;
            var fuelMOGAS;
            var fuel8087;
            var fuelBrand;
            if (url_vars.nearest != null && url_vars.nearest != '')
            {
            }
            if(url_vars.av == 'all') {
                priv = true;
                pub = true;
            } else if(url_vars.av == 'pu') {
                priv = false;
                pub = true;
            } else if(url_vars.av == 'pr') {
                priv = true;
                pub = false;
            } else {
                priv = false;
                pub = false;
            }
            range = getUrlRad(url_vars.rad);
            if(url_vars.FacType == null) {
                factype = 'Airport';
            } else {
                factype = url_vars.FacType;
            }
        if (url_vars.FacType == 'airport')
        {
        }
        else if(url_vars.FacType == 'balloonport')
        {
        }
        else if(url_vars.FacType == 'heliport')
        {
        }
        else if(url_vars.FacType == 'seaplane')
        {
        }
        else if(url_vars.FacType == 'stolport')
        {
        }
        else
        {
        }
        if (url_vars.rwy == null)
        {
        runwayLength = 'Any';
        }
        if(url_vars.rwy == 'lt5000')
        {
        runwayLength = 'lt5000';
        }
        else if(url_vars.rwy == 'gt5000')
        {
        runwayLength = 'gt5000';
        }
        else
        {
        runwayLength = 'Any';
        }
        if (url_vars.app == null)
        {
        aptApproach = 'all';
        }
        if(url_vars.app == 'ils')
        {
        aptApproach = 'ils';
        }
        else if(url_vars.app == 'instrument')
        {
        aptApproach = 'instrument';
        }
        else
        {
        aptApproach = 'all';
        }
        if (url_vars.fueljeta == 'true')
        {
        fuelJETA = 'true';
        }
        else
        {
        fuelJETA = 'false';
        }
        if (url_vars.fuel100ll == 'true')
        {
        fuel100LL = 'true';
        }
        else
        {
        fuel100LL = 'false';
        }
        if (url_vars.fuelmogas == 'true')
        {
        fuelMOGAS = 'true';
        }
        else
        {
        fuelMOGAS = 'false';
        }
        if (url_vars.fuel8087 == 'true')
        {
        fuel8087 = 'true';
        }
        else
        {
        fuel8087 = 'false';
        }
        if (url_vars.fuelbrand == 'AirBP')
        {
        fuelBrand = 'AirBP';
        }
        else if(url_vars.fuelbrand == 'AvFuel')
        {
        fuelBrand = 'AvFuel';
        }
        else if(url_vars.fuelbrand == 'Chevron')
        {
        fuelBrand = 'Chevron';
        }
        else if(url_vars.fuelbrand == 'ExxonMobil')
        {
        fuelBrand = 'ExxonMobil';
        }
        else if(url_vars.fuelbrand == 'Phillips66')
        {
        fuelBrand = 'Phillips66';
        }
        else if(url_vars.fuelbrand == 'Shell')
        {
        fuelBrand = 'Shell';
        }
        else if(url_vars.fuelbrand == 'Texaco')
        {
        fuelBrand = 'Texaco';
        }
        else if(url_vars.fuelbrand == 'Independent')
        {
        fuelBrand = 'Independent';
        }
        else
        {
        fuelBrand = '';
        }
            GetAptInRadius(point.lat(), point.lng(), range, priv, pub, factype, runwayLength, aptApproach, fuelJETA, fuel100LL, fuelMOGAS, fuel8087, fuelBrand);
        });
    }
    $('FuelAptList').insert('<br />' + ad);
}
/*
   Function: banmanPro

   Function initializes Banman Pro and creates the appropriate html string depending on browser.
   
   Parameters:

      Null
   
   Returns:
   
      ad - HTML string containing an iframe for advertisements
*/

function banmanPro() {
    //start Banman Code
	var ad = "";
	var browName = navigator.appName;
	var browDateTime = (new Date()).getTime();
	var browVersion = parseInt(navigator.appVersion);
	var ua=navigator.userAgent.toLowerCase();
	var adcode='';
	if (browName=='Netscape')
	{
		if (browVersion>=5) 
		{
			ad = '<iframe src="http://www.globalair.com/banmanpro/banman.asp?ZoneID=34&Task=Get&Browser=NETSCAPE6&X=' + browDateTime + '" width=241 height=263 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No></iframe>';
		}
		else if((browVersion>=4)&&(ua.indexOf("mac")==-1))
		{
			ad = '<SCRIPT src="http://www.globalair.com/banmanpro/banman.asp?ZoneID=34&Task=Get&Browser=NETSCAPE4"></script>';
		}
		else if(browVersion>=3) 
		{
			ad = '<A HREF="http://www.globalair.com/banmanpro/banman.asp?ZoneID=34&Task=Click&Mode=HTML&PageID=' + browDateTime + '&RandomNumber=' + browDateTime + '" target="_new"><IMG SRC="http://www.globalair.com/banmanpro/banman.asp?ZoneID=34&Task=Get&Mode=HTML&PageID=' + browDateTime + '&RandomNumber=' + browDateTime + '" width="241" height="263" border="0"></A>';
		}
	}
	if(browName=='Microsoft Internet Explorer')
	{
		ad = '<iframe src="http://www.globalair.com/banmanpro/banman.asp?ZoneID=34&Task=Get&X=' + browDateTime + '" width=241 height=263 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No></iframe>';
	}
	//end Banman code
	return ad;
}
/*
   Function: getUrlRad

   Function sets the radius in the dropdown to match the radius passed through the url. This is critical for the 
   draw circle function to work correctly as it pulls its radius from the drop down menu at the time of drawing the circle.
   
   Parameters:

      rad - Radius int
   
   Returns:
   
      range - Radius int
*/
function getUrlRad(rad) {
    if(rad == null) {
        range = 10;
    } else {
        var distance_index;
        switch(parseInt(rad)) {
            case 10:
                distance_index = 0;
                break;
            case 20:
                distance_index = 1;
                break;
            case 30:
                distance_index = 2;
                break;
            case 40:
                distance_index = 3;
                break;
            case 50:
                distance_index = 4;
                break;
            case 60:
                distance_index = 5;
                break;
            case 70:
                distance_index = 6;
                break;
            case 80:
                distance_index = 7;
                break;
            case 90:
                distance_index = 8;
                break;
            case 100:
                distance_index = 9;
                break;
        }
        range = rad;
    }
    return range;
}
/*
   Function: getUrlVars

   Function pulls the get variables from the url and sorts them into an associative array.
   
   Parameters:

      Null
   
   Returns:
   
      vars - An associative array of variables passed through the url.
*/
function getUrlVars() {
    var vars = [], hash;
    var variableValue;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    
    for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        
        variableValue = hash[1];
        variableValue = variableValue.replace("%20"," ");
        
        vars[hash[0]] = variableValue;
    }
    return vars;
}
function showSearchingIcon(message) {
    searchingIcon = true;
    Modalbox.show(getSearchingIconBox(message), { title: 'GlobalAir.com', width: 300, overlayClose: false, slideDownDuration: 0, slideUpDuration: 0, resizeDuration: 0 });
    
}
function hideSearchingIcon() {
    if(searchingIcon) {
        Modalbox.hide();
        searchingIcon = false;
    }
}
function getSearchingIconBox(message) {
    return "<div align='center'><img src='/airport/images/loading.gif' width='100' height='100' /><br /><br /><b>" + message + "</b></div>";
}