var infoWindow = new google.maps.InfoWindow();

function googleMap(data) {
    if (document.getElementById(data['container']) != null) {
    
        var bounds = new google.maps.LatLngBounds();
        var latLng = new Array();


        for (i = 0; (typeof data['data'][i]) == 'object'; i++) {
            if ((typeof data['data'][i]) == 'object') {
                latLng[i] = new google.maps.LatLng(data['data'][i]['latitude'], data['data'][i]['longitude']);
                bounds.extend(latLng[i]);
            }
        }

        var mWidth = data['markerWidth'];
        var mHeight = data['markerHeight']
        if ('markerPointX' in data) {
            var pX = data['markerPointX'];
        } else {
            var pX = mWidth / 2; pX = Math.round(pX);
        }
        if ('markerPointY' in data) {
            var pY = data['markerPointY'];
        } else {
            var pY = mHeight / 2; pY = Math.round(pY);
        }

        var icon = new google.maps.MarkerImage(data['marker'],
            new google.maps.Size(mWidth, mHeight),
            new google.maps.Point(0, 0),
            new google.maps.Point(pX, pY));

        var mapCenter = bounds.getCenter();

        var mapOptions = {
            zoom: data['zoom'],
            navigationControl: true,
            mapTypeControl: true,
            mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.HORIZONTAL},
            scaleControl: true,
            navigationControl: true,
            navigationControlOptions: {style: google.maps.NavigationControlStyle.DEFAULT},
            center: mapCenter,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        if (data['largeNavigation'] == true) {
            mapOptions['navigationControlOptions'] = {style: google.maps.NavigationControlStyle.ZOOM_PAN};
        }

        var map = new google.maps.Map(document.getElementById(data['container']), mapOptions);
        var trafficLayer = new google.maps.TrafficLayer();
        trafficLayer.setMap(map);

        if (data['fluster'] == true) {
            var fluster = new Fluster2(map);
            fluster.styles = {
                // This style will be used for clusters with more than 0 markers
                0: {
                    image: '/images/common/10.png',
                    textColor: '#FFFFFF',
                    width: 36,
                    height: 14,
                    textIndent: 13
                },
                10: {
                    image: '/images/common/10.png',
                    textColor: '#FFFFFF',
                    width: 36,
                    height: 14,
                    textIndent: 13
                },
                25: {
                    image: '/images/common/25.png',
                    textColor: '#FFFFFF',
                    width: 36,
                    height: 14,
                    textIndent: 13
                },
                50: {
                    image: '/images/common/50.png',
                    textColor: '#FFFFFF',
                    width: 36,
                    height: 14,
                    textIndent: 13
                },
                100: {
                    image: '/images/common/100.png',
                    textColor: '#FFFFFF',
                    width: 45,
                    height: 14,
                    textIndent: 13
                }
            };
        }

        if (data['autoCenter'] == true) {
            map.fitBounds(bounds);
        }

        var marker = new Array();
        var infoWindowData = new Array();

        for (i = 0; (typeof data['data'][i]) == 'object'; i++) {
            if ((typeof data['data'][i]) == 'object') {
                marker[i] = new google.maps.Marker({position: latLng[i], 
                    map: map, 
                    icon: icon, 
                    title: data['data'][i]['title']});

                if (data['data'][i]['infoWindow'] != '') {
                    //var x = data['data'][i]['infoWindow'];
                    //var y = marker[i];
                    doInfoWindow(data['data'][i]['infoWindow'], marker[i], map);
                    //google.maps.event.addListener(marker[i], 'click', function () { loadWindow(x,y); } );
                }
                if (data['fluster'] == true) {
                    fluster.addMarker(marker[i]);
                }

            }
        }
        if (data['fluster'] == true) {
            fluster.initialize();
        }
    } else {
        return false;
    }
}
function doInfoWindow(x,y,z){
    google.maps.event.addListener(y, 'click', function () { infoWindow.setContent(x); infoWindow.open(z, y); } );
}
