﻿/// <reference path="googlemaps-intellisense.js" />

//REGION Fields****************************************************************


//variables used to control the street view
var StreetView_overlayInstance = null;//the blue street outline indicating street view
var StreetView_markerGuy;//the marker guy
var StreetView_lastMarkerLocation = null;//for keeping track of where the marker was before it was dragged, or somone clicked on the map, or moved by the pano
var StreetView_panorama;//the actual panorama
var StreetView_guyIcon = StreetView_CreateGuyIcon();
var StreetView_isFirstTimeViewed = true;//a flag inidicating this is the first time the street view control has been used
var StreetView_panoDiv;//the div where the pano is to be created
var StreetView_lastMapMode;//used to get the state of the map before changing to STREET (For returning to this state when street view is completed)

var StreetView_button; //div element for the street view button control


//Street view guy event handlers
var StreetView_guyDragStartHandler = null;
var StreetView_guyDraggingHandler = null;
var StreetView_guyDragEndHandler = null;
var StreetView_guyClickHandler = null;


//Panoramic event handlers
var StreetView_newPanoHandler = null;//fires when the user uses the arrows to move up and down a street in the pano
var StreetView_yawChangedHandler = null;//fires when the user turns the pano view with the arrows
var StreetView_panoErrorsHandler = null;//fires when the user does not have flash and other errors


//END REGION Fields************************************************************


//KK street view button control
function StreetView_Control() {
}
StreetView_Control.prototype = new GControl();
// Creates DIV for sreet view button.
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
StreetView_Control.prototype.initialize = function(map) {
  var container = document.createElement("div");
  StreetView_button = document.createElement("div");
  this.setButtonStyle_(StreetView_button);
  container.appendChild(StreetView_button);
  StreetView_button.appendChild(document.createTextNode("Street View"));
  GEvent.addDomListener(StreetView_button, "click", function() {
  StreetView_ToggleStreetViewOverlay();
  });
  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
//StreetView_Control.prototype.getDefaultPosition = function() {
//  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(35, 7));
//}


// Sets the proper CSS for the given button element.
StreetView_Control.prototype.setButtonStyle_ = function(button) {
  //button.style.textDecoration = "underline";
  button.style.color = "black";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "1px";
  button.style.marginBottom = "2px";
  button.style.textAlign = "center";
  button.style.width = "6em";
  button.style.cursor = "pointer";
  button.style.align = "left";
  button.style.position = "absolute";
}

//END CONTROL

//REGION: Lifetime*************************************************************


function StreetView_InitializePanorama() { 
    //get a reference to the pano-wrapper and create the pano
    
    panoClient = new GStreetviewClient();    
    StreetView_panoDiv = document.getElementById('stview');
    StreetView_panorama = new GStreetviewPanorama(StreetView_panoDiv);
    
    //register for events
    StreetView_newPanoHandler = GEvent.addListener(StreetView_panorama, "initialized", StreetView_OnNewLocation);
    StreetView_yawChangedHandler = GEvent.addListener(StreetView_panorama, "yawchanged", StreetView_OnYawChange);
    StreetView_panoErrorsHandler = GEvent.addListener(StreetView_panorama, "error", StreetView_HandlePanoErrors);
    
   //KK added listener:
    GEvent.addListener(map,"click", function(overlay,latlng) {
    StreetView_MapClicked(overlay, latlng);
});  // end click listener  

};//end function

function StreetView_CleanUP()
{
    if(StreetView_panorama)
    {
        StreetView_RemoveGuyFromMap();
        map.removeOverlay(StreetView_overlayInstance);
        StreetView_overlayInstance = null;
                //KK  hide the panorama div
        StreetView_panoDiv.style.visibility="hidden";
        StreetView_panoDiv.style.height="0px";

        StreetView_panorama.hide();

        GEvent.removeListener(StreetView_newPanoHandler);
        GEvent.removeListener(StreetView_yawChangedHandler);
        GEvent.removeListener(StreetView_panoErrorsHandler); 
        
        GEvent.removeListener(StreetView_guyDragStartHandler); 
        GEvent.removeListener(StreetView_guyDraggingHandler); 
        GEvent.removeListener(StreetView_guyDragEndHandler); 
        GEvent.removeListener(StreetView_guyClickHandler); 
    }//end if StreetView
};//end function


//END REGION: Lifetime*********************************************************




//REGION: External Entry Points************************************************


//This is the external entry point
function StreetView_ToggleStreetViewOverlay() {
   var latlng;
    if (!StreetView_overlayInstance) {
        //adding streetview
        StreetView_panoDiv.style.visibility="visible";
        StreetView_panoDiv.style.height="200px";
        StreetView_overlayInstance = new GStreetviewOverlay();
        map.addOverlay(StreetView_overlayInstance);

        //Init the panorama
        StreetView_InitializePanorama();
        
        StreetView_DropGuyOnMap();
        //KK StreetView_lastMapMode = mapMode;
       //KK mapMode = "STREET";
       latlng = map.getCenter();
    StreetView_markerGuy.setLatLng(latlng);
    panoClient.getNearestPanorama(latlng, StreetView_GetNearestPanorama_Response);            
    }
    else {
        //removing street view
        StreetView_CleanUP();
        //set the global map mode back to what it was before streetview was called up
       //KK mapMode = StreetView_lastMapMode;
    }    
};//end function


function StreetView_MapClicked(overlay, point)
{
    //This is working just yet, need to redrop the guy on the map
    StreetView_markerGuy.setLatLng(point);
    panoClient.getNearestPanorama(point, StreetView_GetNearestPanorama_Response);
}


//END REGION: External Entry Points********************************************



//REGION: Private Methods******************************************************

function StreetView_DropGuyOnMap() {
    //set the position of the marker guy
    var latlng;
    if (StreetView_lastMarkerLocation != null) {
        latlng = StreetView_lastMarkerLocation;
    }
    else {
        latlng = map.getCenter();
    }
    
    //create and add the marker
    StreetView_markerGuy = new GMarker(latlng, { icon: StreetView_guyIcon, draggable: true });
    map.addOverlay(StreetView_markerGuy);

    //add event handlers for marker
    
    StreetView_guyClickHandler = GEvent.addListener(StreetView_markerGuy, "click", StreetView_ShowPanorama);
    StreetView_guyDragStartHandler = GEvent.addListener(StreetView_markerGuy, "dragstart", StreetView_OnDragStart);
    StreetView_guyDraggingHandler = GEvent.addListener(StreetView_markerGuy, "drag", StreetView_OnDragging);
    StreetView_guyDragEndHandler = GEvent.addListener(StreetView_markerGuy, "dragend", StreetView_OnDragEnd);   
    panoClient.getNearestPanorama(latlng, StreetView_GetNearestPanorama_Response);


};//end function


function StreetView_RemoveGuyFromMap() {
    map.removeOverlay(StreetView_markerGuy);
    GEvent.removeListener(StreetView_guyDragEndHandler);
    GEvent.removeListener(StreetView_guyClickHandler);
};//end function

function StreetView_CreateGuyIcon() {
    var myGuyIcon = new GIcon(G_DEFAULT_ICON);
    myGuyIcon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
    myGuyIcon.transparent = "http://maps.google.com/intl/en_us/mapfiles/cb/man-pick.png";
    myGuyIcon.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
     ];
    myGuyIcon.iconSize = new GSize(49, 52);
    myGuyIcon.iconAnchor = new GPoint(25, 35);  // near base of guy's feet
    myGuyIcon.infoWindowAnchor = new GPoint(25, 5);  // top of guy's head

    return myGuyIcon;
};//end function


function StreetView_ShowPanorama() { 
    //set the latLng of the StreetView_panorama
    myLatLng = StreetView_markerGuy.getLatLng();
    StreetView_panorama.setLocationAndPOV(myLatLng, null);
    //let the pano load
    setTimeout("StreetView_panorama.show()",200);
};//end function


function StreetView_GetNearestPanorama_Response(response) {
    var latLng;
    if (response.code != 200) {
        if (StreetView_lastMarkerLocation != null) {
            latLng = StreetView_lastMarkerLocation;
        }
        else {
            latLng = map.getCenter();
        }
    } 
    else {
        var latLng = new GLatLng(response.Location.lat, response.Location.lng);
    } //end if not error

    //set the last location
    StreetView_lastMarkerLocation = latLng;

    //open the appropriate window
    if (StreetView_isFirstTimeViewed || response.code != 200) {
        StreetView_isFirstTimeViewed = false;
        StreetView_markerGuy.setLatLng(StreetView_lastMarkerLocation);
       // KK StreetView_markerGuy.openInfoWindowHtml("<div class=mapbubble><b>Street View</b></p><p>Drag me onto a blue outlined street.<br/>Or you can also just click on a blue outlined street.</p></DIV>");
       //KK added showpanorama: 
        StreetView_ShowPanorama();
    }
    else {
        StreetView_ShowPanorama();
    } //end if not first time viewed

};//end function

//END REGION: Private Methods**************************************************

//REGION: Panoramic Event Handlers*********************************************


function StreetView_OnYawChange(newYaw) {
    var GUY_NUM_ICONS = 16;
    var GUY_ANGULAR_RES = 360 / GUY_NUM_ICONS;
    if (newYaw < 0) {
        newYaw += 360;
    }
    guyImageNum = Math.round(newYaw / GUY_ANGULAR_RES) % GUY_NUM_ICONS;
    guyImageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
    StreetView_markerGuy.setImage(guyImageUrl);
};//end function

function StreetView_OnNewLocation(pLocation) {
    var latlng = pLocation.latlng;
    StreetView_lastMarkerLocation = StreetView_markerGuy.getLatLng();
    StreetView_markerGuy.setLatLng(latlng);
};//end function



function StreetView_HandlePanoErrors(errorCode) {
    if (errorCode == 603) {
        alert("Error: Flash doesn't appear to be supported by your browser");
        return;
    }

    //Do nothing else for other errors at this point...
};//end function



//END REGION: Panoramic Event Handlers*****************************************




//REGION: Marker Guy Event handlers********************************************

function StreetView_OnDragStart(){
    //close the info window if it is open
    StreetView_markerGuy.closeInfoWindow();
};//end function

function StreetView_OnDragging(){
    //TODO: animate cursor like on Google maps
};//end function

function StreetView_OnDragEnd() {
    //the marker guy has been dragged, we need to reposition the marker guy 
    //and show the pano if he was released in an area with street view
    var latlng = StreetView_markerGuy.getLatLng();
    panoClient.getNearestPanorama(latlng, StreetView_GetNearestPanorama_Response);
};//end function

//END REGION: Marker Guy Event handlers****************************************







