

function FapMaps() {
    this.map = null;
    this.map_initialized = false;
    this.flash_support = true;
    this.map_used = false;
    this.earth_radius = 3963.191;
    this.panoClient;
    this.myPano;
    this.proplatlng;
    this.myPOV;

};

FapMaps.prototype.showMap = function() {
    var street_obj = $('#streetview');
    if (street_obj) {
        street_obj.hide();
        var map = $('#map');
        map.show();
    }
};

FapMaps.prototype.showStreetView = function() {
    var streetview = $('#streetview');
    if (streetview) {
        //TODO:  this.showStreetViewTab();
        $('#map').hide();
        streetview.show();
    }
};

FapMaps.prototype.setMapType = function(map_type, showTypeView) {
    if (typeof (showTypeView) == 'undefined') showTypeView = false;
    if (typeof (map_type) == "undefined" || map_type == '') {
        var map_type = "G_MAP_TYPE";
    }
    $('#mapViewControl li').removeClass("selected");
    switch (map_type) {
        case "G_MAP_TYPE":
        case "G_NORMAL_MAP":
            this.map.setMapType(G_NORMAL_MAP);
            $("#G_MAP_TYPE").addClass("selected");
            break;
        case "G_SATELLITE_TYPE":
            this.map.setMapType(G_SATELLITE_MAP);
            $("#G_SATELLITE_TYPE").addClass("selected");
            break;
        case "G_HYBRID_TYPE":
            this.map.setMapType(G_HYBRID_MAP);
            $("#G_HYBRID_TYPE").addClass("selected");
            break;
        case "G_STREET_TYPE":
            $("#G_STREET_TYPE").addClass("selected");
            break;
        case "G_TERRAIN_TYPE":
            $('#G_TERRAIN_TYPE').addClass("selected");
            this.map.setMapType(G_PHYSICAL_MAP);
            break;

    }

    this.map_type = map_type;

    if (showTypeView) {
        if (map_type == "G_STREET_TYPE") {
            this.showStreetView();
        }
        else {
            this.showMap();
        }
    }
};

FapMaps.prototype.initMapWithStreetView = function() {
    var map_el = $('#map');

 //   if (map_el && (map_el.css('display') == 'none')) return;
    if (!GBrowserIsCompatible()) {
        $('#map').html('<p><br /><br /><center>Sorry, your browser is not<br />compatible with our maps.</p><p>Visit <a href="http://maps.google.com" target="_blank">Google Maps</a> to view a<br />list of compatible browsers.</center></p>');
        return;
    }

    this.map = new GMap2($('#map')[0]);
    if (typeof (map_control_type) != 'undefined' && map_control_type == 'small') {
        this.map.addControl(new GSmallMapControl());
    } else {

        this.map.addControl(new GLargeMapControl());
    }



    //map.addControl(new GMapTypeControl());
    var point = new GLatLng(_CENLAT, _CENLONG);
    this.map.setCenter(point, 15);
    var marker = new GMarker(point);
    this.map.addOverlay(marker);

    var map_type = 'G_NORMAL_MAP';
    if (typeof (_default_map_view) == 'undefined') {
        if (map_type == '' || map_type == null) {
            map_type = 'G_NORMAL_MAP';
        }
    }
    else {
        map_type = _default_map_view;
    }

    this.initStreetView();
    this.setMapType(map_type, true);

    this.map_initialized = true;
};

FapMaps.prototype.addPanoramio = function() { if (this.map != null) this.map.addOverlay(new GLayer("com.panoramio.all")); }
FapMaps.prototype.addWiki = function() {
    if (this.map != null) this.map.addOverlay(new GLayer("org.wikipedia.en"));
}
FapMaps.prototype.initMap = function() {
    var map_el = $('#map');

   // if (map_el && (map_el.css('display') == 'none')) return;
    if (!GBrowserIsCompatible()) {
        $('#map').html('<p><br /><br /><center>Sorry, your browser is not<br />compatible with our maps.</p><p>Visit <a href="http://maps.google.com" target="_blank">Google Maps</a> to view a<br />list of compatible browsers.</center></p>');
        return;
    }

    this.map = new GMap2($('#map')[0]);
    if (typeof (map_control_type) != 'undefined' && map_control_type == 'small') {
        this.map.addControl(new GSmallMapControl());
    } else {

        this.map.addControl(new GLargeMapControl());
    }
    //map.addControl(new GMapTypeControl());
    var point = new GLatLng(_CENLAT, _CENLONG);
    this.map.setCenter(point, 15);
    var marker = new GMarker(point);
    this.map.addOverlay(marker);

    var map_type = 'G_NORMAL_MAP';
    if (typeof (_default_map_view) == 'undefined') {
        if (map_type == '' || map_type == null) {
            map_type = 'G_NORMAL_MAP';
        }
    }
    else {
        map_type = _default_map_view;
    }
    this.setMapType(map_type, true);

    this.map_initialized = true;

}



FapMaps.prototype.initStreetView = function() {

    if (typeof (_LOCATION_UNKNOWN) != 'undefined' && _LOCATION_UNKNOWN == 0) {

        if ($('#streetview') && typeof (_CENLONG) != 'undefined' && typeof (_CENLAT) != 'undefined') {
            //$('#G_STREET_TYPE').show();

            this.proplatlng = new GLatLng(_CENLAT, _CENLONG);
            this.panoClient = new GStreetviewClient();
            this.myPano = new GStreetviewPanorama($('#streetview')[0], { features: { userPhotos: false }, enableFullScreen: false });
            GEvent.addListener(this.myPano, "error", this.handleNoFlash);

            this.panoClient.getNearestPanorama(this.proplatlng, this.showPanoData);
         
        }
    }
};

FapMaps.prototype.computeAngle = function(T, O) {
    var R = Math.PI / 180;
    var J = 180 / Math.PI;
    var K = O.lat() - T.lat();
    var M = O.lng() - T.lng();
    var yaw = Math.atan2(M * Math.cos(O.lat() * R), K) * J;

    return this.wrapAngle(yaw);
};

FapMaps.prototype.showPanoData = function(panoData) {
    if (panoData.code != 200) {
        $('#G_STREET_TYPE').hide();
    }
    else {
        $('#G_STREET_TYPE').show();
        var lat = 0;
       
        if (typeof (panoData.Location.lat) != 'undefined') latv = panoData.Location.lat;
        if (typeof (panoData.Location.lng) != 'undefined') lngv = panoData.Location.lng;
        var vanlatlng = new GLatLng(latv, lngv);
        var yaw;
        if (typeof (_YAW) != 'undefined') {
            yaw = parseInt(_YAW);
            if (isNaN(yaw)) yaw = 0;
        }
        else {
            yaw = FMAP.computeAngle(vanlatlng, FMAP.proplatlng);
        }



        FMAP.myPOV = { 'yaw': yaw, 'pitch': -10 };
        FMAP.myPano.setLocationAndPOV(FMAP.proplatlng, FMAP.myPOV);
        
        if (typeof (_default_map_view) != 'undefined' && _default_map_view != 'G_STREET_TYPE') {

            this.setMapType(_default_map_view, true);

        }
        else {
            if (this.flash_support) {
                this.setMapType('G_STREET_TYPE', true);

            }
        }
    }

};

FapMaps.prototype.wrapAngle = function(B) {
    if (B >= 360) B -= 360;
    else if (B < 0) B += 360;
    return B;
};

FapMaps.prototype.cleanUpMarkers = function() {

    GUnload();

};

FapMaps.prototype.handleNoFlash = function(errorCode) {
    if (errorCode == 603) {

        FMAP.flash_support = false;
        $('#G_STREET_TYPE').hide();
        this.setMapType('G_NORMAL_MAP', true);
        return;
    }
};

  
    
    
