﻿/*
 * Copyright © 2006 Thierry Karsenti <thierry.karsenti@umontreal.ca>
 * et Martin Ledoux <martin.ledoux@gmail.com>. Tous droits réservés.
 * Pour de plus amples informations : http://eduportfolio.org
 */


/**
 * Cache/affiche un élément, selon son état antérieur.
 *
 * id	Identifiant XML de l'élément en cause
 */
function basculerElement(id)
{
	var obj = $(id);

	var desc = obj.select('[id^="description"]');
	if (obj.style.display == 'none')
	{
	    for (var i=0; i<desc.length; i++)
		tinyMCE.execCommand("mceAddControl", false, desc[i].id);
	    obj.style.display = 'block';
	}
	else
	{
	    for (var i=0; i<desc.length; i++)
		tinyMCE.execCommand("mceRemoveControl", false, desc[i].id);
	    obj.style.display = 'none';
	}
}


/**
 * Cache tous les éléments divN de la page.
 */
function cacherElements()
{
	var n = document.forms['formCompteur'].elements['compteur'].value;

	for (i = 0; i < n; i++)
	{
		var nom = 'div' + i;
		if (document.getElementById(nom))
		{
			document.getElementById(nom).style.display = 'none';
		}
	}
	
	return;
}


/**
 * Cache un élément du plan.
 */
function planCacher(id)
{
	document.getElementById('plan_sections_' + id).style.display = 'none';
	document.getElementById('plan_developper_' + id).style.display = 'inline';
	document.getElementById('plan_cacher_' + id).style.display = 'none';
	return;
}

/**
 * Développe un élément du plan.
 */
function planDevelopper(id)
{
	document.getElementById('plan_sections_' + id).style.display = 'block';
	document.getElementById('plan_developper_' + id).style.display = 'none';
	document.getElementById('plan_cacher_' + id).style.display = 'inline';
	return;
}


/**
 * Cache tous les éléments divN puis révèle l'élément demandé.
 *
 * id	Identifiant XML de l'élément à révéler
 */
function revelerElement(id)
{
	var n = document.forms['formCompteur'].elements['compteur'].value;
	var etat = document.getElementById(id).style.display;

	for (var i = 0; i < n; i++)
	{
		var nom = 'div' + i;
		if (document.getElementById(nom))
		{
			document.getElementById(nom).style.display = 'none';
		}
	}

	if (etat != 'block')
	{
	    var num = id.substring(3);
	    tinyMCE.execCommand("mceAddControl", true, 'description'+num);
	    tinyMCE.execCommand("mceAddControl", true, 'commentaires'+num);
	    document.getElementById(id).style.display = 'block';
	    if ($('captcha_'+num)) {
		Recaptcha.create('6Ldh4AcAAAAAAGXLSfjRWHQNVN7gBTTjlnKybiE4', 'captcha_'+num, {theme: 'clean', lang:langue});
	    }
	}
	return;
}

function revelerElement2(id, base)
{
    var el = $(id);
    //var els = $$('[id^='+base+']');
    
    var etat = el.style.display;
    var count = 0;
    var other = $(base + '' + count);
    while (other)
    {
	// if (els[i].id.replace(base, '').search(/^[0-9]+$/) == 0)
	other.style.display = 'none';
	count++;
	other = $(base + '' + count);
    }
    
    if (etat != 'block') {
	el.style.display = 'block';
	el.up().scrollTo();
    }
    return;
}

function plusMinus(el, base)
{
  var elem = $(el);
  //var els = $$('[id^='+base+']');

  var etat = elem.innerHTML;

  var count = 0;
  var other = $(base + '' + count);
  while (other)
  {
      other.update('+');
      count++;
      other = $(base + '' + count);
  }

  if (etat != '-')
    elem.update('-');

}

function showHide(el)
{
  var elem = $(el);
  var etat = elem.style.display;

  if (etat != 'block')
    elem.style.display = 'block';
  else
    elem.style.display = 'none';
}

/**
 * Valide le bordereau pour commenter un élément, puis le soumet.
 *
 * id	Identifiant XML du bordereau
 * lang Langue à utiliser
 */
function validerCommenterElement(id, lang)
{
//	if (document.forms['' + id].elements['data[Portfolio][captcha]'] && document.forms['' + id].elements['data[Portfolio][captcha]'].value == '')
//	{
//	    alert(__("Vous devez saisir la chaîne apparaissant dans l'image."));
//	    return;
//	}
//	else
//	{
		document.forms['' + id].submit();
//	}
}

function ajouterDocument(id, lang)
{
  if (ajoutDocValide(id) > 0)
    return;

  var el = $(id);

    var desc = el.select('[id^="description"]');
    for (var i=0; i<desc.length; i++)
	tinyMCE.execCommand("mceRemoveControl", true, desc[i].id);

  var children = el.childElements();

  var copy = children[1].innerHTML;
  copy = copy.replace(/\[0]/g, '[' + (children.length - 2) + ']');
  copy = copy.replace(/_0_/g, '_' + (children.length - 2) + '_');
  copy = copy.replace(/Portfolio0/g, 'Portfolio' + (children.length - 2));
  copy = copy.replace(/<legend>/g, '<h3>');
  copy = copy.replace(/<\/legend>/g, '<\/h3>');
  
  children[1].insert({bottom:copy});
 // children[children.length - 1].insert({before:copy});

    desc = el.select('[id^="description"]');
    for (var i=0; i<desc.length; i++)
	tinyMCE.execCommand("mceAddControl", true, desc[i].id);

}
