function EnableMusic(path) {
    parent.music.StopPlayMusic(path);
    //SetMusicIcon(0, path);
    SetMusicText(0);
}
function SetMusicIcon(value, path) {
    var img;
    img = document.getElementById("imgMusic");
    if (parent.music.ReturnPlayValue() == value)
        img.src = path + "musica/sound_low.png";
    else
        img.src = path + "musica/sound_mute.png";
}
function SetMusicText(value) {
    var txt;
    var testo;
    txt = document.getElementById("spanMusic");
    if (parent.music.ReturnPlayValue() == value)
        testo = "SOUND OFF";
    else
        testo = "SOUND ON";

    if (document.all)
        txt.innerText = testo;
    else
        txt.textContent = testo;
}
function initializeGeo(loc) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        if (loc=='FR')
            map.setCenter(new GLatLng(47.542726, 2.207522), 5);
        else
            map.setCenter(new GLatLng(41.895466, 12.482324), 5);
        geocoder = new GClientGeocoder();
    }
}

function searchLocations(path) {
    var address = document.getElementById('address').value;
    geocoder.getLatLng(address, function(latlng) {
        //alert(address);
        if (!latlng) {
            alert(address + ' not found');
        } else {
            startPoint = latlng;
            searchLocationsNear(latlng, path);
        }
    });
}
 

function searchLocationsNear(center,path) {
    var searchUrl = path+'negozi/elenco_negozi.asp?lat=' + center.lat() + '&long=' + center.lng();
    GDownloadUrl(searchUrl, function(data) {
        var xml = GXml.parse(data);
        markers = xml.documentElement.getElementsByTagName('marker');
        map.clearOverlays();

        var sidebar = document.getElementById('sidebar');
        sidebar.innerHTML = '';
        if (markers.length == 0) {
            sidebar.innerHTML = 'Nessun risultato.';
            map.setCenter(new GLatLng(42.557633, -31.030526), 6);
            return;
        }

        var bounds = new GLatLngBounds();
        for (var i = 0; i < markers.length; i++) {
            var city = markers[i].getAttribute('city');
            var address = markers[i].getAttribute('address');
            var phone = markers[i].getAttribute('phone');
            var open = markers[i].getAttribute('open');
            var fax = markers[i].getAttribute('fax');
            var distance = parseFloat(markers[i].getAttribute('distance'));
            var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                     parseFloat(markers[i].getAttribute('lng')));

            var marker = createMarker(point, city, address, phone, i);
            map.addOverlay(marker);
            var sidebarEntry = createSidebarEntry(marker, city, address, phone, fax, open, distance, i);
            sidebar.appendChild(sidebarEntry);
            bounds.extend(point);
        }
        toHere(0);
        //map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    });
}

function createMarker(point, city, address, phone, i) {
    var marker = new GMarker(point, PESicon);
    var html = '<div onclick="toHere(' + i + ')" style="font-size:11px;font-family:Arial,Verdana"><b>' + city + '</b> ';
    if (phone) html += '<br/>Tel: ' + phone
    html += '<br/>' + address;
    html += '<br/><br /><a style="color:#ff0000; font-weight:bold;" href="javascript:toHere(' + i + ')">Come raggiungerci</a></div>';
    GEvent.addListener(marker, 'click', function() {
        //marker.openInfoWindowHtml(html);
    });
    return marker;
}

function createSidebarEntry(marker, city, address, phone, fax, open, distance, i) {
    var div = document.createElement('div');
    var s;
    s = ""+open;
    s = s.replace(/!c!/g, "<br/>");
    var html = '<br/><b>' + city + '</b><br/>' + address + '<br/>';
    if (phone) html += ' Tel: ' + phone
    if (fax) html += ' Fax: ' + fax;
    if (open) html += '<br/>' + s;
    //if (distance) html += '<br/>Distanza in Km: ' + distance;
    //html += '<br/><br/><a href="javascript:toHere(' + i + ')" style="color:#ff0000; font-weight:bold;">COME RAGGIUNGERGI</a>'
    div.innerHTML = '<a href="javascript:toHere(' + i + ')">' + html + '</a>';
    div.setAttribute("id", "divMap"+i)
    //div.setAttribute("onclick", "toHere(" + i + ");");
    if (alterna == 0) {
        alterna = 1;
        div.style.backgroundColor = '#f4f3ed';
        div.style.fontFamily = 'Arial,Verdana';
        div.style.fontSize = '10px';
        div.style.color = '#000000';
        div.style.height = '25%';
        div.style.textAlign = 'left';
        div.style.verticalAlign = 'middle';
        div.style.paddingLeft = '10px';
        //div.style.cursor = 'hand';
        /*div.style.paddingTop = '5px';*/
        //div.setAttribute("class", "rigaPari")
    }
    else {
        alterna = 0;
        div.style.backgroundColor = '#eceadf';
        div.style.fontFamily = 'Arial,Verdana';
        div.style.fontSize = '10px';
        div.style.color = '#000000';
        div.style.height = '25%';
        div.style.textAlign = 'left';
        div.style.verticalAlign = 'middle';
        div.style.paddingLeft = '10px';
        //div.style.cursor = 'hand';
        /*div.style.paddingTop = '5px';*/
        //div.setAttribute("class", "rigaDispari")
    }
    //div.style.cursor = 'pointer';
    //div.style.marginBottom = '5px';
    //div.style.backgroundImage = "url(/pes2009/portals/0/imgpes/visto.png)";
    div.style.backgroundPosition = "98% 2%";
    div.style.backgroundRepeat = "no-repeat";
    GEvent.addDomListener(div, 'click', function() {
        //toHere(i);
        GEvent.trigger(marker, 'click');
    });
    GEvent.addDomListener(div, 'mouseover', function() {
        app = div.style.backgroundColor;
        div.style.backgroundColor = '#d7d4c3';
        //alert(div.getAttribute("class"));
    });
    GEvent.addDomListener(div, 'mouseout', function() {
        div.style.backgroundColor = app; //'#FBF2F3';
    });
    return div;
}


function setDirections(fromAddress, toAddress) {
    gdir.load("from: " + fromAddress + " to: " + toAddress,
                    { "locale": "it" });
}

function handleErrors() {
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("Indirizzo non trovato.\nError code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("Indirizzo errato.\n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("Non avete inserito l\'indirizzo di partenza.\n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
        alert("Errore di comunicazione con il server di mapping. \n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("L\'indirizzo non e\' nel formato corretto'.\n Error code: " + gdir.getStatus().code);

    else alert("Errore di ricerca.");

}

function onGDirectionsLoad() {
    // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
}
function toHere(indice) {
    var newpoint = markers[indice].getAttribute('lat') + ',' +
            markers[indice].getAttribute('lng');
    setDirections(startPoint.toUrlValue(), newpoint);
    document.getElementById("divMap0").style.backgroundImage = "url(/pes2009/portals/0/imgpes/visto1.png)";
    document.getElementById("divMap1").style.backgroundImage = "url(/pes2009/portals/0/imgpes/visto1.png)";
    document.getElementById("divMap2").style.backgroundImage = "url(/pes2009/portals/0/imgpes/visto1.png)";
    document.getElementById("divMap3").style.backgroundImage = "url(/pes2009/portals/0/imgpes/visto1.png)";
    document.getElementById("divMap" + indice).style.backgroundImage = "url(/pes2009/portals/0/imgpes/visto.png)";
    //alert(document.getElementById("divMap" + indice).id);
}
//]]>
function VerificaInvio(e,path) {
    var numero;
    if (window.event) // IE
    {
        numero = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        numero = e.which;
    }
    //return !isNaN(carattere);
    if (numero == 13) {
        searchLocations(path);
        return false;
    }
    else
        return true;
}