/*
 * Created on 28 janv. 2008
 *
 * Fonctions de gestion des maillons, coté client
 * (liste de maillons, colorier les maillons, etc ...)
 * s'appuie sur les utilitaires SVG / VML
 */

// En mode SVG, inclure
// <script language="JavaScript" type="text/javascript" src="../include/js/addeo_svg_utils.js"></script>

var VERT_MAILLON = "#00CC00";
var ORANGE_MAILLON ="#ff9933";
var ROUGE_MAILLON = "#ff0000";
var NOIR_MAILLON = "#000000";
var GRIS_MAILLON = "#ADADAD";

// --------------------------------------------------------------------------------------------
// retourne des noeuds "actifs", lignes, poly, etc ... situés dans l'arbre d'un element svg (balise <g id=... >
// dans un tableau js
function lire_liste_noeuds_element_svg(doc, elementName)
{
var liste;
var liste2;
var i, j, node;

// liste de tous les noeud de cet elt (y compris texte, attributs, comments ...)
liste = getNodeList_SVGElement(doc, elementName);	
if(!liste)
	return null;
if(typeof liste == "undefined")
	return null;
	
// la liste à retourner
liste2 = new Array(); 

j = 0;
for(i=0; i < liste.length; i++)
	{
	node = liste[i];
	//alert("nodeName:" + node.nodeName)
	if(typeof node == "undefined")
		continue;
	if(node.nodeType != 1)		// on ne prend que les noeuds "elements" ...
		continue;
	if((node.nodeName == "polygon") || (node.nodeName == "polyline") || (node.nodeName == "line"))		// ... qui nous intressent	
		{
		liste2[j] = node;
		j++;
		}
	}
//alert("liste2, nb : " + liste2.length);
return(liste2);	
}

//--------------------------------------------------------------------------------------------
// changer_couleur_liste_noeuds(liste_noeuds, couleur)
// la couleur s'applique au noeud de type polygon & polyline


function changer_couleur_liste_noeuds(liste_noeuds, couleur)
{
var i, node;

if(liste_noeuds == null)
	return;
for(i=0; i < liste_noeuds.length; i++)
	{
	node = liste_noeuds[i];
	if((node.nodeName == "polyline") || 	(node.nodeName == "line"))
		{
		if(mode_SVG)
			node.setAttribute('stroke', couleur);	
		else // mode_VML	
			{
			//alert(node.getAttribute('strokecolor'));
			node.setAttribute('strokecolor', couleur);
			}
		}
	if(node.nodeName == "polygon")
		{
		if(mode_SVG)
			node.setAttribute('fill', couleur);		
		}	
	}
}

//--------------------------------------------------------------------------------------------
function changer_couleur_liste_maillons(doc, maillons, couleur)
{
var i;
for(i=0; i < maillons.length; i++)
	changer_couleur_maillon(doc, maillons[i], couleur);	
	
}

//--------------------------------------------------------------------------------------------
// changer_couleur_maillon
// la couleur s'applique aux noeuds utiles de ce maillon
function changer_couleur_maillon(doc, nom_maillon, couleur)
{

var noeuds_maillon;

noeuds_maillon = lire_liste_noeuds_element_svg(doc, nom_maillon);
changer_couleur_liste_noeuds(noeuds_maillon, couleur);

}

//--------------------------------------------------------------------------------------------
// mà de tous les maillons retournés par la table du serveur
function maj_couleur_maillons(table)
{
var i, N;
var maillon_xml;
var nom_maillon, couleur;
var doc;
var now, o_date, delta, str_time_rocade;

// accès au document SVG (actuellement repéré par une balise <embed id="..."> )
if(mode_SVG)
	{
	doc = getSVGDoc("Calque_1");
	//alert("maj_couleur_maillons SVG");
	}
else // VML : // accès au document VML (actuellement dans une Iframe )
	{
	doc = getVMLDoc("Calque_1");
	//alert("maj_couleur_maillons VML dans le doc : " + doc.nodeName );
	}

N = table.length;
for(i=0; i<N; i++)
	{
	maillon_xml = table[i];
	couleur = lire_couleur_maillon_xml (maillon_xml);
	nom_maillon = maillon_xml['id'];

	changer_couleur_maillon(doc, nom_maillon, couleur);
	}

if(liste_maillons.time_R && liste_maillons.time_U)
	{
	str_time_rocade	= liste_maillons.time_R;
	str_time_urbain = liste_maillons.time_U;
	o_dateR = convertir_date_serveur_2_client(str_time_rocade);
	o_dateU = convertir_date_serveur_2_client(str_time_urbain);
	if(o_dateR && o_dateU)
		{
		delta = o_dateU.getTime() - o_dateR.getTime();	 // delta en ms
		if (delta > 30*60*1000)
			griser_maillons_rocade(table);
		}	
	}	
}

//--------------------------------------------------------------------------------------------
function lire_couleur_maillon_xml(m)
{
var etat = m['trafficStatus']
var type = m['type']

if(!etat)
	return(GRIS_MAILLON); 

etat = etat.toUpperCase();
type = type.toUpperCase();

if(type == "R")
	return (lire_couleur_maillon_rocade(etat));
if(type == "U")
	return (lire_couleur_maillon_urbain(etat));

return(GRIS_MAILLON);	
}
//--------------------------------------------------------------------------------------------
function lire_couleur_maillon_rocade(etat)
{
if(etat == "FREEFLOW")
 	return(VERT_MAILLON);
if(etat == "HEAVY")
 	return(ORANGE_MAILLON);
if(etat == "CONGESTED")
 	return(ROUGE_MAILLON);
if(etat == "UNKNOWN")
 	return(GRIS_MAILLON);
return(GRIS_MAILLON);	
}

//--------------------------------------------------------------------------------------------
function lire_couleur_maillon_urbain(etat)
{
if(etat == "FREEFLOW")
	return(VERT_MAILLON);
if(etat == "DENSE")
	return(ORANGE_MAILLON);	
if(etat == "HEAVY")
	return(ROUGE_MAILLON);
if(etat == "CONGESTED")
	return(NOIR_MAILLON); 
if(etat == "UNKNOWN")
	return(GRIS_MAILLON); 
return(GRIS_MAILLON);	
}

//--------------------------------------------------------------------------------------------
function griser_maillons_rocade(table_maillons)
{
var doc;
var N;
var maillon_xml;

// accès au document SVG (actuellement repéré par une balise <embed id="..."> )
if(mode_SVG)
	{
	doc = getSVGDoc("Calque_1");
	}
else // VML : // accès au document VML (actuellement dans une Iframe )
	{
	doc = getVMLDoc("Calque_1");
	}

N = table_maillons.length;
for(i=0; i<N; i++)
	{
	maillon_xml = table_maillons[i];
	if(maillon_xml.type.toUpperCase() == 'R')
		{
		nom_maillon = maillon_xml['id'];
		changer_couleur_maillon(doc, nom_maillon, GRIS_MAILLON);
		}
	}
}


