/*
 * 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 LocationMarker(point, ct_info) {
	this.info = ct_info;
	this.address_str = ct_info[2];
	
	if(ct_info[6])
		this.coordinates = ct_info[6];
	
	this.marker = null;
	this.land_overlay = null;

	this.point = point;
	
	this.lng = ct_info[1];
	this.lat = ct_info[0];

	this.radiusPolyline = new Array(0);
	this.init(point, ct_info);
	listingMarkers.push(this);
	//this.marker = new GMarker(point);
	//map.addOverlay(this.marker);
}	

/*
 * Initializes the overlay
 *
 * @param info     see ct_info in the comment above
 *
 */
LocationMarker.prototype.init = function(point, info) {
	if(this.marker != null){
		map.removeOverlay(this.marker);
	}
	
	this.point = point;
	
	var icon = new GIcon();
	//icon.image = "/maps/genMarker.php?text=*&color=#FFCC00";	
	icon.image = "/website/power_lines/markers/dd-start.png";	
	icon.iconSize = new GSize(20, 34);
	icon.shadowSize = new GSize(37, 34);
	icon.iconAnchor = new GPoint(10, 30);
	icon.infoWindowAnchor = new GPoint(10, 30);
	
	//var marker = new GMarker(new GPoint(this.lat, this.lng), icon);
	//this.addMarker("http://ctimaps.localhost/website/nice_n_easy/genMarker.php?text=O&color=yellow", point);
	this.addGMarker(point, icon);
	finishLoadingMap();
};

/*
 * Generates the default GMarker
 *
 * @param point 	 latitude/longitude of overlay
 *
 */
LocationMarker.prototype.addGMarker = function(point, icon)
{
	//alert("ADDING MARKER");
	this.marker = new GMarker(point, icon);
	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
 */
LocationMarker.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
 */
LocationMarker.prototype.center_and_zoom = function()
{
	map.setCenter(this.point, 16);
}

/*
 *  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
 */
LocationMarker.prototype.addListener = function (marker, info, lat, lng)
{
	var land_icon_url = "genLandImage.php?locationid=" + info[0] + "&start_n=" + info[22] + "&start_w=" + info[23];
	land_icon_url += "&height=" + this.icon_height + "&width=" + this.icon_width;
	//getMarkerHtml(info[2]);	
	
	//Event listener for clicking on the listing marker on the map
	GEvent.addListener(marker, "click", function() {
		//map.removeOverlay(marker);

		var picOpenWindow = "window.open(\"" + land_icon_url + "&diagram=true\");";
		var view_diagram = "<div onclick='" + picOpenWindow + "'  style=\"cursor: hand\"><font size='1' color='red'>View Diagram</font></div>";

		marker.openInfoWindowHtml(mainMarkerBubbleHtml);
	});
	
	icon_width = this.icon_width;
	icon_height = this.icon_height;
}

/**
 * Centers on the location marker
 *
 */
LocationMarker.prototype.centerOnMarker = function(){
	map.setCenter(new GLatLng(this.lng, this.lat));
}

/*
 * Removes all radii from the map associated with
 * this overlay.
 */
LocationMarker.prototype.removeRadii = function()
{
	try{
		for(var i=0; i< this.radiusPolyline.length; i++)
		{
			map.removeOverlay(this.radiusPolyline[i]);
		}
		
		this.radiusPolyline	= new Array(0);
	}
	catch(e){
		alert(e.message);
	}
}

/*
 * Removes all overlays associated with this
 * object from the map. 
 *
 * ie : radii, selected marker, regular markers
 * 
 *
 */
LocationMarker.prototype.hideMarker = function()
{
	map.removeOverlay(this.marker);
	this.marker = null;
	//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
 *
 */
LocationMarker.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
 */
LocationMarker.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]);
} 

LocationMarker.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;
	
	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);
	map.addOverlay(this.land_overlay, "#000000", 10);
	//alert(polyPointArray);

}

LocationMarker.prototype.getAddress= function()
{
	return this.address_str;
}

LocationMarker.prototype.openInfoWindow= function()
{
	this.marker.openInfoWindowHtml(mainMarkerBubbleHtml);
	//alert(mainMarkerBubbleHtml);

}

function getLandHTML_Directions(info)
{
 		var prop_address = "<div id='the_address'><b>" + info[1] + "<br>" + info[2] + "</b>";
		var mls_address = info[1] + ", " + info[2];
		
		var mlsOpenWindow = "window.open(\"http://uticaromerealtor.localhost/search/SearchQuick.php?mlsnum=" + info[0] + "\");";
		var view_mls = "<br><div onclick='" + mlsOpenWindow + "'  style=\"cursor: hand\"><font size='1' color='red'>View Listing</font></div></div>";
		
		var frm = '<form name="dirs"><div id="my_address" style="display:none"><input type="text" name="search_address" value="" style="width:12em">';
		frm = frm + '<input type=button onclick="getDirections(\'' + mls_address + '\');" value="Get Directions"/></div></form>';
		var directions='<div style="font-size: small; margin-top: 0.4em">Directions:'+" <a href=\"javascript:showDirectionForm(true)\">To here</a> - "+"<a href=\"javascript:showDirectionForm(false)\">From here</a></div>";
	
		var html = prop_address+view_mls+frm+directions;
 
 		return html;
}