/*
 * Created on August 18, 2005
 *
 * Capraro Technologies default map overlay for land.
 * 
 * @param point    latitude/longitude of overlay
 * @param ct_info  array containing miscellaneous 
 *				   information that can be passed in
 *				   to generate the info window
 *
 */
function LandMarker(point, ct_info, generic_id) {
	this.info = ct_info;
	
	if(ct_info[6])
		this.coordinates = ct_info[6];
	
	this.marker = null;
	this.land_overlay = null;

	this.point = point;
	
	this.lng = ct_info[5];
	this.lat = ct_info[4];
	this.generic_name_id = generic_id;

	this.radiusPolyline = new Array(0);
	this.id = parseInt(ct_info[0]);

	this.init(point, ct_info);
	this.html = ct_info[9];
	
	//alert("ID: " + this.id);
	listingMarkers.push(this);
	//this.marker = new GMarker(point);
	//map.addOverlay(this.marker);
}	

/*
 * Initializes the overlay
 *
 * @param info     see ct_info in the comment above
 *
 */
LandMarker.prototype.init = function(point, info) 
{
	landMarkers.push(this);
	this.drawPolylines(this.coordinates);
	
	if(this.generic_name_id != 1)
		this.addGMarker(point);
};

/*
 * Generates the default GMarker
 *
 * @param point 	 latitude/longitude of overlay
 *
 */
LandMarker.prototype.addGMarker = function(point)
{
	//alert("ADDING MARKER");
	this.marker = new GMarker(point);
	map.addOverlay(this.marker);
	this.addListener(this.marker, this.info, this.selectionMarker);
}
/*
 * Generates a marker along with polylines to 
 * mark an outline and places it on the map
 *
 * @param latArray   array at latitude points
 * @param lngArray 	 array of longitude points
 */
LandMarker.prototype.addMarker = function(latArray, lngArray)
{
	/**
		First, get the outside edges of the land.
		Then, draw a rectangle around it.  The marker
		will be put in the center of this rectangle.
	**/	
	var west = getMax(latArray);
	var north = getMax(lngArray);
	var east = getMin(latArray);
	var south = getMin(lngArray);
	
	//drawOutline(east, west, north, south);
	
	var lr = ((east - west)/2);
	var ud = ((north - south)/2);
	
	this.info[20] = (east-west) * 95000; //width of land outline
	this.info[21] = (north-south) * 127000;//height of land outline
	this.info[22] = north;
	this.info[23] = west;
	
	this.icon_width = (east-west);//The scale for these vars is set in the genLandImage.php
	this.icon_height = (north-south);
	
	var centerEW = parseFloat(lr) + parseFloat(west);
	var centerNS = parseFloat(ud) + parseFloat(south);
	
	//map.centerAndZoom(new GPoint(centerEW, centerNS), 3);
	
	this.longitude = centerNS;
	this.latitude = centerEW;
	
	this.marker = new GMarker(new GPoint(this.lat, this.lng));
	//this.marker = new GMarker(new GPoint(centerEW, centerNS));
	//this.point = new GPoint(centerEW, centerNS);
	
	//map.addOverlay(this.marker);
	//this.addListener(this.marker, this.info, centerEW, centerNS);
	//this.addListener(this.marker, this.info, this.lng, this.lat);

}

/*
 * Center and zooms on the marker
 */
LandMarker.prototype.center_and_zoom = function()
{
	alert("SET CENTER");
	map.setCenter(this.point, 4);
}

/*
 *  Listener will bring up the information bubble
 *  when the overlay is clicked.  This will also
 *  mark the overlay as being selected.
 *
 *  Zoom listener will overlay the image of the land
 *  when zoomed in all the way.
 *
 * @param marker   current overlay marker
 * @param info     see ct_info
 * @param lng    	longitude
 * @param lat	 	latitude
 */
LandMarker.prototype.addListener = function (marker, info)
{
	//Event listener for clicking on the listing marker on the map
	GEvent.addListener(marker, "click", function() {

		marker.openInfoWindowHtml(info[9]);
		
		selectMarker(info);

	});
	
	icon_width = this.icon_width;
	icon_height = this.icon_height;
}

/*
 * Removes all radii from the map associated with
 * this overlay.
 */
LandMarker.prototype.removeRadii = function()
{
	for(var i=0; i<=this.radiusPolyline.length; i++)
	{
		map.removeOverlay(this.radiusPolyline[i]);
	}
	
	this.radiusPolyline	= new Array(0);
}

/*
 * Removes all overlays associated with this
 * object from the map. 
 *
 * ie : radii, selected marker, regular markers
 * 
 *
 */
LandMarker.prototype.hideMarker = function()
{
	map.removeOverlay(this.marker);
	//map.removeOverlay(this.land_overlay);
	map.closeInfoWindow();
	//this.removeRadii();
}

/*
 * Initializes the overlay
 *
 * @param mileage    total mileage the circles should cover
 * @param circles_per_mile  
 *                     number of circles that will represent
 *					   1 mile
 *
 */
LandMarker.prototype.drawRadii = function(mileage, circles_per_mile)
{	
	this.removeRadii();
	//alert(this.point.x + ":" + this.point.y);
	
	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, 3, 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
 */
LandMarker.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]);
} 

LandMarker.prototype.drawPolylines= function(polyArray)
{
	//document.getElementById("message").innerHTML = gPointArray;

	var tokenArray = polyArray.split("),(");
		//alert(tokenArray.length);
	var polyPointArray = new Array(0);
	var localXArray = new Array(0);
	var localYArray = new Array(0);
	
	var lastLat;
	var lastLng;
	
	//alert(this.id);
	if(this.id > 32 && this.id < 37){
		color = "#ff0000";
	}
	else if (this.id > 36) {
		color = "#C71585";
	}
	else{
		color = "#00008B";
	}
		
	for(var i=0; i<tokenArray.length; i++)
	{
		
		tokenArray[i] = tokenArray[i].replace("(", "");
		tokenArray[i] = tokenArray[i].replace(")", "");
		var coordinates = tokenArray[i].split(",");
					
		lastLat = coordinates[0];
		lastLng = coordinates[1];
		localXArray.push(lastLat);
		localYArray.push(lastLng);
		polyPointArray.push(new GPoint(coordinates[0], coordinates[1]));
	}		
	
	this.addMarker(localXArray, localYArray, polyPointArray);
	this.land_overlay = new GPolyline(polyPointArray, color,4,0.5);
	map.addOverlay(this.land_overlay);
	//alert(polyPointArray);
}
