var map;
var centerLatitude = 51.39920565355378;
var centerLongitude = 10.4150390625;
var startZoom = 6;
var currentLang = 0;
var currentLong = 0;


//Create an icon for the clusters


var iconCluster = new GIcon();
iconCluster.image = '/images/markerCluster/image.png';
iconCluster.printImage = '/images/markerCluster/printImage.gif';
iconCluster.mozPrintImage = '/images/markerCluster/mozPrintImage.gif';
iconCluster.iconSize = new GSize(53,52);
iconCluster.shadow = '/images/markerCluster/shadow.png';
iconCluster.transparent = 'marke/images/markerClusterrs/transparent.png';
iconCluster.shadowSize = new GSize(79,52);
iconCluster.printShadow = 'mark/images/markerClusterers/printShadow.gif';
iconCluster.iconAnchor = new GPoint(27,52);
iconCluster.infoWindowAnchor = new GPoint(27,0);
iconCluster.imageMap = [32,0,35,1,37,2,39,3,41,4,42,5,43,6,44,7,45,8,44,9,43,10,42,11,41,12,40,13,38,14,38,15,37,16,36,17,36,18,37,19,52,20,52,21,52,22,52,23,52,24,52,25,52,26,52,27,52,28,52,29,52,30,52,31,52,32,52,33,52,34,51,35,51,36,50,37,50,38,49,39,49,40,48,41,47,42,47,43,46,44,45,45,44,46,43,47,41,48,40,49,38,50,36,51,17,51,15,50,13,49,11,48,10,47,9,46,8,45,7,44,6,43,5,42,5,41,4,40,3,39,3,38,2,37,2,36,2,35,1,34,1,33,1,32,0,31,0,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,1,20,1,19,17,18,17,17,16,16,15,15,14,14,13,13,12,12,11,11,10,10,9,9,8,8,8,7,9,6,10,5,12,4,13,3,16,2,18,1,21,0];







function init() {
    map = new GMap2(document.getElementById("map_canvas"));
    map.addControl(new GSmallMapControl());
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
    map.setMapType(G_HYBRID_MAP);
    map.enableScrollWheelZoom();
    //var mcOptions = { gridSize: 50, maxZoom: 12};
    //var marker = new MarkerClusterer(map, marker, mcOptions);

    updateMarkers();
    GEvent.addListener(map,'moveend',function() {
        updateMarkers();
    });
}

function updateMarkers() {

    //remove the existing points
  map.clearOverlays();
  markAfterScroll();
    //create the boundry for the data to provide
    //initial filtering
    var bounds = map.getBounds();
    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();
    var getVars = '?nelg=' + ne.lng() + '&nelat='+ ne.lat() +'&swlg=' + sw.lng()+'&swlat='+sw.lat();

    //log the URL for testing
    //GLog.writeUrl('server.php?'+getVars);

    //retrieve the points
    var request = GXmlHttp.create();
    request.open('POST', 'water/mapdata.html'+getVars, true);
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            var jscript = request.responseText;
            var points;
            eval(jscript);

            //create each point from the list
            for (i in points) {
                var point = new GLatLng(points[i].lat,points[i].lng);
                if(points[i].type != 'c'){
                    var marker = createMarker(point,points[i].type,"<h3><a href='gewaesser/"+points[i].slug+".html'>"+points[i].name+"</a></h3>");
                }
                else{
                    var marker = createMarker(point,points[i].type,"",points[i].count);
                }
                map.addOverlay(marker);
            }
        }
    }
    request.send(null);
}

function createMarker(point, type, html, count) {
    if(type=='c') {
        if(count<=9){
            markerOptions = {
                icon:iconCluster,
                labelText: count,
                labelClass: "glabel",
                labelOffset: new GSize(-3, -30)
            };
        }
        if(count <= 99 && count > 9){
            markerOptions = {
                icon:iconCluster,
                labelText: count,
                labelClass: "glabel",
                labelOffset: new GSize(-6, -30)
            };
        }
        if(count <= 999 && count > 99){
            markerOptions = {
                icon:iconCluster,
                labelText: count,
                labelClass: "glabel",
                labelOffset: new GSize(-9, -30)
            };
        }
        if(count > 999){
            markerOptions = {
                icon:iconCluster,
                labelText: count,
                labelClass: "glabel",
                labelOffset: new GSize(-12, -30)
            };
        }

        var marker = new LabeledMarker(point,markerOptions);

    } else {


        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(html);
        });
    }
    return marker;
}

function mapForForm(geolong,geolat){
    if(!this.checkInitial(geolong, geolat)){
        this.init();
        this.markObject(geolong, geolat);
    }
        
    else{
        this.customInitial(geolong,geolat);
        this.markObject(geolong, geolat);
    }
        
}

function checkInitial(geolong,geolat){
    if(geolong.val()=="")
        return false;
    else
        return true;

}

function customInitial(geolong,geolat){
    map = new GMap2(document.getElementById("map_canvas"));
    map.addControl(new GSmallMapControl());
    map.setCenter(new GLatLng(geolat.val(), geolong.val()), 11);
    map.setMapType(G_HYBRID_MAP);
    map.enableScrollWheelZoom();
    var myMarker = new GMarker(new GLatLng(geolat.val(), geolong.val()));
    map.addOverlay(myMarker);
    
}

/**
 * 
 * @param String geolong
 * @param String geolat
 * @return void
 */
function initShow(geolong,geolat){
    map = new GMap2(document.getElementById("map_canvas"));
    map.addControl(new GSmallMapControl());
    map.setCenter(new GLatLng(geolat,geolong), 11);
    map.setMapType(G_HYBRID_MAP);
    map.enableScrollWheelZoom();
    var myMarker = new GMarker(new GLatLng(geolat, geolong));
    map.addOverlay(myMarker);
    
}

function markObject(geolong,geolat){
    GEvent.addListener(map,"click", function(overlay,latlng) {
            map.clearOverlays();
            var myMarker = new GMarker(latlng);
            map.addOverlay(myMarker);
            geolong.val(myMarker.getPoint().lng());
            geolat.val(myMarker.getPoint().lat());
            currentLang = myMarker.getPoint().lat();
            currentLong = myMarker.getPoint().lng();
        });
  
}

function markAfterScroll(){
    if(currentLang!=0){
        var myMarker = new GMarker(new GLatLng(currentLang, currentLong));
        map.addOverlay(myMarker);
    }
    
}



