var centerLatitude = 34.900000;
var centerLongitude = -65.900000;
var startZoom = 2;
var markerArray = new Array();
var lineArray = new Array();
var pointArray = new Array();;
var hub;
var map;

function ToolTip(marker,html,width) {
	this.html_ = html;
	this.width_ = (width ? width + 'px' : 'auto');
	this.marker_ = marker;
}

ToolTip.prototype = new GOverlay();

ToolTip.prototype.initialize = function(map) {
	var div = document.createElement("div");
	div.style.display = 'none';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);

	this.map_ = map;
	this.container_ = div;
}

ToolTip.prototype.remove = function() {
	this.container_.parentNode.removeChild(this.container_);
}

ToolTip.prototype.copy = function() {
	return new ToolTip(this.html_);
}

ToolTip.prototype.redraw = function(force) {
	if (!force) return;

	var pixelLocation = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	this.container_.innerHTML = this.html_;
	this.container_.style.position = 'absolute';
	this.container_.style.left = pixelLocation.x+10 + "px";
	this.container_.style.top = pixelLocation.y-10 + "px";
	this.container_.style.width = this.width_;
	this.container_.style.font = '9px verdana, arial, sans';
	this.container_.style.border = '1px solid black';
	this.container_.style.background = 'white';
	this.container_.style.padding = '2px';


	this.container_.style.whiteSpace = 'nowrap';
	if(this.width_ != 'auto') this.container_.style.overflow = 'hidden';
	this.container_.style.display = 'block';
}

GMarker.prototype.ToolTipInstance = null;

GMarker.prototype.openToolTip = function(content) {
	if(this.ToolTipInstance == null) {
		this.ToolTipInstance = new ToolTip(this,content)
		map.addOverlay(this.ToolTipInstance);
	}
}

GMarker.prototype.closeToolTip = function() {
	if(this.ToolTipInstance != null) {
		map.removeOverlay(this.ToolTipInstance);
		this.ToolTipInstance = null;
	}
}

function init() {

    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(centerLatitude,centerLongitude), startZoom);
    map.addControl(new GLargeMapControl());

    map.addMapType(G_PHYSICAL_MAP);
    var mapControl = new GHierarchicalMapTypeControl();
    mapControl.clearRelationships();
    mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, null, false);
    map.addControl(mapControl);
    map.setMapType(G_PHYSICAL_MAP);
}

function onDisplayRoutes() {

    removeMarkers(map, markerArray);
    removeMarkers(map, lineArray);
    retrieveRoutes();
}

function fitMap(map) {

   var bounds = new GLatLngBounds();
   for (var i=0; i< pointArray.length; i++) {
      bounds.extend(pointArray[i]);
   }
   map.setZoom(map.getBoundsZoomLevel(bounds));
   map.setCenter(bounds.getCenter());
}

function removeMarkers(map, array) {

    if (isNaN(array.length)) {
        map.removeOverlay(array);
    }
    else if (array.length > 0) {
        for (var x = 0; x < array.length; x++) {
            map.removeOverlay(array[x]);
        }
    }
    return true;
}

function createMarkers(depApt, destApt, depAptLat, depAptLon, destAptLat, destAptLon) {

    var mArray = new Array();
    var depMarker = createMarker(depApt, depAptLat, depAptLon);
    mArray.push(depMarker);
    var destMarker = createMarker(destApt, destAptLat, destAptLon);
    mArray.push(destMarker);
    markerArray.push(depMarker);
    markerArray.push(destMarker);
    return mArray;
}

function createMarker(apt, aptLat, aptLon) {

    var markerApt;
    var latlng = new GLatLng(parseFloat(aptLat),parseFloat(aptLon));
    pointArray.push(latlng);
    var baseIcon = new GIcon();
    baseIcon.iconSize = new GSize(20, 20);
    baseIcon.iconAnchor = new GPoint(10, 10);
    baseIcon.infoWindowAnchor = new GPoint(10, 10);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);
    var icon = new GIcon(baseIcon);

    if (apt == hub) {
        icon.image = 'hub.png';
    }
    else {
        icon.image = 'plane2.png';
    }
    var iconOptions = { icon: icon };

    var markerApt = new GMarker(latlng, iconOptions);
    markerApt.lat = aptLat;
    markerApt.lon = aptLon;
    markerApt.id = apt;

	GEvent.addListener(markerApt,'mouseover',function() {
		markerApt.openToolTip(apt);
	});

	GEvent.addListener(markerApt,'mouseout',function() {
		markerApt.closeToolTip();
	});
    return markerApt;
}

function retrieveRoutes() {

    var request = GXmlHttp.create();

    hub = document.getElementById("hub").options[document.getElementById("hub").selectedIndex].value;
    var aircraft = document.getElementById("aircraft").options[document.getElementById("aircraft").selectedIndex].value;

    if (hub == "" || aircraft == "") {
        alert("Please select a Hub and Aircraft, then click 'Display Routes'");
        return;
    }

    request.open('GET', 'retrieveHubRoutes.php?hub=' + hub + '&aircraft=' + aircraft, true);
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            var xmlDoc = request.responseXML;
            var routes = xmlDoc.documentElement.getElementsByTagName("route");

            pointArray.length = 0;
            if (routes.length > 0) {
                for (var i = 0; i < routes.length; i++) {
                    var depApt = routes[i].getAttribute("depApt");
                    var destApt = routes[i].getAttribute("destApt");
                    var depAptLat = routes[i].getAttribute("depAptLat");
                    var depAptLon = routes[i].getAttribute("depAptLon");
                    var destAptLat = routes[i].getAttribute("destAptLat");
                    var destAptLon = routes[i].getAttribute("destAptLon");

                    var routeMarkers = createMarkers(depApt, destApt, depAptLat, depAptLon, destAptLat, destAptLon);
                    showRoute(routeMarkers);
                }
                fitMap(map);
            }
            else {
                map.setCenter(new GLatLng(centerLatitude,centerLongitude), startZoom);
                alert("No routes for this Hub-Aircraft pair.");
            }
        }
    }
    request.send(null);
}

function showRoute(routeMarkers) {

    var routeWaypoints = new Array();
    var markerDepApt = routeMarkers[0];
    var markerDestApt = routeMarkers[1];

    var p = new GLatLng(parseFloat(markerDepApt.lat), parseFloat(markerDepApt.lon));
    routeWaypoints.push(p);
    p = new GLatLng(parseFloat(markerDestApt.lat), parseFloat(markerDestApt.lon));
    routeWaypoints.push(p);
    var routeLine = new GPolyline(routeWaypoints, '#4080AF', 2, 0.7);
    lineArray.push(routeLine);
    map.addOverlay(markerDepApt);
    map.addOverlay(routeLine);
    map.addOverlay(markerDestApt);
}
