﻿
var gLatLng, gStreetviewPanorama, yaw, pitch, zoom;

function disableStreetview() {
    $(streetviewDropdownId).hide();
    disposeStreetview();
}

function disposeStreetview() {
    try {
        GUnload();
    }
    catch (er) {
        $(streetviewDivId).html("Failed to dispose Google Maps objects.\n\n" + er.description);
    }
}

function hidePanorama() {
    if (streetviewPushpin) {
        veMap.DeleteShape(streetviewPushpin);
    }
    $(streetviewDivId).html("");
    disposeStreetview();
}

function onGetNearestPanorama(gStreetviewData) {
    if (gStreetviewData && gStreetviewData.code == 200) {

        // Clear any text in the target div and display the panorama.
        $(streetviewDivId).html("");
        gStreetviewPanorama.setLocationAndPOV(gLatLng, { yaw: yaw, pitch: pitch, zoom: zoom });

        // Wire other events.
        GEvent.addListener(gStreetviewPanorama, "initialized", onPanoramaInitialized);
        GEvent.addDomListener($(streetviewDivId)[0], "mouseup", function(e) { hideMenus(); });
    }
    else {
        $(streetviewDivId).html("The requested location does not have Google Maps Street View data.");
        disposeStreetview();
    }
}

function onPanoramaError(errorCode) {
    if (errorCode == 603) {
        $(streetviewDivId).html("Your browser does not appear to have the Flash plugin. This plugin is required to use Google Maps Street View.");
        disableStreetview();
    }
}

function onPanoramaInitialized(location) {
    resizeSplitter();

    // Draw pushpin on Bing map.    
    drawStreetviewPushpin(location.lat, location.lng);
}

function resizePanorama() {
    if (gStreetviewPanorama) {
        gStreetviewPanorama.checkResize();
    }
}

function showPanorama(lat, lon) {
    if (GBrowserIsCompatible()) {
        gLatLng = new GLatLng(lat, lon);
        gStreetviewPanorama = new GStreetviewPanorama($(streetviewDivId)[0]);
        GEvent.addListener(gStreetviewPanorama, "error", onPanoramaError);
        var gStreetviewClient = new GStreetviewClient();
        gStreetviewClient.getNearestPanorama(gLatLng, onGetNearestPanorama);
    }
    else {
        $(streetviewDivId).html("Your browser does not support Google Maps.");
        disableStreetview();
    }
}

