var outilsChamp = false;
var outilsAffiche = false;
function afficheOutils(event,champ)
{
	if(event.keyCode == 17 && !outilsAffiche)
	{
	    outilsChamp = champ;
	    outilsAffiche = true;
	    document.getElementById("outils").style.left = mousePosX - 100 + "px";
	    document.getElementById("outils").style.top = mousePosY - 100 + "px";
		Show("outils");
	}
}
var mousePosX = 0;
var mousePosY = 0;
function cacheOutils()
{
    if(outilsAffiche)
    {
		Hide("outils");
        outilsChamp.focus();
        outilsAffiche = false;
	}
}
function captureMousePos(event)
{
	mousePosX = event.clientX;
	mousePosY = event.clientY;
}

var selStart;
var selEnd;
var selText;
function getSelText()
{
	selText = false;
	selStart = false;
	selEnd = false;
    if (document.selection && document.selection.createRange)
	{
		var sel;
		sel = document.selection.createRange();
		if(sel.parentElement()==outilsChamp)
			selText = sel.text;
	}
	else if(String(typeof outilsChamp.selectionStart)!="undefined")
	{
		var longueur = parseInt(outilsChamp.textLength);
		selStart = outilsChamp.selectionStart;
		selEnd = outilsChamp.selectionEnd;
		if((outilsChamp.value).substring(selEnd-1,selEnd) == " ")
		    selEnd--;
		if (selEnd == 2 || selEnd == 1)
			selEnd = longueur;

		selText = (outilsChamp.value).substring(selStart,selEnd);
	}
	return selText;
}

function replaceSelText(newText)
{
	if(selStart && selEnd)
		outilsChamp.value = (outilsChamp.value).substring(0,selStart) + newText + (outilsChamp.value).substring(selEnd,parseInt(outilsChamp.textLength));
	else if(selStart)
		outilsChamp.value = (outilsChamp.value).substring(0,selStart) + newText;
	else if(selEnd)
		outilsChamp.value = newText + (outilsChamp.value).substring(selEnd,parseInt(outilsChamp.textLength));
	else
	    selText = newText;
}

var url = false;
var lien = false;
var formulaire;
var clics = new Array();

var remplacer = false;
function active(champ)
{
	if(remplacer && !clics[champ.name])
	{
		clics[champ.name] = true;
		champ.value = "";
	}
}

function balise(val,bouton)
{
	//active(outilsChamp);
	var str = getSelText();
	if(str)
	{
		if(val == "lien")
		{
		    cacheOutils();
		    url = false;
			ouvreMsgBox("Adresse internet :","info","saisie");
			setSaisie("http://");
			lier("ok",lien1);
		}
		else
		{
			if(val == "article")
			{
				replaceSelText("[[" + str + "]]");
			}
 			else
 			{
 				replaceSelText("[" + val + "]" + str + "[/" + val + "]");
 			}
 		}
	}
	else
	{
		if(val == "lien")
		{
		    cacheOutils();
			url = false;
			ouvreMsgBox("Adresse internet :","info","saisie");
			setSaisie("http://");
			lier("ok",lien1);
		}
		else
		{
			if(val == "article")
			{
				if(bouton.value.substring(0,1) != "/")
				{
					outilsChamp.value += "[[";
					bouton.value = "/" + bouton.value;
				}
				else
				{
					outilsChamp.value += "]]";
					bouton.value = bouton.value.substring(1,10);
				}
			}
			else
			{
				if(bouton.value.substring(0,1) != "/")
				{
					outilsChamp.value += "[" + val + "]";
					bouton.value = "/" + bouton.value;
				}
				else
				{
					outilsChamp.value += "[/" + val + "]";
					bouton.value = bouton.value.substring(1,10);
				}
			}
			outilsChamp.focus();
		}
	}
}

function balise1(val,bouton)
{
	//active(outilsChamp);	
	var str = getSelText();
	if(str)
	{
		replaceSelText("[" + val + "]" + str);
	}
	else
	{
	outilsChamp.value += "[" + val + "]";
	outilsChamp.focus();
	}
}

function lien1()
{
	if(!url)
		url = recupSaisie();
	if(url == false || url == "")
	    balise(form,'lien');
	else if(selText)
	{
		replaceSelText("[lien=" + url + "]" + selText + "[/lien]");
	}
	else
	{
		ouvreMsgBox("Nom du lien :","info","saisie");
		setSaisie(url);
		lier("ok",lien2);
	}
}
function lien2()
{
	lien = recupSaisie();
	if(lien == null || lien == false || lien == "")
	    lien1(form);
	else
	{
		if(url != lien)
			outilsChamp.value += "[lien=" + url + "]" + lien + "[/lien]";
		else
			outilsChamp.value += "[lien]" + url+ "[/lien]";
		outilsChamp.focus();
	}
}

var auteur;
var chapitre;
function chron()
{
	getSelText();
	cacheOutils();
	ouvreMsgBox("Auteur de la chronique :","question","saisie");
	lier("ok",chron1);
}
function chron1()
{
	auteur = recupSaisie();
	if(auteur == false || auteur == "")
	    chron();
	else
	{
		ouvreMsgBox("Chapitre (facultatif)","question","saisie");
		lier("ok",chron2);
	}
}
function chron2()
{
	chapitre = recupSaisie();

	if(selText)
	{
	    chron3();
	}
	else
	{
		ouvreMsgBox("Texte à afficher","question","saisie");
		lier("ok",chron3);
	}
}
function chron3()
{
	if(selText)
		texte = selText;
	else
		texte = recupSaisie();
	if(texte == null || texte == false || texte == "")
	    chron2();
	else
	{
		if(selText)
		{
			if(chapitre == false || chapitre == "")
				replaceSelText("[chron=" + auteur + "]" + texte + "[/chron]");
			else
				replaceSelText("[chron=" + auteur + "#" + chapitre + "]" + texte + "[/chron]");
		}
		else
		{
			if(chapitre == false || chapitre == "")
				outilsChamp.value += "[chron=" + auteur + "]" + texte + "[/chron]";
			else
				outilsChamp.value += "[chron=" + auteur + "#" + chapitre + "]" + texte + "[/chron]";
		}
	}
}

function font(type,bouton)
{
	val = valueList(document.getElementById("outils_" + type));
	active(outilsChamp);
	var str = getSelText();
	if(str)
	{
		replaceSelText("[" + type + "=" + val + "]" + selText + "[/" + type + "]");
	}
	else
	{
		if(bouton.value.substring(0,1) != "/")
		{
			outilsChamp.value += "[" + type + "=" + val + "]";
			bouton.value = "/" + bouton.value;
		}
		else
		{
			outilsChamp.value += "[/" + type + "]";
			bouton.value = bouton.value.substring(1,10);
		}
	}
}

var schapitre;
var tpage;
function page(bouton)
{
    getSelText();
	cacheOutils();
	tpage = valueList(document.getElementById("outils_page"));
	ouvreMsgBox("Chapitre (facultatif)","question","saisie");
	lier("ok",page2);
}
function page2()
{
	chapitre = recupSaisie();
	if(parseInt(chapitre) > 0)
	{
		ouvreMsgBox("Sous-chapitre (facultatif)","question","saisie");
		lier("ok",page3);
	}
	else
	    page3();
}
function page3()
{
	schapitre = recupSaisie();
	if(selText)
	    page4();
	else
	{
		ouvreMsgBox("Texte à afficher","question","saisie");
		if(parseInt(chapitre) > 0 && schapitre != null && schapitre != false && schapitre != "")
			setSaisie(tpage + " (chap " + chapitre + "." + schapitre + ")");
		else if(parseInt(chapitre) > 0)
			setSaisie(tpage + " (chap " + chapitre + ")");
		else
			setSaisie(tpage);
		lier("ok",page4);
	}
}
function page4()
{
	if(selText)
	    texte = selText;
	else
		texte = recupSaisie();
	if(texte == null || texte == false || texte == "")
	    page3();
	else
	{
		if(selText)
		{
			if(chapitre == false || chapitre == "")
				replaceSelText("[" + tpage + "]" + texte + "[/" + tpage + "]");
			else if(schapitre == false || schapitre == "")
				replaceSelText("[" + tpage + "=" + chapitre + "]" + texte + "[/" + tpage + "]");
			else
				replaceSelText("[" + tpage + "=" + chapitre + "#" + schapitre + "]" + texte + "[/" + tpage + "]");
		}
		else
		{
			if(chapitre == false || chapitre == "")
				outilsChamp.value += "[" + tpage + "]" + texte + "[/" + tpage + "]";
			else if(schapitre == false || schapitre == "")
				outilsChamp.value += "[" + tpage + "=" + chapitre + "]" + texte + "[/" + tpage + "]";
			else
				outilsChamp.value += "[" + tpage + "=" + chapitre + "#" + schapitre + "]" + texte + "[/" + tpage + "]";
		}
	}
}


