/*
	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
 	
*/
var _opToolTip="";
var _myOPShowTime;
var _myOPHideTime;
var _myOPShowTime2;
var _myOPHideTime2;
var _mkrType=0;
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,title,styl) {
	function createMarker(typ,lng, lat, html1,html2,title,id,dir) {
	
	    try
	   { 
		    gpt = new GPoint(lng, lat);
		    mkr = new GMarker(gpt, _markerIcon,false);
		    mkr.attached = false;
		    mkr.infoHtml1 = html1;
		    mkr.infoHtml2 = html2;
		    mkr.streaming=false;
		    mkr.title=title;
		    mkr.type=typ;
		    mkr.id=id;
		    mkr.dir=dir;
			
				if(typ==1)
				{
					GEvent.addListener(mkr, "mouseover", function(){hidetip();_mkrType=mkr.type;_opToolTip=html1;clearTimeout(_myOPHideTime);clearTimeout(_myOPShowTime);_myOPShowTime=setTimeout("ShowOPToolTip()",_showOPTipDelay);});					
					GEvent.addListener(mkr, "mouseout", function(){_mkrType=-1;clearTimeout(_myOPHideTime);clearTimeout(_myOPShowTime);_myOPHideTime=setTimeout("hidetip()",_hideOPTipDelay);});
				}
				else if(typ==2)
				{
					GEvent.addListener(mkr, "mouseover", function(){hidetip2();_mkrType=mkr.type;_opToolTip2=html1;clearTimeout(_myOPHideTime2);clearTimeout(_myOPShowTime2);_myOPShowTime2=setTimeout("ShowOPToolTip2()",_showOPTipDelay2);});					
					GEvent.addListener(mkr, "mouseout", function(){_mkrType=-1;clearTimeout(_myOPHideTime2);clearTimeout(_myOPShowTime2);_myOPHideTime2=setTimeout("hidetip2()",_hideOPTipDelay2);});				
				}
				else
				{
		            //the marker needs handlers for events
		            //the event has to be added on show and removed on hide
		            //else the tool tip will not show after the tg show-hide-show
		            //it will show on the first show tg
		            //events are added in displayOrHideMrks()
		            mkr.theHandler1=null;
		            mkr.theHandler3=null;			
				}
				
			
			_markers.push(mkr);
			return mkr;
		}
		catch(e)
		{
			var sErr=e.message;
		}
	}
	function displayOrHideMarkers()
	{
		try
		{
			bounds = _googleMap.getBounds();
			var NE=bounds.getNorthEast();
			var SW=bounds.getSouthWest();
			var maxX=NE.lng();
			var maxY=NE.lat();
			var minX=SW.lng();
			var minY=SW.lat();
			for (i = 0; i < _markers.length; i++) {
			if(_markersVisible) {
					var pt=_markers[i].getPoint();
					var ptX=pt.lng();
					var ptY=pt.lat();
					if (ptX >= minX && ptX <= maxX && ptY >= minY && ptY <= maxY && _markers[i].attached == false) {
						if(_markers[i].type=="Sgn"||_markers[i].type=="Inc"||_markers[i].type=="PE")
						{
						    _markers[i].theHandler1=GEvent.addListener(_markers[i], "mouseover", openMrkWindow);
						   _markers[i].theHandler3=GEvent.addListener(_markers[i], "mouseout", closeMrkWindow);  
						}
						_googleMap.addOverlay(_markers[i]);
						_markers[i].attached = true;
					}
				} else {
					if(_markers[i].attached) {
						_googleMap.removeOverlay(_markers[i]);
						_markers[i].attached = false;
					}
				}
			}
		}
		catch(e)
		{
			var sErr=e.message;
		}
	}
	
	function openMrkWindow() {
		//this.openInfoWindowHtml(this.infoHtml);
		try
		{
		    if(_tmId2)
		    {
		        clearTimeout(_tmId2);
		    }		
    		
		    if(!_upDating )
		    {
		        if(this.type=="Sgn")
		        {
		            _msg1="";
		           _msg2=""; 
		             if(this.infoHtml2!=""&&this.infoHtml1!="")
		            { 
		                _msg1=this.infoHtml1;
		                _msg2=this.infoHtml2;
		                _tmId2= self.setTimeout("IniSgnTm()",500);
		            }
		                showtip("<table cellpadding=0 cellspacing=0 class=tblDev><tr><td class=tdSgnName wrap=hard align=center>" + this.title + "</td></tr><tr><td class=tdSgn id=tdMsg>" + this.infoHtml1 + "</td></tr></table>");
		        }		    
    		    
		        if(this.type=="Inc"||this.type=="PE")
		        {
		            if(this.infoHtml2=="")
		            {
		               if(!_upDating ) 
		                    showtip("<table cellpadding=0 cellspacing=0><tr><td class=tdInc>" + this.infoHtml1 + "</td></tr></table>");
		            }
    		        
		        }
		    }
		
		}
		catch(e)
		{
		    alert(e.message + " - openMrkWindow");
		}
	}
	
	function closeMrkWindow() {
	    
	   if(this.type=="Sgn"&&this.infoHtml2!="")
	   { 
	        StpSgnTm();
	        clearTimeout(_tmId2);
		}
	    hidetip();
	}	
	
	
}

function HideOPToolTip()
{
	hidetip();	
}
function ShowOPToolTip()
{
	showtip(_opToolTip);
}

function HideOPToolTip2()
{
	hidetip2();	
}
function ShowOPToolTip2()
{
	showtip2(_opToolTip2);
}
	
