﻿
var map, geocoder, pano;

function InitMapByAddress(canvasId, address, hintText) {
    if (GBrowserIsCompatible()) {
        //alert("comparable");
        map = new GMap2(document.getElementById(canvasId));        
        geocoder = new GClientGeocoder();
        showAddress(address, hintText, function(result) {
            if (!result) {              
                document.getElementById(canvasId).style.display = "none";
            }
        });        
    }
}

function InitMapByLatLong(canvasId, point) {
    //TODO
}

function showAddress(address, hintText, callback) {
   // alert(address);
   // alert(geocoder);
   // alert(map);
    
    geocoder.getLatLng(address, function(point) {    
        callback(ShowMapPoint(point, hintText))
    });

    
}
function ShowMapPoint(point, hintText) {
    if (!point) {
        //alert(address + " not found");        
        return false;
        
    } else {
        //alert(point);
        map.setCenter(point, 13);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        if (hintText != "")
            marker.openInfoWindowHtml(hintText);
        return true;
    }
}



function InitStreetViewByAddress(canvasId, address, hintText, alsoShowMap) {
    if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
        //alert(geocoder);
        showStreetViewByAddress(canvasId, address, hintText, alsoShowMap);        
        
    }
}


function showStreetViewByAddress(canvasId, address, hintText, alsoShowMap) {
    //alert(address);
    //alert(geocoder);
    geocoder.getLatLng(address, function(point) {
        //alert("Point: " + point);
        if (point == null) {
            HideMap(canvasId);
            return;
        }
        var panoramaOptions = { latlng: point };
        var panoDiv = document.getElementById(canvasId);
        panoDiv.innerHTML = '';
        pano = new GStreetviewPanorama(panoDiv, panoramaOptions);
        //alert("Pano: " + pano);
        if (alsoShowMap != '') {
            map = new GMap2(document.getElementById(alsoShowMap));
            if (!ShowMapPoint(point, hintText)) {
                HideMap(alsoShowMap);
            }
        } else {

        }
        GEvent.addListener(pano, "error", function(errorCode) {

            //alert("fuck: " + errorCode);
            //if (errorCode == 600) {

            //} else if (errorCode == 603) {

            //}
            //HideMap(canvasId);

            var mapDiv = document.getElementById(alsoShowMap);            
            panoDiv.appendChild(mapDiv);
        });



    });



}

function HideMap(canvasId) {
    document.getElementById(canvasId).style.display = "none";
}

