﻿var tbDirFrom = "";
var ddDirTo = "";

function init() {
    if (GBrowserIsCompatible()) {

        var mapDataArray = document.getElementById("ctl00_contentBody_hfGeMapData").value.split("~");
        
        mapDataArray.splice((mapDataArray.length - 1), 1)
        //var streetView = new GStreetviewPanorama(document.getElementById("streetView"));

        //map = new GMap2(document.getElementById("mapDiv"), { mapTypes: [G_NORMAL_MAP, G_SATELLITE_3D_MAP] });
        map = new GMap2(document.getElementById("mapDiv"), { mapTypes: [G_NORMAL_MAP] });
        map.setCenter(new GLatLng(mapDataArray[0], mapDataArray[1]), 15);
        center = map.getCenter();
        zoom = 15;

        if (document.getElementById("ctl00_contentBody_hfGeLocGroupMapData")) {
            mapDataGrpArray = document.getElementById("ctl00_contentBody_hfGeLocGroupMapData").value.split("~");
            zoom = 10;
            var alt = parseInt(mapDataGrpArray[2]);

            if (alt > 0 & alt < 3000) {
                zoom = 14;
            };

            if (alt > 3001 & alt < 11000) {
                zoom = 13;
            };

            if (alt > 11001 & alt < 18000) {
                zoom = 12;
            };

            if (alt > 18001 & alt < 26500) {
                zoom = 11;
            };
            if (alt > 27501 & alt < 36000) {
                zoom = 10;
            };
            if (alt > 36001) {
                zoom = 9;
            };
            fillColorHex = "#" + RGBtoHex(mapDataGrpArray[3], mapDataGrpArray[4], mapDataGrpArray[5]);
            strokeColorHex = "#" + RGBtoHex(mapDataGrpArray[6], mapDataGrpArray[7], mapDataGrpArray[8]);

            var polyPoints = [
            new GLatLng(mapDataGrpArray[9], mapDataGrpArray[10]),
            new GLatLng(mapDataGrpArray[11], mapDataGrpArray[12]),
            ];

            for (i = 13; i < mapDataGrpArray.length - 1; i = i + 2) {
                polyPoints.push(new GLatLng(mapDataGrpArray[i], mapDataGrpArray[i + 1]));
            };

            var polygon = new GPolygon(polyPoints, strokeColorHex, 1, 0.7, fillColorHex, 0.2);  //strokeColor, strokeWidth, storkeAlpha, fillColor, fillAlpha
            map.addOverlay(polygon);

            map.setCenter(new GLatLng(mapDataGrpArray[0], mapDataGrpArray[1]), zoom);       
        }

        GEvent.addListener(map, "moveend", function() {
            center = map.getCenter();
            zoom = map.getZoom();
        });
        
        map.addControl(new GLargeMapControl3D());
        map.addControl(new GMapTypeControl());
        //svOverlay = new GStreetviewOverlay();
        //map.addOverlay(svOverlay);

        //map.getEarthInstance(getEarthInstanceCB);

        var drivingDirection = document.getElementById("drivingDirection");
        directions = new GDirections(map, drivingDirection);
//        GEvent.addListener(directions, "load", function() {
//            alert("Could not generate a route for the current start and end addresses");
//        });

        // Create marker icon
        var bug = new GIcon();
        bug.image = "http://www.saclibrary.org/image/UI/SPLlogo-no-text40x40.png";
        bug.shadow = "http://www.saclibrary.org/image/UI/logoDropShadow80x40.png";
        bug.iconSize = new GSize(40, 40);
        bug.shadowSize = new GSize(80, 40);
        bug.iconAnchor = new GPoint(20, 20);                   

        for (i = 0; i < mapDataArray.length; i = i + 4) {
            var point = new GLatLng(mapDataArray[i], mapDataArray[i + 1]);
            markerOptions = { icon: bug, title: mapDataArray[i + 2] };
            map.addOverlay(createMarker(point, mapDataArray[i + 3], markerOptions));
        }

    } else {// ! GBrowserIsCompatible
        alert("Your web browser is not compatible with the map application.");
    } //GBrowserIsCompatible

    function createMarker(point, infoHtml, markerOptions) {
        var marker = new GMarker(point, markerOptions);
        GEvent.addListener(marker, "click", function() {
            map.openInfoWindowHtml(point, infoHtml);
            var ddLoc = document.getElementById("ctl00_contentBody_ddLoc");
            //alert(marker.getTitle());
            //Set the dropdown "To:" the marker clicked
            for (var i = 0; i <= ddLoc.length - 1; i = i + 1) {
                var ddlText = ddLoc.options[i].outerText;
                txtText = marker.getTitle();
                if (ddlText == txtText) {
                    ddLoc.selectedIndex = i;
                    break;
                }
            }
        });
        return marker;
    }
}

function getDirections() {
    directions.clear();  
    tbDirFrom = document.getElementById("ctl00_contentBody_tbDirFrom").value;
    var ddLoc = document.getElementById("ctl00_contentBody_ddLoc");
    ddDirTo = ddLoc.options[ddLoc.selectedIndex].value;
    ddLibName = ddLoc.options[ddLoc.selectedIndex].text;
    directions = new GDirections(map, drivingDirection);
    directions.load("from: " + tbDirFrom + " to: " + ddDirTo);
    GEvent.addListener(directions, "error", function() { alert("Could not find the From address. Try including more information, like the city."); }); 
}

function directionsError() {
    alert("Error");
}

//Google Earth Stuff
var ge;
function getEarthInstanceCB(pluginInstance) {
    ge = pluginInstance;
    // add some layers
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
}

//Print link
function openPrintWindow() {
    if (tbDirFrom != "") {
        window.open('mapPrint.aspx?to=' + ddDirTo + '&from=' + tbDirFrom + '&libName=' + ddLibName + ' ', 'Printing_Map', 'width=620,height=25,resizable=yes');
    }else{
        window.open('mapPrint.aspx?ctr=' + center + '&zoom=' + zoom, 'Printing_Map', 'width=620,height=25,resizable=yes');
    }
}

//RGB To Hex
function RGBtoHex(R, G, B) {
    return toHex(R) + toHex(G) + toHex(B)
   }

function toHex(N) {
    if (N == null) return "00";
    N = parseInt(N);
    if (N == 0 || isNaN(N)) return "00";
    N = Math.max(0, N);
    N = Math.min(N, 255);
    N = Math.round(N);
    return "0123456789ABCDEF".charAt((N - N % 16) / 16) + "0123456789ABCDEF".charAt(N % 16);
}

