/*
	MarkerGroup.js
	created by Scott Matthewman <scott@matthewman.net>
	for The Stage Newspaper Limited
	(c) 2005 The Stage Newspaper Limited

	Released under the GNU General Public License (http://www.gnu.org/copyleft/gpl.html). 
	You are free to use, modify and generally do what you will with this code, for either
	commercial or non-commercial products. About the only thing you shouldn't do with it 
	is claim that you wrote it. That'd just be mean.
	
	Feedback of any sort is much appreciated, to scott@matthewman.net
	
	Change history:
 	08-Jul-05 	Initial build
 	
*/
function MarkerGroup(map, icon) {
	// private member variables
	var _googleMap = null;
	var _markerIcon = null;
	var _markersVisible = false;
	var _markers = new Array();
	
	setGoogleMap(map);
	setMarkerIcon(icon);

	
	// public method pointers
	this.getGoogleMap  = getGoogleMap; // setGoogleMap is private
	this.getMarkerIcon = getMarkerIcon; // setMarkerIcon is private
	this.getMarkersVisible = getMarkersVisible;
	this.setMarkersVisible = setMarkersVisible;
	this.toggleMarkersVisible = toggleMarkersVisible;
	this.createMarker = createMarker;
	
	function getGoogleMap() {
		return _googleMap;
	}
	function setGoogleMap(parameter) {
		_googleMap = parameter;
		GEvent.addListener(_googleMap, 'moveend', displayOrHideMarkers);
	}
	
	function getMarkerIcon() {
		return _markerIcon;
	}
	function setMarkerIcon(parameter) {
		_markerIcon = parameter;
	}
	
	function getMarkersVisible() {
		return _markersVisible;
	}
	function setMarkersVisible(parameter) {
		_markersVisible = parameter;
		displayOrHideMarkers();
	}
	function toggleMarkersVisible() {
		_markersVisible = !_markersVisible;
		displayOrHideMarkers();
	}
	
	function createMarker(lng, lat, html) {
		gpt = new GPoint(lng, lat);
		mkr = new GMarker(gpt, _markerIcon);
		mkr.attached = false;
		mkr.infoHtml = html;
		GEvent.addListener(mkr, "click", openMarkerWindow);
		_markers.push(mkr);
		return mkr;
	}
	
    function displayOrHideMarkers() {

                bounds = _googleMap.getBounds();

                for (i = 0; i < _markers.length; i++) {
                  if(_markersVisible) {
                                if (bounds.contains(_markers[i].getPoint()) && _markers[i].attached == false) {
                                        _googleMap.addOverlay(_markers[i]);
                                        _markers[i].attached = true;
                                }
                        } else {
                                if(_markers[i].attached) {
                                        _googleMap.removeOverlay(_markers[i]);
                                        _markers[i].attached = false;
                                }
                        }
                }
        }


	
	function openMarkerWindow() {
		this.openInfoWindowHtml(this.infoHtml);
	}
}

