var map01;
var mapView;
var geocoder;
var buildingLatlng;
var hasView = true;
var streetView;
var lastSearchBasicParameter;

var iconMan = new GIcon();
iconMan.image = "/img/icons/googlemap/man_arrow-0.png";
iconMan.iconSize = new GSize(49, 52);
//iconMan.shadowSize = new GSize(22, 20);
iconMan.iconAnchor = new GPoint(24, 34);
var markerMan;

var icon = new GIcon();
icon.image = "/img/icons/icon_map2.png";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(30, 40);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(18, 40);
icon.infoWindowAnchor = new GPoint(5, 1);

function showMap(latlng, lastSearchParameter){
    lastSearchBasicParameter = lastSearchParameter;
    if (latlng){
        showNearByMapSearch(latlng);
        buildingLatlng = latlng;
        markerMan = new GMarker(latlng, {draggable: true, icon: iconMan});
        showMapCore(latlng);

        GEvent.addListener(markerMan, 'dragstart', function(latlng) {
            map01.addOverlay(streetView);
        });

        GEvent.addListener(markerMan, 'dragend', function(latlng) {
            showNearByMapSearch(latlng);
            mapView.setLocationAndPOV(latlng);
            markerMan.setPoint(latlng);
            map01.removeOverlay(streetView);
        });
        map01.addOverlay(markerMan);
    }
}

function showMapFree(latlng){
    if (latlng){
        showMapCore(latlng);
    }
}

function showNearByMapSearch(latlng) {
    var url = '/search/map?lat=' + latlng.lat().toString() + '&lng=' + latlng.lng().toString();
    if (lastSearchBasicParameter != null && lastSearchBasicParameter != undefined && lastSearchBasicParameter != '') {
        url += '&' + lastSearchBasicParameter;
    }
    var atag = $($$('#mapInfo .btnNearby a')[0]);
    atag.href = url;
    atag.style.display = 'block';
}

function showMapWithoutView(latlng)
{
    if (latlng) {
        showMapCore(latlng);
    }
}

function showMapCore(latlng) {
    if (latlng){
        map01.setCenter(latlng, 18);
        map01.addOverlay(new GMarker(latlng, icon));
    }
}

function showView(latlng){
    if (latlng){
        mapView = new GStreetviewPanorama(document.getElementById("map02"));
        mapView.setLocationAndPOV(latlng);
        GEvent.addListener(mapView, "initialized", viewInitialize);
        GEvent.addListener(mapView, "error", viewError);

        GEvent.addListener(mapView, 'yawchanged', function(yaw) {
            imgPath = '/img/icons/googlemap/man_arrow-' + Math.round(yaw / 24) + '.png';
            markerMan.setImage(imgPath);
        });
    }
}

function viewError(code)
{
    if (code == 600) {
        $('map02').innerHTML = 'ストリートビュー未対応地域です';
        $('map02').hide();
        $('map01').style.width = '925px';
        $($('map01').ancestors()[0]).style.width = '925px';
        map01.removeOverlay(markerMan);
        map01.removeOverlay(streetView);
        map01.checkResize();
        map01.setCenter(buildingLatlng);
        return;
    }
}

function viewInitialize(location)
{
    markerMan.setLatLng(location.latlng);
    pov = this.getPOV();
    imgPath = '/img/icons/googlemap/man_arrow-' + Math.round(pov['yaw'] / 24) + '.png';
    if (yaw = calcYaw(location.latlng)) {
        imgPath = '/img/icons/googlemap/man_arrow-' + Math.round(yaw / 24) + '.png';
        var pov = {
            yaw:   yaw,
            pitch: location.pov.pitch,
            zoom:  0
        };
        mapView.panTo(pov);
    }
    markerMan.setImage(imgPath);
}

function calcYaw(currentLatlng) {
    if (currentLatlng.equals(buildingLatlng)) {
        return 0;
    }

    var yaw = 90 - Math.atan2(buildingLatlng.lat() - currentLatlng.lat(), buildingLatlng.lng() - currentLatlng.lng()) * 180 / Math.PI;
    if (yaw < 0) {
        yaw += 360;
    }

    return yaw;
}


