var locations = [];

var PARK_LOCATION = 0;
var HIKING_LOCATION = 1;
var WILDLIFE_LOCATION = 2;
var TOURISM_LOCATION = 3;

var map;

function JLoad(centre_x, centre_y, zoom_level) {
	if (GBrowserIsCompatible()) {
		setUpParkLocations();
		
		// set up map
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(centre_x, centre_y), zoom_level);
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		fromLocation = 0;
		toLocation = 0;
		
		// add items to map
		for (i = 0; i < locations.length; i++) {
			// create marker at to location
			icon = createIcon(locations[i]);
			
			locations[i].createMarker(icon);
			map.addOverlay(locations[i].marker);
			
			locations[i].registerClickListener();
		}
	}
	else {
		alert("You browser does not support google maps.");
	}
}

function createIcon(location) {
	// HOW CAN THIS BE DONE OO LIKE?
	
	if (location.locationType == PARK_LOCATION) {
		return createParkIcon();
	}
	else if (location.locationType == HIKING_LOCATION) {
		return createHikingIcon();
	}
	else if (location.locationType == WILDLIFE_LOCATION) {
		return createWildlifeIcon();
	}
	else if (location.locationType == TOURISM_LOCATION) {
		return createTourismIcon();
	}
	else {
		alert("Unknown icon type requested (" + location.locationType + ") location " + location.name + " cannot have icon created");
		return null;
	}
}

function createHikingIcon() {
	icon = new GIcon();
	 
	icon.image = "http://jameselliot.co.uk/parks_map/pointer_hiking.png";
	icon.shadow = "http://jameselliot.co.uk/parks_map/pointer_hiking_shadow.png";
	icon.iconSize = new GSize(35, 53);
	icon.shadowSize = new GSize(81,53);
	icon.iconAnchor = new GPoint(14, 53);
	icon.infoWindowAnchor = new GPoint(15, 52);

	return icon;
}

function createWildlifeIcon() {
	icon = new GIcon();
	 
	icon.image = "http://jameselliot.co.uk/parks_map/pointer_wildlife.png";
	icon.shadow = "http://jameselliot.co.uk/parks_map/pointer_wildlife_shadow.png";
	icon.iconSize = new GSize(35, 53);
	icon.shadowSize = new GSize(81,53);
	icon.iconAnchor = new GPoint(14, 53);
	icon.infoWindowAnchor = new GPoint(15, 52);

	return icon;
}

function createParkIcon() {
	icon = new GIcon();
	 
	icon.image = "http://jameselliot.co.uk/parks_map/pointer_park.png";
	icon.shadow = "http://jameselliot.co.uk/parks_map/pointer_park_shadow.png";
	icon.iconSize = new GSize(30, 41);
	icon.shadowSize = new GSize(60,40);
	icon.iconAnchor = new GPoint(15, 41);
	icon.shadowAnchor = new GPoint(18, 52);
	icon.infoWindowAnchor = new GPoint(15, 52);

	return icon;
}

function createTourismIcon() {
	icon = new GIcon();
	 
	icon.image = "http://jameselliot.co.uk/parks_map/pointer_tourism.png";
	icon.shadow = "http://jameselliot.co.uk/parks_map/pointer_tourism_shadow.png";
	icon.iconSize = new GSize(30, 41);
	icon.shadowSize = new GSize(60,40);
	icon.iconAnchor = new GPoint(15, 41);
	icon.shadowAnchor = new GPoint(18, 52);
	icon.infoWindowAnchor = new GPoint(15, 52);

	return icon;
}

function addClickListener(marker, infoPanel, location) {
	GEvent.addListener(marker, "click", function() {
		map.openInfoWindow(location, infoPanel);
	});
}

/*
Park Location class
*/
function ParkLocation(locationType, name, x, y, imageUrl, linkName) {
	// set instance members
	this.locationType = locationType;
	this.x = x;
	this.y = y;
	this.name = name;
	this.location = new GLatLng(x, y);
	this.linkName = linkName
	this.imageUrl = imageUrl
	this.marker = "null";
	
	// define functions
	this.registerClickListener = ParkLocation_registerClickListener;
	this.createMarker = ParkLocation_createMarker;
}

function ParkLocation_createMarker(icon) {
	this.marker = new GMarker(this.location, icon);
}

function ParkLocation_registerClickListener() {
	div = document.createElement("div");
	div_center = document.createElement("center");
	div.appendChild(div_center);
	
	title_span = document.createElement("span");
	title_span.setAttribute("class", "map_info_title");
	title_span.setAttribute("style", "background: #ABABAB;");
	
	img = document.createElement("img");
	img.setAttribute("style", "border: 0; margin: 0; padding: 0;");
	img.setAttribute("src", "slate_small_dark.jpg");
	
	title = document.createElement("h3");
	title.setAttribute("class", "map_info_title");
	title.appendChild(img);
	title.appendChild(document.createTextNode(" "));
	title.appendChild(document.createTextNode(this.name));
	
	title_span.appendChild(title);
	
	div_center.appendChild(title_span);
	
	parkimg = document.createElement("img");
	parkimg.setAttribute("src", "parks_map/" + this.imageUrl);
	parkimg.setAttribute("style", "border: 0; margin: 0; padding: 0; width: 200px; height: 50px;");
	
	div_center.appendChild(parkimg);
	div_center.appendChild(document.createElement("br"));
	
	link = document.createElement("a");
	
	link.setAttribute("href", "#" + this.linkName);
	//link.setAttribute("target", "top");
	
	link.appendChild(document.createTextNode("View More Information"));
	
	div_center.appendChild(link);
	
	infoLocation = this.location;
	
	addClickListener(this.marker, div, this.location);
}
