jQuery.fn.extend({
	textarea: function() {
		function addBalise(textarea, bbdebut, bbfin) {
			// Enregistre la position du curseur (Car le snavigateur deplace quand ajout de donner)
			var sl = textarea.attr("scrollLeft");
			var st = textarea.attr("scrollTop");
			
			var input = textarea;
			input.focus();
			
			/* pour IE */
			if(typeof document.selection != 'undefined') {
				var range = document.selection.createRange();
				var insText = range.text;
				range.text = bbdebut + insText + bbfin;
				range = document.selection.createRange();
				if (insText.length == 0) {
					range.move('character', -bbfin.length);
				} else {
					range.moveStart('character', bbdebut.length + insText.length + bbfin.length);
				}
				range.select();
			} else { /* pour les navigateurs plus récents que IE comme Firefox... */
				var start = textarea.attr("selectionStart");
				var end = textarea.attr("selectionEnd");
				var insText = textarea.val().substring(start, end);
				textarea.val(textarea.val().substr(0, start) + bbdebut + insText + bbfin + textarea.val().substr(end));
				var pos;
				if (insText.length == 0) {
					pos = start + bbdebut.length;
				} else {
					pos = start + bbdebut.length + insText.length + bbfin.length;
				}
				input.selectionStart = pos;
				input.selectionEnd = pos;
			}
			
			// Reposition le curseur a sa place
			textarea.attr("scrollLeft", sl);
			textarea.attr("scrollTop", st);
		}
		
		$(this).attr("rows", "5");
		
		// Redimensione la textarea suivnat le contenu
		$(this).keypress(function() {
			if($(this).val().split("\n").length > 3 && $(this).val().split("\n").length < 20) {
				$(this).attr("rows", $(this).val().split("\n").length+1);
			}
		});
		
		$(this).wrap("<div class=\"richTextArea\"></div>");
		$(this).parent().prepend('<div class="richTextAreaZone"></div>');
		$(this).parent().prepend('<div class="richTextAreaSmiley"></div>');
		
		// Smile 1
		$(this).parent().find(".richTextAreaSmiley").append("<div class=\"richTextBt richTextBtSmile1\"><img src=\"./richtext/smile1.png\" title=\"Smile :)\" \/></div>");
		$(this).parent().find(".richTextAreaSmiley").find(".richTextBtSmile1").click(function() {
			addBalise($(this).parent().parent().find("textarea"), ':)', '');
		});
		
		// Smile 2
		$(this).parent().find(".richTextAreaSmiley").append("<div class=\"richTextBt richTextBtSmile2\"><img src=\"./richtext/smile2.png\" title=\"Smile xD\" \/></div>");
		$(this).parent().find(".richTextAreaSmiley").find(".richTextBtSmile2").click(function() {
			addBalise($(this).parent().parent().find("textarea"), 'xD', '');
		});
		
		// Smile 3
		$(this).parent().find(".richTextAreaSmiley").append("<div class=\"richTextBt richTextBtSmile3\"><img src=\"./richtext/smile3.png\" title=\"Smile :}\" \/></div>");
		$(this).parent().find(".richTextAreaSmiley").find(".richTextBtSmile3").click(function() {
			addBalise($(this).parent().parent().find("textarea"), ':}', '');
		});
		
		// Smile 4
		$(this).parent().find(".richTextAreaSmiley").append("<div class=\"richTextBt richTextBtSmile4\"><img src=\"./richtext/smile4.png\" title=\"Smile ;D\" \/></div>");
		$(this).parent().find(".richTextAreaSmiley").find(".richTextBtSmile4").click(function() {
			addBalise($(this).parent().parent().find("textarea"), ';D', '');
		});
		
		// Smile 5
		$(this).parent().find(".richTextAreaSmiley").append("<div class=\"richTextBt richTextBtSmile5\"><img src=\"./richtext/smile5.png\" title=\"Smile :(\" \/></div>");
		$(this).parent().find(".richTextAreaSmiley").find(".richTextBtSmile5").click(function() {
			addBalise($(this).parent().parent().find("textarea"), ':(', '');
		});
		
		// Smile 6
		$(this).parent().find(".richTextAreaSmiley").append("<div class=\"richTextBt richTextBtSmile6\"><img src=\"./richtext/smile6.png\" title=\"Smile :o\" \/></div>");
		$(this).parent().find(".richTextAreaSmiley").find(".richTextBtSmile6").click(function() {
			addBalise($(this).parent().parent().find("textarea"), ':o', '');
		});
		
		// Smile 7
		$(this).parent().find(".richTextAreaSmiley").append("<div class=\"richTextBt richTextBtSmile7\"><img src=\"./richtext/smile7.png\" title=\"Smile :p\" \/></div>");
		$(this).parent().find(".richTextAreaSmiley").find(".richTextBtSmile7").click(function() {
			addBalise($(this).parent().parent().find("textarea"), ':p', '');
		});
		
		// Gras
		$(this).parent().find(".richTextAreaZone").append("<div class=\"richTextBt richTextBtGras\"><img src=\"./richtext/gras.png\" title=\"Gras\" \/></div>");
		$(this).parent().find(".richTextAreaZone").find(".richTextBtGras").click(function() {
			addBalise($(this).parent().parent().find("textarea"), '[g]', '[/g]');
		});
		
		// Italique
		$(this).parent().find(".richTextAreaZone").append("<div class=\"richTextBt richTextBtItal\"><img src=\"./richtext/ital.png\" title=\"Italique\" \/></div>");
		$(this).parent().find(".richTextAreaZone").find(".richTextBtItal").click(function() {
			addBalise($(this).parent().parent().find("textarea"), '[i]', '[/i]');
		});
		
		// Souligner
		$(this).parent().find(".richTextAreaZone").append("<div class=\"richTextBt richTextBtSouli\"><img src=\"./richtext/souligner.png\" title=\"Souligner\" \/></div>");
		$(this).parent().find(".richTextAreaZone").find(".richTextBtSouli").click(function() {
			addBalise($(this).parent().parent().find("textarea"), '[s]', '[/s]');
		});
		
		// Barré
		$(this).parent().find(".richTextAreaZone").append("<div class=\"richTextBt richTextBtBarre\"><img src=\"./richtext/barre.png\" title=\"Barré\" \/></div>");
		$(this).parent().find(".richTextAreaZone").find(".richTextBtBarre").click(function() {
			addBalise($(this).parent().parent().find("textarea"), '[b]', '[/b]');
		});
		
		// Lien
		$(this).parent().find(".richTextAreaZone").append("<div class=\"richTextBt richTextBtLien\"><img src=\"./richtext/lien.png\" title=\"Ajouter un lien\" \/></div>");
		$(this).parent().find(".richTextAreaZone").find(".richTextBtLien").click(function() {
			$('.cmsWindow').remove();
			
			$(this).parent().parent().prepend('<div class="cmsWindow richTextWindowLien"></div>');
			
			$(this).parent().parent().find(".richTextWindowLien").append('Nom du lien : <input type="text" class="richTextLienSelectNom"><br /><br />');
			$(this).parent().parent().find(".richTextWindowLien").append('URL du lien : <input type="text" class="richTextLienSelectUrl">');
			
			$(this).parent().parent().find(".richTextWindowLien").append('<input class="richTextLienAnnuler" type="button" value="Annuler" />');
			$(this).parent().parent().find(".richTextWindowLien").find(".richTextLienAnnuler").click(function() {
				$(this).parent().remove();
			});
			
			$(this).parent().parent().find(".richTextWindowLien").append('<input class="richTextLienValider" type="button" value="Valider" />');
			$(this).parent().parent().find(".richTextWindowLien").find(".richTextLienValider").click(function() {
				addBalise($(this).parent().parent().find("textarea"), '[url='+$(this).parent().find('.richTextLienSelectUrl').val()+']'+$(this).parent().find('.richTextLienSelectNom').val()+'[/url]', '');
				$(this).parent().remove();
			});
		});
		
		
		// Image
		$(this).parent().find(".richTextAreaZone").append("<div class=\"richTextBt richTextBtImage\"><img src=\"./richtext/image.png\" title=\"Ajouter une image\" \/></div>");
		$(this).parent().find(".richTextAreaZone").find(".richTextBtImage").click(function() {
			// Ferme les fenetres ouverte
			$('.cmsWindow').remove();
			
			$(this).parent().parent().prepend('<div class="cmsWindow richTextWindowImage"></div>');
			
			$(this).parent().parent().find(".richTextWindowImage").append('Nom de l\'image : <input type="text" class="richTextImageNom"><br /><br />');
			$(this).parent().parent().find(".richTextWindowImage").append('Description : <input type="text" class="richTextImageDescription"><br /><br />');
			
			$(this).parent().parent().find(".richTextWindowImage").append('Position : <input type="radio" name="richTextImagePosition" id="richTextImagePosition" value="gauche" checked="checked" />Gauche');
			$(this).parent().parent().find(".richTextWindowImage").append('<input type="radio" name="richTextImagePosition" id="richTextImagePosition" value="centre" />Centre');
			$(this).parent().parent().find(".richTextWindowImage").append('<input type="radio" name="richTextImagePosition" id="richTextImagePosition" value="droite" />Droite<br /><br />');
			
			$(this).parent().parent().find(".richTextWindowImage").append('Dimmension : <input type="radio" name="richTextImageDimmension" id="richTextImageDimmension" value="normale" checked="normale" />Normale');
			$(this).parent().parent().find(".richTextWindowImage").append('<input type="radio" name="richTextImageDimmension" id="richTextImageDimmension" value="miniature" />Miniature<br />');
			
			$(this).parent().parent().find(".richTextWindowImage").append('<br />');
			
			$(this).parent().parent().find(".richTextWindowImage").append('<input class="richTextLienAnnuler" type="button" value="Annuler" />');
			$(this).parent().parent().find(".richTextWindowImage").find(".richTextLienAnnuler").click(function() {
				$(this).parent().remove();
			});
			
			$(this).parent().parent().find(".richTextWindowImage").append('<input class="richTextLienValider" type="button" value="Valider" />');
			$(this).parent().parent().find(".richTextWindowImage").find(".richTextLienValider").click(function() {
				addBalise($(this).parent().parent().find("textarea"), '[image:'+$(this).parent().find('.richTextImageNom').val()+'|'+$(this).parent().find('.richTextImageDescription').val()+']', '');
				$(this).parent().remove();
			});
		});
		
	}
});