﻿var Map = null;         // Contains the virtual earth map
var dragObject = null;  // Used to drag the rooftop
var shapeLayer = null;  // A layer containing the pins and rooftop
var bestPhoto = null;   // The best photo 
var firstRun = false;   // on the first run we set some default visiblity values
var viewer = null;      // Holds the main fisheye viewer
var roof = null;
var ClearStatus;

// When the virtual earth map object is loaded begin processing the page
function MapLoaded(sender, ev) {
    firstRun = true;
    Map = $find(mapId).get_VEMap();

    // Controls the drag and drop of the roof top
    Map.AttachEvent("onmousedown", mouseDown);
    Map.AttachEvent("onmouseup", mouseUp);
    Map.AttachEvent("onmousemove", mouseMove);

    // Controls the clicking of pins to navigate
    Map.AttachEvent("onclick", loadViewer);

    // Gets the lat long that was passed to this page
    var latlong = new VELatLong(document.getElementById("ctl00_ContentPlaceHolder1_hlat").value,
        document.getElementById("ctl00_ContentPlaceHolder1_hlong").value);

    // The shape for the rooftop pin is loaded and added to the map control
    roof = new VEShape(VEShapeType.Pushpin, latlong);
    roof.SetCustomIcon("/Images/CentroidGreen.png");
    roof.SetTitle("Rooftop");
    roof.SetDescription("Move this pushpin to the rooftop of the desired house");
    var RoofShapeLayer = new VEShapeLayer();
    RoofShapeLayer.AddShape(roof);
    Map.AddShapeLayer(RoofShapeLayer);

    shapeLayer = new VEShapeLayer();
    Map.AddShapeLayer(shapeLayer);

    // Downloads the best pins for a given lat long
    StreetScapeViewer.Default1.LoadPins(roof.Latitude,
            roof.Longitude,
            document.getElementById("ctl00_ContentPlaceHolder1_hID").value,
            document.getElementById("ctl00_ContentPlaceHolder1_hToken").value,
            Complete, Failure);
    
}

function onViewChange(Event) {
    var currentPhotoId = viewer.getImageID();
    if (stepTracker.Step == 0) {
        for (var i = 0; i < stepTracker.Steps[0].Photos.length; i++) {
            if ((stepTracker.Steps[0].Photos[i].ID == currentPhotoId) && (stepTracker.Steps[0].Id != currentPhotoId)) {
                stepTracker.Steps[0].ActiveImage = i;
                stepTracker.Steps[0].Id = currentPhotoId;
                stepTracker.Steps[0].POV = viewer.getPOV();
                if (!stepTracker.Steps[0].Loading)
                    stepTracker.Steps[0].Changed = true;
                else
                    stepTracker.Steps[0].Loading = false;
                updatePins();
            }
            else if (stepTracker.Steps[0].Photos[i].ID == currentPhotoId) {
                stepTracker.Steps[0].POV = viewer.getPOV();
                if (!stepTracker.Steps[0].Loading)
                    stepTracker.Steps[0].Changed = true;
                else
                    stepTracker.Steps[0].Loading = false;
            }
        }
    }
    else if (stepTracker.Step == 2) {
    stepTracker.Steps[2].Id = currentPhotoId;
        stepTracker.Steps[2].POV = viewer.getPOV();
    }
}

// Prevents the page from moving up and down when the mouse wheel is used over a flash object
function wheel(event) {
    return false;
}

// Loads a photo into the fisheye viewer
function loadViewer(ev) {
    if (ev.elementID != null) {
        var pin = Map.GetShapeByID(ev.elementID);
        if ((pin._customIcon == "/Images/redpin.png") && (!dragObject)) {
            for (var i = 0; i < stepTracker.Steps[0].Photos.length; i++) {
                if (stepTracker.Steps[0].Photos[i].Pin.iid == pin.iid) {
                    loadPicture(stepTracker.Steps[0].Photos[i].ID, stepTracker.Steps[0].Photos[i].yaw,
                    stepTracker.Steps[0].Photos[i].pitch, stepTracker.Steps[0].Photos[i].hFov);
                }
            }
        }
    }
}

// Loads a picture into the main fisheye viewer, has not been initialized it will do that aswell
function loadPicture(id, yaw, pitch, fov) {
    if (viewer == null) {
        viewer = new ilookabout.PanoViewer(document.getElementById("divFisheye"));
        viewer.onViewChange = ilookabout.Event.addListener(viewer, "viewchange", onViewChange);
        viewer.getSwfObject().onmousewheel = wheel;
    }
    viewer.setImageIdAndPOV(id, new ilookabout.Pov(yaw, pitch, fov));
}

// The user is attempting to drag the rooftop
function mouseDown(ev) {
    if (ev.elementID != null) {
        var pin = Map.GetShapeByID(ev.elementID);
        if (pin._customIcon == "/Images/CentroidGreen.png") {
            dragObject = pin;
            startLatLong = new VELatLong(dragObject.Latitude, dragObject.Longitude);
        }
    }
}

// The user is moving the rooftop
function mouseMove(ev) {
    if (dragObject) {
        var x = ev.mapX;
        var y = ev.mapY;
        pixel = new VEPixel(x, y);
        var LL = Map.PixelToLatLong(pixel);
        dragObject.SetPoints(LL);
        return true;
    }
}

// The user is droping the rooftop
function mouseUp(ev) {
    if (dragObject) {
        stepTracker.Steps[0].Changed = true;
        StreetScapeViewer.Default1.LoadPins(dragObject.Latitude,
                dragObject.Longitude,
                document.getElementById("ctl00_ContentPlaceHolder1_hID").value,
                document.getElementById("ctl00_ContentPlaceHolder1_hToken").value,
                Complete, Failure);
        document.getElementById("ctl00_ContentPlaceHolder1_hlat").value = dragObject.Latitude;
        document.getElementById("ctl00_ContentPlaceHolder1_hlong").value = dragObject.Longitude;
        Map.PanToLatLong(new VELatLong(dragObject.Latitude, dragObject.Longitude));
        dragObject = null;
    }
}

function updatePins() {
    shapeLayer.DeleteAllShapes();

    // For each photo add a red pin to the map control
    for (var i = 0; i < stepTracker.Steps[0].Photos.length; i++) {
        if (i == stepTracker.Steps[0].ActiveImage) {
            var latlong = new VELatLong(stepTracker.Steps[0].Photos[i].latitude, stepTracker.Steps[0].Photos[i].longitude);
            var shape = new VEShape(VEShapeType.Pushpin, latlong);
            shape.SetCustomIcon("/Images/greenpin.png");
            shape.SetTitle("StreetScape Photo");
            shape.SetDescription("The green pin displays the photo that is in the viewer");
            shapeLayer.AddShape(shape);
            stepTracker.Steps[0].Photos[i].Pin = shape;
        }
        else {
            var latlong = new VELatLong(stepTracker.Steps[0].Photos[i].latitude, stepTracker.Steps[0].Photos[i].longitude);
            var shape = new VEShape(VEShapeType.Pushpin, latlong);
            shape.SetCustomIcon("/Images/redpin.png");
            shape.SetTitle("StreetScape Photo");
            shape.SetDescription("Click on a red pin to display that photo in the viewer");
            shapeLayer.AddShape(shape);
            stepTracker.Steps[0].Photos[i].Pin = shape;
        }
    }
}

// When the service has returned photos related to the given postion
function Complete(result, args) {

    // Save the list of photos
    var photos = result;
    var bestPhoto;

    if (photos.length != 0) {
        shapeLayer.DeleteAllShapes();
        current = 0;

        // For each photo add a red pin to the map control
        for (var i = 0; i < photos.length; i++) {
            if (((photos[i].isBest) && (stepTracker.Steps[0].Changed)) || ((stepTracker.Steps[0].Id == photos[i].ID) && (!stepTracker.Steps[0].Changed))) {
                bestPhoto = photos[i];
                current = i;
                stepTracker.Steps[0].ActiveImage = i;

                var latlong = new VELatLong(photos[i].latitude, photos[i].longitude);
                var shape = new VEShape(VEShapeType.Pushpin, latlong);
                shape.SetCustomIcon("/Images/greenpin.png");
                shape.SetTitle("StreetScape Photo");
                shape.SetDescription("The green pin displays the photo that is in the viewer");
                shapeLayer.AddShape(shape);
                photos[i].Pin = shape;
            }
            else {
                var latlong = new VELatLong(photos[i].latitude, photos[i].longitude);
                var shape = new VEShape(VEShapeType.Pushpin, latlong);
                shape.SetCustomIcon("/Images/redpin.png");
                shape.SetTitle("StreetScape Photo");
                shape.SetDescription("Click on a red pin to display that photo in the viewer");
                shapeLayer.AddShape(shape);
                photos[i].Pin = shape;
            }
        }


        if (stepTracker.Steps[0].Changed) {
            loadPicture(bestPhoto.ID, bestPhoto.yaw, bestPhoto.pitch, bestPhoto.hFov);
            stepTracker.Steps[0].Id = bestPhoto.ID;
            stepTracker.Steps[0].POV = new ilookabout.Pov(bestPhoto.yaw, bestPhoto.pitch, bestPhoto.hFov);
            stepTracker.Steps[0].Photos = photos;
        }
        else {
            loadPicture(stepTracker.Steps[0].Id, stepTracker.Steps[0].POV.yaw, stepTracker.Steps[0].POV.pitch, stepTracker.Steps[0].POV.vfov);
            stepTracker.Steps[0].Photos = photos;
        }
    }
    else {
        alert("No photos availible for that location.");
        stepTracker.Steps[0].Id = null;
    }
}

// When the service has returned photos related to the given postion
function Step1Complete(result, args) {

    // Save the list of photos
    if (result.length != 0) {
        stepTracker.Steps[1].Photos = result;
        pv = new PivotViewer("Pivot", true, stepTracker.Steps[1].OneClick);
        pv.sensitivity = 1;
        if (!stepTracker.Steps[0].Changed) {
            var i = 0;
            while ((i < result.length) && (result[i].ID != stepTracker.Steps[1].PreviousBestId))
                i++;

            if (i < result.length) {
                if (stepTracker.Steps[1].Pivot) {
                    stepTracker.Steps[1].PivotStart = i - stepTracker.Steps[1].PreviousBestPosition;
                    stepTracker.Steps[1].PivotActiveImage = i;
                    stepTracker.Steps[1].PivotEnd = i + ((stepTracker.Steps[1].PreviousSize - 1) - stepTracker.Steps[1].PreviousBestPosition);
                }
                else {
                    stepTracker.Steps[1].DriveByStart = i - stepTracker.Steps[1].PreviousBestPosition;
                    stepTracker.Steps[1].DriveByActiveImage = i;
                    stepTracker.Steps[1].DriveByEnd = i + ((stepTracker.Steps[1].PreviousSize - 1) - stepTracker.Steps[1].PreviousBestPosition);
                }
                modifyimage("Pivot", i);
            }
        }
        LoadSlider();
    }
    else
        alert("No photos availible for that location.");
}

// There was a failure to move the house's lat long
function Failure(result, args) {
    alert(result._message);
}


