// Javascript hack to show hotsplots data on google maps
// (c) 2006 hotsplots GmbH
// 

// a few global vars here...
//
var gmarkers = [];
var htmls = [];
var map;
var clusterer;
var showHotspots = true;

// our icons
//      
var iconFlatAct = new GIcon();
iconFlatAct.image = "map/marker_flat_act.png";
iconFlatAct.shadow = "map/shadow50.png";
iconFlatAct.iconSize = new GSize(17, 39);
iconFlatAct.shadowSize = new GSize(37, 34);
iconFlatAct.iconAnchor = new GPoint(9, 34);
iconFlatAct.infoWindowAnchor = new GPoint(9, 2);
iconFlatAct.infoShadowAnchor = new GPoint(18, 25);

var iconFlatInact = new GIcon();
iconFlatInact.image = "map/marker_flat_inact.png";
iconFlatInact.shadow = "map/shadow50.png";
iconFlatInact.iconSize = new GSize(17, 39)
iconFlatInact.shadowSize = new GSize(37, 34);
iconFlatInact.iconAnchor = new GPoint(9, 34);
iconFlatInact.infoWindowAnchor = new GPoint(9, 2);
iconFlatInact.infoShadowAnchor = new GPoint(18, 25);

var iconNoflatAct = new GIcon();
iconNoflatAct.image = "map/marker_noflat_act.png";
iconNoflatAct.shadow = "map/shadow50.png";
iconNoflatAct.iconSize = new GSize(17, 39);
iconNoflatAct.shadowSize = new GSize(37, 34);
iconNoflatAct.iconAnchor = new GPoint(9, 34);
iconNoflatAct.infoWindowAnchor = new GPoint(9, 2);
iconNoflatAct.infoShadowAnchor = new GPoint(18, 25);

var iconNoflatInact = new GIcon();
iconNoflatInact.image = "map/marker_noflat_inact.png";
iconNoflatInact.shadow = "map/shadow50.png";
iconNoflatInact.iconSize = new GSize(17, 39);
iconNoflatInact.shadowSize = new GSize(37, 34);
iconNoflatInact.iconAnchor = new GPoint(9, 34);
iconNoflatInact.infoWindowAnchor = new GPoint(9, 2);
iconNoflatInact.infoShadowAnchor = new GPoint(18, 25);

var clusterIcon = new GIcon();
clusterIcon.image = 'map/marker_cluster.png';
clusterIcon.shadow = 'map/shadow_cluster.png';
clusterIcon.iconSize = new GSize( 26, 60 );
clusterIcon.shadowSize = new GSize( 56, 51 );
clusterIcon.iconAnchor = new GPoint( 13, 34 );
clusterIcon.infoWindowAnchor = new GPoint( 13, 3 );
clusterIcon.infoShadowAnchor = new GPoint( 27, 37 );



// ----------------------------------------
// create markers and popups for hotspots
//
function createHotspotMarker(splot) {

    var html = '<div style="white-space:nowrap; font-size: 9px;">' 
        + '<strong>'+splot.public_name+'</strong><br/>';

    if (splot.email) {
        html = html + '<a href="mailto:'+splot.email+'">'+splot.email+'</a><br/>';
    }
    html = html + splot.address + '<br/>'
        + splot.zip + ' ' + splot.city + '<br/>'
        + splot.desc + '<br/>'
        + splot.rate + '<br/>'
        + '<a href="http://' + splot.www + '" target="_blank">'+ splot.www + '</a>'
        + '</div>';

    if (splot.flat) {
        if (splot.active)
            var marker = new GMarker(splot.point, iconFlatAct);
        else
            var marker = new GMarker(splot.point, iconFlatInact);
    } else {
        if (splot.active)
            var marker = new GMarker(splot.point, iconNoflatAct);
        else
            var marker = new GMarker(splot.point, iconNoflatInact);
    }

    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });

    gmarkers[splot.nasid] = marker;
    htmls[splot.nasid] = html;

    return marker;
}


// ----------------------------------------
// picks up the click and opens the corresponding info window
//
function showSpot(strName) {
    gmarkers[strName].openInfoWindowHtml(htmls[strName]);
}

// ----------------------------------------
// perform search
//
function search() {

    document.getElementById("resultDiv").innerHTML = '<h2>Suche Hotspots</h2>Bitte warten...';
    xajax_searchSpots(xajax.getFormValues('queryForm'), 'de');
}

// ----------------------------------------
// show help infos
//
function help(){

    document.getElementById("resultDiv").innerHTML = 
         '<h2>Hilfe zur Suche</h2>' +
        '&bull; Postleitzahlen: "1" findet alle Hotspots im PLZ-Bereich 1xxxx, "10" im Bereich 10xxx, usw.<br/><br/>'+
        '&bull; Städte: mit "B" finden Sie z.B. alle Standorte in Städten, die mit "B" beginnen.<br/><br/>'+
        '&bull; Über den Filter können Sie die Art der Hotspots einschränken. '+
       '<h2>Hilfe zur Karte</h2>' +
        '&bull; Der Schieberegler auf der linken Seite steuert den Kartenausschnitt.<br/><br/>'+
        '&bull; Die Karte kann mit der Maus "verschoben" werden. Klicken Sie dazu in die Karte und bewegen Sie die Maus mit gedrückter Taste.';

}


// ----------------------------------------.
// get hotspot data from hotsplots
//
function getHotspots() {

    clusterer.ClearAll();
    map.clearOverlays();

    // Read the data 
    var request = GXmlHttp.create();
    request.open("GET", "map/getHotspots.php", true);
    request.onreadystatechange = function() {

        if (request.readyState == 4) {
            
            var xmlDoc = request.responseXML;
            // obtain the array of markers and loop through it
            var markers = xmlDoc.documentElement.getElementsByTagName("splot");
          
            for (var i = 0; i < markers.length; i++) {
                // obtain the attribues of each marker1
                var splot = new Object();
                
                splot.point = new GPoint(parseFloat(markers[i].getAttribute("lng")),
                                         parseFloat(markers[i].getAttribute("lat")));
            
                splot.nasid   = markers[i].getAttribute("name");
                splot.public_name   = markers[i].getAttribute("public_name");
                splot.dest = '<a target="_blank" href="http://maps.google.com/maps?f=d&hl=de&q='+markers[i].getAttribute("dest")+'">';
                splot.address = splot.dest + markers[i].getAttribute("address")+"</a>";
                splot.zip     = markers[i].getAttribute("zip");
                splot.city    = markers[i].getAttribute("city");
                splot.desc    = markers[i].getAttribute("desc");
                splot.active  = parseInt(markers[i].getAttribute("active"));
                splot.rate    = markers[i].getAttribute("rate");
                splot.www     = markers[i].getAttribute("www");
                splot.email   = markers[i].getAttribute("email");
                splot.flat    = parseInt(markers[i].getAttribute("flat"));

                var marker = createHotspotMarker(splot);

                // create clusterer object
                clusterer.AddMarker(marker, splot.nasid);
            }
        }
    }
    request.send(null);
}


// ----------------------------------------
// create the map
//
function showMap() {

    map = new GMap2(document.getElementById("map"));
    //map.addControl(new GLargeMapControl());
    //map.addControl(new GMapTypeControl());
    //map.addControl(new GScaleControl());
    map.setCenter(new GLatLng(51.412912, 10.546875), 6);
    map.setUIToDefault();
    map.setMapType(G_NORMAL_MAP);

    //var overV=new GOverviewMapControl(new GSize(200, 150));
    //map.addControl(overV);
    
    
    // create the clusterer
    clusterer = new Clusterer(map);
      
    // set the clusterer parameters if you dont like the defaults
    clusterer.icon = clusterIcon;      
    clusterer.maxVisibleMarkers = 100;
    clusterer.gridSize = 15;
    clusterer.minMarkersPerClusterer = 10;
    clusterer.maxLinesPerInfoBox = 6;

    // get and show hotspot data
    getHotspots();

    help();
}


if (window.attachEvent) {
  window.attachEvent("onload", function() { showMap(); });
} else {
  window.addEventListener("load", function() { showMap(); }, false);
}




    
