var roadworkTours = [];

var tourMarkers = [];

$(document).ready(function () {
	$("input[type=checkbox].roadworks").bind("click", function () {

		var checked = $(this).attr("checked");
		
		// update all other roadworks checkbox to match this one
		$("input[type=checkbox].roadworks").attr("checked", checked);
		
		if (checked) {
			
			displayRoadworks();
			
		}
		else {
			
			hideRoadworks();
			
		}
		
	});
	
	function displayRoadworks() {
		
		if (roadworkTours.length == 0) {

			var icon = new GIcon();	
			
			icon.image = "http://google-maps-icons.googlecode.com/files/accident.png";
			icon.iconSize = new GSize(32,37);
			icon.iconAnchor = new GPoint(16,37);
			icon.infoWindowAnchor = new GPoint(16,0);		
			//icon.imageMap = [29,0,30,1,31,2,31,3,31,4,31,5,31,6,31,7,31,8,31,9,31,10,31,11,31,12,31,13,31,14,31,15,31,16,31,17,31,18,31,19,31,20,31,21,31,22,31,23,31,24,31,25,31,26,31,27,31,28,31,29,30,30,29,31,23,32,22,33,21,34,20,35,19,36,12,36,11,35,10,34,9,33,8,32,2,31,1,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,1,1,2,0];
			icon.imageMap = [0,0,32,0,32,37,0,37];
			
			// build the routs and add them to the map
			var events = roadworks.length;
			
			var tour_count = 0;
			for(var e = 0; e < events; e++) {
				
				var event = roadworks[e];
				
				var tours = event.Tour.length;
				
				for (var t=0; t< tours; t++) {
					
					var tour = event.Tour[t];

					icon.image = "http://google-maps-icons.googlecode.com/files/construction.png";
					
					var marker = new GMarker(new GLatLng(0,0), {
						icon: icon,
						hidden: true
					});
					
					marker.tour_id = tour_count;
					tour_count++;
					
					map.addOverlay(marker);
					
					marker.routes = [];
					
					var routes = tour.Route.length;
					
					for (r = 0; r < routes; r++) {
						
						var route = tour.Route[r];
						
						var polyline = new GPolyline.fromEncoded({
							color: "#" + route.polyline_colour,
							weight: 4,
							points: route.polyline_points.replace("\\\\", "\\"),
							levels: route.polyline_levels,
							zoomFactor: 2,
							numLevels: 18
						});
						
						map.addOverlay(polyline);
						
						if (r==0) {
							
							var start = polyline.getVertex(0);
							marker.setLatLng(start);
							
						}
						
						marker.routes.push(polyline);
						polyline.hide();
						
					}
					
					GEvent.addListener(marker, "mouseover", function () {
						
						var marker = this;
						//highlight each route in this tour
						
						for(var i=0; i<marker.routes.length; i++) {
							marker.routes[i].setStrokeStyle({weight: 8});
						}
					});

					GEvent.addListener(marker, "mouseout", function () {
						
						var marker = this;
						//highlight each route in this tour
						
						for(var i=0; i<marker.routes.length; i++) {
							marker.routes[i].setStrokeStyle({weight: 4});
						}
					});

					GEvent.addListener(marker, "click", function () {
						
						showTourMarkers(this);
						
					});
					
					// add annotations for this tour
					marker.annotations = [];
					
					var annotations = tour.Annotation.length;
					
					for(a = 0; a < annotations; a++) {
						
						var annotation = tour.Annotation[a];
						
						icon.image = "http://google-maps-icons.googlecode.com/files/text.png";
						
						var aMarker = new GMarker(new GLatLng(annotation.lat, annotation.lng), {
							icon: icon,
							hidden: true
						});
						
						aMarker.bindInfoWindowHtml("<h2>" + annotation.name + "</h2>" + annotation.body, {
							maxWidth: 400
						});
						
						map.addOverlay(aMarker);
					
						marker.annotations.push(aMarker);
						
					}
					
					marker.locations = [];
					                 
					var pois = tour.Location.length;
					
					for(var p=0; p < pois; p++) {
						
						var poi = tour.Location[p];
						
						
						pMarker = addMarker({
							map: map,
							icon: poi.icon_url
						});
						
						pMarker.type="known";
						pMarker.id = poi.id;
						
						pMarker.setLatLng(new GLatLng(poi.lat, poi.lng));
						
						marker.locations.push(pMarker);
					
					}
					
					tourMarkers.push(marker);
					
				}
				
			}
			
			if (events==0) {
				alert('There are currently no known roadworks.');
			}
			
		} 
		
		for(var i=0; i<tourMarkers.length; i++) {
		
			var marker = tourMarkers[i];
			marker.show();
			
			for(var j=0; j<marker.routes.length;j++) {
				marker.routes[j].show();
			}

			for(var j=0; j<marker.annotations.length;j++) {
				marker.annotations[j].hide();
			}
			
		}
		
	}
	
	function hideRoadworks() {

		for(var i=0; i<tourMarkers.length; i++) {
			
			var marker = tourMarkers[i];
			marker.hide();
			
			for(var j=0; j<marker.routes.length;j++) {
				marker.routes[j].hide();
			}
			
			for(var j=0; j<marker.annotations.length; j++) {
				marker.annotations[j].hide();
			}

			for(var j=0; j<marker.locations.length; j++) {
				marker.locations[j].hide();
			}
			
		}
	
	}

	function showTourMarkers(marker) {
		
		for(var i = 0; i < tourMarkers.length; i++) {
			
			if (tourMarkers[i].tour_id != marker.tour_id) {
				
				for(var j=0; j<tourMarkers[i].annotations.length; j++) {
					tourMarkers[i].annotations[j].hide();
				}

				for(var j=0; j<tourMarkers[i].locations.length; j++) {
					tourMarkers[i].locations[j].hide();
				}
			
			}
			
		}
		
		var annotations = marker.annotations.length;
		
		for(var i=0; i< annotations; i++) {
			marker.annotations[i].show();
		}

		var locations = marker.locations.length;
		
		for(var i=0; i< locations; i++) {
			marker.locations[i].show();
		}
		
	}
	
});

