/*
 * Created on August 16, 2005
 *
 * Capraro Technologies default map overlay.
 * 
 * @param point    latitude/longitude of overlay
 * @param ct_info  array containing miscellaneous 
 *				   information that can be passed in
 *				   to generate the info window
 *
 */
function ctMarker(point, ct_info) {
	this.point = point;
	this.info = ct_info;
	
	this.lng = ct_info[5];
	this.lat = ct_info[4];

	this.radiusPolyline = new Array(0);
	this.selectionMarker = null;

	if(!isSelected(selectedProperty, ct_info[0]))	
		this.init(point, ct_info);	
		
	//selectMarker(ct_info);
}	

/*
 * Initializes the overlay
 *
 * @param point    latitude/longitude of overlay
 * @param ct_info  see above
 *
 */
ctMarker.prototype.init = function(point, ct_info) {
	listingMarkers.push(this);	
	//this.addMarker("img/COMMERCIAL.gif", point);
	//this.addMarker("genMarker.php?text=O", point);
	//this.addGMarker(point);
};

/*
 *  Listener will bring up the information bubble
 *  when the overlay is clicked.  This will also
 *  mark the overlay as being selected.
 *
 * @param marker   current overlay marker
 * @param info     see ct_info
 */
ctMarker.prototype.addListener = function (marker, info)
{
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(getHTML(info));
	
		//If this marker is not selected yet, push it onto
		//the stack
		//selectMarker(info);
	});
}

/*
 * Generates a marker from an icon and places it on the map
 *
 * @param icon_url   image that will be displayed on the map
 * @param point 	 latitude/longitude of overlay
 *
 */
ctMarker.prototype.addMarker = function(icon_url, point)
{
	var icon = new GIcon();
	icon.image = icon_url;	
	icon.iconSize = new GSize(20, 34);
	icon.iconSize = new GSize(20, 34);
	icon.shadowSize = new GSize(37, 34);
	icon.iconAnchor = new GPoint(24, 24);
	icon.infoWindowAnchor = new GPoint(20, 34);
	
	this.marker = new GMarker(point, icon);
	//this.marker = new GMarker(point);
	map.addOverlay(this.marker);
	this.addListener(this.marker, this.info, this.selectionMarker);
}

/*
 * Generates the default GMarker
 *
 * @param point 	 latitude/longitude of overlay
 *
 */
ctMarker.prototype.addGMarker = function(point)
{
	this.marker = new GMarker(point);
	map.addOverlay(this.marker);
	this.addListener(this.marker, this.info, this.selectionMarker);
}

/*
 * Selects this marker, placing an A, B, C... overlay
 * on top of the current marker.  Then, information on
 * that marker can be displayed
 *
 * @param info     see ct_info
 *
 * This has been deprecated.  This work is done in an
 * external function below.
 */
ctMarker.prototype.selectMarker = function(info)
{
	if(selectedProperty.length <= 9)
	{
		if(!isSelected(selectedProperty, info[0]))
		{
			selectedProperty.push(new Array(info[0], info[5], info[4], info));
			drawSelectionMarkers();
			refreshMenu();
		}
	}
	else
		alert("You have selected the maximum number of properties");
}

/*
 * Removes all overlays associated with this
 * object from the map. 
 *
 * ie : radii, selected marker, regular markers
 * 
 *
 */
ctMarker.prototype.hideMarker = function()
{
	map.removeOverlay(this.marker);
	map.removeOverlay(this.selectionMarker);
	map.closeInfoWindow();
	this.removeRadii();
}

/*
 * Displays the default marker on the screen
 */
ctMarker.prototype.showMarker = function()
{
	map.addOverlay(this.marker);
}

/*
 * Center and zooms on the marker
 */
ctMarker.prototype.center_and_zoom = function()
{
	map.centerAndZoom(this.point, 4);
}

/*
 * Removes all radii from the map associated with
 * this overlay.
 * 
 *
 */
ctMarker.prototype.removeRadii = function()
{
	for(var i=0; i<=this.radiusPolyline.length; i++)
	{
		map.removeOverlay(this.radiusPolyline[i]);
	}
	
	this.radiusPolyline	= new Array(0);
}

/*
 * Initializes the overlay
 *
 * @param mileage    total mileage the circles should cover
 * @param circles_per_mile  
 *                     number of circles that will represent
 *					   1 mile
 *
 */
ctMarker.prototype.drawRadii = function(mileage, circles_per_mile)
{	
	this.removeRadii();
	
	if(this.radiusPolyline.length == 0)
	{
		for(var i=0; i<=mileage * circles_per_mile; i++)
		{
			var radius = i/circles_per_mile;
	
			color = "#FF0000";
			trans = .3;

			this.drawRadius(this.point.x, this.point.y, radius, color, 5, trans, i);
		}
	}
}
/*
 * Draws polylines on the map.  This function draws
 * a "circle".  You can draw other shapes by changing
 * "PGsides" in the code below.
 *
 *
 * @param lng    	longitude
 * @param lat	 	latitude
 * @param PGRadius	radius of the circle
 * @param color		color of the polylines
 * @param width		width of the polylines
 * @param trans		transparency of the polylines
 * @param index		index of the current circle that is being drawn
 */
ctMarker.prototype.drawRadius = function (lng,lat,PGradius, color, width, trans, index)
{

   lng = parseFloat(lng);
   lat = parseFloat(lat);
   var PGcolor = color;   	// polygon color blue
   var PGwidth = width;     // width pixels
   var PGtrans = trans;     // transparency 0 - 1
   var d2r = Math.PI/180;   // degrees to radians
   var r2d = 180/Math.PI;   // radians to degrees
   var PGsides = 36;


   var PGlat = (PGradius/3963)*r2d; // using 3963 miles as earth's radius
   var PGlng = PGlat/Math.cos(lat*d2r);
   var PGpoints = new Array(0);
   for (var i=-1; i < PGsides; i++) {
      var theta = ((2*i+1)/PGsides-0.5)*Math.PI;
      PGx = lng + (PGlng * Math.cos(theta));
      PGy = lat + (PGlat * Math.sin(theta));
      PGpoints.push(new GPoint(PGx,PGy));
   };

	this.radiusPolyline.push(new GPolyline(PGpoints,PGcolor,PGwidth,PGtrans));
	//statusMessage.innerHTML=statusMessage.innerHTML+PGpoints+"<BR>";
	map.addOverlay(this.radiusPolyline[index]);
} 

/*
 * Selects this marker, placing an A, B, C... overlay
 * on top of the current marker.  Then, information on
 * that marker can be displayed
 *
 * @param info     see ct_info
 */
function selectMarker(info)
{
	if(selectedProperty.length <= 9)
	{
		if(!isSelected(selectedProperty, info[0]))
		{
			selectedProperty.push(new Array(info[0], info[5], info[4], info));
			drawSelectionMarkers();
			refreshMenu();
			statusMessage.innerHTML = "Use the menu on the right to get more information about the location you've just selected";
		}
	}
	//else
	//	alert("You have selected the maximum number of properties");
}