function isIE()
{
  if(document.all){
    return true;
  }else{
    return false;
  }
}

// Classe evento di traffico: utilizza google maps api versine 2

function eventoTraffico(map, dato)
{
    this.map = map;
    var ne = map.getBounds().getNorthEast();
    var sw = map.getBounds().getSouthWest();
    this.flag = '';
    this.marker = null;
    this.row1 = null;
    this.row2 = null;
    this.tooltip = null;
    this.dirn = null;
    this.dirp = null;
    this.situazione  = dato['F_ID'];     // id della situazione che identifica l'evento di traffico
    this.icona = dato['F_ICON'];         // icona dell'evento
    if (this.icona) {
        this.lat = dato['F_YCOORD_DA'].replace(',', '.');
        this.lng = dato['F_XCOORD_DA'].replace(',', '.');
    }
    this.breve = dato['F_TRADUZIONE_BREVE'];
    if (dato['F_ROADNUMBER']) {
        this.strada = dato['F_ROADNUMBER']+' '+dato['F_ROADNAME'];
    } else if (dato['F_ROADNAME']) {
        this.strada = dato['F_ROADNAME'];
    } else {
        this.strada = '';
    }
    this.dove = dato['DOVE'];
    this.dir = dato['F_DIR'];
    if (this.dir == null) this.dir='';
    if ((this.lng>=sw.lng()) && (this.lng<=ne.lng()) &&
        (this.lat>=sw.lat()) && (this.lat<=ne.lat())) {
        var clickable = (this.icona != 'null');
        this.creaMarker(clickable);             // oggetto marker sulla mappa Google
        this.creaSituazione(clickable);         // oggetto inserito nell'elenco (accordion)
        this.creaTooltip(clickable);            // oggetto tooltip da visualizzare quando si clicka l'icona
    } else if (this.strada == "NESSUNA SEGNALAZIONE PERVENUTA") {
        this.creaSituazione(false);
    }
    this.trattaColorata(dato);
}

eventoTraffico.prototype.scriviFumetto = function()
{
    var html = '<table class="fumetto" cellpadding="10px" cellspacing="0px" style="font-size:11px"><tbody>';
    html += '<tr style="background-color: rgb(230,230,230);"><td style="padding-left: 5px; padding-right: 5px; padding-bottom: 5px;><font color="#000080"><b>'+this.strada+'</b></font><br>'+this.dove+'</td>';
    html += '<td style="padding:10px;"><img src="/icone/28/'+this.icona+'.gif"></td></tr>';
    html += '<tr><td colspan=2 style="padding-top:10px;padding-bottom: 5px; padding-left: 5px; padding-right: 5px;">'+this.dir+'</td></tr>';
    html += '<tr><td colspan=2 style="padding: 5px;">'+this.breve+'</td></tr></tbody></table>';
    return html;
}

eventoTraffico.prototype.showFumetto = function()
{
    var point = new GLatLng(this.lat, this.lng);
    var evtHtml = this.scriviFumetto();
    this.map.openInfoWindowHtml(point, evtHtml);
}

eventoTraffico.prototype.creaMarker = function(clickable)
{
    if (!clickable) return;
    if (this.icona) {
        var point = new GLatLng(this.lat, this.lng);
        
        var blueIcon = new GIcon(G_DEFAULT_ICON);
        blueIcon.image = "/icone/28/"+this.icona+".gif";
        //blueIcon.shadow = '';
        blueIcon.iconSize = new GSize(20,20);
    
        markerOptions = {
            icon:blueIcon
        };
        this.marker = new GMarker(point, markerOptions);
        this.marker.value = this.situazione;
        var evtHtml = this.scriviFumetto();
        var evtMap = this.map;
        GEvent.addListener(this.marker, "click", function() {
            evtMap.openInfoWindowHtml(point, evtHtml);
        });
        this.map.addOverlay(this.marker);
    }
}

eventoTraffico.prototype.creaSituazione = function(clickable)
{
    var html = '';
    var tabEventi = elemento("tabEventi");

    this.row1 = tabEventi.insertRow(-1);
    this.row1.id = 'row'+this.situazione;
    cell1 = this.row1.insertCell(-1);
    //cell1.className = "textTd";
    if (isIE()) {
        cell1.style.color = "#52637A";
        if (clickable) cell1.style.cursor = "pointer";
    } else {
        html = 'color: rgb(82, 99, 122);';
        if (clickable) html += 'cursor: pointer;';
        cell1.setAttribute('style', html);
    }
    html = '<span class="strada"';
    if (clickable) html += ' onclick="apriFumetto('+this.situazione+');"';
    html += '>'+this.strada+'  </span><span class="dove">  '+this.dove+'</span>';
    cell1.innerHTML = html;
    cell2 = this.row1.insertCell(-1);
    if (isIE()) {
        cell2.style.width = "32px";
        if (clickable) cell2.style.cursor = "pointer";
    } else {
        html = 'width: 32px;';
        if (clickable) html += 'cursor: pointer;';
        cell2.setAttribute('style', html);
    }
    html = 'img class="image" style="width: 28px; height: 28px;" src="/icone/28/'+this.icona+'.gif"';
    if (clickable) html += 'onclick="apriFumetto('+this.situazione+');"';
    cell2.innerHTML = '<'+html+'>';
    //tabEventi.appendChild(this.row1);

    this.row2 = tabEventi.insertRow(-1);
    this.row2.id = 'sep'+this.situazione;
    if (isIE()) {
        this.row2.style.height = "6px";
    } else {
        this.row2.setAttribute('style',  "height: 6px;");
    }
    cell3 = this.row2.insertCell(0);
    if (isIE()) {
        cell3.colSpan = "2";
        cell3.style.textAlign = "center";
    } else {
        cell3.setAttribute('colspan' , "2");
        cell3.setAttribute('style', "text-align:center");
    }
    cell3.innerHTML = '<img src="/immagini/FieraMilano/separatore_eventi.png">';
    //tabEventi.appendChild(this.row2);
}

eventoTraffico.prototype.creaTooltip = function(clickable)
{
    // gia' creato in creaMarker
}

eventoTraffico.prototype.trattaColorata = function(dato)
{
    if (dato['DIR_N']) {
        this.dirn = new GGeoXml(dato['DIR_N']);
        this.map.addOverlay(this.dirn);
    }
    if (dato['DIR_P']) {
        this.dirp = new GGeoXml(dato['DIR_P']);
        this.map.addOverlay(this.dirp);
    }
}

eventoTraffico.prototype.destroy = function()
{
    // elimino il marker
    try {
        if (this.marker) {
            this.map.removeOverlay(this.marker);
            this.marker = null;
        }
        // elimino la situazione
        if (this.row1) elemento("tabEventi").removeChild(this.row1);
        if (this.row2) elemento("tabEventi").removeChild(this.row2);
        this.elenco = null;
        // elimino il tooltip
        this.tooltip = null;
        if (this.dirn) {
            this.map.removeOverlay(this.dirn);
            this.dirn = null;
        }
        if (this.dirp) {
            this.map.removeOverlay(this.dirp);
            this.dirp = null;
        }
    } catch (e) {
        ;
    }
}

