if (typeof RamenMap == 'undefined') RamenMap = {};

// setup
RamenMap.setup = function() {
    RamenMap.Property.Map.setup();
    RamenMap.Property.StreetView.setup();
}

// execute setup
jQuery(function() {
    if (RamenMap.setup) RamenMap.setup();
});

if (typeof RamenMap.Property == 'undefined') RamenMap.Property = {};

/**********************************************************
Google Map表示
**********************************************************/
RamenMap.Property.Map = (function () {
    var map = null;
    var pin = null;
    var currentLatLng = null;
    var currentYaw = null;
    var manicon = null;
    var fovMarker = null;
    
    return {
        setup: function() {
            if (!GBrowserIsCompatible()) return;
            jQuery(window).unload(GUnload);

            lat = parseInt(property_lat) / 3600000.0;
            lng = parseInt(property_lng) / 3600000.0;

            var start = {
                latlng: new GLatLng(lat, lng)
            }
            currentLatLng = start.latlng;

            map = new GMap2(jQuery('#Gmap_'+property_size)[0]);
            map.disableInfoWindow();
            map.enableContinuousZoom();

            map.setCenter(start.latlng, property_zoom);
            if (property_size == 'S')
                map.addControl(new GSmallMapControl());
            else if (property_size == 'L')
                map.addControl(new GLargeMapControl());

            var icon = new GIcon();
            icon.image = '/img/common/icon_map.png';
            icon.iconSize = new GSize(37, 29);
            icon.shadow = '/img/common/icon_map_shadow.png';
            icon.shadowSize = new GSize(61, 34);
            icon.iconAnchor = new GPoint(13, 18);
            icon.ramen_icon = true;

            pin = new GMarker(start.latlng, icon);
            map.addOverlay(pin);
        },

        setStreetViewPin: function(latlng) {
            currentLatLng = latlng;
            manicon = new GIcon(G_DEFAULT_ICON);
            manicon.image = "http://maps.gstatic.com/mapfiles/cb/man_arrow-0.png";
            manicon.transparent = "http://maps.gstatic.com/mapfiles/cb/man-pick.png";
            manicon.iconSize = new GSize(49, 52);
            manicon.iconAnchor = new GPoint(25, 35);
            manicon.infoWindowAnchor = new GPoint(25, 25);
            manicon.imageMap = [
                26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
                16,20, 16,14, 19,13, 22,8
            ];
            
            RamenMap.Property.Map.placeFovMarker();         

            var timeout;
            GEvent.addListener(map, "click", function(overlay, point) {
                if (overlay && overlay.getIcon() && overlay.getIcon().ramen_icon) return;
                if (timeout) clearTimeout(timeout);
                timeout = setTimeout(function () {
                    map.setCenter(point);
                    RamenMap.Property.StreetView.move(point);
                    timeout = undefined;
                }, 500);
            });
//          svOverlay = new GStreetviewOverlay();
//          map.addOverlay(svOverlay);
            
            return
        },

        handleInitialized: function(location) {
            map.setCenter(location.latlng);
            currentLatLng = location.latlng;
            RamenMap.Property.Map.placeFovMarker();
            return;
        },

        handleYawChange: function(yaw) {
            RamenMap.Property.Map.placeFovMarker();
            return;
        },

        placeFovMarker: function() {
            if (fovMarker != undefined) {
                map.removeOverlay(fovMarker);
            }
            fovMarker = new GMarker(currentLatLng, {icon:manicon, clickable: false});
            map.addOverlay(fovMarker);
            if (jQuery("#panoflash2")) {
//              jQuery("#panoflash1").hide();
            }

            return;
        }
    }
})();

RamenMap.Property.StreetView = (function() {
    var start = null;
    var streetView = null;
    
    return {
        setup: function() {

            lat = parseInt(property_lat) / 3600000.0;
            lng = parseInt(property_lng) / 3600000.0;

            start = {
                latlng: new GLatLng(lat, lng)
            }

            client = new GStreetviewClient();
            client.getNearestPanoramaLatLng(start.latlng, function(latlng){
                if (latlng == null) {
                    return RamenMap.Property.StreetView.onError(600);
                }

                var yaw = RamenMap.Property.StreetView.calcYaw(latlng, start.latlng);
                if (yaw < 0) {
                    yaw = 0;
                }
                var pov = {
                    yaw: yaw
                };

                var panoOpts = {features: {userPhotos: false}};
                streetView = new GStreetviewPanorama(document.getElementById("GmapStreet_"+property_size) , panoOpts);
                GEvent.addListener(streetView, "error", function (err) {
                    if (err !== GStreetviewPanorama.ErrorValues.FLASH_UNAVAILABLE) return;
                    jQuery('#GmapStreet_'+property_size).html('<div class="noflash"><p>Adobe Flash Playerがインストールされてないか、<br />古いバージョンをお使いです。<br />このコンテンツをご覧になるには<br />最新のAdobe Flash Playerが必要です。<br />下のアイコンのリンクより<br />ダウンロード・インストールしてください。</p><p><a href="http://get.adobe.com/jp/flashplayer/" target="_blank"><img src="/img/common/btn_getFlash.gif" width="112" height="33" alt="get flash player" /></a></p><!-- /.noFlash --></div>');
                });
                streetView.setLocationAndPOV(latlng, pov);
                        
                RamenMap.Property.Map.setStreetViewPin(latlng);
                GEvent.addListener(streetView, "initialized", RamenMap.Property.Map.handleInitialized);
                GEvent.addListener(streetView, "yawchanged", RamenMap.Property.Map.handleYawChange);
                GEvent.addListener(streetView, "error", RamenMap.Property.StreetView.handleNoView);

                return;
            });
        },
        
        calcYaw: function(fromLatLng, toLatLng) {
            var yaw = 90 - Math.atan2(
                toLatLng.lat() - fromLatLng.lat(),
                toLatLng.lng() - fromLatLng.lng()
            ) * 180 / Math.PI;
            if (yaw < 0) {
                yaw += 360;
            }
            return yaw;
        },
        
        handleNoView: function(error_code) {
            if (error_code == 600) {
                //jQuery("#GmapStreet_"+property_size).html("Googleストリートビュー未対応地域です。");
                return;
            }
        },
        
        onError: function(error_code) {
            if (error_code == 600 || error_code == 603) {
                //jQuery("#Gmap_"+property_size).height(jQuery("#googleService").height());
                jQuery("#Gmap_"+property_size).height(property_arht);
                jQuery("#GmapStreet_"+property_size).hide();
                RamenMap.Property.Map.setup(start.latlng.lat(), start.latlng.lng());
                return;
            }
        },
        move: function(latLng) {
            streetView.setLocationAndPOV(latLng);
        }
    }
})();

