/* Cartographie */
function Cartographie(div) {
	
	this.div = div;
	this.map = null;
	this.zoom = 17;
	this.course = null;
	
  	this.init =  function() {
  		if (GBrowserIsCompatible()) {
	
			this.map = new GMap2($(this.div));
			
			this.map.addControl(new GSmallZoomControl());
			this.map.addControl(new GMapTypeControl());
			this.map.addControl(new GScaleControl());
		
			this.map.enableInfoWindow();
		}
  	}
  	
	this.display = function() {
	}
  	
  	this.panTo = function(lat, lng) {
  		this.map.panTo(new GLatLng(lat, lng));
  	}
  	
  	this.clear = function() {
  		this.map.clearOverlays();
  	}
  	
  	this.destroy = function() {
  		GUnload();
  	}
};

function Course(id) {
	
	this.id = id;
	this.coordonnees =null;
	this.participants = null;
	this.balises = null;
	
	// Init
	this.init = function() {
		// Instance
		var instance = this;
		
		// Chargement des participants
		this.participants = new Hash();
		
		new Ajax.Request('/ajax/cartographie/get_marqueurs.php?course='+this.id, {
			method: 'get',
			contentType : 'application/json',
			asynchronous: false,
			onCreate: function() {
			},
			onSuccess: function(transport) {
				transport.responseJSON.each(function(participant_json) {
					var participant = new Marqueur();
					participant.id=participant_json.id;
					participant.ident=participant_json.ident;
					participant.trace=participant_json.trace;
					participant.infos="<img src='"+participant_json.icon+"'><br><strong>"+participant.ident+"</strong><br>"+participant_json.infos+"<br>";
					
					participant.gicon = new GIcon();
					participant.gicon.image = participant_json.icon;
					participant.gicon.iconSize = new GSize(20,36);
					//participant.gicon.shadowSize = new GSize(10,18);
					participant.gicon.iconAnchor = new GPoint(20,36);
					participant.gicon.infoWindowAnchor = new GPoint(10,0);
			
					instance.participants.set(participant.id, participant);
				});
			}
		});
		
		// Chargement des coordonnées
		new Ajax.Request('/ajax/cartographie/get_coordonnees.php?course='+this.id, {
			method: 'get',
			contentType : 'application/json',
			asynchronous: false,
			onCreate: function() {
			},
			onSuccess: function(transport) {
				instance.coordonnees = transport.responseJSON;
			}
		});
	}
	
	// Display
	this.display = function(cartographie) {
		// Instance
		var instance = this;
		
		if (this.coordonnees!=null) {
			cartographie.map.setCenter(new GLatLng(parseFloat(this.coordonnees[0].ref.lat), parseFloat(this.coordonnees[0].ref.lng)),cartographie.zoom );
			
			var date_array = this.coordonnees[0].ref.date.split('-');
			var heure_array = this.coordonnees[0].ref.time.split(':');
			
			var date_display = new Date(date_array[0], date_array[1]-1, date_array[2], heure_array[0], heure_array[1], heure_array[2]);
			$('cartographie_infos').update("<strong>&nbsp;Relevé effectué le :</strong> " + date_display.toLocaleString());
		}
	}
	
	// Run
	this.runtrace = function(cartographie) {
		// Instance
		var instance = this;
		var trace = this.trace;
		
		trace(cartographie, instance);
		setInterval(function(){trace(cartographie, instance);},50000);
	}
	
	this.trace = function(cartographie, instance) {
		instance.clear(cartographie);
		
		position_tmp = instance.get_participant(1).marker.getLatLng();
		mouve_lat = Math.round(Math.random())/1000;
		mouve_long = Math.round(Math.random())/500;
	
		instance.get_participant(1).move(cartographie, (position_tmp.lat()-mouve_lat),(position_tmp.lng()-mouve_long));
		
		position_tmp = instance.get_participant(2).marker.getLatLng();
		mouve_lat = Math.round(Math.random())/1000;
		mouve_long = Math.round(Math.random())/500;
	
		instance.get_participant(2).move(cartographie, (position_tmp.lat()-mouve_lat),(position_tmp.lng()-mouve_long), 0);
	
	}
	
	// Live
	this.runlive = function(cartographie) {
		// Instance
		var instance = this;
		var live = this.live;
		
		live(cartographie, instance);
		setInterval(function(){live(cartographie, instance);},50000);
	}
	
	this.live = function(cartographie, instance) {
		instance.clear(cartographie);
		instance.balises = new Array();
		
		// Chargement des coordonnées en live
		new Ajax.Request('/ajax/cartographie/get_coordonnees.php?course='+instance.id, {
			method: 'get',
			contentType : 'application/json',
			asynchronous: false,
			onCreate: function() {
			},
			onSuccess: function(transport) {
				var date_array = transport.responseJSON[0].ref.date.split('-');
				var heure_array = transport.responseJSON[0].ref.time.split(':');
			
				var date_display = new Date(date_array[0], date_array[1]-1, date_array[2], heure_array[0], heure_array[1], heure_array[2]);
				$('cartographie_infos').update("<strong>&nbsp;Relevé effectué le :</strong> " + date_display.toLocaleString());
			
				transport.responseJSON[0].coordonnees.each(function(coordonnees_json) {
				if (coordonnees_json.id!=0) {
					// Participant
					var participant = instance.get_participant(coordonnees_json.id);
					if (typeof participant!="undefined") {
						participant.move(cartographie, parseFloat(coordonnees_json.lat), parseFloat(coordonnees_json.lng), coordonnees_json.vitesse);
					}
				} else {
					// Balise
					instance.balises.push(Marqueur.displayBalise(cartographie, parseFloat(coordonnees_json.lat), parseFloat(coordonnees_json.lng), coordonnees_json.icon));
				}
				});
			}
		});
	}
	
	//  Get Participant
	this.get_participant = function (id) {
		return this.participants.get(id);
	}
	
	// Clear
	this.clear = function(cartographie) {
		// Nettoyage des balises
		if (this.balises!=null) {
			this.balises.each(function(balise) {
				cartographie.map.removeOverlay(balise);
			});
		}
	}
}

function Marqueur() {
	this.id = null;
	this.ident = null;
	this.trace = "#FFFFFF";
	this.infos = null;
	this.gicon = null;
	this.marker = null;
	
	// Init
	this.init = function() {
	}
	
	// Display
	this.display = function(cartographie, lat, lng, vitesse) {
		// Instance
		var instance = this;
		var marker;
		
		marker = new GMarker(new GLatLng(lat,lng), this.gicon);
		cartographie.map.addOverlay(marker);
		
		var infosTpl = new Template("<strong>Latitude : </strong>#{lat}<br><strong>Longitude : </strong>#{lng}<br><strong>Vitesse : </strong>#{vitesse} km/h");
		
		GEvent.addListener(marker, "click", function() {
			cartographie.panTo(lat, lng);
			marker.openInfoWindowHtml(instance.infos + "<br>"+infosTpl.evaluate({lat: lat, lng: lng, vitesse: vitesse}));
		});
		
		this.marker = marker;
	}
	
	// Move
	this.move = function(cartographie, lat, lng, vitesse) {
		// Instance
		var instance = this;
		
		if (this.marker==null) {
			this.display(cartographie, lat, lng, vitesse);
		} else {
			var position;
			position = this.marker.getLatLng();
			
			var new_position = new GLatLng(lat,lng);
			this.marker.setLatLng(new_position);
		
			var ligne_tmp = new GPolyline([position,new_position],  instance.trace,  2,  1);
			cartographie.map.addOverlay(ligne_tmp);
			
			var infosTpl = new Template("<strong>Latitude : </strong>#{lat}<br><strong>Longitude : </strong>#{lng}<br><strong>Vitesse : </strong>#{vitesse} km/h");
			
			GEvent.addListener(instance.marker, "click", function() {
				instance.marker.openInfoWindowHtml(instance.infos + "<br>"+infosTpl.evaluate({lat: lat, lng: lng, vitesse: vitesse}));
			});
		}
	}
	
	// Display balise
	Marqueur.displayBalise = function(cartographie, lat, lng, icon) {
		var gicon;
		var marker;
		
		gicon = new GIcon();
		gicon.image = icon;
		gicon.iconSize = new GSize(32,32);
		gicon.shadowSize = new GSize(56,32);
		gicon.iconAnchor = new GPoint(32,32);
		gicon.infoWindowAnchor = new GPoint(16,0);
					
		marker = new GMarker(new GLatLng(lat,lng), gicon);
		cartographie.map.addOverlay(marker);
		
		var infosTpl = new Template("<strong>Latitude : </strong>#{lat}<br><strong>Longitude : </strong>#{lng}");
		
		GEvent.addListener(marker, "click", function() {
			cartographie.panTo(lat, lng);
			marker.openInfoWindowHtml("<strong>Balise</strong><br><br>"+infosTpl.evaluate({lat: lat, lng: lng}));
		});
		
		return marker;
	}
}
