﻿var wazeHazard = "images/wazetraffic/hazard.png";
var wazeOther = "images/wazetraffic/other.png";
var wazeAccident = "images/wazetraffic/accident.png";
var wazeTrafficJam = "images/wazetraffic/trafficjam.png";
var wazeCop = "images/wazetraffic/cop.png";

var wazeWidget = null;

var wazePushPingWidth = 34;
var wazePushPingHeight = 44;

function GoTrafficEvents() {
    
    var a = null;

    if (!_mapTrafficEvents.visible) {        
        dojo.removeClass("btnWaze", "btnWaze");
        dojo.addClass("btnWaze", "btnWaze_over");
        _mapTrafficEvents.show();
        _mapTraffic.show();

        /*
        if(wazeWidget==null)
            wazeWidget = new lw.Traffic("wazeWidget");

        a = document.getElementById("wazeContainer");
        a.style.display = "block";*/


        $("#trafficLegend").show(0, function () {
            $("#trafficLegend").css("opacity", 1);
            $("#trafficLegend").position({
                of: $("#content"),
                my: "center center",
                at: "center bottom",
                collision: "none",
                offset: "30 -30"
            });
        });
        setTimeout('HideTrafficLegend()', 30000);


    }
    else {
        dojo.removeClass("btnWaze", "btnWaze_over");
        dojo.addClass("btnWaze", "btnWaze");
        _mapTrafficEvents.hide();
        _mapTraffic.hide();
        $("#trafficLegend").hide();
        /*a = document.getElementById("wazeContainer");
        a.style.display = "none";*/

    }
    return false;
}

function DoTheToggle() {    

    var s = dojo.byId("wazeWidget");
    if (s.style.display == "none") {        
        s.style.display = "block";
    }
    else
        s.style.display = "none";

}

function GoTraffic(c) {
    if (c) {
        _mapTraffic.show();        
    }
    else {
        _mapTraffic.hide();        
    }
    return false;
}

function LoadWazeEvents() {
    PageMethods.GetWazeEvents(ProcessWazeEventsResult);
    ShowLoading();
}

function ProcessWazeEventsResult(result) {
    _mapTrafficEvents.clear();

    if (result.length > 0) {
        var points = result.split("|");

        for (j = 0; j < points.length; j++) {
            var z = points[j].split(':')
            var x = z[0].split(',');
            var f = z[1].split('*');
            var t = f[0];
            var u = f[1];
            var s = new esri.symbol.PictureMarkerSymbol(getWazePushPing(t), wazePushPingWidth, wazePushPingHeight);
            var point = new esri.geometry.Point(parseFloat(x[1]), parseFloat(x[0]), _map.spatialReference);
            point = GetConvertedPoint(point);
            var attr = { "eventType": getTraficcEventType(t), "location": u, "time": f[2] };
            var g = new esri.Graphic(point, s);
            g.attributes = attr;
            _mapTrafficEvents.add(g);
        }
    }
    dojo.connect(_mapTrafficEvents, "onMouseMove", OnMouseOverTrafficEvent);
    dojo.connect(_mapTrafficEvents, "onMouseOut", OnMouseOutTrafficEvent);
    HideLoading();

}
function OnMouseOutTrafficEvent(event) {
    dojo.style("infoTrafficEvent", {
        "display": "none"
    });
}
function OnMouseOverTrafficEvent(event) {

    var g = event.graphic;

    var infoTrafficEvent = dojo.byId("infoTrafficEvent");

    infoTrafficEvent.innerHTML = "<b>" + g.attributes["eventType"] + "</b>" + ( g.attributes["location"] != "" ? "<br/>" + g.attributes["location"] : "" ) + "<br/>Hace: " + g.attributes["time"] + " minutos";

    dojo.style("infoTrafficEvent", {
        "display": "block",
        "position": "absolute",
        "top": event.clientY + 10 + "px",
        "left": event.clientX - 150 + "px"
    });    

}

function getTraficcEventType(t) {
    switch (t) {
        case "JAM":
            return "Embotellamiento";
        case "ACCIDENT":
            return "Accidente";
        case "WEATHERHAZARD":
            return "Peligro";
        case "POLICEMAN":
            return "Policía";
        case "OTHER":
            return "Chit Chat";
        default:
            return "Otro";
    }
}

function getWazePushPing(t) {
    switch (t) {
        case "JAM":
            return wazeTrafficJam;
        case "ACCIDENT":
            return wazeAccident;
        case "WEATHERHAZARD":
            return wazeHazard;
        case "POLICEMAN":
            return wazeCop;
        case "OTHER":
            return wazeOther;
        default:
            return wazeOther;
    }
}

function LoadWazeTraffic() {

    PageMethods.GetWazeTraffic(ProcessWazeTrafficResult);
    ShowLoading();
}

function ProcessWazeTrafficResult(result) {
    _mapTraffic.clear();

    for (i = 0; i < result.length; i++) {
        var line = new esri.geometry.Polyline(_map.spatialReference);
        line.addPath(getPath(result[i].Line));

        var w = (result[i].StartNode == "" || result[i].EndNode == "") ? (result[i].EndNode == "" ? (result[i].StartNode == "" ? (result[i].City == "" ? "Tráfico " + GetTrafficSeverity(result[i].Severity) : "Tráfico " + GetTrafficSeverity(result[i].Severity) + " en " + result[i].City) : "Tráfico " + GetTrafficSeverity(result[i].Severity) + " en " + result[i].StartNode) : "Tráfico " + GetTrafficSeverity(result[i].Severity) + " en " + result[i].EndNode) : ("Tráfico " + GetTrafficSeverity(result[i].Severity) + " entre " + result[i].StartNode + (result[i].EndNode.substr(0, 1).toUpperCase() == "I" ? " e " : " y ") + result[i].EndNode);

        var attr = { "where": w, "velocity": result[i].Speed, "street": result[i].Street };
        var s = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, GetTrafficColor(result[i].Severity), 5);
        line.attributes = attr;
        _mapTraffic.add(new esri.Graphic(GetConvertedPoint(line), s)).setAttributes(attr);

    }

    dojo.connect(_mapTraffic, "onMouseMove", OnMouseOverTraffic);
    dojo.connect(_mapTraffic, "onMouseOut", OnMouseOutTraffic);
    HideLoading();

}

function GetTrafficColor(s) {
    switch (s) {
        case 1:
            return new dojo.Color([100, 100, 100]);
        case 2:
            return new dojo.Color("#C43100");
        case 3:
            return new dojo.Color([255, 0, 0]);
        case 4:
            return new dojo.Color([0, 0, 0]);
        default:
            return new dojo.Color([200, 200, 200]);
    }   
}

function GetTrafficSeverity(s) {
    switch(s)
    {
        case 1:
            return "Bajo";
        case 2:
            return "Moderado";
        case 3:
            return "Alto";
        case 4:
            return "Detenido";
        default:
            return "";
    }
}

function OnMouseOutTraffic(event) {
    dojo.style("infoTraffic", {
        "display": "none"
    });
}

function OnMouseOverTraffic(event) {
    var g = event.graphic;

    var infoTraffic = dojo.byId("infoTraffic");

    infoTraffic.innerHTML = "<b>" + g.attributes["where"] + "</b>" + (g.attributes["street"] == "" ? "" : "<br />" + g.attributes["street"]) + "<br/>Velocidad promedio: " + g.attributes["velocity"] + " km/h";

    dojo.style("infoTraffic", {
        "display": "block",
        "position": "absolute",
        "top": event.clientY + 10 + "px",
        "left": event.clientX + 10 + "px"
    });
}

function getPathOld(stream) {
    var coords = [];
    if (stream.length == 0)
        return coords;
    var points = stream.split("|");
    for (ic = 0; ic < points.length - 1; ic++) {
        var p = points[ic].split(",");
        coords.push([p[1], p[0]]);
    }
    return coords;
}

function getPath(stream) {
    var coords = [];
    if (stream.length == 0)
        return coords;
    var points = stream.split(" ");
    for (ic = 0; ic < points.length; ic=ic+2) {
        //var p = points[ic].split(",");
        coords.push([points[ic+1], points[ic]]);
    }
    return coords;
}
