/************************************************************************
  			Textarea.js - Copyright Jonathan Adami
**************************************************************************/

/**
 * This class is able to manage BBCodes
 */
TextArea = function (name) {
    this._init(name);
}

/**
 * TextArea's constructor
 */
TextArea.prototype._init = function (name) {
	this.name = name;
	this.textarea = document.getElementById(this.name);
}

/**
 * The effective tag operation
 */
TextArea.prototype.tag = function (bStart,bEnd) {
	this.textarea.focus();
	if (typeof(document.selection) != 'undefined') {
		return this.tagIE(bStart,bEnd);
	} else {
		return this.tagGecko(bStart,bEnd);
	}
}

TextArea.prototype.tagIE = function (bStart,bEnd) {
	var range = document.selection.createRange();
	var insText = range.text;
	if (bStart == '<p>')
		if (insText == '') 
			insText = '&nbsp;';
	range.text = bStart + insText + bEnd;
	range = document.selection.createRange();
	if (insText.length == 0) {
		range.move('character', -(bEnd.length));
	} else {
		range.moveStart('character', bStart.length + insText.length + bEnd.length);
	}
	range.select();
}

TextArea.prototype.tagGecko = function (bStart,bEnd) {
	var start = this.textarea.selectionStart;
	var end = this.textarea.selectionEnd;
	var insText = this.textarea.value.substring(start, end);
	//alert ('toto');
	if (bStart == '<p>')
		if (insText == '') 
			insText = '&nbsp;';
	this.textarea.value = this.textarea.value.substr(0, start) + bStart + insText + bEnd + this.textarea.value.substr(end);
	var pos = insText.length ? start + bStart.length + insText.length + bEnd.length : start + bStart.length;
	this.textarea.selectionStart = pos;
	this.textarea.selectionEnd = pos;
}

/**
 * Automatically tag start and end by using a simple open/close tag
 */
TextArea.prototype.simpleTag = function (tag) {
	//alert ('toto');
	this.tag('<'+tag+'>','</'+tag+'>');
}

/**
 * Automatically tag start and end for the text
 */
TextArea.prototype.simpleTagAlign = function (tag) {
	this.tag('<div align="'+tag+'">','</div>');
}
TextArea.prototype.simpleTagBold = function () {
	this.tag('<span class="bold">','</span>');
}

/**
 * Automatically tag start and end by using an open/close tag with a param asked to user
 * by a prompt
 */
TextArea.prototype.promptTagSize = function (message) {
	rep = prompt(message, "");
	if (rep && rep.length)	this.tag('<font size="'+rep+'">','</font>');
}

TextArea.prototype.promptTagUrl = function (message) {
	rep = prompt(message, "");
	if (rep && rep.length)	this.tag('<a href="'+rep+'">','</a>');
}

TextArea.prototype.promptTagMail = function (message) {
	rep = prompt(message, "");
	if (rep && rep.length)	this.tag('<a href="mailto:'+rep+'">','</a>');
}

TextArea.prototype.promptTagImg = function (message) {
	rep = prompt(message, "");
	if (rep && rep.length)	this.tag('<img src="'+rep+'" alt=""/>', '');
}

TextArea.prototype.promptTagVideo = function (message) {
	this.tag('<embed type="application/x-mplayer2"  pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" name="mediaplayer1" showstatusbar="1" autostart="true" balance="0"  currentPosition="0.8491248" playCount="1" autoStart="-1" currentMarker="0" invokeURLs="-1" volume="100" mute="0" loop="0" src="'+message+'"></embed>', '');
}

TextArea.prototype.promptTagImg2 = function (rep) {
	this.tag('<img src="'+rep+'" alt=""/>', '');
}

TextArea.prototype.promptTagColor = function (message) {
	rep = prompt(message, "");
	if (rep && rep.length)	this.tag('<font color="'+rep+'">', '</font>');
}

TextArea.prototype.preview = function () {
	//alert (this.textarea.value);
	w=open("",'Prévisualisation','width=600,height=400,toolbar=no,scrollbars=no,resizable=yes,top=300,left=300');	
	w.document.write("<html><head><TITLE>Prévisualisation</TITLE>");
	w.document.write("<style type=\"text/css\">.style28 {color: #560D93;font-weight: bold;font-size: 24px;}</style></head>");
	w.document.write("<BODY><font style=\"color: #560D93;font-weight: bold;font-size: 20px;\"><center> Voici la prévisualisation du texte mis en forme : </center></font><BR><BR>");
	//w.document.write (this.tag('',''));
	w.document.write(this.textarea.value);
	w.document.write("</BODY></html>");
	w.document.close();
}

TextArea.prototype.recup = function (rep)
{
	alert (rep);
	this.tag('<img src="'+rep+'" alt=""/>', '');
}

function popup (text)
{
	if (document.body)
	{
		var larg = (document.body.clientWidth);
		var haut = (document.body.clientHeight);
	}
	else
	{
		var larg = (window.innerWidth);
		var haut = (window.innerHeight);
	}
	
	window.open(text,'wclose','width=520,height=300,toolbar=yes,status=no,resizable=yes,left='+(larg/2-250)+',top='+(haut/2-150)+',scrollbars=1');
	
}
