var participants = new Array();

// creates a GLatLng object
function getPoint(latitude, longitude) {
	return new GLatLng(latitude, longitude);
}

//creates a participant marker
function createAndPaintParticipantMarker(point, title, text) {
	var marker = new GMarker(point, {title: title, icon: getParticipantIcon()});
	GEvent.addListener(marker, "click", function() {
		map.openInfoWindowHtml(point, text);
	});

	map.addOverlay(marker);
}

//return the GIcon object to be used as icon for participant markers
function getParticipantIcon() {
	var icon = new GIcon();
	icon.image = contextPath + "/common/img/maps/depart1-rouge-13x20.png";
	icon.iconSize = new GSize(13, 20);
	icon.iconAnchor = new GPoint(6, 20);
	return icon;
}

function getEventIcon() {
	var icon = new GIcon();
	icon.image = contextPath + "/common/img/maps/evenements2-bleu-32x32.png";
	icon.iconSize = new GSize(32, 32);
	icon.iconAnchor = new GPoint(16, 32);
	icon.infoWindowAnchor = new GPoint(16, 32);
	return icon;
}

//creates a participant object
function createParticipant(name, uid, lat, lng, address) {
	var participant = new Object();
	participant.name = name;
	participant.uid = uid;
	participant.point = getPoint(lat, lng);
	participant.address = address;
	return participant;
}

//adds a new participant to the "participants" array
function addParticipant(name, uid, lat, lng, address) {
	lat = parseFloat(lat);
	lng = parseFloat(lng);
	
	// algo pour déplacer légèrement les marqueurs qui sont exactement au même endroit
	if (participants.length != 0) {
		do {
			var recheck = false;
			participants.each(
				function(otherparticipant) {
					if (otherparticipant.point.lat() == lat && otherparticipant.point.lng() == lng) {
						lat += 0.0005;
						lng += 0.0005;
						recheck = true;
					}
				}
			);
		} while (recheck);
	}

	var participant = createParticipant(name, uid, lat, lng, address);
	participants.push(participant);
}

//returns the html content of an info window for a given participant
var windowCounter = 0; 
function getHtmlForParticipant(participant) {
	if (isFinished || !isConnected || isInscrit || (uid == participant.uid)) {
		html = "<form id=\"formMsg"+ windowCounter +"\" action=\""+contextPath+"/covoiturage-evenement-send-email.do\" method=\"post\">" +
				"<div class=\"participantInfoWindow\" >";
		html+="<div>";
		if (!isFinished) {
			html+="Participant : " + participant.name + "<br/>";
		}
		html+="<br/>Départ : " + participant.address+"</div>";
		
		if (!isFinished && uid != participant.uid) {
			html +=	"<div class=\"participantInfoWindow-writeTo\">Ecrire à " + participant.name + "</div>" + 
					"<textarea name=\"msg\" style=\"width: 250px; height: 85px\"></textarea><br/>";
			
			if (!isConnected) {
				html +=	"<i>Adresse e-mail avec laquelle vous êtes inscrit</i><br/>" + 
				"<input type=\"text\" name=\"from\" maxlength='150' size='35' /><br/>";
			}
			
			html += "<input type=\"hidden\" name=\"uid\" value=\""+ participant.uid +"\" />";
			html += "<input type=\"hidden\" name=\"eventId\" value=\""+ eventId +"\" />";
			html += "<input type=\"hidden\" name=\"eventCode\" value=\""+ eventCode +"\" />";
			html += "<input type=\"button\" value=\"Envoyer le message\" onclick=\"sendEmail(" + windowCounter + ")\" />"; 
			
			html +=	"</div>";
		}
		html += "</form>";
	} else {
		html = "<div class=\"participantInfoWindow\" >" +
				"Pour contacter cette personne,<br/>veuillez <a href=\"" + createTrajetUrl + "\">créer un trajet pour cet évènement</a>." +
				"</div>";
	}
	
	windowCounter++;
		
	return html;
}

function sendEmail(windowId) {
	var url = contextPath + "/covoiturage-evenement-send-email.do";
	var formSelector = '#formMsg' + windowId;
	var uid = jQ(formSelector + ' [name=uid]').val();
	var eventId = jQ(formSelector + ' [name=eventId]').val();
	var eventCode = jQ(formSelector + ' [name=eventCode]').val();
	var from = jQ(formSelector + ' [name=from]').val();
	var msg = jQ(formSelector + ' [name=msg]').val();

	jQ.post(url, {msg: msg, from: from, uid: uid, eventId: eventId, eventCode: eventCode}, function(data) {
		var response = data.response;
		if (response.isError) {
			alert(response.errorMessage);
		} else {
			alert("Le message a été envoyé");
			map.closeInfoWindow();
		}
	}, "json");
}
