// JavaScript Document



function insertTag(startTag, endTag, textareaId, tagType) {
	var field = document.getElementById(textareaId);
	var scroll = field.scrollTop;
	field.focus();
	
	
	if (window.ActiveXObject) {
		var textRange = document.selection.createRange();            
		var currentSelection = textRange.text;
	} else {
		var startSelection   = field.value.substring(0, field.selectionStart);
		var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
		var endSelection     = field.value.substring(field.selectionEnd);
	}	
	
	if (tagType) {
		switch (tagType) {
			case "lien":
					endTag = "</lien>";
					if (currentSelection) {
							if (currentSelection.indexOf("http://") == 0 || currentSelection.indexOf("https://") == 0 || currentSelection.indexOf("ftp://") == 0 || currentSelection.indexOf("www.") == 0) {
									var label = prompt("Quel est le libellé du lien ?") || "";
									startTag = "<lien url=\"" + currentSelection + "\">";
									currentSelection = label;
							} else {
									var URL = prompt("Quelle est l'url ?");
									startTag = "<lien url=\"" + URL + "\">";
							}
					} else {
							var URL = prompt("Quelle est l'url ?") || "";
							var label = prompt("Quel est le libellé du lien ?") || "";
							startTag = "<lien url=\"" + URL + "\">";
							currentSelection = label;                     
					}
			break;
			case "image":
					var URL = prompt("Quelle est l'url de l'image ?") || "";
					startTag = "<image url=\"";
					currentSelection = URL;   
					endTag = "\">";
			break;
			case "liste":
					c = "";
					startTag = "<liste>\n";
					while(tmp=prompt("Saisir le contenu d'une puce (si vous voulez arrêter ici, cliquez sur annuler)")) {
						c+="<puce>"+tmp+"</puce>\n"
					}					
					currentSelection = c;   
					endTag = "</liste>";
			break;
		}
	}
	
	if (window.ActiveXObject) {
		textRange.text = startTag + currentSelection + endTag;
		textRange.moveStart('character', -endTag.length-currentSelection.length);
		textRange.moveEnd('character', -endTag.length);
		textRange.select();  
	} else { // Ce n'est pas IE
		field.value = startSelection + startTag + currentSelection + endTag + endSelection;
		field.focus();
		field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);
	}  
	
	field.scrollTop = scroll;   
}

function preview(textareaId, previewDiv) {
	var field = textareaId.value;
	if (field) {
		field = field.replace(/&/g, '&amp;');
		field = field.replace(/</g, '&lt;').replace(/>/g, '&gt;');
		field = field.replace(/\n/g, '<br />').replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		
		field = field.replace(/&lt;gras&gt;/g, '<strong>');
		field = field.replace(/&lt;\/gras&gt;/g, '</strong>');
		field = field.replace(/&lt;italique&gt;/g, '<em>');
		field = field.replace(/&lt;\/italique&gt;/g, '</em>');
		field = field.replace(/&lt;barre&gt;/g, '<s>');
		field = field.replace(/&lt;\/barre&gt;/g, '</s>');
		field = field.replace(/&lt;souligne&gt;/g, '<u>');
		field = field.replace(/&lt;\/souligne&gt;/g, '</u>');
		
		field = field.replace(/&lt;gauche&gt;/g, '<div style="text-align:left;">');
		field = field.replace(/&lt;droite&gt;/g, '<div style="text-align:right;">');
		field = field.replace(/&lt;centre&gt;/g, '<div style="text-align:center;">');
		field = field.replace(/&lt;justifie&gt;/g, '<div style="text-align:justify;">');
		
		field = field.replace(/&lt;\/gauche&gt;/g, '</div>');
		field = field.replace(/&lt;\/droite&gt;/g, '</div>');
		field = field.replace(/&lt;\/centre&gt;/g, '</div>');
		field = field.replace(/&lt;\/justifie&gt;/g, '</div>');
		
		field = field.replace(/&lt;image url="([\s\S]*?)"&gt;/g, '<img src="$1" alt="Image" />');
		field = field.replace(/&lt;lien url="([\s\S]*?)"&gt;/g, '<a href="$1" title="Visitez le lien">');
		field = field.replace(/&lt;\/lien&gt;/g, '</a>');
		field = field.replace(/&lt;liste&gt;/g, '<ul>');
		field = field.replace(/&lt;\/liste&gt;/g, '</ul>');
		field = field.replace(/&lt;puce&gt;/g, '<li>');
		field = field.replace(/&lt;\/puce&gt;/g, '</li>');
		document.getElementById(previewDiv).innerHTML = field;
	}
}
var accordeonContainer = null;
var accordeonPanes = null;
var accordeonHeaders = null;
var accordeonGoToX = null;
var accordeonTimer = null;
var accordeonIndex = -1;
var accordeonCurrentBack = 0;

function initAccordeon(container) {
    accordeonContainer = document.getElementById(container);
    var first = accordeonContainer.firstChild;    
    var first2;
    accordeonPanes = new Array();
    accordeonHeaders = new Array();
    while (first.nextSibling) {
	    if (first.nodeName == "DIV") {
	        accordeonPanes.push(first);
	        first2 = first.firstChild;
	        while (first2.nodeName != "DIV") first2 = first2.nextSibling;
	        accordeonHeaders.push(first2);
	    }
	    first = first.nextSibling;
    }
    accordeonContainer.style.backgroundImage = "url(" + accordeonBackgrounds[0] + ")";
    if (accordeonBackgrounds.length > 1) setInterval("setNextBackground()", 3000);
	
}

function setNextBackground() {
    accordeonContainer.style.backgroundImage = "url(" + accordeonBackgrounds[(accordeonCurrentBack =
        (accordeonCurrentBack + 1) % accordeonBackgrounds.length)] + ")";
}

function showAccordeon(pane) {
    var paneIndex = 0;
    var i = 0;
    accordeonGoToX = new Array();
    while (paneIndex < accordeonPanes.length && accordeonPanes[paneIndex] != pane) { paneIndex++; }
    if (paneIndex == 0 && accordeonIndex == 0) {
        for (i = 0; i < accordeonPanes.length; i++)
            accordeonGoToX.push(740 - ((accordeonPanes.length - i) * 30));
        accordeonHeaders[0].style.backgroundPosition = "0px 0px";
        if (accordeonTimer) clearInterval(accordeonTimer);
        accordeonTimer = setInterval("movePanes()", 50);
        accordeonIndex = -1;
        return;
    }
    if (paneIndex == accordeonIndex) return;
    accordeonIndex = paneIndex;    
    for (i = 0; i <= paneIndex; i++) {
        accordeonGoToX.push(i * 30);
        accordeonHeaders[i].style.backgroundPosition = "0px 0px";
    }
    for (i; i < accordeonPanes.length; i++) {
        accordeonGoToX.push(740 - ((accordeonPanes.length - i) * 30));
        accordeonHeaders[i].style.backgroundPosition = "0px 0px";
    }
    accordeonHeaders[paneIndex].style.backgroundPosition = "0px -300px";
    if (accordeonTimer) clearInterval(accordeonTimer);
    accordeonTimer = setInterval("movePanes()", 50);
}

function movePanes() {
    var tmpSize, offsetLeft, finished = true;
    for (var i = 0; i < accordeonPanes.length; i++) {
        offsetLeft = parseInt(accordeonPanes[i].style.left);
        if (offsetLeft != accordeonGoToX[i]) {
            tmpSize = Math.round((offsetLeft - accordeonGoToX[i]) / 4.0);
            if ((tmpSize >= -1) && (tmpSize <= 1))
                accordeonPanes[i].style.left = accordeonGoToX[i] + "px";
            else
                accordeonPanes[i].style.left = (offsetLeft - tmpSize) + "px";
        }
    }
    for (var i = 0; (i < accordeonPanes.length && finished); i++) {
        offsetLeft = parseInt(accordeonPanes[i].style.left);
        if (offsetLeft != accordeonGoToX[i])
            finished = false;
    }
    if (finished) clearInterval(accordeonTimer);
}



function init() {
	initAccordeon("Accordeon");
	
}
