// Google map
var gMap; // TODO

Import('http://maps.google.com/maps?file=api&v=2&hl=' + GOOGLE_API_HL +'&key='+
       GOOGLE_API_KEY);

function mapInit() {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GMapTypeControl());
        map.addControl(new GSmallMapControl());
        gMap = map;

        mapDefaultView();
    } 
}

function mapDefaultView() {
	var result = localeValue.split('_');
	
	if ( (result[1] != 'us') && (result[1] != 'ca') ) {
		Lat = 53.583333;
		Lng = 23.100000;
	}
	
	gMap.setCenter(new GLatLng(Lat, Lng),
                      DEFAULT_ZOOM_LEVEL);
}


function isPointVisible(latlng) {
    var bounds = gMap.getBounds();
    if (bounds.contains(latlng))
        return true;
    return false;

}

function FlightMapController(flightController, lat1, long1, lat2, long2) {
    /* Data */
    this.controller = flightController;
//    this.points = new Array;
//    this.points.push(new GLatLng(lat1, long1));
//    this.points.push(new GLatLng(lat2, long2));
    var points = [];
    points.push(new GLatLng(lat1, long1));
    points.push(new GLatLng(lat2, long2));

    points.sort(function(p1, p2) {
        return p1.lng() - p2.lng();
    });

    this.points = points;
    this.displayed = false;

    this.polyline  = new GPolyline(points);
    this.srcMarker = new GMarker(new GLatLng(lat1, long1));
    this.dstMarker = new GMarker(new GLatLng(lat2, long2));

    GEvent.addListener(this.srcMarker, 'click', this.showSrcInfo.bind(this));
    GEvent.addListener(this.dstMarker, 'click', this.showDstInfo.bind(this));

    this.centerPoint = new GLatLng((parseInt(lat1) + parseInt(lat2))/2,
                                   (parseInt(long1) + parseInt(long2))/2);
        
//    this.center();
}

FlightMapController.prototype.getHtmlInfo = function() {
    var fc = this.controller;
    var makeLabel = function(label) {return "<div class='mapinfo_label'>"+label+"</div>";}
    var makeData = function(data) {return "<div class='mapinfo_data'>"+data+"</div>";}
	var makeCarbon = function(carbon) {
		var carb = formatNumber(carbon) + ' lbs';
		if (carbonUnits == CARBON_MTS) {
			carb = formatNumber((Math.round(carbon * 10000 / MTS_LBS)/10000), true) + ' mTs';
		} else if(carbonUnits == CARBON_KGS) {
			carb = formatNumber((Math.round(carbon * 10000000 / KGS_LBS)/10000000)) + ' kg';
		}
		return carb;
	}
	
	var makeDistance = function(distance) {
		var dist = formatNumber(distance) + ' miles';
		if (distanceUnits == DISTANCE_KMS) {
			dist = formatNumber((distance * KMS_MIS)) + ' km';
		}
		return dist;
	}

    var html = "<div class='mapinfo'>" + 
    formatName(fc.departureCityName);
	
	if (fc.rndtrip == 1) {
    	html += ' <span class="arrow">&rarr;</span> ';
	} else {
		html += ' <span class="arrow">&harr;</span> ';
	}
    html += formatName(fc.arrivalCityName) + '<br/>' + 
    makeLabel(MAPINFO_LABEL_DIST) + 
    makeData(makeDistance(fc.distance())) + '<br/>' + 
    makeLabel(MAPINFO_LABEL_CO2) + 
    makeData(makeCarbon(fc.get_carbon())) + '<br/>' +
    "</div>";
    
    return html;
};

FlightMapController.prototype.showSrcInfo = function() {
    var html = this.getHtmlInfo();
    this.srcMarker.openInfoWindowHtml(html);
};

FlightMapController.prototype.showDstInfo = function() {
    var html = this.getHtmlInfo();
    this.dstMarker.openInfoWindowHtml(html);
};

FlightMapController.prototype.update = function() {
    this.refresh();
};

FlightMapController.prototype.refresh = function() {
    if (!this.displayed) {
        gMap.addOverlay(this.srcMarker);
        gMap.addOverlay(this.dstMarker);
        
        try {
            gMap.addOverlay(this.polyline);
        } catch(e) {
           alert('2: '+e.message);
        }

        this.displayed = true;
    }
};

FlightMapController.prototype.remove = function() {
    if (this.displayed) {
        try {
        gMap.removeOverlay(this.polyline);
        } catch(e) {
        }

        gMap.removeOverlay(this.srcMarker);
        gMap.removeOverlay(this.dstMarker);

        this.displayed = false;
    }
};

FlightMapController.prototype.center = function() {
    try {
    if (!isPointVisible(this.points[0]) ||
        !isPointVisible(this.points[1])) {
        gMap.setCenter(this.centerPoint, DEFAULT_ZOOM_LEVEL);
    }

    if (!isPointVisible(this.points[0]) ||
        !isPointVisible(this.points[1])) {
        gMap.zoomOut();
    }

    if (!isPointVisible(this.points[0]) ||
        !isPointVisible(this.points[1])) {
        gMap.zoomOut();
    }
    } catch(e) {
//        alert('center: '+e.message);
    }
};
