google.load( "jquery", "1.3.1" );
google.load( "jqueryui", "1.5.3" );
google.setOnLoadCallback( 
	function() {
		registerVisualEffects();
	}
);

function registerVisualEffects(){
	$("#mapJsContent").css( 'visibility', 'visible' );
	$("#mapQuickLinksHeading").css( 'cursor', 'move' );
	$("#mapQuickLinksHeadingIcon").css( 'cursor', 'pointer' );

	$("#mapQuickLinks").hover(
		function(){
			$(this).css("opacity", 1.0);
		},
		function(){
			$(this).css("opacity", 0.6);
		}
	);
	$("#mapQuickLinks").draggable(
		{
			handle: '#mapQuickLinksHeading',
			containment: 'parent',
			opacity: 0.6,
			cursor: 'move'
		}
	);
	$("#mapQuickLinksHeading").dblclick(
		function(){
			$("#mapQuickLinksCollapsible").toggle("blind");
		}
	);$("#mapQuickLinksHeadingIcon").click(
		function(){
			$("#mapQuickLinksCollapsible").toggle("blind");
		}
	);
}


//constants
var divMapId = "map";
var divDirectionsDetailsId = "directionsDetails";

//globals
var gMap;
var gDir;
var HomePoint = new GLatLng(-32.34760909803044, 152.53831028938293);

function load() {
	if ( GBrowserIsCompatible()) {
		gMap = new GMap2(document.getElementById(divMapId));

		directionsPanel = document.getElementById( divDirectionsDetailsId );
		gDir = new GDirections(gMap, directionsPanel);
		GEvent.addListener(gDir, "error", handleErrors);

		gMap.addControl(new GSmallMapControl());
		gMap.addControl(new GMapTypeControl());
		gMap.addControl(new GScaleControl());
		//gMap.enableScrollWheelZoom();
		gMap.addControl(new PresetZoomControl());
		gMap.enableContinuousZoom();

		gMap.setCenter(HomePoint, 11);

		changeMapType( "hybrid" );

		// Create marker icon
		var icon = new GIcon();
		icon.image = "./media/icons/mapicon.png";
		icon.iconSize = new GSize(126, 84);
		icon.iconAnchor = new GPoint(0, 84);
		icon.infoWindowAnchor = new GPoint(5, 1);

		var villa3Marker = createMarker(HomePoint, "Villa 3", icon);
		gMap.addOverlay( villa3Marker );
	}
}

// Creates a marker at the given point with the given number label
function createMarker(point, description, icon) {

	if( ! icon ){
		icon = G_DEFAULT_ICON;
	}

	var markerOptions = { 
		icon: icon,
		title: description
	};

	var marker = new GMarker(point, markerOptions );

	GEvent.addListener(marker, "click",
		function() {
			//marker.openInfoWindowHtml( description );
		}
	);
	return marker;
}

function panThenZoom(point, zoomLevel) {

	gMap.setZoom( zoomLevel );
	gMap.panTo(point);
}


function showDirections( ){

	var source = document.getElementById("inputDirections").value;
	var directionsString = source + " to Blueys Way, Blueys Beach, Pacific Palms, NSW, Australia";

	gDir.load( directionsString );
	$("#directionErrors").css( "visibility", "hidden" );

}

function clearDirections( ){   
	$("#directionErrors").css( "visibility", "hidden" );
	gDir.clear();
}

function changeMapType( type ){   
	if( type.toLowerCase() == "hybrid" ){
		gMap.setMapType(G_HYBRID_MAP);
		//$("#mapSimpleView").css( "visibility", "visible" );
		//$("#mapPhotoView").css( "visibility", "hidden" );
	}else if( type.toLowerCase() == "map" ){
		gMap.setMapType(G_NORMAL_MAP);
		//$("#mapSimpleView").css( "visibility", "hidden" );
		//$("#mapPhotoView").css( "visibility", "visible" );
	}else if( type.toLowerCase() == "satellite" ){
		gMap.setMapType(G_SATELLITE_MAP);
		//$("#mapSimpleView").css( "visibility", "visible" );
		//$("#mapPhotoView").css( "visibility", "hidden" );
	}else{
		throw "unrecognised map type: " + type;
	}   
}

function handleErrors(){

	if (gDir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		showDirectionsError("Error Fetching Directions: No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gDir.getStatus().code);
	else if (gDir.getStatus().code == G_GEO_SERVER_ERROR)
		showDirectionsError("Error Fetching Directions: A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gDir.getStatus().code);

	else if (gDir.getStatus().code == G_GEO_MISSING_QUERY)
		showDirectionsError("Error Fetching Directions: The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gDir.getStatus().code);

	else if (gDir.getStatus().code == G_GEO_BAD_KEY)
		showDirectionsError("Error Fetching Directions: The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gDir.getStatus().code);

	else if (gDir.getStatus().code == G_GEO_BAD_REQUEST)
		showDirectionsError("Error Fetching Directions: A directions request could not be successfully parsed.\n Error code: " + gDir.getStatus().code);

	else showDirectionsError("Error Fetching Directions: An unknown error occurred.");
}


function showDirectionsError( errorString ){
	document.getElementById("directionErrorsText").innerHTML = errorString;
	$("#directionErrors").css( "visibility", "visible" );
}


function log( message ){
	document.getElementById("msg").innerHTML += "<br>" + "LOG: " + message + "\n";
}


//custom control for zooming to specific levels
//

// A TextualZoomControl is a GControl that displays textual "Zoom In"
// and "Zoom Out" buttons (as opposed to the iconic buttons used in
// Google Maps).
function PresetZoomControl() {
}
PresetZoomControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
PresetZoomControl.prototype.initialize = function(map) {

	var links = document.getElementById( "mapQuickLinks" );
	map.getContainer().appendChild( links );
	return links;
}

PresetZoomControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 50));
}


