// Google Maps API key.
var apiKey = 'ABQIAAAAlzTlZJj7d150NoP2hw9QfhSwveElV8X20wXr2FEeOf6LQJKwPRRlMzBQ1y5dJkIedUBvx75fj4ou';

// Path to geocode XML script.
var xmlPath = '/includes/geocode.xml.php';

// Map element DOM selector.
var mapSelector = '#map';

// Map control.
var mapControl = new GLargeMapControl();

// Map default initial centre and zoom.
var mapCentre = new GLatLng(48.426272, -123.364788);
var mapZoom = 6;

if (isFullMap == true){
	mapCentre = new GLatLng(42.426272, -119.364788);
	mapZoom = 5;	
}

// Path to marker images.
var imagePath = '/template/markers';

// Marker size and anchoring point.
var iconSize = new GSize(32, 32);
var iconAnchor = new GPoint(16, 32);

// Marker colours.
var markerTypes = {
	1:'idaho',
	2:'oregon',
	3:'california',
	4:'british-columbia',
	5:'washington'
}

// Region centers.
var regionCentres = {
	'british-columbia':new GLatLng(49.755555,-124.797949),
	'washington':new GLatLng(47.576526,-121.838379),
	'idaho':new GLatLng(46.619261,-116.103516),
	'oregon':new GLatLng(44.134913,-122.321777),
	'california':new GLatLng(37.125286,-120.014648)
}

var url = window.location.toString();
for (i in regionCentres){
	if (url.indexOf(i) > 0){
		mapCentre = regionCentres[i];	
		if (i == "california"){ mapZoom = 5; }
	}
}

// Global map variables.
var xml;
var manager;
var map = {};
var icons = {};
var markers = new Array();

function initializeIcons()
{
	for (i in markerTypes) {
		
		// Create an icon object.
		var icon = new GIcon();
		
		// Get the path for this image.
		var image = imagePath + '/' + markerTypes[i] + '.png';
		
		// Set image and shadow parameters.
		icon.image = image;
		icon.iconSize = iconSize;
		icon.iconAnchor = iconAnchor;
		icon.infoWindowAnchor = new GPoint(5, 5);
		
		// Add icon to array.
		icons[i] = icon;
	}
	
	var current = new GIcon();
	current.image = imagePath + '/star.png';
	current.iconSize = new GSize(16, 16);
	current.iconAnchor = new GPoint(8, 8);
	current.infoWindowAnchor = new GPoint(5, 5);
	
	icons['current'] = current;
}

function initializeMap()
{	
	// Create map and add control.
	map = new GMap2($(mapSelector)[0]);
	map.addControl(mapControl);
	//map.addControl(new GMapTypeControl());
	map.setMapType(G_PHYSICAL_MAP);
	
	// Download and parse XML.
	$.get(xmlPath, {}, function(data, responseCode) {
		xml = data;
		
		// Get requested marker id.
		var requestedMarker = getQueryString('s');
		
		// Get requested category id.
		var requestedCat = getQueryString('c');
		
		// Iterate through markers.
		$('marker', data).each(function() {
			
			// Create the marker.
			var point = new GLatLng(parseFloat($(this).attr('lat')), parseFloat($(this).attr('lng')));
			
			if (requestedMarker == $(this).attr('id')) {
				var marker = new GMarker(point, {icon: icons['current'], title: $(this).attr('name')});
			} else {
				var marker = new GMarker(point, {icon: icons[$(this).attr('type').toLowerCase()], title: $(this).attr('name')});
			}
			
			// Bind info window html to the marker.
			//marker.textContent = '<h2>' + $(this).attr('name') + '</h2>' + $(this).text() + '';
			marker.bindInfoWindowHtml($(this).text() + '');
			
			// Add the marker to the markers array.
			if (requestedCat == 'all' || requestedCat == null || requestedCat == $(this).attr('type') || requestedMarker == $(this).attr('id')) {
				markers[String($(this).attr('id'))] = marker;
			}
		});
		
		// Set the map centre.
		if (requestedMarker) {
			highlightMarker(requestedMarker)
			map.setCenter(markers[requestedMarker].getLatLng(), mapZoom + 1);
		} else {
			map.setCenter(mapCentre, mapZoom);
		}
		
		// Create marker manager, add markers, and refresh the map.
		manager = new MarkerManager(map);
		manager.addMarkers(markers, 1);
		manager.refresh();
	});
}

function getQueryString(variable)
{
	querystring = location.search.substr(1).split('&');
	
	for (i in querystring) {
		q = querystring[i];
		
		q = q.split('=');
		if (q[0] == variable) {
			return q[1];
		}
	}
}

function returnToHome()
{
	// Get requested marker id.
	var requestedMarker = getQueryString('s');
	highlightMarker(requestedMarker)
	map.setCenter(markers[requestedMarker].getLatLng(), mapZoom + 1);
}

function returnToCentre()
{
	map.setCenter(mapCentre, mapZoom);
}

function highlightMarker(marker)
{
	GEvent.bind(map, 'load', this, function() {
		showMarker(marker)
	});
}

function showMarker(marker)
{
	map.openInfoWindowHtml(markers[marker].getLatLng(), markers[marker].textContent);
}



// When the DOM is ready, load the map.
$(document).ready(function() {
	
	// Of course, check for map existence first!
	if ($(mapSelector).length != 0) {
		initializeIcons();
		initializeMap();
	}
});
