var googleMap = {
    map: null,
    locations: new Array(),
    wndInfo: null,
    markerIcon: '/images/googlemaps/bubble.png',
    wndCustom: null,

    init: function(startX, startY, startZoom) {
        var mapOptions = {
            zoom: parseInt(startZoom),
            center: new google.maps.LatLng(startX, startY),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        this.wndCustom = ($('#map_popup').attr('id') != undefined) ? $('#map_popup') : null;

        this.map = new google.maps.Map(
            document.getElementById('map_canvas'),
            mapOptions
        );

        google.maps.event.addDomListener(this.map, 'click', this.closeWindow);

        if (this.wndCustom != null)
        {
            google.maps.event.addDomListener(this.map, 'center_changed', this.closeWindow);
        }

        this.wndInfo = new google.maps.InfoWindow({content: ''});
    },
    addLocation: function(latitude, longitude, title, data) {
        var marker = new google.maps.Marker({
            number: this.locations.length,
            position: new google.maps.LatLng(latitude, longitude),
            map: this.map,
            icon: this.markerIcon,
            title: title,
            htmlData: data
        });

        google.maps.event.addDomListener(marker, 'click', this.showWindow);

        this.locations[this.locations.length] = marker;
    },
    setLocation: function(latitude, longitude, title, data) {
        new google.maps.Marker({
            position: new google.maps.LatLng(latitude, longitude),
            map: this.map,
            icon: this.markerIcon,
            title: title,
            htmlData: data
        });
    },
    showWindow: function() {
        if (googleMap.wndCustom != null) {
            googleMap.map.setCenter(this.position);
            googleMap.wndCustom.show();
            $(googleMap.wndCustom.selector + ' .map_popup_content').html(this.htmlData);
        } else {
            googleMap.wndInfo.setContent(this.htmlData);
            googleMap.wndInfo.open(googleMap.map, this);
        }
        //document.location.hash = 'node-' + this.number;
    },
    selectLocation: function(number) {
        if (this.locations[number] != null) {
            if (googleMap.wndCustom != null) {
                googleMap.wndCustom.show();
                $(googleMap.wndCustom.selector + ' .map_popup_content').html(this.locations[number].htmlData);
            } else {
                googleMap.wndInfo.setContent(this.locations[number].htmlData);
                googleMap.wndInfo.open(this.map, this.locations[number]);
            }
        }
    },
    closeWindow: function() {
        if (googleMap.wndCustom != null) {
            googleMap.wndCustom.hide();
        } else {
            googleMap.wndInfo.close();
        }
    }
}

