function $(element) {
    return document.getElementById(element);
}

var PSM = {};

PSM.pflege = null;
PSM.map = null;
PSM.markerClusterer = null;
PSM.markers = [];
PSM.infoWindow = null;

PSM.init = function (lat, lon, zoom, wheel) {
    var latlng = new google.maps.LatLng(lat, lon);
    var options = {
        'scrollwheel': wheel,
        'zoom': zoom,
        'center': latlng,
        'mapTypeId': google.maps.MapTypeId.ROADMAP
    };

    PSM.map = new google.maps.Map($('map'), options);
    if (typeof (data) == "undefined") return;
    PSM.pflege = data.pflege;

    PSM.infoWindow = new google.maps.InfoWindow();

    PSM.showMarkers();
};

PSM.showMarkers = function () {

    PSM.markers = [];
    if (PSM.markerClusterer != null) PSM.markerClusterer.clearMarkers();

    var lg = PSM.pflege.length; //number of markers
    
    var imgHome = new google.maps.MarkerImage('/pics/pin_home.png', new google.maps.Size(21, 34));
    var imgMobil = new google.maps.MarkerImage('/pics/pin_mobil.png', new google.maps.Size(21, 34));
    var imgKombi = new google.maps.MarkerImage('/pics/pin_kombi.png', new google.maps.Size(21, 49));
    var imgHomePay = new google.maps.MarkerImage('/pics/pin_home_pay.png', new google.maps.Size(21, 34));  
    var imgMobilPay = new google.maps.MarkerImage('/pics/pin_mobil_pay.png', new google.maps.Size(33, 70));
    var imgKombiPay = new google.maps.MarkerImage('/pics/pin_kombi_pay.png', new google.maps.Size(33, 70));
    
    for (var i = 0; i < lg; i++) {
        var title = PSM.pflege[i].na;
        var latLng = new google.maps.LatLng(PSM.pflege[i].la, PSM.pflege[i].lo);
        var image = imgHome;
        if (PSM.pflege[i].ty == 1) image = imgMobil;
        if (PSM.pflege[i].ty == 2) image = imgKombi;
        if (PSM.pflege[i].ty == 10) image = imgHomePay;
        if (PSM.pflege[i].ty == 11) image = imgMobilPay;
        if (PSM.pflege[i].ty == 12) image = imgKombiPay;
        var marker = new google.maps.Marker({ 'position': latLng, 'icon': image }); //, 'title': PSM.pflege[i].na }); //, 'icon': image });        

        var fn = PSM.markerClickFunction(PSM.pflege[i], latLng);
        google.maps.event.addListener(marker, 'click', fn);
        //google.maps.event.addDomListener(title, 'click', fn);
        PSM.markers.push(marker);
    }

    window.setTimeout(PSM.time, 0);
};

PSM.markerClickFunction = function (pflege, latlng) {
    return function (e) {
        e.cancelBubble = true;
        e.returnValue = false;
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
        jQuery.get("/Verzeichnis/Kartendaten.aspx?id=" + pflege.id, function (data) {

            PSM.infoWindow.setContent(data);
            PSM.infoWindow.setPosition(latlng);
            PSM.infoWindow.open(PSM.map);
        });
    };
};

PSM.time = function () {
    var mcOptions = { gridSize: 35, maxZoom: 11 };    
    PSM.markerClusterer = new MarkerClusterer(PSM.map, PSM.markers, mcOptions);    
};
