/* Collection de fonction js pour les elements emission/edition */

// Ajoute un objet de reference edition à la collection tabPathEdition pour le player audio
function addTabPathEdition(tabPathEdition, cheminSon, pl_urlPageDesc, pl_video, pl_p_s, pl_p_s2, pl_langue, duree, titre, description)
{
	if(cheminSon.length > 0)
	{
		obj = new Object();
		obj.pl_fichier = cheminSon;
		obj.pl_urlPageDesc = pl_urlPageDesc;
		obj.pl_video = pl_video;
		obj.pl_p_s = pl_p_s;
		obj.pl_p_s2 = pl_p_s2;
		obj.pl_langue = pl_langue;
		obj.duree = duree;
		obj.titre = titre;
		obj.description = description;
	tabPathEdition[tabPathEdition.length] = obj;
	}
}

/* Ecrit la toolbar de l'edition
Variables externes : (générées par le template EDI_TOOLBAR_JSINIT)
- traduc_ecouter
- traduc_minute
- traduc_telecharger
- traduc_avertissement

générées dans la page
- tabPathEdition
*/
function writeEditionToolBar(affiche_duree)
{
	// End script pour l'affichage de la derniere edition
	var lt = String.fromCharCode(60)
		, gt = String.fromCharCode(62)
		, nbsp = "&nbsp;"
		, args = []
		, msg = ''
		, delim = "','";// Separateur d'argument

	if (tabPathEdition.length > 0)
	{
		// Html "Ecouter"
		// ----- Args playerAudio();		
		args[args.length] = tabPathEdition[tabPathEdition.length - 1].pl_fichier;// Arg 1
		args[args.length] = tabPathEdition[tabPathEdition.length - 1].pl_urlPageDesc;// Arg 2
		args[args.length] = tabPathEdition[tabPathEdition.length - 1].pl_video;// Arg 3
		args[args.length] = tabPathEdition[tabPathEdition.length - 1].pl_p_s;// Arg 4
		args[args.length] = tabPathEdition[tabPathEdition.length - 1].pl_p_s2;// Arg 5
		args[args.length] = tabPathEdition[tabPathEdition.length - 1].pl_langue;// Arg 6

		var duree = '';

		if (affiche_duree == null)
			duree = ' ' + tabPathEdition[tabPathEdition.length - 1].duree;

		msg = "".concat(
			lt + 'p class="ecouter"' + gt
			, lt + 'a href="javascript:playerAudio(\''
			, args.join(delim)
			, '\')"' + gt // Fermeture balise ouvrante
			, traduc_ecouter + duree
			, lt + '/a' + gt
			, lt + '/p' + gt			
		);

		// Html "Télécharger"
		// ----- Args telecharger();
		/*args = [];
		args[args.length] = tabPathEdition[tabPathEdition.length - 1].pl_fichier;// Arg 1
		args[args.length] = tabPathEdition[tabPathEdition.length - 1].titre;// Arg 2
		args[args.length] = tabPathEdition[tabPathEdition.length - 1].description;// Arg 3
		args[args.length] = tabPathEdition[tabPathEdition.length - 1].pl_langue;// Arg 4

		msg = msg.concat(
			lt + 'p class="telecharger"' + gt
			, lt + 'a href="javascript:telecharger(\''
			, args.join(delim)
			, '\')"' + gt // Fermeture balise ouvrante
			, traduc_telecharger
			, lt + '/a' + gt
			, lt + '/p' + gt
		);*/

		//alert(msg);// Debug
		
		document.write(msg);
		document.close();
	}
	else
	{
	/*
		if (traduc_avertissement.length > 0)
		{
			paragraphe = lt + 'p class="avertissement"' + gt;
			paragraphe += traduc_avertissement;
			paragraphe += lt + '/p' + gt;			  
			document.write(paragraphe);
			document.close();
		}
	*/
	}
}

/* Deplace la toolbar d'une edition SUJET_REPRIS = true dans le corps de l'edition précédente ayant SUJET_REPRIS = false.
Fonction spécifique à la page de listing des dernières éditions (RADIOFR).
- tabIdEdition : tableau d'objet caracterisant un corps d'edition (voir cas d'utilisation pour détails)
*/
function moveToolbarToRootEdition(tabIdEdition)
{
	var prev_edi = null
		, tmp = null
		, currTopEdition = null
		, groupes = [];

	// Trouve les debuts de sujet et les sujets associés
	for(var i = 0;i < tabIdEdition.length; i++)
	{
		if(tabIdEdition[i].repris != 'true')
		{
			// Init l'edition racine
			currTopEdition = tabIdEdition[i];
			currTopEdition.childs = [];
			currTopEdition.TagAfterAccesseur = null;
			currTopEdition.TagCorps = null;
			currTopEdition.TagEntete = null;
			groupes[groupes.length] = currTopEdition;
			// trouve le corps
			tmp = getElementsByTagAndClassFromNode(currTopEdition.div, "div", "corps");
			currTopEdition.TagCorps = tmp[0];
			// trouve l'entete pour la gestion du nbr de partie dans l'entete
			tmp = getElementsByTagAndClassFromNode(currTopEdition.div, "h1", "");
			if(tmp.length) currTopEdition.TagEntete = tmp[0];
			// trouve son noeud d'insertion de toolbar
			tmp = getElementsByTagAndClassFromNode(currTopEdition.div, "div", "accesseur");
			if(tmp.length)// j'ai une toolbar dans l'edition racine ?
			{
				currTopEdition.TagAfterAccesseur = tmp[0].nextSibling;
			} else {
				// trouve le titre (part du principe que j'en ai toujours un)
				tmp = getElementsByTagAndClassFromNode(currTopEdition.div, "h2", "");
				if(tmp.length) currTopEdition.TagAfterAccesseur = tmp[0].nextSibling; // trouve le tag suivant le titre
			}
		} else {
			if(currTopEdition != null)
			{
				// trouve la toolbar à déplacer
				tmp = getElementsByTagAndClassFromNode(tabIdEdition[i].div, "div", "accesseur acc_edition toolbar");
				tabIdEdition[i].ToolbarSource = (tmp.length)? tmp[0] : null;// renvoi l'accesseur
				currTopEdition.childs[currTopEdition.childs.length] = tabIdEdition[i];
			}
		}
	}		

	// loop sur les groupes et deplace les toolbar des sujets suivit vers les sujets racine
	for(var i = 0;i < groupes.length; i++)
	{
		if(groupes[i].TagAfterAccesseur == null) continue;
		// loop sur les sujets suivit
		for(var j = groupes[i].childs.length-1;j>-1;j--)
		{
			// deplacement
			if(groupes[i].childs[j].ToolbarSource != null)
			{
				groupes[i].TagCorps.insertBefore(
					groupes[i].childs[j].ToolbarSource
					, groupes[i].TagAfterAccesseur.nextSibling
				);
			}
		}
	}

	// Formate le titre
	FormatTitleRootEdition(groupes);
}

/* declenchement du formatage du titre des editions en plusieurs parties regroupées */
function UpdateTitleRootEdition(tabIdEdition)
{
	/*var prev_edi = null
		, tmp = null
		, currTopEdition = null
		, groupes = [];

	// Trouve les debuts de sujet et les sujets associés
	for(var i = 0;i < tabIdEdition.length; i++)
	{
		if(tabIdEdition[i].repris != 'true')
		{
			// Init l'edition racine
			currTopEdition = tabIdEdition[i];
			currTopEdition.childs = [];
			currTopEdition.TagEntete = null;
			groupes[groupes.length] = currTopEdition;
			// trouve l'entete pour la gestion du nbr de partie dans l'entete
			tmp = getElementsByTagAndClassFromNode(currTopEdition.div, "h1", "");
			if(tmp.length)
			{
				currTopEdition.TagEntete = tmp[0].getElementsByTagName("a")[0];// Trouve le lien dans le H1
			}
		} else {
			if(currTopEdition != null)
			{
				// Référence l'enfant
				currTopEdition.childs[currTopEdition.childs.length] = tabIdEdition[i];
			}
		}
	}		

	// Formate le titre
	FormatTitleRootEdition(groupes);*/
}

/* fonction de formatage du titre d'une edition en plusieurs parties regroupées. */
function FormatTitleRootEdition(groupes)
{
	var str = '';
	// loop sur les groupes et ajoute le nbr de partie dans l'entete du groupe
	for(var i = 0;i < groupes.length; i++)
	{
		if(groupes[i].TagEntete == null) continue;
		str = '';
		if(groupes[i].childs.length)// L'édition a des sujets repris
		{
			str = (groupes[i].no_partie == 1)? ' - ' + groupes[i].no_partie + '<sup>&#232;re</sup>' : ' - ' + groupes[i].no_partie + '<sup>&#232;me</sup>';
			for(var j = 0, len = groupes[i].childs.length; j < len ; j++)
			{
				if(j == (len-1))// Dernier
				{
					str += ' et ' + (groupes[i].childs[j].no_partie) + '<sup>&#232;me</sup> partie.';
				} else {
					str += ', ' + (groupes[i].childs[j].no_partie) + '<sup>&#232;me</sup> ';
				}
				
			}
			groupes[i].TagEntete.innerHTML += str;
		} else if(groupes[i].no_partie > 1) {// est une edition racine au milieu d'un ensemble.
			str = ' - ' + groupes[i].no_partie + '<sup>&#232;me</sup>  partie.';
			groupes[i].TagEntete.innerHTML += str;
		} else if(groupes[i].no_partie == 1 && groupes[i].nbr_partie > 1) {// est la premiere édition dans une emission à multiple parties
			str = ' - 1<sup>&#232;re</sup>  partie.';
			groupes[i].TagEntete.innerHTML += str;
		}
	}
}

/* 
	Ajoute un objet de référence edition à la collection tabIdEdition pour usage futur de
	moveToolbarToRootEdition(tabIdEdition) ou UpdateTitleRootEdition(tabIdEdition).
*/
function addEditionReferenceObject(tabIdEdition, id, repris, no_partie, nbr_partie)
{
	IdEditionObj = new Object();
	IdEditionObj.id = id;
	IdEditionObj.div = document.getElementById(id);
	IdEditionObj.repris = repris;
	IdEditionObj.no_partie = no_partie;
	IdEditionObj.nbr_partie = nbr_partie;
	tabIdEdition[tabIdEdition.length] = IdEditionObj;
}
