var curObjectID;
var popUpWin;
var minimap;
var popUpWinVisible = false;
var interval = 1000;

var mouseOverEventListener;
var mouseOutEventListener;

function openContextPopupWin(objectID, lat, lng) 
{	
	//debugWrite('openContextPopupWin:' + objectID);
	if(popUpWin == null) {
		popUpWin = document.createElement('div');
		
		Element.extend(popUpWin);
		
		popUpWin.id = 'contextPopupWindow';
		
		popUpWin.setStyle( { 
			display : 'visible',
			border : 'solid 1px',
			position : 'absolute',
			height : '150px',
			width : '250px',
			backgroundcolor : 'white',
			zindex : '998'
		});
		
		document.body.appendChild(popUpWin);
	}

	if(curObjectID != null && curObjectID == objectID) {
		//debugWrite('openContextPopupWin:' + objectID + "==" + curObjectID);
	
		//new Ajax.Updater("contextPopupWindow", url, { method: 'get' })
		//$("contextPopupWindow").update();
		
		var x = Element.cumulativeOffset($(objectID)).left;
		var y = Element.cumulativeOffset($(objectID)).top;
		
		var yOffset = 0 - Element.getHeight("contextPopupWindow");
		var xOffset = Element.getWidth(objectID) / 2;
	
		Element.clonePosition($("contextPopupWindow"), curObjectID, { setWidth: false, setHeight: false, offsetLeft: xOffset, offsetTop: yOffset});
		//$("contextPopupWindow").setStyle( { top : y + 'px;', left : x + 'px;' });
		initializeMiniMap(lat, lng);
		
		Effect.Appear("contextPopupWindow");
		
		//Event.observe($("contextPopupWindow"), 'mouseover', contextPopupWinMouseOver);
		mouseOverEventListener = GEvent.addListener(minimap, "mouseover", contextPopupWinMouseOver);
		
		setTimeout("setUpPopupCloseEvent();",1500);
		popUpWinVisible = true;
	}
}

function setUpPopupCloseEvent()
{
	//Event.observe($("contextPopupWindow"), 'mouseout', contextPopupWinMouseOut);
	mouseOutEventListener = GEvent.addListener(minimap, "mouseout", contextPopupWinMouseOut);
}

function contextPopupWinMouseOver(event, lat, long)
{
	curObjectID = "contextPopupWindow";
	//debugWrite('contextPopupWinMouseOver:' + curObjectID);
}

function contextPopupWinMouseOut(event)
{
	//debugWrite('contextPopupWinMouseOut:' + curObjectID);

	Effect.Fade("contextPopupWindow");
	GEvent.removeListener(mouseOverEventListener);
	GEvent.removeListener(mouseOutEventListener);
	popUpWinVisible == false;
}

function contextPopupHrefMouseOver(event, lat, long)
{
	curObjectID = Event.element(event).id;	
	//debugWrite('contextPopupHrefMouseOver:' + curObjectID);

	setTimeout("openContextPopupWin('" + curObjectID + "'," + lat + "," + long + ");",interval);
	Event.observe(Event.element(event), 'mouseout', contextPopupHrefMouseOut);
}

function contextPopupHrefMouseOut(event)
{	
	var eventObjID = Event.element(event).id;
	//Let's give the browser time to fire any mouseover events before completing this logic
	setTimeout("contextPopupHrefMouseOut1('" + eventObjID + "');",100);
}

function contextPopupHrefMouseOut1(eventObjID)
{	
	//debugWrite('contextPopupHrefMouseOut1:' + eventObjID);
	
	if(curObjectID == eventObjID) {
		//debugWrite('contextPopupHrefMouseOut1:' + curObjectID + "==" + eventObjID);
		curObjectID = null;
		Event.stopObserving($(eventObjID), 'mouseout', contextPopupHrefMouseOut);
		
		if(popUpWinVisible == true) {
			Effect.Fade("contextPopupWindow");
			popUpWinVisible == false;
		}
	}
}

function debugWrite(text) {
	$('debug1').value += text + "\n";
}

var curDisplay = Cookie.get('curDisplay');

if(curDisplay == null) {
  	curDisplay = 'boatText';
    Cookie.set('curDisplay',curDisplay,1);
}
   
function switchDisplay(newDisplay, textOn, textOff)
{
	$(textOn).setStyle({ fontWeight:'bold' });
	$(textOff).setStyle({ fontWeight:'normal' });
	
	if(newDisplay != curDisplay) {
		Effect.Fade(curDisplay);
		Effect.Appear(newDisplay);
  			
		curDisplay = newDisplay;
   		Cookie.set('curDisplay',newDisplay,1);
   	}
}
	
function setDisplay()
{
	Effect.Appear(curDisplay);
	
	$(curDisplay + 'Link').setStyle({ fontWeight:'bold' });
}

function initializeMiniMap(lat, lng) {
  if (GBrowserIsCompatible()) {
	if(minimap == null) {
    	minimap = new GMap2(document.getElementById('contextPopupWindow'));
	}
	
    minimap.setCenter(new GLatLng(lat, lng),15);

	minimap.setMapType(G_SATELLITE_MAP);
    //minimap.addControl(new GSmallMapControl());
    minimap.addControl(new GMapTypeControl());

    // 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, text) {
       var blueIcon = new GIcon(G_DEFAULT_ICON);
       blueIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";         

      // Set up our GMarkerOptions object
      markerOptions = { icon:blueIcon };
      var marker = new GMarker(point, markerOptions);

      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(text);
      });
      return marker;
    }

    var latlng = new GLatLng(lat, lng);
    
    minimap.addOverlay(createMarker(latlng, ""));
  }
}

function addPoint(lat, long, locationTypeID) 
{
      var point = new GLatLng(lat,long);
 
		  var ic = new GIcon();

		   //ic.iconSize = new GSize(46, 29);
		   ic.iconSize = new GSize(44, 34);
		   ic.iconAnchor = new GPoint(23, 15);
		   ic.infoWindowAnchor= new GPoint(23, 27);
		   ic.shadow = "";

   //Boat Access Point
   if(locationTypeID == 5)
   {
    	 ic.image = "../../images/icon_ramp.png";         
   }

   //Marina
   if(locationTypeID == 6)
   {
    	 ic.image = "../../images/icon_marina.png";  
   }
   
   //Historical/Cultural Sites / Route Location
   if(locationTypeID == 7 || locationTypeID == 13)
   {
    	 ic.image = "../../images/icon_interest.png";
   }

  // Set up our GMarkerOptions object
      markerOptions = { icon:ic };
      
      curMarker = new GMarker(point, markerOptions);
      map.addOverlay(curMarker);
      
      map.panTo(point);
}

function removePoint()
{
	//map.removeOverlay(curMarker);	
}

function moveMap(event)
{
	var pos = $('contentholderText').cumulativeOffset()[1];
	var offset = $('contentholderText').cumulativeScrollOffset()[1];
	var newpos = offset - pos;
	
	if(newpos + 10 > 0) 
	{
		$('mapSmall').setStyle({ top:(newpos + 75) + 'px' });
	} else {
		$('mapSmall').setStyle({ top:'45px' });
	}
}

    function initializeMap() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById('mapSmall'));
        map.setCenter(new GLatLng(39.92, -75.14), 15);
		map.setMapType(G_SATELLITE_MAP);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());

        // 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, text) 
        {
           var blueIcon = new GIcon(G_DEFAULT_ICON);
           blueIcon.image = "http://maps.google.com/mapfiles/ms/micons/sailing.png";         

          // Set up our GMarkerOptions object
          markerOptions = { icon:blueIcon };
          var marker = new GMarker(point, markerOptions);

          GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(text);
          });
          return marker;
        }
      } 
    }

