/*
	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();
	var _altHtmls = new Array();
	var _infoHtmls = 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.infoText = infoText;
	this.toggleMarkersVisible = toggleMarkersVisible;
	this.createMarker = createMarker;
	this.showInfo = showInfo;
	this.update_tabs = update_tabs;
	this.showInfoTab = showInfoTab;
	this.nummarkers = numMarkers;
	this.setEventMarkers = setEventMarkers;
	this.openMarkerWindow = openMarkerWindow;

	this._markers = new Array();
    
     function setEventMarkers()
     {
	   for (i=0; i<_markers.length; i++)
        {
          if (i==0) alert(_markers[i].marker_num);
          //GEvent.addListener(_markers[i], "click", new Function("myGroup.showInfo("+_markers[i].marker_num+");") );
          GEvent.addListener(_markers[i], "click", openMarkerWindow );
	     setMarkerOverlays(i);
        }
     }
     function setMarkerOverlay(i)
     {
          if (i==0) { alert("Setting Overlays: " + i); }
          _googleMap.addOverlay(_markers[i]);
     }  
	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(id, lng, lat, zoom, html, althtml) {
		mnum = this._markers.length;
          point = new GLatLng(lat, lng);
		marker = new GMarker(point, _markerIcon);
		marker.infoHtml = html;
		marker.lat = lat;
		marker.lng = lng;
		marker.zoom = zoom;
		marker.tid = id;
		_altHtmls.push(althtml);
		_infoHtmls.push(html);

		if (this._markers.length == undefined)
		{
		   marker.marker_num = 0;
		} else {
	    	   marker.marker_num = this._markers.length;
		}
		GEvent.addListener(marker, "click", new Function (
		    "myGroup.showInfo("+mnum+");"
              //marker.openInfoWindowHtml(html);
              //alert("You Clicked!!");
		  ));
    	
		this._markers.push(marker);
		return marker;
	}
	
	function displayOrHideMarkers() {
		bounds = _googleMap.getBounds();
		for (i = 0; i < myGroup._markers.length; i++) {
		  if(_markersVisible) {
				marker = _markers[i];
				//if (_markers[i].point.x >= bounds.minX && _markers[i].point.x <= bounds.maxX && _markers[i].point.y >= bounds.minY && _markers[i].point.y <= bounds.maxY && _markers[i].attached == false) {
					//alert(_markers[i].zoom);
					_googleMap.addOverlay(marker);
                         
					_googleMap.panTo(map.getCenter(new GLatLng(marker.lat, marker.lng)), (marker.zoom));
					marker.attached = true;
				//}
			} else {
				if(marker.attached) {
					_googleMap.removeOverlay(marker);
					marker.attached = false;
				}
			}
		}
	}
	function infoText(tMkr, b)
	{
	   //alert(_altHtmls.length + " " + b + " " + tMkr);
	   //alert(_altHtmls[tMkr]);

         if (parseFloat(b) > 1)
         {
            return _altHtmls[tMkr];
         } else {
            return _infoHtmls[tMkr];
         }
	}
	function showInfo(invar)	{
	     //alert("TEST: "+invar);
		map.setCenter(map.getCenter(new GLatLng(myGroup._markers[invar].lat, myGroup._markers[invar].lng)), (myGroup._markers[invar].zoom));
	    myGroup._markers[invar].openInfoWindowHtml(infoText(invar,1));
	    myGroup.iloc = invar;
	    detaildivAptName = document.getElementById("selected_detailsAptName");
		detaildiv = document.getElementById("selected_detailsApt");
		detaildivBr = document.getElementById("selected_detailsBrBath");
		detaildivFeats = document.getElementById("selected_detailsFeats");
		detaildivLink = document.getElementById("selected_detailsLink");
		//alert(detaildiv);
	    if (detaildiv != undefined && detaildiv != "")
	    {
	      
	      //alert(myGroup._markers[invar].tid);
	      //alert(aptDetail.length);
	      //alert(aptDetail[this.tid]["aname"]);
	      detaildivAptName.innerHTML = 
           detaildiv.innerHTML = "<a href=\"index.php?tab=17&property_id="+myGroup._markers[invar].tid+
                                 "\" target=\"_new\">"+
	                            aptDetail[myGroup._markers[invar].tid]["aname"] + "</a><br>";
	      detaildiv.innerHTML = aptDetail[myGroup._markers[invar].tid]["addr"] + "<br>";
	      detaildiv.innerHTML += aptDetail[myGroup._markers[invar].tid]["phone"] + "<br>";
	      detaildiv.innerHTML += "<a href=\"index.php?tab=17&property_id="+myGroup._markers[invar].tid+"\" target=\"_new\">"+
	          aptDetail[myGroup._markers[invar].tid]["email"] + "</a>";
	      detaildiv.innerHTML += aptDetail[myGroup._markers[invar].tid]["image_path"] + "<br/>";
	      detaildivBr.innerHTML = aptDetail[myGroup._markers[invar].tid]["bedbath"];
	      detaildivFeats.innerHTML = aptDetail[myGroup._markers[invar].tid]["features"];
	      detaildivLink.innerHTML = "<a href=\"index.php?tab=17&property_id="+myGroup._markers[invar].tid+"\" target=\"_new\">"+
	          "More..." + "</a><br>";
	      scrollToTop();
	    }	    
	    
	}
	function showInfoTab(invar, b)	{
      myGroup._markers[invar].openInfoWindowHtml(infoText(invar,b));	    
      myGroup.iloc = invar;

	}
	function openMarkerWindow() {
          alert(this.infoHtml);  return;
		//myGroup.iloc = this.marker_num;
          //return true;
		//this.openInfoWindowHtml(this.infoHtml);
	     //map.setCenter(new GLatLng(this.lat, this.lng), (12-this.zoom));
		map.openInfoWindowHtml(new GLatLng(this.lat, this.lng), (this.zoom),  this.infoHtml);		
		myGroup.iloc = this.marker_num;
		detaildivAptName = document.getElementById("selected_detailsAptName");
		detaildiv = document.getElementById("selected_detailsApt");
		detaildivBr = document.getElementById("selected_detailsBrBath");
		detaildivFeats = document.getElementById("selected_detailsFeats");
		detaildivLink = document.getElementById("selected_detailsLink");
		
		//alert(detaildiv);
	    if (detaildiv != undefined && detaildiv != "")
	    {
	      
	      //alert(myGroup._markers[invar].tid);
	      //alert(aptDetail.length);
	      //alert(aptDetail[this.tid]["aname"]);
	      detaildivAptName.innerHTML = "<a href=\"index.php?tab=17&property_id="+this.tid+"\" target=\"_new\">"+
	                  aptDetail[this.tid]["aname"] + "</a><br>";
	      detaildiv.innerHTML = aptDetail[this.tid]["addr"] + "<br>";
	      detaildiv.innerHTML += aptDetail[this.tid]["phone"] + "<br>";
	      detaildiv.innerHTML += aptDetail[this.tid]["email"] + "<br>";
	      detaildiv.innerHTML += "<a href=\"index.php?tab=17&property_id="+this.tid+"\" target=\"_new\">"+
	              aptDetail[this.tid]["image_path"] + "</a>";
	      detaildivBr.innerHTML = aptDetail[this.tid]["bedbath"];
	      detaildivFeats.innerHTML = aptDetail[this.tid]["features"];
	      detaildivLink.innerHTML = "<a href=\"index.php?tab=17&property_id="+this.tid+"\" target=\"_new\">"+
	                  "More..." + "</a><br>";
	      scrollToTop(); 
	    }
	}
	function numMarkers()
	{
	  return this._markers.length;
	}

     function update_tabs (a,b,c) {
         if (a == "page") {
           myGroup.showInfoTab(myGroup.iloc,b);
         }
      }
     dummy_function = function(a,b,c) {};
     //iw = _googleMap.getInfoWindow();
     //iw.addContext("iwstate",update_tabs);
     //iw.addContext("iwnavigate",dummy_function);

}

