
// ************************************************************************************
// ************************************************************************************
// fonction d'affichage de la liste des lignes de tram dans la combobox
// et de recuperation des informations sur les points des lignes
// ************************************************************************************
// ************************************************************************************
function pointTram() {
	var url = 'pointTram.php';
	var myAjax = new Ajax.Request(
			url,
			{
				method: 'get',
				//parameters: pars,
				onComplete: pointTramResponse,
				onFailure: reportError
			});
}
function pointTramResponse(originalRequest) {
	var result = originalRequest.responseText;
	selectTram = '<select onchange="testOption(this.options[this.selectedIndex].value)">' +
				'<option value="">Choisissez une ligne</option>';
	selectTram += '<option value="0">Voir toutes les lignes</option>';
	var lg1 = result.split("//");
	lg1.each(function(une_Ligne) {
		var lg2 = une_Ligne.split('||');
		selectTram += "<option value='"+lg2[1]+"'>"+lg2[0]+"</option>";
	});
	selectTram += '</select>';
	$("#sel2").html(selectTram);
}
function testOption(value){
if (value)
	{ voirTram(value); }
}

function reportError(request)
	{
		alert('Sorry. There was an error.');
	}

// ************************************************************************************
// ************************************************************************************
// fonction d'affichage des lignes de tram
// ************************************************************************************
// ************************************************************************************

function voirTram(id) {
	var url = 'voirTram.php';
	var pars = "id=" + id;
	var myAjax = new Ajax.Request(
			url,
			{
				method: 'get',
				parameters: pars,
				onComplete: voirTramResponse,
				onFailure: reportError
			});
}

function voirTramResponse(originalRequest) {
	// on efface les lignes deja affichees
	ligne_tram.each(function( une_ligne ) {
		map.removeOverlay(une_ligne);
	});

	// on efface les infobulles
	map.closeInfoWindow();

	// on vide les tableaux
	tram2.splice(0, tram2.length);
	tram.splice(0, tram.length);
	ligne_tram.splice(0, ligne_tram.length);

	// on place le resultat de la requete ajax dans une variable
	var result = originalRequest.responseText;

	// on met a zero le centrage de la carte et on instancie une variable
	// de stockage pour calculer un cadre contenant les donnees a afficher
	map.setCenter(new GLatLng(0,0),0);
	var bounds = new GLatLngBounds();

	// on travaille la requete ajax
	var div0 = result.split("//");
	var divpass;
	div0.each(function (un_div) {
		var div1 = un_div.split("||");
		divpass = div1[0];
		tram.push(div1[1]);
	});

	tram.each(function (un_tram) {
		var separe = un_tram.split(" ");
		separe.pop();
		separe.each(function (un_sep) {
				var der = un_sep.split(",");
				var latlng = new GLatLng(parseFloat(der[0]),parseFloat(der[1]));
				bounds.extend(latlng);
				tram2.push(latlng);
		});
		ligne_tram.push(new GPolyline(tram2, '#B22222', 6));
		tram2.splice(0, tram2.length);
	});
	map.setZoom(map.getBoundsZoomLevel(bounds));
	map.setCenter(bounds.getCenter());
	ligne_tram.each(function (une_liTram) {
		map.addOverlay(une_liTram);
	});
	var pass = "";
	if (div0.length >1) {
			pass = "tout";
		} else {
			pass = divpass;;
		}

	Station(pass);
	if (pass == 'tout') {
		$("#panel").css("height", "550px");
		$("#panel_int").css("height", "536px");
	} else {
	$("#panel").css("height", "");
	$("#panel_int").css("height", "");
	}
	$("#panel").show();
	$("#conteneur").show();

}

// ************************************************************************************
// ************************************************************************************
// fonction ajax qui r?cup?re les informations sur les stations du tram
// et qui affiche les points
// ************************************************************************************
// ************************************************************************************
function Station(ligne) {
	var url = 'station.php';
	var pars = "ligne=" + ligne;
	var myAjax = new Ajax.Request(
			url,
			{
				method: 'get',
				parameters: pars,
				onComplete: stationResponse,
				onFailure: reportError
			});
}

function stationResponse(originalRequest) {
	// on remet l'affichage ? zero
	station_metro.each(function(une_station) {
		map.removeOverlay(une_station);
	});
	st.splice(0, st.length);
	station_metro.splice(0, station_metro.length);
	station_comment.splice(0, station_comment.length);
	var result = originalRequest.responseText;
	var voir = "<br /><p style='font-size:8pt;'>";
	var retour = result.split("//");
	retour.each(function(un_ret, indice){
		var detail = un_ret.split("||");
		var cpt = detail[2].split(",");
		st.push(new GLatLng(parseFloat(cpt[0]),parseFloat(cpt[1])));
		voir += "<a href='#' onclick=' centre("+ parseFloat(cpt[0]) +" ,"+ parseFloat(cpt[1]) +","+ indice +"  ) '>"+ detail[0] + "</a><br />";
		var st_mo = createMarkerSquel(st[indice], detail[0], detail[1], tramway);
		station_metro.push(st_mo);
		station_comment.push("<div id='fenetre'><span id='titre'>Nom :</span> "+ detail[0] +" <br /><br /><span id='titre'>Description :</span> "+ detail[1] +"<br /></div>");
		map.addOverlay(st_mo);
	});
	voir += "<br /><a href='#' onclick='toutArret()'>Voir tous les arr\u00EAts</a>";
	voir += "</p>";
	$("#conteneur").html(voir);
}

// ************************************************************************************
// ************************************************************************************
// fonction de centrage sur la station de tram
// ************************************************************************************
// ************************************************************************************
function centre(nt1,nt2,n) {

	station_metro.each(function(une_station) {
		une_station.hide();
	});
	station_metro[n].show();
	station_metro[n].openInfoWindowHtml(station_comment[n]);
	map.setCenter(new GLatLng(0,0),0);
	var bounds = new GLatLngBounds();
	var bord1 = new GLatLng(nt1 + 0.003, nt2 - 0.003);
	var bord2 = new GLatLng(nt1 - 0.003, nt2 + 0.003);
	bounds.extend(bord1);
	bounds.extend(bord2);
	map.setZoom(map.getBoundsZoomLevel(bounds));
	map.setCenter(bounds.getCenter());
}

// ************************************************************************************
// ************************************************************************************
// fonction de visualisation de tous les arrets
// ************************************************************************************
// ************************************************************************************
function toutArret() {
	for(var x = 0; x<station_metro.length; x++) {
		station_metro[x].show();
	}
	map.closeInfoWindow();
	map.setCenter(new GLatLng(0,0),0);
	var bounds = new GLatLngBounds();
	for (var u = 0; u<st.length; u++) {
		bounds.extend(st[u]);
	}
	map.setZoom(map.getBoundsZoomLevel(bounds));
	map.setCenter(bounds.getCenter());
}
