/*

Adapté de http://fr.wikipedia.org/skins-1.5/common/wikibits.js

*/

// Détection de l'Agent Utilisateur
var clientPC = navigator.userAgent.toLowerCase();
var is_gecko = ((clientPC.indexOf('gecko') != -1) && (clientPC.indexOf('spoofer') == -1) && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0') == -1));


function wrapSelection(tagOpen, tagClose, destTextareaId, sampleText)
{
	var destTextarea = document.getElementById(destTextareaId);

	if(document.selection && !is_gecko)
	{ // IE et Opéra (et potentiellement d'autres)
		var theSelection = document.selection.createRange().text;
		if(!theSelection)
		{
			theSelection = sampleText;
		}

		destTextarea.focus();

		if(theSelection.charAt(theSelection.length - 1) == " ")
		{ // exclude ending space char, if any
			theSelection = theSelection.substring(0, theSelection.length - 1);
			document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
		}
		else
		{
			document.selection.createRange().text = tagOpen + theSelection + tagClose;
		}
	}
	else if(destTextarea.selectionStart || destTextarea.selectionStart == '0')
	{ // Mozilla
		var replaced = false;
		var startPos = destTextarea.selectionStart;
		var endPos = destTextarea.selectionEnd;
		if(endPos - startPos)
		{
			replaced = true;
		}

		var scrollTop = destTextarea.scrollTop;
		var myText = (destTextarea.value).substring(startPos, endPos);
		if (!myText)
		{
			myText = sampleText;
		}

		if (myText.charAt(myText.length - 1) == " ")
		{ // exclude ending space char, if any
			subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
		}
		else
		{
			subst = tagOpen + myText + tagClose;
		}

		destTextarea.value = destTextarea.value.substring(0, startPos) + subst +
		destTextarea.value.substring(endPos, destTextarea.value.length);

		destTextarea.focus();

		if(replaced)
		{//set new selection
			var cPos = startPos + (tagOpen.length + myText.length + tagClose.length);
			destTextarea.selectionStart = cPos;
			destTextarea.selectionEnd = cPos;
		}
		else
		{
			destTextarea.selectionStart = startPos + tagOpen.length;   
			destTextarea.selectionEnd = startPos + tagOpen.length + myText.length;
		}	
		destTextarea.scrollTop = scrollTop;
	}
	
	if(destTextarea.createTextRange)
	{// reposition cursor if possible
		destTextarea.caretPos = document.selection.createRange().duplicate();
	}
}


function wrapWikiLink(destTextareaId, sampleText)
{
	if(linkUrl = prompt('Lien :', 'http://'))
	{
		var destTextarea = document.getElementById(destTextareaId);

		if(document.selection && !is_gecko)
		{ // IE et Opéra (et potentiellement d'autres)
			var theSelection = document.selection.createRange().text;
			if(!theSelection)
			{
				theSelection = sampleText;
			}

			destTextarea.focus();

			if(theSelection.charAt(theSelection.length - 1) == " ")
			{ // exclude ending space char, if any
				theSelection = theSelection.substring(0, theSelection.length - 1);
				document.selection.createRange().text = '[' + theSelection + '|' + linkUrl + "] ";
			}
			else
			{
				document.selection.createRange().text = '[' + theSelection + '|' + linkUrl + "]";
			}
		}
		else if(destTextarea.selectionStart || destTextarea.selectionStart == '0')
		{ // Mozilla
			var replaced = false;
			var startPos = destTextarea.selectionStart;
			var endPos = destTextarea.selectionEnd;
			if(endPos - startPos)
			{
				replaced = true;
			}

			var scrollTop = destTextarea.scrollTop;
			var myText = (destTextarea.value).substring(startPos, endPos);
			if (!myText)
			{
				myText = sampleText;
			}

			if (myText.charAt(myText.length - 1) == " ")
			{ // exclude ending space char, if any
				subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
			}
			else
			{
				subst = tagOpen + myText + tagClose;
			}

			destTextarea.value = destTextarea.value.substring(0, startPos) + subst +
			destTextarea.value.substring(endPos, destTextarea.value.length);

			destTextarea.focus();

			if(replaced)
			{//set new selection
				var cPos = startPos + (tagOpen.length + myText.length + tagClose.length);
				destTextarea.selectionStart = cPos;
				destTextarea.selectionEnd = cPos;
			}
			else
			{
				destTextarea.selectionStart = startPos + tagOpen.length;   
				destTextarea.selectionEnd = startPos + tagOpen.length + myText.length;
			}	
			destTextarea.scrollTop = scrollTop;
		}
	
		if(destTextarea.createTextRange)
		{// reposition cursor if possible
			destTextarea.caretPos = document.selection.createRange().duplicate();
		}
	}
}