/* ***** BEGIN LICENSE BLOCK *****
 *
 * Copyright (c) 2004 Olivier Meunier and contributors. All rights
 * reserved.
 *
 * Adapté pour PunBB par Vincent Garnier
 *
 * This script is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This script is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ***** END LICENSE BLOCK ***** */

function toolBar(textarea,bt_img_path,smilies_img_path)
{
	this.addButton		= function() {};
	this.addSmiley		= function() {};
	this.addSpace		= function() {};
	this.draw			= function() {};
	this.moreSmilies	= function() {};
	this.btSizeMinus	= function() {};
	this.btSizePlus		= function() {};
	this.btSup			= function() {};
	this.btSub			= function() {};
	this.btStrong		= function() {};
	this.btEm			= function() {};
	this.btIns			= function() {};
	this.btDel			= function() {};
	this.btQ			= function() {};
	this.btBquote		= function() {};
	this.btCode			= function() {};
	this.btLeft			= function() {};
	this.btCenter		= function() {};
	this.btRight		= function() {};
	this.btJustify		= function() {};
	this.btColor		= function() {};
	this.btPre			= function() {};
	this.btPhp			= function() {};
	this.btYou			= function() {};
	this.btGvid			= function() {};
	this.btHr			= function() {};
	this.btLink			= function() {};
	this.btAcronym		= function() {};
	this.btEmail		= function() {};
	this.btNospam		= function() {};
	this.btImgLink		= function() {};
	this.btSmilies		= function() {};
	this.barSmilies		= function() {};

	if (!document.createElement) {
		return;
	}

	if ((typeof(document['selection']) == 'undefined') 
	&& (typeof(textarea['setSelectionRange']) == 'undefined')) {
		return;
	}

	var toolbar = document.createElement('div');
	toolbar.id = 'toolbar';

	var smilies = document.createElement('div');
	smilies.id = 'smilies';
	smilies.style.display = 'none';
	smilies.style.padding = '0.3em 0';

	function addButton(src, title, fn)
	{
		var i = document.createElement('img');
		i.src = src;
		i.title = title;
		i.onclick = function() { try { fn() } catch (e) { } return false };
		i.tabIndex = 400;
		toolbar.appendChild(i);
		addSpace(2);
	}

	function addSmiley(src, txt)
	{
		var i = document.createElement('img');
		var htxt = txt;
		htxt = htxt.replace(new RegExp(/&amp;/g),'&');
		htxt = htxt.replace(new RegExp(/&quot;/g),'"');
		htxt = htxt.replace(new RegExp(/&lt;/g),'<');
		htxt = htxt.replace(new RegExp(/&gt;/g),'>'); 
		i.src = src;
		i.title = txt;
		i.style.verticalAlign = 'middle';
		
		i.onclick = function() { try { encloseSelection(htxt, '') } catch (e) { } return false };
		i.tabIndex = 400;
		smilies.appendChild(i);
		addSpace(2);
	}

	function addSpace(w)
	{
		s = document.createElement('span');
		s.style.padding='0 '+w+'px 0 0';
		s.appendChild(document.createTextNode(' '));
		toolbar.appendChild(s);
	}

	function encloseSelection(prefix, suffix, fn)
	{
		textarea.focus();
		var start, end, sel, scrollPos, subst;

		if (typeof(document['selection']) != 'undefined') {
			sel = document.selection.createRange().text;
		} else if (typeof(textarea['setSelectionRange']) != 'undefined') {
			start = textarea.selectionStart;
			end = textarea.selectionEnd;
			scrollPos = textarea.scrollTop;
			sel = textarea.value.substring(start, end);
		}

		if (sel.match(/ $/)) { // exclude ending space char, if any
			sel = sel.substring(0, sel.length - 1);
			suffix = suffix + " ";
		}

		if (typeof(fn) == 'function') {
			var res = (sel) ? fn(sel) : fn('');
		} else {
			var res = (sel) ? sel : '';
		}

		subst = prefix + res + suffix;

		if (typeof(document['selection']) != 'undefined') {
			var range = document.selection.createRange().text = subst;
			textarea.caretPos -= suffix.length;
		} else if (typeof(textarea['setSelectionRange']) != 'undefined') {
			textarea.value = textarea.value.substring(0, start) + subst +
			textarea.value.substring(end);
			if (sel) {
				textarea.setSelectionRange(start + subst.length, start + subst.length);
			} else {
				textarea.setSelectionRange(start + prefix.length, start + prefix.length);
			}
			textarea.scrollTop = scrollPos;
		}
	}

	function draw()
	{
		textarea.parentNode.insertBefore(smilies, textarea);
		textarea.parentNode.insertBefore(toolbar, textarea);
	}

	function moreSmilies(txt)
	{
		l = document.createElement('strong');
		l.style.padding='1em';
		l.style.cursor='pointer';
		l.onclick = function() { popup_smilies(); };
		l.appendChild(document.createTextNode(txt));
		smilies.appendChild(l);
	}

	// ---
	function singleTag(tag)
	{
		var stag = '['+tag+']';
		var etag = '[/'+tag+']';
		encloseSelection(stag,etag);
	}

	function btSizeMinus(label)
	{
		addButton(bt_img_path+'bt_size_minus.png',label,
		function() { singleTag('small'); });
	}

	function btSizePlus(label)
	{
		addButton(bt_img_path+'bt_size_plus.png',label,
		function() { singleTag('large'); });
	}

	function btSup(label)
	{
		addButton(bt_img_path+'bt_sup.png',label,
		function() { singleTag('sup'); });
	}

	function btSub(label)
	{
		addButton(bt_img_path+'bt_sub.png',label,
		function() { singleTag('sub'); });
	}

	function btStrong(label)
	{
		addButton(bt_img_path+'bt_strong.png',label,
		function() { singleTag('b'); });
	}

	function btEm(label)
	{
		addButton(bt_img_path+'bt_em.png',label,
		function() { singleTag('i'); });
	}

	function btIns(label)
	{
		addButton(bt_img_path+'bt_ins.png',label,
		function() { singleTag('u'); });
	}

	function btDel(label)
	{
		addButton(bt_img_path+'bt_del.png',label,
		function() { singleTag('s'); });
	}

	function btQ(label)
	{
		addButton(bt_img_path+'bt_quote.png',label,
		function() { singleTag('q'); });
	}

	function btBquote(label)
	{
		addButton(bt_img_path+'bt_bquote.png',label,
		function() { singleTag('quote'); });
	}

	function btCode(label)
	{
		addButton(bt_img_path+'bt_code.png',label,
		function() { singleTag('c'); });
	}

	function btLeft(label)
	{
		addButton(bt_img_path+'bt_align_left.png',label,
		function() { singleTag('left'); });
	}

	function btCenter(label)
	{
		addButton(bt_img_path+'bt_align_center.png',label,
		function() { singleTag('center'); });
	}

	function btRight(label)
	{
		addButton(bt_img_path+'bt_align_right.png',label,
		function() { singleTag('right'); });
	}

	function btJustify(label)
	{
		addButton(bt_img_path+'bt_align_justify.png',label,
		function() { singleTag('justify'); });
	}

	function btPre(label)
	{
		addButton(bt_img_path+'bt_pre.png',label,
		function() { singleTag('code'); });
	}

	function btPhp(label)
	{
		addButton(bt_img_path+'bt_php.png',label,
		function() { 
			var stag = '[code=php]';
			var etag = '[/code]';
			encloseSelection(stag,etag);
		});
	}

	function btYou(label)
	{
		addButton(bt_img_path+'bt_you.png',label,
		function() { 
			var stag = '[youtube]';
			var etag = '[/youtube]';
			encloseSelection(stag,etag);
		});
	}

	function btGvid(label)
	{
		addButton(bt_img_path+'bt_gvid.png',label,
		function() { 
			var stag = '[googlevid]';
			var etag = '[/googlevid]';
			encloseSelection(stag,etag);
		});
	}

	function btColor(label)
	{
		addButton(bt_img_path+'bt_color.png',label,
		function() { popup_color_picker(); });
	}

	function btImgLink(label,msg_url,msg_align)
	{
		addButton(bt_img_path+'bt_img_link.png',label,
		function() {
			encloseSelection('','',
			function(str) {
				var href = window.prompt(msg_url,str);
				if (!href) { return str; }

				var align = window.prompt(msg_align,'');

				if (align) {
					return '[img align='+align+']'+href+'[/img]';
				} else {
					return '[img]'+href+'[/img]';
				}
			});
		});
	}

	function btHr(label)
	{
		addButton(bt_img_path+'bt_hr.png',label,
		function() {
			encloseSelection('[---]','');
		});
	}

	function btLink(label,msg_url,msg_label)
	{
		addButton(bt_img_path+'bt_link.png',label,
		function() {
			encloseSelection('','',
			function(str) {
				var href = window.prompt(msg_url,str);
				if (!href) { return str; }

				var label = window.prompt(msg_label,str);

				if (label) {
					return '[url="'+href+'"]'+label+'[/url]';
				} else {
					return '[url]'+href+'[/url]';
				}
			});
		});
	}

	function btAcronym(label,msg_title,msg_label)
	{
		addButton(bt_img_path+'bt_acronym.png',label,
		function() {
			encloseSelection('','',
			function(str) {
				var label = window.prompt(msg_label,str);
				if (!label) { return str; }

				var title = window.prompt(msg_title,'');

				if (title) {
					return '[acronym="'+title+'"]'+label+'[/acronym]';
				} else {
					return '[acronym]'+label+'[/acronym]';
				}
			});
		});
	}

	function btEmail(label,msg_addresse,msg_label)
	{
		addButton(bt_img_path+'bt_email.png',label,
		function() {
			encloseSelection('','',
			function(str) {
				var href = window.prompt(msg_addresse,'');
				if (!href) { return str; }

				var label = window.prompt(msg_label,str);

				if (label) {
					return '[email="'+href+'"]'+label+'[/email]';
				} else {
					return '[email]'+href+'[/email]';
				}
			});
		});
	}

	function btNospam(label,msg_addresse,msg_label)
	{
		addButton(bt_img_path+'bt_nospam.png',label,
		function() {
			encloseSelection('','',
			function(str) {
				var href = window.prompt(msg_addresse,'');
				if (!href) { return str; }

				var label = window.prompt(msg_label,str);

				if (label) {
					return '[nospam="'+href+'"]'+label+'[/nospam]';
				} else {
					return '[nospam]'+href+'[/nospam]';
				}
			});
		});
	}

	function btSmilies(label)
	{
		addButton(bt_img_path+'bt_smilies.png',label,
		function()
		{
			element = document.getElementById('smilies');

			if (element.style.display == 'block' )
			{
				textarea.focus();
				element.style.display = 'none';
			}
			else {
				textarea.focus();
				element.style.display = 'block';
			}
		});
	}

	function barSmilies(smiliesTxt,smiliesImg)
	{
		for (var i=0; i<smiliesTxt.length; i++) {
			addSmiley(smilies_img_path+smiliesImg[i],smiliesTxt[i]);
		}
	}

	// methods
	this.addButton		= addButton;
	this.addSmiley		= addSmiley;
	this.addSpace		= addSpace;
	this.draw			= draw;
	this.moreSmilies	= moreSmilies;
	this.btSizeMinus	= btSizeMinus;
	this.btSizePlus		= btSizePlus;
	this.btSup			= btSup;
	this.btSub			= btSub;
	this.btStrong		= btStrong;
	this.btEm			= btEm;
	this.btIns			= btIns;
	this.btDel			= btDel;
	this.btQ			= btQ;
	this.btBquote		= btBquote;
	this.btCode			= btCode;
	this.btLeft			= btLeft;
	this.btCenter		= btCenter;
	this.btRight		= btRight;
	this.btJustify		= btJustify;
	this.btColor		= btColor;
	this.btPre			= btPre;
	this.btPhp			= btPhp;
	this.btYou			= btYou;
	this.btGvid			= btGvid;
	this.btHr			= btHr;
	this.btLink			= btLink;
	this.btAcronym		= btAcronym;
	this.btEmail		= btEmail;
	this.btNospam		= btNospam;
	this.btImgLink		= btImgLink;
	this.btSmilies		= btSmilies;
	this.barSmilies		= barSmilies;
}
