$(function() {
	refreshFancyBox();
});

function refreshFancyBox() {
	$("a.fancybox").fancybox({
		'callbackOnStart': 
	    function() { 
	    	if (navigator.userAgent.toLowerCase().indexOf('msie') < 0) {
		    	$('#google_earth_wrapper').hide();
		    }
      },
     'callbackOnClose': 
	    function() { 
	    	if (navigator.userAgent.toLowerCase().indexOf('msie') < 0) {
		    	$('#google_earth_wrapper').show();
		    }
      },     
		'overlayShow' : true,
		'overlayOpacity' : 0.6
	});
	
	$('a.view_gallery').click(function() {
		$(this).parent().parent('div.gallery').find(':first').click();
		return false;
	});
}

function loadMarkerViaAjax(url) {

	$('#marker_content').block({ 
		message: $('#blockMessage'),
		css: { 
		 border: 'none', 
		 padding: '5px 10px', 
		 backgroundColor: 'transparent' 
		}
	});
	$('#marker_content').load(url+'?ajax',{},function() {
		refreshFancyBox();
	});
	$('#marker_content').unblock();
}

// See: http://code.google.com/p/earth-api-samples/issues/detail?id=141

function displayNoneWorkaround(earthNodeID) {
  // IE isn't affected by this bug
  if (navigator.userAgent.toLowerCase().indexOf('msie') < 0) {
    // Firefox, Safari, etc. are affected
    var earthNode = document.getElementById(earthNodeID);
    var earthWatchNode = earthNode.parentNode;
    
    earthNode.style.position = 'absolute';
    earthNode.style.left = earthNode.style.top = '0';
    earthNode.style.width = earthNode.style.height = '0';
    document.body.appendChild(earthNode);
    
    function repositionEarth() {
      earthNode.style.height = earthWatchNode.offsetHeight + 'px';
      earthNode.style.width = earthWatchNode.offsetWidth + 'px';
      
      // calculate position on page
      var left = earthWatchNode.offsetLeft;
      var top = earthWatchNode.offsetTop;
      var p = earthWatchNode.offsetParent;
      
      while (p && p != document.body) {
        if (isFinite(p.offsetLeft) && isFinite(p.offsetTop)) {
          left += p.offsetLeft;
          top += p.offsetTop;
        }
        
        p = p.offsetParent;
      }
      
      earthNode.style.left = left + 'px';
      earthNode.style.top = top + 'px';
    }
    
    repositionEarth();
    
    // reposition every 100ms
    window.setInterval(function() {
      repositionEarth();
    }, 100);
  }
}

function addMyMarker(name, url, latitude, longitude, focus, ajax) {
	var placemark = ge.createPlacemark('');
	placemark.setName(name);
	ge.getFeatures().appendChild(placemark);
	
	// Create style map for placemark
	
	// NOTE: we need to use JC as the marker here
	
	var normal = ge.createIcon('');
	normal.setHref('http://www.cousteau.org/google_repository/jc.gif');
	var iconNormal = ge.createStyle('');
	iconNormal.getIconStyle().setIcon(normal);
	var highlight = ge.createIcon('');
	highlight.setHref('http://www.cousteau.org/google_repository/jc.gif');
	var iconHighlight = ge.createStyle('');
	iconHighlight.getIconStyle().setIcon(highlight);
	var styleMap = ge.createStyleMap('');
	styleMap.setNormalStyle(iconNormal);
	styleMap.setHighlightStyle(iconHighlight);
	placemark.setStyleSelector(styleMap);
	  
	// Disable placemark bubbles:
  google.earth.addEventListener(placemark, "click", function(event) {
		event.preventDefault();
		if (ajax) {
			loadMarkerViaAjax(url);
		}
		else {
			document.location = url;
			return false;
		}
		// Ajax load goes here		    
    var lookAt = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);
		lookAt.setLatitude(event.getLatitude());
		lookAt.setLongitude(event.getLongitude());
		// lookAt.setAltitude(4000000);
		ge.getView().setAbstractView(lookAt);
	}); 				
	
	// Create main [presumably "starting"] point for Expedition
	var point = ge.createPoint('');
	point.setLatitude(latitude);
	point.setLongitude(longitude);
	placemark.setGeometry(point);
	
	if (focus) {
		var lookAt = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);
		lookAt.setLatitude(point.getLatitude());
		lookAt.setLongitude(point.getLongitude());
		lookAt.setAltitude(2000000);
		ge.getView().setAbstractView(lookAt);
	}
					
	return false;
	
}
function focusOn(latitude,longitude) { 
	var lookAt = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);
	lookAt.setLatitude(latitude);
	lookAt.setLongitude(longitude);
	// lookAt.setAltitude(4000000);
	ge.getView().setAbstractView(lookAt);
}

$(function() {
	$('#ogone_submit').click(function() {
		$('#ogone_form').submit();
		return false;
	});
});

