

/* ************************************************************************** */
function extentClick(latlng)
{
	if(!(latlng && latlng.lat && latlng.lng)){ return; }
	
	// Close any info window that feels like opening
	_liveblue.map.closeInfoWindow();
	
	// Add the marker
	addMarker(latlng);
}
/* ************************************************************************** */


/* ************************************************************************** */
function addMarker(latlng)
{
	if(!(latlng && latlng.lat && latlng.lng)){ return; }
	
	// If this is the first click (marker doesn't exist yet), track the click
	if(!_liveblue.marker){ trackEvent("/" + _liveblue.extent_id + "/plot_click"); }
	
	// If there's a marker on the map already, remove it.
	if(_liveblue.marker){ _liveblue.map.removeOverlay(_liveblue.marker); }
	
	// Create the marker and add it to the map
	_liveblue.marker = new GMarker(latlng, { icon: getActiveCubeIcon(), draggable: true });
	_liveblue.map.addOverlay(_liveblue.marker);
	
	// Update and activate the cube
	updateCube(_liveblue.marker.getLatLng());
	activateCube(_liveblue.marker.getLatLng());
	
	// Snap the marker to the proper location
	snapToSecond(_liveblue.marker, _liveblue.marker.getLatLng());
	
	// Event handler: dragstart
	GEvent.addListener(_liveblue.marker, "dragstart", function()
	{
		_liveblue.map.closeInfoWindow();
		
		// Hold on to the coords of the marker before we drag it, so we can snap back if need be
		drag_start_lat = _liveblue.marker.getLatLng().lat();
		drag_start_lng = _liveblue.marker.getLatLng().lng();
	});
	
	/* 
	// NOTE Eliminated drag updating for performance reasons with Contains()
	// Event handler: drag
	GEvent.addListener(_liveblue.marker, "drag", function()
	{
		updateCube(_liveblue.marker.getLatLng());
		
		if(extentContains(_liveblue.marker.getLatLng()))
		{
			activateCube(_liveblue.marker.getLatLng());
		}
		else
		{
			deactivateCube();
		}
	});
	*/
	
	// Event handler: dragend
	GEvent.addListener(_liveblue.marker, "dragend", function()
	{
		// Start spinner, this could take a bit in some browsers
		$("#spinner").show();
		
		// Check to see if the drag endpoint is in our extent
		if(extentContains(_liveblue.marker.getLatLng()))
		{
			// Snap the cube to its proper location
			snapToSecond(_liveblue.marker, _liveblue.marker.getLatLng());
			
			// Update and activate the cube
			updateCube(_liveblue.marker.getLatLng());
			activateCube(_liveblue.marker.getLatLng());
		}
		else
		{
			// Snap the cube back to its original location, pre-drag
			snapToSecond(_liveblue.marker, new GLatLng(drag_start_lat, drag_start_lng));
			
			// Update and activate the cube
			updateCube(new GLatLng(drag_start_lat, drag_start_lng));
			activateCube(new GLatLng(drag_start_lat, drag_start_lng));
		}
		$("#spinner").hide();
	});
}
/* ************************************************************************** */


/* ************************************************************************** */
function updateCube(latlng)
{
	if(!(latlng && latlng.lat && latlng.lng)){ return; }
	
	// Update any displayed lat/lng text
	$(".lat").html(latFormattedDms(latlng, true));
	$(".lng").html(lngFormattedDms(latlng, true));
	
	// Update the form fields for lat/lng
	$("#lat").attr("value", latlng.lat());
	$("#lng").attr("value", latlng.lng());
}
/* ************************************************************************** */


/* ************************************************************************** */
function activateCube(latlng)
{
	if(!(latlng && latlng.lat && latlng.lng)){ return; }
	
	var lat = latFormattedDms(latlng, true);
	var lng = lngFormattedDms(latlng, true);
	
	if(lat && lng)
	{
		var product_url = "http://www.zazzle.com/api/create/at-238721440104873781?rf=238721440104873781&ax=Linkover&pd=235653625606679583&fwd=ProductPage&ed=false";
		var lat_url = "&lat=";
		var lng_url = "&lng=";
		
		// TODO!!! I designed the t-shirt backwards, so I'm swapping the lat and lng values here.
		product_url += lat_url + lng + lng_url + lat;
		product_url = product_url.replace(/&#176;/g, "");
		product_url = product_url.replace(/&#8242;/g, "'");
		product_url = product_url.replace(/&#8243;/g, "''");	
		//debug(product_url);
		
		$("#tshirt").removeClass("inactive");
		$("#tshirt").attr("href", product_url);
	}
	
	// Swap the marker to the active image
	if(_liveblue.marker)
	{
		_liveblue.marker.setImage(getActiveCubeIcon().image);
	}
	
	// Remove the inactive state from the panel link
	$("#panel-open").removeClass("inactive");
}
/* ************************************************************************** */


/* ************************************************************************** */
function deactivateCube()
{	
	// Swap the marker image to the inactive state
	if(_liveblue.marker)
	{
		_liveblue.marker.setImage(getInactiveCubeIcon().image);
	}
	
	// Make the panel link inactive
	$("#panel-open").addClass("inactive");
}
/* ************************************************************************** */


/* ************************************************************************** */
function clusterClick(args)
{
	// Center the map on the cluster marker and zoom in one level
	_liveblue.map.setCenter(args.clusterMarker.getLatLng(), _liveblue.map.getZoom() + 1);
}
/* ************************************************************************** */


/* ************************************************************************** */
function getActiveCubeIcon()
{
	var icon = new GIcon();
	icon.image = "/i/icons/cube-on/image.png";
	icon.shadow = "/i/icons/cube-on/shadow.png";
	icon.iconSize = new GSize(33,35);
	icon.shadowSize = new GSize(51,35);
	icon.iconAnchor = new GPoint(17,35);
	icon.infoWindowAnchor = new GPoint(17,0);
	icon.printImage = "/i/icons/cube-on/printImage.gif";
	icon.mozPrintImage = "/i/icons/cube-on/mozPrintImage.gif";
	icon.printShadow = "/i/icons/cube-on/printShadow.gif";
	icon.transparent = "/i/icons/cube-on/transparent.png";
	icon.imageMap = [21,0,23,1,24,2,26,3,27,4,28,5,29,6,30,7,30,8,30,9,31,10,31,11,31,12,31,13,31,14,31,15,32,16,32,17,32,18,32,19,32,20,32,21,32,22,31,23,31,24,31,25,31,26,30,27,29,28,28,29,26,30,25,31,24,32,22,33,19,34,14,34,11,33,9,32,8,31,6,30,5,29,4,28,3,27,2,26,2,25,1,24,1,23,1,22,1,21,1,20,0,19,1,18,1,17,1,16,1,15,1,14,1,13,1,12,1,11,1,10,2,9,2,8,2,7,3,6,4,5,5,4,6,3,8,2,9,1,11,0];
	return icon;
}
/* ************************************************************************** */


/* ************************************************************************** */
function getInactiveCubeIcon()
{
	var icon = getActiveCubeIcon();
	icon.image = "/i/icons/cube-off/image.png";
	return icon;
}
/* ************************************************************************** */


/* ************************************************************************** */
function getClusterCubeIcon()
{
	var icon = new GIcon();
	icon.image = "/i/icons/cube-cluster/image.png";
	icon.shadow = "/i/icons/cube-cluster/shadow.png";
	icon.iconSize = new GSize(30,30);
	icon.shadowSize = new GSize(45,30);
	icon.iconAnchor = new GPoint(15,30);
	icon.infoWindowAnchor = new GPoint(15,0);
	icon.printImage = "/i/icons/cube-cluster/printImage.gif";
	icon.mozPrintImage = "/i/icons/cube-cluster/mozPrintImage.gif";
	icon.printShadow = "/i/icons/cube-cluster/printShadow.gif";
	icon.transparent = "/i/icons/cube-cluster/transparent.png";
	icon.imageMap = [16,5,18,6,19,7,20,8,20,9,20,10,20,11,20,12,20,13,21,14,23,15,25,16,25,17,25,18,25,19,25,20,25,21,25,22,24,23,23,24,21,25,8,25,7,24,5,23,4,22,4,21,4,20,4,19,4,18,4,17,4,16,6,15,7,14,9,13,9,12,9,11,9,10,9,9,9,8,10,7,11,6,13,5];
	return icon;
}
/* ************************************************************************** */
