 var firstPointX=0;
 var firstPointY=0;
 var north=0;
 var south=0;
 var east=0;
 var west=0;
var interactiveMap;
var interactivePolygon;
var interactiveMarker1;
var interactiveMarker2;

 function createInteractiveMap()
 {
      if (GBrowserIsCompatible())
      {
 		interactiveMap = new GMap2(document.getElementById("interactivemap"));
        //interactiveMap.setCenter(new GLatLng(50.5253,0.5972), 2);
        interactiveMap.setCenter(new GLatLng(58.75, -7), 3);
        //interactiveMap.addControl(new GSmallMapControl());
        interactiveMap.setMapType(G_PHYSICAL_MAP);
        //interactiveMap.addControl(new GMapTypeControl());
		//interactiveMap.enableDoubleClickZoom();
		interactiveMap.enableContinuousZoom();
		interactiveMap.enableScrollWheelZoom();
		showInteractiveMenu();

        GEvent.addListener(interactiveMap,"click", function(overlay,latlng)
        {
          if (overlay)
          {
            interactiveMap.clearOverlays();
            north=0;
 			south=0;
 			east=0;
 			west=0;
            return;
          }

          var tilePoint = new GPoint();
          var currentProjection = G_PHYSICAL_MAP.getProjection();
          tilePoint = currentProjection.fromLatLngToPixel(latlng, map.getZoom());
          if(interactiveMarker1==null)
			{
			interactiveMap.clearOverlays();
			interactiveMarker1 = new GMarker(latlng);
			interactiveMap.addOverlay(interactiveMarker1);
			}
			else
			{
			interactiveMarker2 = new GMarker(latlng);
			interactiveMap.addOverlay(interactiveMarker2);
			interactiveMarker1=null;

			}
          if(firstPointX==0&&firstPointY==0)
          {
          	firstPointX=latlng.lng();
          	firstPointY=latlng.lat();
          	north=0;
 			south=0;
 			east=0;
 			west=0;



          }
         else
         {

          	if(latlng.lng()<firstPointX)
         	{
  				east=firstPointX;
 				west=latlng.lng();
         	}
         	else
         	{
     				east=latlng.lng();
 					west=firstPointX;
         	}
         	if(latlng.lat()<firstPointY)
         	{
  				north=firstPointY;
 				south=latlng.lat();

         	}
         	else
         	{
     				north=latlng.lat();
 					south=firstPointY;
         	}
         	firstPointX=0;
 			firstPointY=0;
         }
 		if(north!=0 && south!=0 )
		{

			drawInteractivePolygon();
		}

        });
      }
 }


 function GetNorth()
 {
 	return north;
 }

 function GetSouth()
 {
 	return south;
 }
 function GetEast()
 {
 	return east;
 }
 function GetWest()
 {
 	return west;
 }

function reinitialiseInteractiveBBoxMap()
{
	if(interactiveMap!=null)
	{
		removeInteractivePolygon();
		createInteractiveMap();
	}
}

 /***
 BOUNDING BOX
 Draw the bounding box of the selected item on top of the map.
***/
function drawInteractivePolygon(northvalue,southvalue,eastvalue,westvalue)
{
	north=northvalue;
	south=southvalue;
	east=eastvalue;
	west=westvalue;
	drawInteractivePolygon();

}

function drawInteractivePolygon()
{
	// GPolygon(points,  strokeColor?,  strokeWeight?,  strokeOpacity?,  fillColor?,  fillOpacity?,  opts?)

	removeInteractivePolygon();
	if((north==0)||(south==0)||(east==0)||(west==0))
		return;
	var bounds = new GLatLngBounds();
	var topLeft = new GLatLng(north,west);
	var bottomRight = new GLatLng(south,east);
	bounds.extend(topLeft);
	bounds.extend(bottomRight);
	var zoom=interactiveMap.getBoundsZoomLevel(bounds);
	if(zoom==0)
		zoom=interactiveMap.getZoom();
	if(zoom==0)
		zoom=3;
	interactiveMap.setMapType(G_PHYSICAL_MAP);

	interactivePolygon = new GPolygon([
	new GLatLng(north, west),
	new GLatLng(south, west),
	new GLatLng(south, east),
	new GLatLng(north, east),
	new GLatLng(north, west)
	], "#3a8dc3", 1, .6, "#3a8dc3", .4);
	interactiveMap.addOverlay(interactivePolygon);


	var centerX=north+(27/zoom);
	var centerY=west-(45/zoom);

	var center=	new GLatLng(centerX,centerY);

	//interactiveMap.setCenter(center, zoom);

}


function removeInteractivePolygon(){
	if(interactivePolygon)
		interactiveMap.removeOverlay(interactivePolygon);
	interactiveMap.clearOverlays();
}

/***
 MAP CONTROL MENUE
***/
function showInteractiveMenu(){
	function TextualZoomControl() {
	}
	TextualZoomControl.prototype = new GControl();
	TextualZoomControl.prototype.initialize = function(interactiveMap) {
		var container = document.createElement("div");
		var zoomInDiv = document.createElement("div");
		this.setButtonStyle_(zoomInDiv);
		container.appendChild(zoomInDiv);
		zoomInDiv.innerHTML = '<img src="images/icons/zoom-in.gif" title="Zoom In">';
		GEvent.addDomListener(zoomInDiv, "click", function() {
			interactiveMap.zoomIn();
		});
		var zoomOutDiv = document.createElement("div");
		this.setButtonStyle_(zoomOutDiv);
		container.appendChild(zoomOutDiv);
		zoomOutDiv.innerHTML = '<img src="images/icons/zoom-out.gif" title="Zoom Out">';
		GEvent.addDomListener(zoomOutDiv, "click", function() {
			interactiveMap.zoomOut();
		});
		var panLeftDiv = document.createElement("div");
		this.setButtonStyle_(panLeftDiv);
		container.appendChild(panLeftDiv);
		panLeftDiv.innerHTML = '<img src="images/icons/arrow-left.gif" title="Pan Left">';
		GEvent.addDomListener(panLeftDiv, "click", function() {
			interactiveMap.panDirection(1, 0);
		});
		var panRightDiv = document.createElement("div");
		this.setButtonStyle_(panRightDiv);
		container.appendChild(panRightDiv);
		panRightDiv.innerHTML = '<img src="images/icons/arrow-right.gif" title="Pan Right">';
		GEvent.addDomListener(panRightDiv, "click", function() {
			interactiveMap.panDirection(-1, 0);
		});
		var panUpDiv = document.createElement("div");
		this.setButtonStyle_(panUpDiv);
		container.appendChild(panUpDiv);
		panUpDiv.innerHTML = '<img src="images/icons/arrow-up.gif" title="Pan Up">';
		GEvent.addDomListener(panUpDiv, "click", function() {
			interactiveMap.panDirection(0, 1);
		});
		var panDownDiv = document.createElement("div");
		this.setButtonStyle_(panDownDiv);
		container.appendChild(panDownDiv);
		panDownDiv.innerHTML = '<img src="images/icons/arrow-down.gif" title="Pan Down">';
		GEvent.addDomListener(panDownDiv, "click", function() {
			interactiveMap.panDirection(0, -1);
		});
		var resetDiv = document.createElement("div");
		this.setButtonStyle_(resetDiv);
		container.appendChild(resetDiv);
		resetDiv.innerHTML = '<img src="images/icons/move.gif" title="Reset Zoom and Position">';
		GEvent.addDomListener(resetDiv, "click", function() {
			interactiveMap.setCenter(new GLatLng(50.5253,0.5972), 3);
			removeInteractivePolygon();
		});

		interactiveMap.getContainer().appendChild(container);
		return container;
	}
	TextualZoomControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
	}
	TextualZoomControl.prototype.setButtonStyle_ = function(button) {
		button.style.color = "#0000cc";
		button.style.backgroundColor = "white";
		button.style.padding = "2px";
		button.style.marginBottom = "3px";
		button.style.width = "16px";
		button.style.height = "16px";
		button.style.cursor = "pointer";
	}
	interactiveMap.addControl(new TextualZoomControl());
}
