// Google Maps (festivals) code

var map = null;
var geocoder = null;
var useragent = navigator.userAgent;

var festivals = new Array();	

function festival(name, address, lat, long, start_date, end_date, code) {
    this.FestivalName = name;
	this.FestivalAddress = address;
	this.FestivalLat = lat;
	this.FestivalLong = long;
	this.FestivalStartDate = start_date;
	this.FestivalEndDate = end_date;
	this.FestivalURL = "http://www.camra.org.uk/page.aspx?o=" + code;
}

function expand_map() {
	
	var useragent = navigator.userAgent;
	var mapdiv = document.getElementById("map_canvas");
	
	if (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1 ) {
		//do nothing
	}
	else {
	
		// get current dimensions
		w = parseInt(mapdiv.style.width.substr(0 ,mapdiv.style.width.length-2));
		h = parseInt(mapdiv.style.height.substr(0, mapdiv.style.height.length-2));
			
		// define new dimensions
		var nw = 570;
		var nh = nw/w*h;
		
		// change container css attributes
		//document.getElementById("map_canvas_container").style.float = "none";
		document.getElementById("map_canvas_container").style.marginLeft = "0px";
		
		// set new dimensions
		mapdiv.style.width = nw + "px";
		mapdiv.style.height = nh + "px";	
		
		if (GBrowserIsCompatible()) {
		
			var zoom = 6;	
			var center_point = new GLatLng(54.361358, -4.306641);
			
			map = new GMap2(mapdiv, mapOptions);
			
			map.setCenter(center_point, zoom);
			
			map.setUIToDefault();
			
			customUI = map.getDefaultUI();
				customUI.maptypes.hybrid = false;
				customUI.maptypes.physical = false;
			
			map.setUI(customUI);
			
			showAddresses();
				
			document.getElementById("expand_reduce_link").href = "javascript:reduce_map();";
			document.getElementById("expand_reduce_link").innerHTML = "Reduce map";
		}
	}	
}

function reduce_map() {
	
	var useragent = navigator.userAgent;
	var mapdiv = document.getElementById("map_canvas");
	
	if (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1 ) {
		//do nothing
	}
	else {
	
		// get current dimensions
		w = parseInt(mapdiv.style.width.substr(0 ,mapdiv.style.width.length-2));
		h = parseInt(mapdiv.style.height.substr(0, mapdiv.style.height.length-2));
			
		// define new dimensions
		var nw = 290;
		var nh = nw/w*h;
		
		// change container css float attribute
		//document.getElementById("map_canvas_container").style.float = "right";
		document.getElementById("map_canvas_container").style.marginLeft = "16px";
		
		// set new dimensions
		mapdiv.style.width = nw + "px";
		mapdiv.style.height = nh + "px";	
		
		if (GBrowserIsCompatible()) {
			
			var zoom = 5;	
			var center_point = new GLatLng(54.361358, -4.306641);
			
			map = new GMap2(mapdiv, mapOptions);
			
			map.setCenter(center_point, zoom);
			
			map.setUIToDefault();
			
			customUI = map.getDefaultUI();
				customUI.maptypes.hybrid = false;
				customUI.maptypes.physical = false;
			
			map.setUI(customUI);
			
			showAddresses();
			
			document.getElementById("expand_reduce_link").href = "javascript:expand_map();";
			document.getElementById("expand_reduce_link").innerHTML = "Expand map";
		}
	}	
}

function showAddresses() {

	for (var i = 0; i < festivals.length; i++) {
		
		showAddress(festivals[i].FestivalName, festivals[i].FestivalAddress, festivals[i].FestivalLat, festivals[i].FestivalLong, festivals[i].FestivalStartDate, festivals[i].FestivalEndDate, festivals[i].FestivalURL);
	}
}

function showAddress(name, address, lat, long, start_date, end_date, url) {
	
	if (address.length > 0) {
	
		var today = new Date();
		
		var date=new Date();
			date.setFullYear(parseInt(end_date.substr(0,4)),(parseInt(end_date.substr(5,2)-1)),parseInt(end_date.substr(8,2)));
			
		if (date >= today) {
		
			start_date_str = get_day(start_date.substr(8,2)) + " " + get_month(start_date.substr(5,2)) + " " + start_date.substr(0,4);
			end_date_str = get_day(end_date.substr(8,2)) + " " + get_month(end_date.substr(5,2)) + " " + end_date.substr(0,4);
									
			var html = "<p style='padding: 0px; margin: 0px'><strong>" + name + "</strong><br />"+ address.replace(/, /g, "<br />") + "<br /><br />" + start_date_str + " to " + end_date_str + "<br /><br /><a href='" + url + "' title='more information'>More information</a></p>";
			
			if ((lat.toString().length > 0) && (long.toString().length > 0)) {
			
				// plot a regular point
				var point = new GLatLng(lat, long);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				GEvent.addListener(
					marker, 
					"click", 
					function() {
						marker.openInfoWindowHtml(html);
					}
				);
			}
			else {
	
				if (geocoder) {
					geocoder.getLatLng(
						address,
							function(point) {
								if (!point) {
									alert(address + " not found");
								} else {
									var marker = new GMarker(point);
									map.addOverlay(marker);
									GEvent.addListener(
										marker, 
										"click", 
										function() {
											marker.openInfoWindowHtml(html);
										}
									);
								}
							}
					);
				}
				
			}
		}
	}
}

function get_month(month) {

	if (month == "01") { return "Jan"; }
	else if (month == "02") { return "Feb"; }
	else if (month == "03") { return "Mar"; }
	else if (month == "04") { return "Apr"; }
	else if (month == "05") { return "May"; }
	else if (month == "06") { return "Jun"; }
	else if (month == "07") { return "Jul"; }
	else if (month == "08") { return "Aug"; }
	else if (month == "09") { return "Sep"; }
	else if (month == "10") { return "Oct"; }
	else if (month == "11") { return "Nov"; }
	else if (month == "12") { return "Dec"; }
}

function get_day(day) {

	if (day.substr(0,1) == "0") { return day.substr(1,1); }
	else { return day; }
}