// A Rectangle is a simple overlay that outlines a lat/lng bounds on the
// map. It has a border of the given weight and color and can optionally
// have a semi-transparent background color.
function TextNote(coords, text, fontsize, innerColour,outerColour,isBold) {
  this.coords_  = coords;
  this.text_ = text;
  this.fontsize_ = fontsize;
  this.innerColour_=innerColour;
  this.outerColour_=outerColour;
  this.isBold_=isBold || false;
}
TextNote.prototype = new GOverlay();

TextNote.prototype.createLabelPart = function (top, left, colour)
{
  var tn;
  var indiv;
    
  indiv = document.createElement("div");
  indiv.style.color = colour;
  if (this.isBold_)
	indiv.style.fontWeight = "bold";
  indiv.style.fontSize = this.fontsize_;
  indiv.style.position = "absolute";
  indiv.style.whiteSpace = "nowrap";
  indiv.style.top = top;
  indiv.style.left = left;
  tn = document.createTextNode(this.text_)
  indiv.appendChild(tn);
  return indiv;
}
TextNote.prototype.createLabelBundle = function ()
{
  var div = document.createElement("div");
  div.style.position = "absolute";
  //div.style.border = "1px solid #ffffff";
  div.appendChild(this.createLabelPart('0px','0px',this.outerColour_));
  div.appendChild(this.createLabelPart('2px','0px',this.outerColour_));
  div.appendChild(this.createLabelPart('0px','2px',this.outerColour_));
  div.appendChild(this.createLabelPart('2px','2px',this.outerColour_));  
  div.appendChild(this.createLabelPart('1px','1px',this.innerColour_));
  return div;
}

// Creates the DIV representing this rectangle.
TextNote.prototype.initialize = function(map) {
  // Our rectangle is flat against the map, so we add our selves to the
  // MAP_PANE pane, which is at the same z-index as the map itself (i.e.,
  // below the marker shadows)
  this.div_ = this.createLabelBundle();
  map.getPane(G_MAP_MAP_PANE).appendChild(this.div_);
  this.map_ = map; 
}

// Remove the main DIV from the map pane
TextNote.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
}

// Copy our data to a new Rectangle
TextNote.prototype.copy = function() {
  return new TextNote(this.coords_, this.text_);
}

// Redraw the rectangle based on the current projection and zoom level
TextNote.prototype.redraw = function(force) {
  // We only need to redraw if the coordinate system has changed
  if (!force) return;

  // Calculate the DIV coordinates of two opposite corners of our bounds to
  // get the size and position of our rectangle
  var c1 = this.map_.fromLatLngToDivPixel(this.coords_);
  //var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());

  // Now position our DIV based on the DIV coordinates of our bounds
  this.div_.style.width = "100px";
//  this.div_.style.height = Math.abs(c2.y - c1.y) + "px";
  this.div_.style.left = ( c1.x)+ "px";
  this.div_.style.top = (c1.y) + "px";
}
var theMap;
var LastCoords;
function panTo(LatLong)
{
LastCoords = LatLong;
theMap.panTo(LastCoords);
}
function Setup()
{
if (GBrowserIsCompatible()) {
window.onunload = GUnload;
var map = new GMap2(document.getElementById("map"));
theMap = map;
LastCoords = new GLatLng(Lat, Long);
map.setCenter(LastCoords, Zoom,G_NORMAL_MAP);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());

var ovcontrol = new GOverviewMapControl(new GSize(150,150)); 
map.addControl(ovcontrol);
var point = new GLatLng(Lat, Long);

var icon = new GIcon();
icon.image = "Images/Maps/mm_20_reddot.png";
icon.shadow = "Images/Maps/mm_20_shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);
//var marker = new GMarker(point, icon);
/*map.addOverlay(marker);
GEvent.addListener(marker, "click", function() {
marker.showMapBlowup({zoomLevel:Zoom,mapType:G_HYBRID_MAP});
});*/
//GEvent.addListener(theMap, "click", function(overlay,point){ GLog.write(point) });
// Display a rectangle in the center of the map at about a quarter of
// the size of the main map
//map.addOverlay(new TextNote(new GLatLng(Lat, Long), TownName, '10pt','#ffffff','#000000',true));
if (HouseArray.length>0)
{	
var addMapHandler = false;
for (i=0;i<HouseArray.length;i++)
 {
   if (!(HouseArray[i][4]))
   {
		map.addOverlay(createMarker(new GLatLng(HouseArray[i][2],HouseArray[i][3]),HouseArray[i][1],true));
		map.addOverlay(new TextNote(new GLatLng(HouseArray[i][2],HouseArray[i][3]), HouseArray[i][0], '9pt','#ccccff','#0000ff',true));
	}
	else
	{
		var marker = createMarker(new GLatLng(HouseArray[i][2],HouseArray[i][3]),HouseArray[i][1],false);
		marker.url = HouseArray[i][0];
		map.addOverlay(marker);
		addMapHandler = true;
	}
}
	if (addMapHandler)
	{
		GEvent.addListener(theMap, "click", function(overlay,point){ window.open(overlay.url,'_top'); });
	}
}

}

var mapSetupTimer = setInterval(
	function() {
		if (ovcontrol)
		{
			clearTimeout(mapSetupTimer);
			mapSetupTimer=null;
			ovcontrol.getOverviewMap().setMapType(G_HYBRID_MAP);
		}
	},500);
}


function positionOverview(x,y) {
var omap=document.getElementById("map_overview");
omap.style.left = x+"px";
omap.style.top = y+"px";

// == restyling ==
omap.firstChild.style.border = "1px solid gray";

omap.firstChild.firstChild.style.left="1px";
omap.firstChild.firstChild.style.top="1px";
omap.firstChild.firstChild.style.width="147px";
omap.firstChild.firstChild.style.height="147px";
}
// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, index, inert) {
  // Create a lettered icon for this point using our icon class
  var letter = String.fromCharCode("A".charCodeAt(0) + index);
/*  var icon = new GIcon(baseIcon);
  icon.image = "Images/Maps/mm_20_red" + letter + ".png";
  var marker = new GMarker(point, icon);*/

var icon = new GIcon();
icon.image = "Images/Maps/mm_20_red" + letter + ".png";
icon.shadow = "Images/Maps/mm_20_shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);
var marker = new GMarker(point, icon, inert);

 /* GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("Marker &lt;b&gt;" + letter + "&lt;/b&gt;");
  });*/
  return marker;
}

var ResizeTimeOut=null;
function resizeMap() {
if (MapOn)
{	var mapDiv=document.getElementById("map");
	mapDiv.style.width = (document.body.clientWidth -290) + 'px';
	if (ResizeTimeOut!=null)
	{clearTimeout(ResizeTimeOut);ResizeTimeOut=null;}
	ResizeTimeOut = setTimeout(function() {
		try {
			var omap=document.getElementById("map_overview");
			if (parseInt(mapDiv.style.width) > 600)
			{
				omap.style.visibility='visible';
				//omap.style.position="absolute";
				//positionOverview(mapDiv.clientLeft+parseInt(mapDiv.style.width)-150,mapDiv.offsetTop+parseInt(mapDiv.style.height)-150);
			}
			else
				omap.style.visibility='hidden';
			} catch (e){}theMap.panTo(LastCoords);
		},100)
}
}