// JavaScript Document

/*

	N.B. this is a copy of the URCM/Cstools unt_map javascript. 


*/


var unt_map = {
	
	"latitude":"0",
	"longitude":"0",
	"zoomlevel":1,
	
	get_marker:function(key, value) {
		unt_map.sets.each( function(n) {
			return n.Points.detect( function(m) { return (m[key] == value); } );
																} );
	},
	
	set_feed:function(json) {
		unt_map.sets = new Array();
		unt_map.sets = json;
		unt_map.render_feed();
	},
	
	render_feed:function(json) {
		if (!unt_map.map) { unt_map.load_map(); }
		unt_map.sets = json;
		unt_map.map.clearOverlays();
	
		var miny = 90;
		var maxy = -90;
		var minx = 180;
		var maxx = -180;
		
		$('itinerary').update('');

		unt_map.sets.each( function(n) {
			var style = n.style;
			var vertices = [];
			index = 0;
			var min_sequence = 5000;
			var max_sequence = 0;
			n.Points.each( function(m) {
				//var latlng = new GLatLng(m.latitude, m.longitude);				
				miny = Math.min(m.latitude, miny);
				maxy = Math.max(m.latitude, maxy);
				minx = Math.min(m.longitude, minx);
				maxx = Math.max(m.longitude, maxx);
				min_sequence = Math.min(m.sequence, min_sequence);
				max_sequence = Math.max(m.sequence, max_sequence);

				if (style !== 1) { unt_map.add_marker(m); }
				if (style !== 0) { vertices[index++] = new GLatLng(m.latitude, m.longitude); }
				
				
			} ); // n.points.each
			
			var line = new GPolyline(vertices, '#ff0000', 3, 0.3, {geodesic:true});
			unt_map.map.addOverlay(line);
			
//			var first = unt_map.get_marker('sequence', first_sequence);
//			var first_point = new GLatLng(first.latitude, first.longitude);
//			unt_map.marker_manager.addMarker(new GMarker(point,{draggable:false,title:'Start'}), 1, 17);
			
//			alert('min_sequence ' + min_sequence + '\max_sequence' + max_sequence);
			
			
			$('itinerary').insert('<p><a href="" id="reset_map">View Full Tour</a></p>');
			$$('a.map_center').invoke('observe', 'click', function(e) {
				var lat = this.getAttribute('latitude');
				var lng = this.getAttribute('longitude');
				unt_map.map.setZoom(unt_map.map.zoomlevel + 2);
				unt_map.map.panTo(new GLatLng(lat, lng));
				e.stop();
			});			
			
			$('reset_map').observe('click', function(e) {
				unt_map.map.setZoom(unt_map.map.zoomlevel);
				unt_map.map.panTo(new GLatLng(unt_map.map.latitude, unt_map.map.longitude));
				e.stop();																		 	
			} );
																									

		} );	
		
		
		unt_map.marker_manager.refresh();
		var centerx = (parseFloat(minx) + parseFloat(maxx)) / 2.0;
		var centery = (parseFloat(miny) + parseFloat(maxy)) / 2.0;
		var center = new GLatLng(centery, centerx);
		var se = new GLatLng(miny, minx);
		var nw = new GLatLng(maxy, maxx);
		var bounds = new GLatLngBounds(se, nw);
		
		
		
		//console.log(bounds);
		var zoom = unt_map.map.getBoundsZoomLevel(bounds);
		zoom = (zoom > 12) ? 12 : zoom;
		var center = bounds.getCenter();
		unt_map.map.latitude = center.lat();
		unt_map.map.longitude = center.lng();
		unt_map.map.zoomlevel = zoom;
		unt_map.map.setZoom(zoom);
		unt_map.map.panTo(new GLatLng(unt_map.map.latitude, unt_map.map.longitude));
	},
	
	add_marker:function(point) {
		var lat_long = new GLatLng(point.latitude, point.longitude);
		
		var icon2 = new GIcon();
//    icon2.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icon2.iconSize = new GSize(12, 20);
    icon2.shadowSize = new GSize(22, 20);
    icon2.iconAnchor = new GPoint(6, 20);
    icon2.infoWindowAnchor = new GPoint(5, 1);

		var today = new Date;
//		today.setFullYear(2008,06,10);
		var activate = null;
		var deactivate = null;
		
		// activate_at is not blank
		if (point.activate_at !== '') {
			var activate = new Date;
			var a = point.activate_at;
			activate.setFullYear(a.substring(0,4), parseInt(a.substring(5,7) - 1), a.substring(8,10));
			activate.setHours(0);
			activate.setMinutes(0);
			activate.setSeconds(0);
		}
		
		if (point.deactivate_at !== '') {
			var deactivate = new Date;
			var d = point.deactivate_at;
			deactivate.setFullYear(d.substring(0,4), parseInt(d.substring(5,7) - 1), d.substring(8,10));
			deactivate.setHours(0);
			deactivate.setMinutes(0);
			deactivate.setSeconds(0);
		}


		icon2.image = "http://www.unt.edu/jazzeurope/images/mm_20_green.png";		
		//point.class = '';
		if (activate && deactivate) {
			if ( (activate <= today) && (deactivate >= today) ) {
				icon2.image = "http://www.unt.edu/jazzeurope/images/mm_20_red.png";
				//point.class = 'current';
			}
		}
		
		var label = (point.label !== '') ? point.label : point.activate;
		//label = parseInt(activate.getMonth() + 1) + '/' + activate.getDate()+ '/' + activate.getFullYear();
		point.marker = new GMarker(lat_long,{draggable:false,title:label,icon:icon2});
		
		google.maps.Event.addListener(point.marker, 'click', function() {
			unt_map.map.setZoom(unt_map.map.zoomlevel + 2);		
			//console.log(this);
			unt_map.map.panTo(this.getPoint());
		});
		unt_map.marker_manager.addMarker(point.marker, point.minzoom, point.maxzoom);	
		
		
		var template = new Template('<li><a href="#" class="map_center #{class}" longitude="#{longitude}" latitude="#{latitude}">#{label}</a></li>');
		$('itinerary').insert(template.evaluate(point));
	},

	render_marker:function(marker) {
		var point = new GLatLng(marker.latitude, marker.longitude);
		marker.marker = new GMarker(point,{draggable:false,title:marker.label});
		unt_map.marker_manager.addMarker(marker.marker, 1, 17);
	}, 
	
	load_map:function() {
		unt_map.map = new google.maps.Map2(document.getElementById('map'));
		unt_map.map.setCenter(new GLatLng(unt_map.latitude, unt_map.longitude), unt_map.zoomlevel);
		unt_map.map.disableDragging();	
		unt_map.map.enableContinuousZoom();
		unt_map.map.enableScrollWheelZoom();
		// unt_map.map.addControl(new GMapTypeControl()); 
		// unt_map.map.addControl(new GSmallMapControl());
		GEvent.addListener(unt_map.map, "click", unt_map.handle_map_click);
		GEvent.addListener(unt_map.map, "dblclick", unt_map.handle_map_dblclick);
		GEvent.addListener(unt_map.map, "rightclick", unt_map.handle_map_rightclick);
		unt_map.marker_manager = new GMarkerManager(unt_map.map);
	},
	
	handle_map_click:function(overlay, point) { 
		
	},
	handle_map_dblclick:function(point) { },
	handle_map_rightclick:function(point) { }
}