﻿/// <reference path="../VEJS/VeJavaScriptIntellisenseHelper.js" />

// NOTE:    The above reference is used only at design time.
//          It provides a skeleton of the VE API to enable 
//          intellisense.  Notice that the Default.aspx 
//          page DOES NOT reference VeJavaScriptIntellisenseHelper.js.
//          At run time, we use the real VE API.


var map
var oPRShapeL = new VEShapeLayer();  // park-n-ride layer
var oStationShapeL = new VEShapeLayer();
var findLayer = new VEShapeLayer();
var findArray = new Array();
var bTrafficLoaded;


var index = 0;
var results = null;

if (typeof (DESIGN_TIME) == 'undefined') {
    map = null;
}
else {
    map = new VEMap();
}

function pageLoad() {
    try {
        map = new VEMap("myMap");
        bTrafficLoaded = false;
        map.onLoadMap = map_Loaded;
        map.LoadMap(new VELatLong(35.175800, -80.879100), 12, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
        oStationShapeL.SetTitle = "Stations";
        oPRShapeL.SetTitle = "ParkRideLoc";
        document.getElementById('MSVE_obliqueNotification').innerHTML = "";
    }
    catch (e) {

    }
}

function GetSmallMap() {
    map = new VEMap("myMap");
    bTrafficLoaded = false;
    map.onLoadMap = map_Loaded;
    map.LoadMap(new VELatLong(35.175800, -80.879100), 12, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
    oStationShapeL.SetTitle = "Stations";
    oPRShapeL.SetTitle = "ParkRideLoc";
}

function pageUnload() {
    if (map != null) {
        map.Dispose();
    }
}

function PrintMap() {
    var pOptions = new VEPrintOptions(true);
    map.SetPrintOptions(pOptions);
    window.print();
    pOptions = new VEPrintOptions(false);
    map.SetPrintOptions(pOptions);
}

function map_Loaded() {
    AddRail(map);
    AddStations(map, 1);
    LoadParkRideLoc();
    ToggleTraffic();
    AddEvent(map);
}

function AddEvent(map) {
    //    var points1 = new VELatLong(33.431521, -111.940700, 0, VEAltitudeMode.RelativeToGround)
    //    var shape1 = new VEShape( VEShapeType.Pushpin, points1)
    //    shape1.SetDescription("<div style='z-index:5000;background-color: #FF0000;text-align:center'><h2><a href='http://tempemusicfestival.com/'><u>Tempe Music Festival</u></a></h2><br/>April 3 & 4 <br /><img src='http://tempemusicfestival.com/images/photoGallery/thumbs08/fergietv4_thumb.jpg' alt='Tempe Music Festival'/><br/> <img src='http://tempemusicfestival.com/images/photoGallery/thumbs08/fergie2_thumb.jpg' alt='Tempe Music Festival'/><img src='http://tempemusicfestival.com/images/photoGallery/thumbs08/Puddle%20of%20Mud%203_thumb.jpg' alt='Tempe Music Festival'/><img src='http://tempemusicfestival.com/images/photoGallery/thumbs08/Peachcake_thumb.jpg' alt='Tempe Music Festival'/></div>"); 
    //    shape1.SetCustomIcon("http://www.downtownlightrail.com/light-rail-maps/Images/camera39_big.gif");
    //    map.AddShape(shape1);

}

//loads the light rail map
function AddRail(map) {
    var points;

    //blue Line
    points = new Array(
            new VELatLong(35.1063, -80.8828, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1086, -80.8831, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1102, -80.8833, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1115, -80.8837, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1125, -80.8838, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1134, -80.8838, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1153, -80.8837, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1161, -80.8836, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1173, -80.8832, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1184, -80.8826, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1197, -80.8819, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1237, -80.8798, 0, VEAltitudeMode.RelativeToGround),
            
            new VELatLong(35.1283, -80.8772, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1305, -80.8761, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1311, -80.8759, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1317, -80.8758, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1325, -80.8758, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1330, -80.8758, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1335, -80.8759, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1356, -80.8764, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1425, -80.8778, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1459, -80.8786, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1464, -80.8786, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1471, -80.8786, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1476, -80.8786, 0, VEAltitudeMode.RelativeToGround),

            new VELatLong(35.1482, -80.8785, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1503, -80.8780, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1525, -80.8776, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1549, -80.8771, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1555, -80.8770, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1560, -80.8770, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1571, -80.8770, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1580, -80.8771, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1584, -80.8772, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1595, -80.8775, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1598, -80.8776, 0, VEAltitudeMode.RelativeToGround),
            
            new VELatLong(35.1601, -80.8776, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1606, -80.8776, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1632, -80.8776, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1650, -80.8775, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1655, -80.8776, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1659, -80.8776, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1703, -80.8779, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1709, -80.8780, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1714, -80.8781, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1717, -80.8782, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1749, -80.8789, 0, VEAltitudeMode.RelativeToGround),
            
            new VELatLong(35.1760, -80.8792, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1770, -80.8794, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1778, -80.8795, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1783, -80.8796, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1786, -80.8795, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1791, -80.8795, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1798, -80.8794, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1825, -80.8789, 0, VEAltitudeMode.RelativeToGround),
            
            new VELatLong(35.1839, -80.8786, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1845, -80.8783, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1852, -80.8781, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1859, -80.8777, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1865, -80.8775, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1904, -80.8753, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1951, -80.8726, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1960, -80.8721, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1962, -80.8720, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1967, -80.8716, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.1975, -80.8709, 0, VEAltitudeMode.RelativeToGround),  
            
            new VELatLong(35.2032, -80.8663, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2091, -80.8615, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2120, -80.8591, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2137, -80.8577, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2141, -80.8574, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2145, -80.8570, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2147, -80.8567, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2149, -80.8564, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2157, -80.8552, 0, VEAltitudeMode.RelativeToGround), 
            
            new VELatLong(35.2188, -80.8508, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2209, -80.8478, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2217, -80.8463, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2225, -80.8451, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2231, -80.8440, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2232, -80.8438, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2245, -80.8420, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2253, -80.8409, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2261, -80.8399, 0, VEAltitudeMode.RelativeToGround),

            new VELatLong(35.2262, -80.8397, 0, VEAltitudeMode.RelativeToGround),
            new VELatLong(35.2276, -80.8377, 0, VEAltitudeMode.RelativeToGround)
            );        
    myPolygon = new VEShape(VEShapeType.Polyline, points);
    myPolygon.SetLineWidth(6);
    myPolygon.HideIcon();
    myPolygon.SetMinZoomLevel(8);
    map.AddShape(myPolygon);
}

//formats the station pushpins
function onStationsFeedLoad(feed) {
    if (feed != null) {
        var numShapes = feed.GetShapeCount();
        for (var i = 0; i < numShapes; ++i) {
            try {
                var shape1 = feed.GetShapeByIndex(i);
                var shapeHTML = shape1.GetDescription();
                var latlong = shape1.GetPoints()[0];
                var lat = latlong.Latitude;
                var lon = latlong.Longitude;

                shapeHTML = shapeHTML + "<br /><hr /><a onclick='JavaScript:DoZoom(" + lat + "," + lon + ") ;' style='background-color:#BBC423; cursor:pointer; color:black; padding:3px;border: solid 1px black;'>Zoom to this station</a>";
                var sImage = "";

                sImage = sImage + "<br />";
                shape1.SetDescription("<div style='z-index:5000;'>" + sImage + shapeHTML + "</div>");
            }
            catch (e) {

            }
        }
    }
}

//imports Stations from the data feed.
function AddStations(map, bestlocation) {
    var shapeSource = new VEShapeSourceSpecification(VEDataType.ImportXML, 'http://www.downtownlightrail.com/light-rail-maps/StationFeed.ashx?lineId=11', oStationShapeL);
    shapeSource.MaxImportedShapes = 500;
    map.ImportShapeLayerData(shapeSource, onStationsFeedLoad, bestlocation);
}

function DoZoom(lat, lon) {
    map.SetCenterAndZoom(new VELatLong(lat, lon), 19);
    map.SetMapStyle(VEMapStyle.Hybrid);
}

//formats the pushpins for the park-n-rides
function onParkRideLocFeedLoad(feed) {
    if (feed != null) {
        var numShapes = feed.GetShapeCount();
        for (var i = 0; i < numShapes; ++i) {
            try {
                var shape = feed.GetShapeByIndex(i);
                shape.SetCustomIcon("http://www.downtownlightrail.com/light-rail-maps/images/car.gif");
            }
            catch (e) {

            }
        }
    }
}

//gets the park and ride locations and loads them in the shapelayer. 
function LoadParkRideLoc() {
    //    var shapeSource = new VEShapeSourceSpecification (VEDataType.ImportXML, 'http://www.downtownlightrail.com/light-rail-maps/Parking.ashx?lineId=11', oPRShapeL);
    //    map.ImportShapeLayerData(shapeSource, onParkRideLocFeedLoad, 0);
}

//function Resize()
//{
//    var mapDiv = document.getElementById("myMap");
//    var sqlTXT = document.getElementById("txtSQL");
//    var accordion = document.getElementById("Accordion1");
//    var windowWidth = document.body.clientWidth;
//    var windowHeight = document.body.clientHeight;
//    mapWidth = windowWidth - 305;
//    mapHeight = windowHeight  - 155;
//    mapDiv.style.width = mapWidth + "px";
//    mapDiv.style.height = mapHeight + "px";
//    sqlTXT.style.width = mapWidth - 175 + "px";
//    accordion.style.height = (windowHeight - 45) + "px";
//    map.Resize(mapWidth, mapHeight);
//    map.ShowMiniMap(mapWidth-205, 13, VEMiniMapSize.Large);
//}

//This function is used to hide and show the station shape layer after it is loaded.
function ToggleStationLayer() {
    if (oStationShapeL.GetVisibility()) {
        oStationShapeL.Hide();
    }
    else {
        oStationShapeL.Show();
    }
}

//This function is used to hide and show the park-n-ride shape layer after it is loaded.
function ToggleParknRideLayer() {
    if (oPRShapeL.GetVisibility()) {
        oPRShapeL.Hide();
    }
    else {
        oPRShapeL.Show();
    }
}

//Toggles traffic off and on.
function ToggleTraffic() {
    if (bTrafficLoaded) {
        bTrafficLoaded = false;
        map.HideTrafficLegend();
        map.ClearTraffic();
    }
    else {
        bTrafficLoaded = true;
        map.ShowTrafficLegend(20, 470);
        map.SetTrafficLegendText("Traffic");
        map.LoadTraffic(bTrafficLoaded);
    }
}

//Does the find functionality - yellow book find. bNext = -1 means a new search.
function DoFind(numResults, bNext) {
    try {
        var what = document.getElementById('txtWhat').value;
        findLayer.DeleteAllShapes();
        findLayer = new VEShapeLayer();

        map.AddShapeLayer(findLayer);

        if (bNext == -1) {
            index = 0;
        }
        else if (bNext != 1) {
            index = parseInt(index) - (numResults * 2)
        }

        if (index < 0) index = 0;
        if (numResults > 20) numResults = 20;
        results = map.Find(what,
                          null,
                          null,
                          findLayer,
                          index,
                          numResults,
                          true,
                          true,
                          true,
                          true,
                          MoreResults);
        index = parseInt(index) + numResults;
    }
    catch (e) {
        alert(e.message);
    }
}

function MoreResults(layer, resultsArray, places, hasMore, veErrorMessage) {
    var r = "";
    if (hasMore) {
        if (index > 10) {
            r = "<a href='#' onclick='javascript:DoFind(10,0);'>&lt;Prev</a>&nbsp;&nbsp;";
        }
        r = r + "<a href='#' onclick='javascript:DoFind(10,1);'>Next></a><hr />";
    }
    else if (index < 10) {
        r = "<a href='#' onclick='javascript:DoFind(10,1);'>Next></a><hr />";
    }
    else {
        if (index > 9) {
            r = "<a href='#' onclick='javascript:DoFind(10,0);'>&lt;Prev</a>";
        }
    }

    document.getElementById('results').innerHTML = r;
    var resList = "";

    if (layer != null) {
        var numShapes = layer.GetShapeCount();
        for (var i = 0; i < numShapes; ++i) {
            var x = i + 1
            var shape = layer.GetShapeByIndex(i);
            shape.SetCustomIcon("http://www.downtownlightrail.com/light-rail-maps/images/Black" + x + ".gif");

            try {
                var fName = resultsArray[i].Name;
                var fDescrip = resultsArray[i].Description;
                var fPhone = resultsArray[i].Phone;
            }
            catch (e) {

            }
            resList = resList + "<img src='http://www.downtownlightrail.com/light-rail-maps/images/Black" + x + ".gif'</img>" + fName + "<br />" + fDescrip + "<br />" + fPhone + "<br /><hr />";
        }
    }
    document.getElementById('resultslist').innerHTML = resList;
}

function hideFindDiv() {
    //hide the find part of the control
    if (document.getElementById) { // DOM3 = IE5, NS6 
        document.getElementById('findControl').style.visibility = 'hidden';
    }
    else {
        if (document.layers) { // Netscape 4 
            document.findControl.visibility = 'hidden';
        }
        else { // IE 4 
            document.all.findControl.style.visibility = 'hidden';
        }
    }

    //hide the results part of the control
    if (document.getElementById) { // DOM3 = IE5, NS6 
        document.getElementById('results').style.visibility = 'hidden';
    }
    else {
        if (document.layers) { // Netscape 4 
            document.results.visibility = 'hidden';
        }
        else { // IE 4 
            document.all.results.style.visibility = 'hidden';
        }
    }
}

function showFindDiv() {
    //show the find part of the control
    if (document.getElementById) { // DOM3 = IE5, NS6 
        document.getElementById('findControl').style.visibility = 'visible';
    }
    else {
        if (document.layers) { // Netscape 4 
            document.findControl.visibility = 'visible';
        }
        else { // IE 4 
            document.all.findControl.style.visibility = 'visible';
        }
    }

    //show the results part of the control
    if (document.getElementById) { // DOM3 = IE5, NS6 
        document.getElementById('results').style.visibility = 'visible';
    }
    else {
        if (document.layers) { // Netscape 4 
            document.results.visibility = 'visible';
        }
        else { // IE 4 
            document.all.results.style.visibility = 'visible';
        }
    }
} 


