/* ***** BEGIN LICENSE BLOCK *****
 * This file is part of DotClear.
 * Copyright (c) 2004 Olivier Meunier and contributors. All rights
 * reserved.
 *
 * DotClear 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.
 * 
 * DotClear 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DotClear; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * Contributor : Troopers (http://l.houdusse.free.fr/blog/)
 */

 
/* Crée une toolbar de smilies pour la zone de text textarea
 * avec les images de img_path */
function smToolBar(textarea,img_path)
{
	this.addSmiley		= function() {}
	this.addSpace		= function() {}
	this.draw			= function() {}
	this.insSmiley		= function() {}
	
  if (!document.createElement) {
		return;
	}
	
	if ((typeof(document["selection"]) == "undefined")
	&& (typeof(textarea["setSelectionRange"]) == "undefined")) {
		return;
	}
	
	var toolbar = document.createElement("div");
	toolbar.id = "smtoolbar";
		
	function addSmiley(src, title, txt) {
		var i = document.createElement('img');
		i.src = img_path + src;
		i.title = title;
		i.onclick = function() { try { insSmiley(txt) } catch (e) { } return false };
		i.tabIndex = 400;
		toolbar.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 draw(msg) 
  {
  	p = document.createElement('em');
		p.style.display='block';
		p.style.margin='-0.5em 0 0.5em 0';
		p.appendChild(document.createTextNode(msg));
    textarea.parentNode.insertBefore(document.createElement('br'), textarea);
    textarea.parentNode.insertBefore(p, textarea);
		textarea.parentNode.insertBefore(toolbar, textarea);
	}
	
	function insSmiley(smiley) 
	{
		smiley = ' ' + smiley + ' ';
    
    textarea.focus();
		var start, end, scrollPos
    
    if (typeof(document["selection"]) != "undefined") 
    {
    	document.selection.createRange().text = smiley;
			textarea.caretPos += smiley.length;
		}
    else if (typeof(textarea["setSelectionRange"]) != "undefined")
    {
    	start = textarea.selectionStart;
			end = textarea.selectionEnd;
			scrollPos = textarea.scrollTop;
			textarea.value = textarea.value.substring(0, start)+smiley+textarea.value.substring(end);
			textarea.setSelectionRange(start + smiley.length, start + smiley.length);
			textarea.scrollTop = scrollPos;
		}
	}
	
	// methods
	this.addSmiley		= addSmiley;
	this.addSpace		= addSpace;
	this.draw			= draw;
	this.insSmiley		= insSmiley;
}

