<?xml version="1.0" encoding="UTF-8" ?> 
<Module>

    <ModulePrefs
                title="Atac mobile"
                title_url="http://atacmobile.it/"
                author="Atac SpA"
                author_email="atacmobile+nospam@gmail.com"
                author_affiliation="Atac SpA"
                author_location="Rome, Italy"
                screenshot="http://atac.oltrelinux.com/screenshot.png"
                thumbnail="http://atac.oltrelinux.com/thumbnail.png"
                description="Mappa del traffico e della mobilit&#224; a Roma. Contenuti forniti dal servizio atacmobile.it. Tempi di percorrenza (sistema UTT)"
    > 
    <Require feature="sharedmap"/>
    </ModulePrefs>

    
    <Content type="html">
    
        <![CDATA[ 
        
        <div id="content_div"></div>

        <script type="text/javascript">
        
        var map = new GMap2();
        var listaTratte = [];
        var listaPannelli = [];
        map.setCenter(new GLatLng(41.89747,12.49866), 12);
        
        var prefix = "http://atac.oltrelinux.com/";
        
        
        var panelIcon = new GIcon(G_DEFAULT_ICON);
        panelIcon.image = prefix + "pannello.png";
        panelIcon.iconSize = new GSize(32, 32);
        
        var parkIcon = new GIcon(G_DEFAULT_ICON);
        parkIcon.image = prefix + "parcheggio.png";
        parkIcon.iconSize = new GSize(16, 16);
        parkIcon.shadow = prefix + 'null.png';
        parkIcon.shadowSize = new GSize(1,1);
        
        var tvccIconOn = new GIcon(G_DEFAULT_ICON);
        tvccIconOn.image = prefix + "tvcc-on.png";
        tvccIconOn.iconSize = new GSize(16, 16);
        tvccIconOn.shadow = prefix + 'null.png';
        tvccIconOn.shadowSize = new GSize(1,1);
        
        var tvccIconOff = new GIcon(G_DEFAULT_ICON);
        tvccIconOff.image = prefix + "tvcc-off.png";
        tvccIconOff.iconSize = new GSize(16, 16);
        tvccIconOff.shadow = prefix + 'null.png';
        tvccIconOff.shadowSize = new GSize(1,1);
        
        var bikeIcon = new GIcon(G_DEFAULT_ICON);
        bikeIcon.image = prefix + "bike.png";
        bikeIcon.iconSize = new GSize(16, 16);
        bikeIcon.shadow = prefix + 'null.png';
        bikeIcon.shadowSize = new GSize(1,1);
        
        markerTempiOptions = { icon:panelIcon };
        
        markerParkOptions = { icon:parkIcon };
        
        markerBikeOptions = { icon:bikeIcon };
        
        function displayMenu() { 
            // XML data
            var url = "http://atac.oltrelinux.com/tratte-geo.xml"; 
            var prefs = new _IG_Prefs();
            
            var lstTratte = new Array();
            
            _IG_FetchXmlContent(url, function (response) {
                if (response == null || typeof(response) != "object" || response.firstChild == null) {
                    //_gel('content_div').innerHTML = 'none: '+ response.firstChild;
                    return;
                }
                
                //_gel('content_div').innerHTML = 'ok';
                
                tratte = response.getElementsByTagName("tratta");
                
                //_gel('content_div').innerHTML = 'tratte.length: ' + tratte.length + '<br/>';

                for (var i = 0; i < tratte.length ; i++) {
                
                    var nodeList = tratte.item(i).childNodes;
                    var lstNodi = new Array();
                    
                    // Loop through child nodes. Extract data from the text nodes that are
                    // the children of the associated name, price, and calories element nodes.
                    //_gel('content_div').innerHTML = 'l: ' + nodeList;
                    for (var j = 0; j < nodeList.length ; j++) {
                        if(nodeList[j].nodeName == 'punto') {
                            //_gel('content_div').innerHTML += nodeList[j].getAttribute("x");
                            node = nodeList.item(j);
                            x = node.getAttribute("x");
                            y = node.getAttribute("y");
                            
                            lstNodi.push(new GLatLng(x, y));
                            if(nodeList[j].getAttribute("extra") == "pannello") {
                                listaPannelli.push({"geom": new GLatLng(x,y), "id": tratte[i].getAttribute("id")});
                            }
                        }
                    }
                    
                    // Aggiunge i nodi alle tratte
                    lstTratte.push({ "id": tratte[i].getAttribute("id"), "geom": lstNodi, "width": tratte[i].getAttribute("width"), "nome": tratte[i].getAttribute("nome"), "da": tratte[i].getAttribute("da"), "a": tratte[i].getAttribute("a")});
                }
                
                for(i in lstTratte) {
                    //_gel('content_div').innerHTML += "i: " + i + "<br/>";
                    map.addOverlay(lstTratte[i]["geom"]);
                }
                
                listaTratte = lstTratte;
                
                aggiorna_dati();
                
            }, {refreshInterval: 0} ); 

        }
        
        function createMarkerTempi(point, markerOpts, nome, tempo, los) {
            var marker = new GMarker(point, markerOpts);
            var txt = '<strong>Tempi di Percorrenza</strong><br/>' + nome + '<br/>';
            if(tempo != 'non disponibile') {
                txt += 'Tempo stimato <strong>' + tempo + ' minuti</strong>, ' + los + '<br/>';    
            } else {
                txt += '<strong>Dati non disponibili</strong></br>';
            }
            
            txt += '<br/><div style="float: left;">';
            txt += '<a href="http://atacmobile.it"><img src="http://atac.oltrelinux.com/logo.jpg"/></a></div>';
            txt += '<div style="margin-left: 150px; text-align: right"><a href="http://twitter.com/atacmobile" style="display: block"><img src="http://atac.oltrelinux.com/twitter-logo.png"/></a>';
            txt += '<a href="http://www.facebook.com/pages/Atac-mobile/45475421324"><img src="http://atac.oltrelinux.com/facebook.gif"/></a>';
            txt += '</div>';
            
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(txt);
            });
            return marker;
        }
        
        function createMarkerPark(point, markerOpts, nome, posti, posti_totali) {
            var marker = new GMarker(point, markerOpts);
            var txt = '<strong>Parcheggio ' + nome + '</strong><br/>';
            if(posti == 'fuori servizio') {
                txt += 'Fuori servizio<br/>';
            } else if(posti != 'non disponibile') {
                txt += 'Posti disponibili <strong>' + posti + '</strong> su <strong>' + posti_totali + '</strong><br/>';    
            } else {
                txt += '<strong>Dati non disponibili</strong></br>';
            }
            
            txt += '<br/><div style="float: left;">';
            txt += '<a href="http://atacmobile.it"><img src="http://atac.oltrelinux.com/logo.jpg"/></a></div>';
            txt += '<div style="margin-left: 150px; text-align: right"><a href="http://twitter.com/atacmobile" style="display: block"><img src="http://atac.oltrelinux.com/twitter-logo.png"/></a>';
            txt += '<a href="http://www.facebook.com/pages/Atac-mobile/45475421324"><img src="http://atac.oltrelinux.com/facebook.gif"/></a>';
            txt += '</div>';
            
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(txt);
            });
            return marker;
        }
        
        function createMarkerTvcc(point, nome, id, pubblicabile, orario) {
            
            var marker = '';
            
            if(pubblicabile == 1)
                marker = new GMarker(point, { icon:tvccIconOn });
            else
                marker = new GMarker(point, { icon:tvccIconOff });
            var txt = '<div style="float: left"><strong>' + nome + '</strong></div>';
            txt += '<div style="margin-left: 200px; padding-right: 10px; text-align: right">' + orario + '</div><br/>';
            
            txt += '<img style="margin-top: 5px" src="' + prefix + id + '.jpg"/><br/>';
            
            txt += '<br/><div style="float: left;">';
            txt += '<a href="http://atacmobile.it"><img src="http://atac.oltrelinux.com/logo.jpg"/></a></div>';
            txt += '<div style="margin-left: 150px; text-align: right"><a href="http://twitter.com/atacmobile" style="display: block"><img src="http://atac.oltrelinux.com/twitter-logo.png"/></a>';
            txt += '<a href="http://www.facebook.com/pages/Atac-mobile/45475421324"><img src="http://atac.oltrelinux.com/facebook.gif"/></a>';
            txt += '</div>';
            
            if(pubblicabile == 1) {
                GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml(txt);
                });
            }
            return marker;
        }
        
        function createMarkerBike(point, nome, disponibili, totali) {
            
            var marker = '';
            
            marker = new GMarker(point, { icon:bikeIcon });
            
            var txt = '<strong>' + nome + '</strong><br/>';
                        
            txt += 'Bici disponibili: <strong>' + disponibili + '</strong> su <strong>' + totali + '</strong><br/>';   
                        
            txt += '<br/><div style="float: left;">';
            txt += '<a href="http://atacmobile.it"><img src="http://atac.oltrelinux.com/logo.jpg"/></a></div>';
            txt += '<div style="margin-left: 150px; text-align: right"><a href="http://twitter.com/atacmobile" style="display: block"><img src="http://atac.oltrelinux.com/twitter-logo.png"/></a>';
            txt += '<a href="http://www.facebook.com/pages/Atac-mobile/45475421324"><img src="http://atac.oltrelinux.com/facebook.gif"/></a>';
            txt += '</div>';
            
            GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml(txt);
            });
            
            return marker;
        }

       
        function aggiorna_dati() {
        
            map.clearOverlays();
            
            // Tempi di Percorrenza
        
            var url = "http://atac.oltrelinux.com/tratte-tempi.xml";
            _IG_FetchXmlContent(url, callback_tempi, { refreshInterval: 0});
            function callback_tempi (response) {
            
                if (response == null || typeof(response) != "object" || response.firstChild == null) {
                    //_gel('content_div').innerHTML = 'none';
                    return;
                }
            
                //_gel('content_div').innerHTML += 'OK';
                
                // Associazione colori/los
                var los = new Array();
                nlsLos = response.getElementsByTagName("los");
                for(var i=0; i < nlsLos.length; i++) {
                    var loslevel = nlsLos.item(i).getAttribute("livello");
                    //los[loslevel]["colore"] = nlsLos.item(i).getAttribute("colore");
                    //los[loslevel]["descrizione"] = nlsLos.item(i).getAttribute("descrizione");
                    var propsArray = {"colore": nlsLos.item(i).getAttribute("colore"), "descrizione": nlsLos.item(i).getAttribute("descrizione")};
                    los[loslevel] = propsArray;
                }
                
                // Tematismi
                nlsTratte = response.getElementsByTagName("tratta");
                
                //_gel('content_div').innerHTML += 'nlsTratte: ' + nlsTratte.length + '<br/>';
                for(var i = 0; i < nlsTratte.length; i++) {
                    var id = nlsTratte.item(i).getAttribute("id");
                    var loslevel = nlsTratte.item(i).getAttribute("los");
                    var loscolor = los[loslevel]["colore"];
                    
                    for(var j = 0; j < listaTratte.length; j++) {
                        if(id == listaTratte[j]["id"]) {
                            listaTratte[j]["tempo"] = nlsTratte.item(i).getAttribute("tempo");
                            listaTratte[j]["los"] = los[nlsTratte.item(i).getAttribute("los")]["descrizione"];
                            map.addOverlay(new GPolyline(listaTratte[j]["geom"], loscolor, listaTratte[j]["width"], 0.7) );
                        }
                    }
                    
                    
                    //_gel('content_div').innerHTML += 'tratte: ' + tratte.length + '<br/>';
                    
                }
                
                //_gel('content_div').innerHTML = '';
                for(j=0; j < listaTratte.length; j++) {
                    for(k=0; k < listaPannelli.length; k++) {
                        //_gel('content_div').innerHTML += 'tratte[j]["id"]: ' + tratte[j]["id"] + 'listaPannelli[k]["id"]: ' + listaPannelli[k]["id"] + '<br/>';
                        if( listaTratte[j]["id"] == listaPannelli[k]["id"] ) {
                            var nomeTratta = listaTratte[j]["nome"];
                            var losId = listaTratte[j]["los"];
                            map.addOverlay(createMarkerTempi(listaPannelli[k]["geom"], markerTempiOptions, nomeTratta, listaTratte[j]["tempo"], listaTratte[j]["los"]));
                        }
                    }
                }
            }
            
            // Parcheggi
            
            var url = "http://atac.oltrelinux.com/parcheggi.xml";
            _IG_FetchXmlContent(url, callback_park, { refreshInterval: 0});
            function callback_park (response) {
            
                if (response == null || typeof(response) != "object" || response.firstChild == null) {
                    //_gel('content_div').innerHTML = 'none';
                    return;
                }
                
                parks = response.getElementsByTagName("parcheggio");
                
                for(var i=0; i<parks.length; i++) {
                    point = new GLatLng(parks.item(i).getAttribute("x"), parks.item(i).getAttribute("y"));
                    nome = parks.item(i).getAttribute("nome");
                    posti = parks.item(i).getAttribute("posti");
                    posti_totali = parks.item(i).getAttribute("posti_totali");
                    map.addOverlay(createMarkerPark(point, markerParkOptions, nome, posti, posti_totali));
                }
            }
            
            // Telecamere
            
            url = prefix + "tvcc.xml";
            _IG_FetchXmlContent(url, callback_tvcc, { refreshInterval: 0});
            function callback_tvcc (response) {
            
                if (response == null) {
                    alert("tvcc null");
                    return;
                }
                
                tvccs = response.getElementsByTagName("telecamera");
                
                for(var i=0; i<tvccs.length; i++) {
                    point = new GLatLng(tvccs.item(i).getAttribute("x"), tvccs.item(i).getAttribute("y"));
                    nome = tvccs.item(i).getAttribute("nome");
                    id = tvccs.item(i).getAttribute("id");
                    pubblicabile = tvccs.item(i).getAttribute("pubblicabile");
                    orario = tvccs.item(i).getAttribute("orario");
                    map.addOverlay(createMarkerTvcc(point, nome, id, pubblicabile, orario));
                }
            }
            
            // Bike sharing
            
            url = prefix + "bike.xml";
            _IG_FetchXmlContent(url, callback_bike, { refreshInterval: 0});
            function callback_bike (response) {
            
                if (response == null) {
                    alert("bike null");
                    return;
                }
                
                stallo = response.getElementsByTagName("stallo");
                
                for(var i=0; i<stallo.length; i++) {
                    point = new GLatLng(stallo.item(i).getAttribute("y"), stallo.item(i).getAttribute("x"));
                    nome = stallo.item(i).getAttribute("nome");
                    bici_libere = stallo.item(i).getAttribute("bici_libere");
                    bici_totali = stallo.item(i).getAttribute("bici_totali");
                    map.addOverlay(createMarkerBike(point, nome, bici_libere, bici_totali));
                }
            }
            
            
        }

        _IG_RegisterOnloadHandler(displayMenu);
        
        window.setInterval("aggiorna_dati()", 120000);
        </script>
        ]]>
    
    </Content>
</Module>

