﻿jQuery(function($){
	if (ids != undefined)
	{
	window.setInterval(function () { if ($('.curseur').css('visibility') == 'visible') $('.curseur').css('visibility', 'hidden'); else $('.curseur').css('visibility', 'visible');  }, 500);

	for (i=0;i<ids.length;i++)
	{
		var ta = $('#'+ids[i]);
		ta.keyup(function(e){ 
			if (e.altKey)
			{
				if (e.keyCode == 66)
				{
					balise('<b>', '</b>', $(e.target).attr("id"));
				}				
				if (e.keyCode == 73)
				{
					balise('<i>', '</i>', $(e.target).attr("id"));
				}
				if (e.keyCode == 85)
				{
					balise('<u>', '</u>', $(e.target).attr("id"));
				}
				if (e.keyCode == 83)
				{
					balise('<s>', '</s>', $(e.target).attr("id"));
				}
			}
			storeCaret(e.target.id);
			parser(e.target.id);
			return false;
		});
		ta.select(function(e){storeCaret(e.target.id);});
		ta.click(function(e){ storeCaret(e.target.id); parser(e.target.id); });
	}
	}
});


function quote(topic,post)
{
	$('#reponse_rapide').show();
	$.ajax({ 
	type: 'POST',
	data: 'post='+post+'&topic='+topic, 
	url: path+"forum/getpost.php", 
	success: setquote
	}); 
	
}

function setquote(post)
{
	avant = $('#message').val();
	if (avant.length > 0) avant+="\n";
	$('#message').val(avant+"\n"+post);
	document.location='#message';
}
 
function storeCaret(id_textarea)
{ 
    champ = document.getElementById(id_textarea);
    if (champ.createTextRange)
	champ.curseur = document.selection.createRange().duplicate();
}

function substr_replace (str, replace, start, length) {
    if (start < 0) { // start position in str
        start = start + str.length;
    }
    length = length !== undefined ? length : str.length;
    if (length < 0) {
        length = length + str.length - start;
    }
    return str.slice(0, start)+replace.substring(0, length)+replace.slice(length)+str.slice(start+length);
}

function balise(balise_debut, balise_fin, id_textarea) {
    var champ = document.getElementById(id_textarea);
    var scroll = champ.scrollTop;
    balise_debut = remplace(balise_debut, '<br />', "\n");
    
    if(balise_fin == '')
	balise_debut = ' ' + balise_debut + ' ';
    
    if (champ.curseur)
	{
	    champ.curseur.text = balise_debut + champ.curseur.text + balise_fin;
	}
    else if (champ.selectionStart >= 0 && champ.selectionEnd >= 0)
	{
	    var debut = champ.value.substring(0, champ.selectionStart);
	    var entre = champ.value.substring(champ.selectionStart, champ.selectionEnd);
	    var fin = champ.value.substring(champ.selectionEnd);
	    champ.value = debut + balise_debut + entre + balise_fin + fin;
	    champ.focus();
	    champ.setSelectionRange(debut.length + balise_debut.length, champ.value.length - fin.length - balise_fin.length);
	}
    else
	{
	    champ.value  += balise_debut + balise_fin;
	    champ.focus();
	}
    champ.scrollTop = scroll;
}

function getselection(id_textarea) {
     champ = document.getElementById(id_textarea);
 	if (champ.setSelectionRange)
 		return champ.value.substring(champ.selectionStart, champ.selectionEnd);
 	else if (document.selection) {
 		champ.focus();
 		return document.selection.createRange().text;
 	}
 }
 

function getcurseur(id_textarea) {
	champ = document.getElementById(id_textarea);
 	if ( typeof champ.selectionStart != 'undefined' )
 		return champ.selectionStart;
 	// IE Support
 	champ.focus();
 	var range = champ.createTextRange();
 	range.moveToBookmark(document.selection.createRange().getBookmark());
 	range.moveEnd('character', champ.value.length);
 	return champ.value.length - range.text.length;
 }
 
function getfincurseur(id_textarea) {
	champ = document.getElementById(id_textarea);
	if ( typeof champ.selectionEnd != 'undefined' )
 		return champ.selectionEnd;
 	// IE Support
 	champ.focus();
 	var range = champ.createTextRange();
 	range.moveToBookmark(document.selection.createRange().getBookmark());
 	range.moveStart('character', champ.value.length*-1);
 	return range.text.length;
 }

function baliseurl(event) {
	id_textarea = $(event.target).parents("div").attr('rel');

	txt_selectionne=getselection(id_textarea);
	if (txt_selectionne.indexOf('http://') == 0
	|| txt_selectionne.indexOf('https://') == 0
	|| txt_selectionne.indexOf('ftp://') == 0
	|| txt_selectionne.indexOf('apt://') == 0)
	{
	texte = 'Veuillez indiquer le texte qui apparaîtra';
	bali = prompt(texte,txt_selectionne);
	if (bali == null) return;
	balise_debut = '<url lien="';
	balise_fin = '">'+bali+'</url>';
	}
	else if (txt_selectionne == '')
	{
	texte = 'Veuillez indiquer l\'URL complète';
	bal = prompt(texte,'http://');
	bali = prompt('Veuillez indiquer le texte qui appraîtra',bal);
	if (bali == null) return;
	balise_debut = '<url lien="'+bal+'">'+bali;
	balise_fin = '</url>';
	}
	else
	{
	texte = 'Veuillez indiquer l\'URL complète';
	bal = prompt(texte,'http://');
	if (bal == null) return;
	balise_debut = '<url lien="'+bal+'">';
	balise_fin = '</url>';

	}
	balise(balise_debut, balise_fin, id_textarea);
	parser();
	return false;
}

function baliseimg(event) {
	id_textarea = $(event.target).parents("div").attr('rel');
	var ts=getselection(id_textarea);
	urlimage = ts;
	if (ts == '') urlimage = prompt("Veuillez indiquer l\'URL complète de l'image à afficher","http://");
	if (urlimage == null) return;
	if (ts == '') balise('<img>'+urlimage, '</img>', id_textarea);
	else balise('<img>', '</img>', id_textarea);
	parser();
	return false;
}

function bold(event) { balise('<b>', '</b>', $(event.target).parents("div").attr('rel')); return false;}
function underline(event) { balise('<u>', '</u>', $(event.target).parents("div").attr('rel')); return false; }
function italic(event) { balise('<i>', '</i>', $(event.target).parents("div").attr('rel')); return false; }
function strike(event) { balise('<s>', '</s>', $(event.target).parents("div").attr('rel')); return false; }

function find(event) {
	id_textarea = $(event.target).parents("div").attr('rel');

	ts = getselection(id_textarea);
	champ=document.getElementById(id_textarea);
	var avant = '';
	var apres = '';
	if (ts == '') { selection = false; }
	else selection = true;

	if (selection == false) ts=champ.value;
	chercher = prompt('Chaine à rechercher :');
	if (chercher == false || chercher == undefined) return;
	remplacer = prompt('Remplacer par (Annuler pour recherche simple) :');
	if (remplacer != false && remplacer != null)
	{
		recherche = new RegExp(chercher, 'gi');
		if (selection == true && champ.curseur) champ.curseur.text = ts.replace(recherche, remplacer);
		else
		{
			if (selection == true) 
			{
				avant = champ.value.substring(0, champ.selectionStart);
				apres = champ.value.substring(champ.selectionEnd);
			}
			ts = ts.replace(recherche, remplacer);
			champ.value=avant+ts+apres;
			parser();
		}
	}
	else
	{
		oucest = champ.value.search(new RegExp(chercher,'gi'));
		if (oucest == -1) alert('Chaine non trouvée');
		else
		{
			champ.setSelectionRange(oucest, oucest+chercher.length);
			champ.focus();
		}
	}
	return false;
}
function setCaretPos(id_textarea, start, end) {
	champ = document.getElementById(id_textarea);
 	end = end || start; 	champ.focus();
 	if (champ.setSelectionRange)
 		champ.setSelectionRange(start, end);
 	else if (document.selection) {
 		var range = champ.createTextRange();
 		range.moveStart('character', start);
 		range.moveEnd('character', - champ.value.length + end);
 		range.select();
 	}
 } 
function parser(id_textarea) {
	if ($("#"+id_textarea+"_preview").css('display') == "none") return;
	
	$("#"+id_textarea+"_preview").css('position','relative');

	texte = document.getElementById(id_textarea).value;
	poscurseur = getcurseur(id_textarea);
	texte = substr_replace(texte, '[curseur]', poscurseur,0);
	texte = texte.replace(/</g, '&lt;');
	texte = texte.replace(/>/g, '&gt;');

	ch('<i>(.+)</i>', '<i>$1</i>');
	ch('<b>(.+)</b>', '<b>$1</b>');
	ch('<u>(.+)</u>', '<u>$1</u>');
	ch('<s>(.+)</s>', '<s>$1</s>');
	ch('<li>(.+)</li>', '<ul><li>$1</li></ul>');
	ch('<quote auteur="(.+)">(.+)</quote>', '<fieldset class="quote"><legend>Citation de <b>$1</b></legend>$2</fieldset>');
	ch('<quote>(.+)</quote>', '<fieldset class="quote"><legend>Citation</legend>$1</fieldset>');
	ch('<titre>(.+)</titre>', '<h2>$1</h2>');
	ch('<soustitre>(.+)</soustitre>', '<h3>$1</h3>');
	ch('<sflottant[ ]/>', '<div class="cl"></div>');
	ch('<video>(.+)</video>', '<hr /><div class="centre">Vidéo ici ($1)</div><hr />');
	ch('<music>(.+)</music>', '<hr /><div class="centre">Musique ici ($1)</div><hr />');
	ch('<sf/>', '<div class="cl"></div>');

	chf('(<img(.+)>(.+)</img>)', 'doimg');
	chf('(<text(.+)>(.+)</text>)', 'dotext');
	chf('(<bloc(.+)>(.+)</bloc>)', 'dobloc');
	chf('(<url(.+)>(.+)</url>)', 'dolien');

	texte = remplace(texte, "\n", "<br />");


	simple(/<\/ul><br \/><ul>/g, '');
	simple(/<\/ul><ul>/g, '');
	simple(/<script/g, '');
	texte = texte.replace('[curseur]', '<span class="curseur"><span>|</span></span>');

	$("#"+id_textarea+"_preview").html(texte);
}

function ch(string1, string2) {
	string1 = string1.toString();
	string1 = string1.replace(/</g, '&lt;');
	string1 = string1.replace(/>/g, '&gt;');
	string1 = string1.replace(/\//g, '\\/');
	string1 = string1.replace(/\(\.\+\)/g, "([\\s\\S]*?)");
	string1 = new RegExp(string1,'g');
	texte = texte.replace(string1, string2);
}

function chf(string1, string2) {
	string1 = string1.toString();
	string1 = string1.replace(/</g, '&lt;');
	string1 = string1.replace(/>/g, '&gt;');
	string1 = string1.replace(/\//g, '\\/');
	string1 = string1.replace(/\(\.\+\)/g, "([\\s\\S]*?)");
	string1 = new RegExp(string1,'g');
	if (string2 == 'doimg') texte = texte.replace(string1, doimg);
	if (string2 == 'dotext') texte = texte.replace(string1, dotext);
	if (string2 == 'dobloc') texte = texte.replace(string1, dobloc);
	if (string2 == 'dolien') texte = texte.replace(string1, dolien);
}

function simple(string1, string2) {
	texte = texte.replace(string1, string2);
}

function remplace(data, search, replace) {
	var temp = data;
	var longueur = search.length;
		while (temp.indexOf(search) > -1)
		{
		pos = temp.indexOf(search);
		temp = (temp.substring(0, pos) + replace + temp.substring((pos + longueur), temp.length));
		}
	return temp;
}


function dolien(s0, index, input) {
	s0 = s0.replace(/&lt;/g, '<');
	s0 = s0.replace(/&gt;/g, '>');
	aumilieu = s0.replace(/^<url([\s\S]*?)>([\s\S]*?)<\/url>/g, '$2');
	lien = aumilieu;
	if (s0.indexOf('lien="') != -1) lien = s0.replace(/^(.*?)lien="(.*?)"(.*?)$/, '$2');
	if (lien.indexOf('[curseur]') != -1) return index;
	return '<a href="'+lien+'">'+aumilieu+'</a>';
}

function trad(mot)
{
mot = mot.replace('centre','center');
mot = mot.replace('gauche','left');
mot = mot.replace('droite','right');
mot = mot.replace('haut','top');
mot = mot.replace('milieu','middle');
mot = mot.replace('bas','bottom');
return mot;
}
function doimg(s0, index, input) {
	s0 = s0.replace(/&lt;/g, '<');
	s0 = s0.replace(/&gt;/g, '>');
	urlimage=s0.replace(/^<img([\s\S]*?)>([\s\S]*?)<\/img>/g, '$2');
	urlimage=urlimage.replace(/^\/([0-9]+)\/images\//, path+'files/$1/images/');
	proprietes=s0.replace(/^<img([\s\S]*?)>([\s\S]*?)<\/img>/g, '$1');
	if (proprietes.indexOf('[curseur]') != -1 || urlimage.indexOf('[curseur]') != -1) return index;

	position = "";
	zoom = "";
	hauteur = "";
	largeur = "";
	add="";
	flottant="";
	if (proprietes.indexOf('position="') != -1 ) position=proprietes.replace(/^([\s\S]*?)position="([\s\S]*?)"([\s\S]*?)$/g, '$2');
	if (proprietes.indexOf('zoom="') != -1 ) zoom=proprietes.replace(/^([\s\S]*?)zoom="([\s\S]*?)"([\s\S]*?)$/g, '$2');
	if (proprietes.indexOf('largeur="') != -1 ) largeur=proprietes.replace(/^([\s\S]*?)largeur="([\s\S]*?)"([\s\S]*?)$/g, '$2');
	if (proprietes.indexOf('hauteur="') != -1 ) hauteur=proprietes.replace(/^([\s\S]*?)hauteur="([\s\S]*?)"([\s\S]*?)$/g, '$2');
	if (proprietes.indexOf('flottant="') != -1 ) flottant=proprietes.replace(/^([\s\S]*?)flottant="([\s\S]*?)"([\s\S]*?)$/g, '$2');

	if (position || flottant)
	{
		add+=' class="';
		if (position) add+= trad(position)+' ';
		if (flottant) add+= flottant.replace('gauche','fl_parse').replace('droite','fr_parse')+' ';
		add+='"';
	}
	if (position) add+=' class="'+trad(position)+'"';
	if (largeur && hauteur) add+=' height="'+hauteur+'" width="'+largeur+'"';
	if (zoom)
	{
		urldepart = zoom.replace(/^\/([0-9]+)\/images\//, path+'files/$1/images/');
		
		return '<a href="'+urldepart+'" rel="lytebox"><img alt="image" src="'+urlimage+'" '+add+' /></a>';
	}

	return '<img alt="image" src="'+urlimage+'" '+add+' />';
}

function dotext(s0, index, input) {
	s0 = s0.replace(/&lt;/g, '<');
	s0 = s0.replace(/&gt;/g, '>');
	text=s0.replace(/^<text(.*)>([\s\S]*?)<\/text>/g, '$2');
	proprietes=s0.replace(/^<text(.*)>([\s\S]*?)<\/text>/g, '$1');
	if (proprietes.indexOf('[curseur]') != -1) return index;
	add="";

	position = (proprietes.indexOf('position="') != -1 ) ? proprietes.replace(/^([\s\S]*?)position="([\s\S]*?)"([\s\S]*?)$/g, '$2') : '';
	police = (proprietes.indexOf('police="') != -1 ) ? proprietes.replace(/^([\s\S]*?)police="([\s\S]*?)"([\s\S]*?)$/g, '$2') : '';
	couleur = (proprietes.indexOf('couleur="') != -1 ) ? proprietes.replace(/^([\s\S]*?)couleur="([\s\S]*?)"([\s\S]*?)$/g, '$2') : '';
	taille = (proprietes.indexOf('taille="') != -1 ) ? proprietes.replace(/^([\s\S]*?)taille="([\s\S]*?)"([\s\S]*?)$/g, '$2') : '';

	if (position) add+='text-align:'+trad(position)+';';
	if (police) add+='font-family:\''+police+'\', Verdana, serif;';
	if (couleur) add+='color:'+couleur+';';
	if (taille) add+='font-size:'+taille+';';

	return '<span style="'+add+'">'+text+'</span>';
}

function dobloc(s0, index, input) {
	s0 = s0.replace(/&lt;/g, '<');
	s0 = s0.replace(/&gt;/g, '>');
	text=s0.replace(/^<bloc([\s\S]*?)>([\s\S]*?)<\/bloc>/g, '$2');
	proprietes=s0.replace(/^<bloc([\s\S]*?)>([\s\S]*?)<\/bloc>/g, '$1');
	if (proprietes.indexOf('[curseur]') != -1) return index;
	position = "";
	hauteur = "";
	largeur = "";
	flottant = "";
	add="";
	if (proprietes.indexOf('position="') != -1 ) position=proprietes.replace(/^(.*)position="([\s\S]*?)"(.*)$/g, '$2');
	if (proprietes.indexOf('largeur="') != -1 ) largeur=proprietes.replace(/^(.*)largeur="([\s\S]*?)"(.*)$/g, '$2');
	if (proprietes.indexOf('hauteur="') != -1 ) hauteur=proprietes.replace(/^(.*)hauteur="([\s\S]*?)"(.*)$/g, '$2');
	if (proprietes.indexOf('flottant="') != -1 )   flottant=proprietes.replace(/^(.*)flottant="([\s\S]*?)"(.*)$/g, '$2');

	classe = new Array();
	if (position) classe.push(trad(position));
	if (flottant=="gauche") classe.push('fl');
	if (flottant=="droite") classe.push('fr');
	if (largeur || hauteur) add+='style="height:"'+hauteur+';width:'+largeur+';"';

	return '<div '+add+' class="'+classe.join(' ')+'">'+text+'</div>';
}

var selects = new Object();
function depend(nameselect,nameselect2) {
	$('select[name="'+nameselect+'"]').change(function() {
	
		value = $(this).children("option:selected").attr('value');
		if (!value) value = $(this).children("option:first").attr('value');
		name = $(this).attr('name');
		
		clone = selects[name].clone();
		clone.children('option').attr('rel','no');
		clone.children('option[value^="'+value+'_"]').attr('rel','yes');
		clone.children('option[value="0_0"]').attr('rel','yes');
		clone.children('option[value="0_00"]').attr('rel','yes');
		clone.children('option[rel="no"]').remove();
		clone.children('option').attr('rel','');
		clone.val('0_0');
		$('select[name="'+name+'_ss"]').empty().html(clone.html());
		/*$('select[name="'+name+'_ss"]').children('option').css('display','none');
		$('select[name="'+name+'_ss"]').children('option[value^="'+value+'_"]').css('display','');
		$('select[name="'+name+'_ss"]').children('option[value="0_0"]').css('display','');
		$('select[name="'+name+'_ss"]').val('0_0');*/
	});
	
	if (nameselect2 == undefined) nameselect2 = nameselect+'_ss';
	selects[nameselect] = $('select[name="'+nameselect2+'"]').clone(true);
	$('select[name="'+nameselect+'"]').change();
}
