/*
	MrkGrp.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 _clickedIcon=null;
function MrkGrp(map, icon) {
try
{
	// private member variables
	var _googleMap = null;
	var _markerIcon = null;
	var _markersVisible = false;
	var _markers = new Array();
	
	function getGoogleMap() {
		return _googleMap;
	}
	function setGoogleMap(parameter) {
		_googleMap = parameter;
		GEvent.addListener(_googleMap, 'moveend', displayOrHideMrks);
	}
	
	function getMrkIcon() {
		return _markerIcon;
	}
	function setMrkIcon(parameter) {
		_markerIcon = parameter;
	}
	
	function getMrksVisible() {
		return _markersVisible;
	}
	function setMrksVisible(parameter) {
		_markersVisible = parameter;
		displayOrHideMrks();
	}
	function tgMrksVisible() {
		_markersVisible = !_markersVisible;
		displayOrHideMrks();
	}
	
	function createMrk(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;
		    //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.theHandler2=null;
		    mkr.theHandler3=null;
    		
		    _markers.push(mkr);
		    mkr = null;
		}
		catch(e)
		{
		    alert(e.message + " - createMrk" );
		}
	}

	/*
	* Get rid of all the objects and pointers while we are at it.
	*/
	function Dispose(boolDispose) {

	    if (boolDispose) {
	        try {
	            for (i = 0; i < _markers.length; i++) {
	                _markers[i] = null;
	            }
	            _markers = null;
	            _markerIcon = null;
	            _googleMap = null;
	            this.getGoogleMap = null; this.getMrkIcon = null;
	            this.getMrksVisible = null; this.setMrksVisible = null;
	            this.tgMrksVisible = null; this.createMrk = null; this.Dispose = null;
	        } catch (e) { }
	    }
	}
	
	function displayOrHideMrks()
	{
		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) {
					        _markers[i].theHandler1 = GEvent.addListener(_markers[i], "mouseover", openMrkWindow);
                            
                           if(_markers[i].type=="Cm") 
                                _markers[i].theHandler2=GEvent.addListener(_markers[i], "click", OpenCmWin);
                           
                           if(_markers[i].type=="Sgn")
                               _markers[i].theHandler2 = GEvent.addListener(_markers[i], "click", OpenSgnWin);
                           
                           if (_markers[i].type == "Tour")
                               _markers[i].theHandler2 = GEvent.addListener(_markers[i], "click", OpenSVTWin);
                             _markers[i].theHandler3=GEvent.addListener(_markers[i], "mouseout", closeMrkWindow); 
						     
						   _markers[i].attached = true;
						   _googleMap.addOverlay(_markers[i]);
						}
						else if (ptX < minX || ptX > maxX || ptY < minY || ptY > maxY  && _markers[i].attached == true) {

						// If the marker is outside of the boundary then remove it from the map. This is an optimization in order to minimize the number of
						// markers on the map.
						detachMarker(_markers[i]); _markers.clicked = false;
						   _clickedIcon=null; 
						}	
				    }
				    else {
				    
					    if(_markers[i].attached) {
					        detachMarker(_markers[i]); _markers.clicked = false;
						   _clickedIcon=null; 
					    }
				}
			}
		}
		catch(e)
		{
			var sErr=e.message;
		}
	}

	function detachMarker(theMarker) {

	    if (theMarker.theHandler1 != null) {
	        GEvent.removeListener(theMarker.theHandler1);
	        _markers[i].theHandler1 = null;
	    }

	    if (theMarker.theHandler2 != null) {
	        GEvent.removeListener(theMarker.theHandler2);
	        theMarker.theHandler2 = null;
	    }

	    if (theMarker.theHandler3 != null) {
	        GEvent.removeListener(theMarker.theHandler3);
	        theMarker.theHandler3 = null;
	    }

	    _googleMap.removeOverlay(theMarker);
	    theMarker.attached = false;

	}
	
	var _now=new Date();
	function OpenSgnWin()
	{	     
	        if(!_upDating)
		      {
		        if(this.type=="Sgn")
		        {		          
		            try
		           {
		               alertDMS(this.id, this.title, this.infoHtml1, this.infoHtml2);
		            }
		           catch(e)
		           {
		                alert(e.message + " - openSgnWin");
		           } 
		        }
		    } 
	}
	function OpenCmWin()
	{    
	        if(!_upDating)
		      {
		        if(this.type=="Cm")
		        {
		          
		            try
		           {
		               alertCCTV(this.id, this.dir, this.title, this.infoHtml1, this.infoHtml2);
		            }
		           catch(e)
		           {
		                alert(e.message + " - openCmWin");
		           } 
		        }
		    }

		}
		function OpenSVTWin() {
		    if (!_upDating) {
		        if (this.type == "Tour") {

		            try {
		                URL = this.title;
		                eval("winvideo = window.open('video.aspx?URL='+URL,'winvideo','status=no,width=324,height=300,scrollbars=no')");
		                winvideo.focus();
		            }
		            catch (e) {
		                alert(e.message + " - openCmWin");
		            }
		        }
		    }

		}
		
		
		function openMrkWindow() {
	
		try
		{
		      if(!_upDating)
		      {
		          if (this.type == "Cm") {
		            if (BrowserDetect.browser == "Safari") {

		                var tooltip = new Tooltip(this, "<table cellpadding=0 cellspacing=0 width=250 style=background-color:#FFFBD0 ><tr><td class=tdDevName  align=center>" + this.title + "</td></tr></table>", 4);
		                this.tooltip = tooltip;
		                _googleMap.addOverlay(tooltip);
		                this.tooltip.show();

		            } else {
		                showtip("<table cellpadding=0 cellspacing=0 ><tr><td class=tdDevName  align=center>" + this.title + "</td></tr></table>");
		            }
		        }

		        if (this.type == "Sgn") {
		        
		            if (BrowserDetect.browser == "Safari") {

		                var tooltip = new Tooltip(this, "<table cellpadding=0 cellspacing=0 width=250 style=background-color:#FFFBD0 ><tr><td class=tdDevName  align=center>" + this.title + "</td></tr></table>", 4);
		                this.tooltip = tooltip;
		                _googleMap.addOverlay(tooltip);
		                this.tooltip.show();

		            } else {
		                showtip("<table cellpadding=0 cellspacing=0 ><tr><td class=tdDevName  align=center>" + this.title + "</td></tr></table>");
		            }
		           
		        }

		        if (this.type == "Evt") {
		        
		            if (BrowserDetect.browser == "Safari") {

		                var tooltip = new Tooltip(this, "<table cellpadding=0 cellspacing=0 ><tr><td class=tdDevName  align=center>" + this.title + "</td></tr><tr><td class=tdTTTxt>" + this.infoHtml1 + "</td></tr></table>", 4);
		                this.tooltip = tooltip;
		                _googleMap.addOverlay(tooltip);
		                this.tooltip.show();

		            } else {
		                showtip("<table cellpadding=0 cellspacing=0 ><tr><td class=tdDevName  align=center>" + this.title + "</td></tr><tr><td class=tdTTTxt>" + this.infoHtml1 + "</td></tr></table>");
		            }
		        }
		        
		        if(this.type=="Spd") {

		            if (BrowserDetect.browser == "Safari") {

		                var tooltip = new Tooltip(this, "<table cellpadding=0 cellspacing=0 ><tr><td class=tdDevName  align=center>" + this.title + "</td></tr><tr><td class=tdTTTxt>" + this.infoHtml1 + " MPH<br>" + this.infoHtml2 + "</td></tr></table>", 4);
		                this.tooltip = tooltip;
		                _googleMap.addOverlay(tooltip);
		                this.tooltip.show();

		            } else {

		                if (this.id != '') {
		                    showtip("<table cellpadding=0 cellspacing=0 ><tr><td class=tdDevName  align=center>" + this.title + "</td></tr><tr><td class=tdTTTxt>" + this.infoHtml1 + " MPH<br>" + this.infoHtml2 + "<br> DOT ID " + this.id + "</td></tr></table>");    
		                } else {
		                    showtip("<table cellpadding=0 cellspacing=0 ><tr><td class=tdDevName  align=center>" + this.title + "</td></tr><tr><td class=tdTTTxt>" + this.infoHtml1 + " MPH<br>" + this.infoHtml2 + "</td></tr></table>");
		                } 
		            }
		        }

		        if (this.type == "Tour") {
		        
		            if (BrowserDetect.browser == "Safari") {

		                var tooltip = new Tooltip(this, "<table cellpadding=0 cellspacing=0 ><tr><td class=tdDevName  align=center>" + this.dir + "</td></tr></table>", 4);
		                this.tooltip = tooltip;
		                _googleMap.addOverlay(tooltip);
		                this.tooltip.show();

		            } else {
		                showtip("<table cellpadding=0 cellspacing=0 ><tr><td class=tdDevName  align=center>" + this.dir + "</td></tr></table>");
		            }
		        }
		     }   
		}
		catch(e)
		{
		    alert(e.message + " - openMrkWindow");
		}
	}
	
	function closeMrkWindow() {

	    if (BrowserDetect.browser == "Safari") {
	        _googleMap.removeOverlay(this.tooltip);
	        this.tooltip.hide();
	    } else {
	        hidetip();
	    }
	   
	    }	
	}
	catch(e)
	{
	    alert(e.message);
	}
	
	setGoogleMap(map);
	setMrkIcon(icon);

	
	// public method pointers
	this.getGoogleMap   = getGoogleMap; // setGoogleMap is private
	this.getMrkIcon     = getMrkIcon; // setMrkIcon is private
	this.getMrksVisible = getMrksVisible;
	this.setMrksVisible = setMrksVisible;
	this.tgMrksVisible  = tgMrksVisible;
	this.createMrk      = createMrk;
	this.Dispose        = Dispose;
}
