

NsCashGMap = Class.extend({
    polandLatLng: undefined,
    geocoder: undefined,
    openedInfoWindow: undefined,
    clusterer: undefined,

    init: function(elementId)
    {
        this.elementId = elementId;
        
        this.polandLatLng = new google.maps.LatLng(52.1739316926, 18.8525390625);
        this.myOptions = {
            zoom: 6,
            center: this.polandLatLng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        this.map = new google.maps.Map(document.getElementById(elementId),
            this.myOptions);

        this.geocoder = new google.maps.Geocoder();


        var me = this;

        this.loadMarkers();
    },

    refresh: function()
    {
        google.maps.event.trigger(this.map, 'resize');
        this.map.setZoom(this.map.getZoom());
        this.map.setCenter(this.polandLatLng);
    },
    
    afterInit: function()
    {

    },

    geocode: function(address)
    {
        this.geocoder.geocode({
            'address': address,
            'partialmatch': true
        }, this.geocodeResult);
    },

    geocodeResult: function(results, status) {
        if (status == 'OK' && results.length > 0) {
            map.getMap().setCenter(results[0].geometry.location);
            map.getMap().setZoom(14);
        } else {
        }
    },

    getMap: function()
    {
        return this.map;
    },

     loadMarkers: function(){
        var url = "/index.php/cashRegister/getList";
        var me = this;

        $.ajax({
            url: url,
            dataType: 'json',
            success: function(data){
                var markers = [];

                jQuery.each(data.data, function(index, value){
                    var marker = me.addMarker(value, me);

                    markers.push(marker);
                });

                me.clusterer = new MarkerClusterer(me.map, markers,{
                    minimumClusterSize: 3,
                    gridSize: 100,
                    maxZoom: 14,
                    styles: [
                        {
                            url: "/images/cashregister/cluster_small.png",
                            width: 20,
                            height: 21,
                            textColor: "white"
                        },
                        {
                            url: "/images/cashregister/cluster_big.png",
                            width: 32,
                            height: 34,
                            textColor: "white"
                        }
                    ]
                });
            },
            failure: this.loadFailed
        });
    },

    addMarker: function(data, gMap)
    {
        var infoWindow = new google.maps.InfoWindow({
            content: '<div class="mapinfo"><b>' + data.name + "</b><br>" + data.address + "</div>"
        });

        var marker = new google.maps.Marker({
            icon: new google.maps.MarkerImage("/images/cashregister/marker.png",new google.maps.Size(39,36),null,null),
            position: new google.maps.LatLng(data.lng, data.lat)
//            map: gMap.map
        });

        google.maps.event.addListener(marker, "click", function() {
            if (gMap.openedInfoWindow != undefined)
            {
                gMap.openedInfoWindow.close();
            }

            infoWindow.open(gMap.map, marker);

            gMap.openedInfoWindow = infoWindow;
        });

        return marker;
    },

     loadFailed: function(){
        
    }





});


