﻿var intersectTimer = 0;

function cleanIntersectionLists(cleanPrimaryStreets, cleanCrossStreets)
{
    if (cleanPrimaryStreets) dojo.byId("selPrimaryStreets").options.length = 0;
    if (cleanCrossStreets) dojo.byId("selCrossStreets").options.length = 0;
}

function getIntersectionStreets(street) {
    //clear out the timer
    clearTimeout(intersectTimer);

    if (street.length < 3) {
        cleanIntersectionLists(true,true);
        return;
    }  
    
    //Don't fire the function until the user has paused for half a second
    //this should help eliminate needless trips back to the server
    intersectTimer = setTimeout(function() {
        //clear all street lists
        cleanIntersectionLists(true,true);

        //show intersection busy
        showIntersectionBusy('PRIMARY');
        
        var params = {"prefixText":'"' + street.toUpperCase() + '"'}
        dojo.xhrGet(
        {
            url: "./wsSearches.asmx/GetStreetNames",
            handleAs: "json",
            contentType: "application/json; charset=utf-8",
            content: params,
            load: OnStreetComplete,
            error: function(error,args){console.warn("error!",error);}
        });    
    }, 800);
}

function getCrossStreets()
{
    //remove the existing list of cross streets
    cleanIntersectionLists(false,true);

    //show intersection busy
    showIntersectionBusy('CROSS');

    //get the selected value from the list box and update the text box
    var street = dojo.byId("selPrimaryStreets").value;
    dojo.byId("txtPrimaryStreet").value = street;
    
    //build the parameter to pass to the web service
    var params = {"streetName":'"' + street + '"'}
    
    //call the webservice to get the data
    dojo.xhrGet(
    {
        url: "./wsSearches.asmx/GetCrossStreets",
        handleAs: "json",
        contentType: "application/json; charset=utf-8",
        content: params,
        load: OnCrossStreetsComplete,
        error: function(error,args){console.warn("error!",error);}
    });            
}

function OnStreetComplete(results, ioArgs) {
    var newResults = dojo.fromJson(results);
    var lBox = dojo.byId("selPrimaryStreets");

    for (var i=0, il=newResults.length; i<il; i++) {
        var street = newResults[i];
        if (street != null) {
            var y=document.createElement('option');
            y.text=street;
            y.value=street;
            lBox.add(y);
        }
    }
    
    hideIntersectionBusy();               
}

function OnCrossStreetsComplete(results, ioArgs) {    
    var newResults = dojo.fromJson(results);
    var selBox = document.getElementById('selCrossStreets')

    for (var i=0, il=newResults.length; i<il; i++) {
        var crossStreet = newResults[i];
        var nextCrossStreet;
        if (i < il - 1) {
            nextCrossStreet = newResults[i + 1];
        } else {
            nextCrossStreet = null;
        }
        
        if (crossStreet != null) {
            //"{"STREET1":"FOLEY DR","STREET2":"BURBANK CIR","INSTANCE":1,"X_COORD":2582446.5042809844,"Y_COORD":636306.505990386,"POINT":"2582446.50428098:636306.505990386"}
            var y=document.createElement('option');

            //if we have more than one instance of this intersection
            //for example a street that loops back to the primary street
            //then add the instance number to the description
            if (nextCrossStreet != null) {
                if (crossStreet.STREET2 == nextCrossStreet.STREET2) {
                    y.text=crossStreet.STREET2 + " #" + crossStreet.INSTANCE;
                } else {
                    if (crossStreet.INSTANCE > 1) {
                        y.text=crossStreet.STREET2 + " #" + crossStreet.INSTANCE;
                    } else {
                        y.text=crossStreet.STREET2;
                    }
                }
            } else {
                if (crossStreet.INSTANCE > 1) {
                    y.text=crossStreet.STREET2 + " #" + crossStreet.INSTANCE;
                } else {
                    y.text=crossStreet.STREET2;
                }                    
            }
            y.value= crossStreet.STREET1 + "|" + crossStreet.STREET2 + "|" + crossStreet.X_COORD + "|" + crossStreet.Y_COORD;
            selBox.add(y);
        }        
    } 
    
    hideIntersectionBusy();  
}

function checkIntersectionData() {
    //debugger;
    if (document.getElementById('selCrossStreets').selectedIndex < 0) {
        alert("Please pick a cross street.");
        return false;
    }
}

function mapIntersection() {
    map.graphics.clear();
    
    var intParams = document.getElementById('selCrossStreets').value.split("|");
    var intersection = intParams[0];

    var intersectionText = intParams[0] + " & " + intParams[1];
    var x = parseInt(intParams[2],10);
    var y = parseInt(intParams[3],10);
            
    //create the point graphics for the map
    var point =  new esri.geometry.Point(x, y, map.spatialReference);
    var lineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0,1]), 2);
    var pointSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_X, 10, lineSymbol, new dojo.Color([255,0,0,1]))
    var graphic = new esri.Graphic(point, pointSymbol);
    map.graphics.add(graphic);

    //create the text graphics for the map
    var intText = new esri.symbol.TextSymbol(intersectionText, "Tahoma", new dojo.Color([255,0,0,1]));
    intText.setOffset(8,0);
    intText.setAlign(esri.symbol.TextSymbol.ALIGN_START);
    map.graphics.add(new esri.Graphic(point, intText));
    
    //set the map extent
    var env = new esri.geometry.Extent(x - 500, y - 500, x + 500, y + 500, map.spatialReference);
    map.setExtent(env);    
}

function showIntersectionBusy(searchingWhat) {
    var x = dojo.byId("divIntersectionBusy");
    switch (searchingWhat.toUpperCase()) {
        case 'PRIMARY':
            x.style.left = "17px";
            x.style.top = "84px";
            x.style.visibility = "visible";
            x.style.display = "block";
            break;
        case 'CROSS':
            x.style.left = "238px";
            x.style.top = "60px";
            x.style.visibility = "visible";
            x.style.display = "block";
            break;
    }
}

function hideIntersectionBusy() {
    var x = dojo.byId("divIntersectionBusy");
    x.style.visibility = "hidden";
    x.style.display = "none";    
}
