/*	
	OBS: Obligatoriu clasa la fiecare textarea pt wysiwyg-> class="textarea"
*/

if (!window.attachEvent) {
	Object.prototype.attachEvent = function(event, handler)	{
		this.addEventListener(event.substring(2), handler, false);					
	}
}


function textarea(textarea)
{
	
		
	
	function format_button(html, action)
	{
		var button = document.createElement('button');
		button.setAttribute("type", "button");
		button.setAttribute("class", "buton_editor");
		button.setAttribute("title", "format text");
		button.innerHTML = html;		
		button.onclick = function()
		{
			richtextarea.contentWindow.document.execCommand(action, false, null);
		}
		return button;				
	}
	

	
    function url_button(html, type) 
    { 
    	var button = document.createElement('button');
		button.setAttribute("type", "button");
		button.setAttribute("class", "buton_editor");
		button.setAttribute("title", "url: link sau imagini");
		button.innerHTML = html;
		button.onclick = function()
		{
            if (!document.all) 
            {
                var url = prompt("Introduceti URL:",""); 
                if(!url.match("(^(http|https|ftp|ftps)://)")) 
                { 
                    url="http://"+url; 
                }
                
                if (type == "url")
                {
                    richtextarea.contentWindow.document.execCommand("inserthtml", false, '<a href="' + url + '">' + url + '</a>'); 
                }
                else if (type == "image")
                {
                    richtextarea.contentWindow.document.execCommand("inserthtml", false, '<img src="' + url + '" />'); 
                }
            } 
            else 
            { 
                if (type== "url")
                {
                    richtextarea.contentWindow.document.execCommand("CreateLink",true); 
                }
                else if (type == "image")
                {
                    richtextarea.contentWindow.document.execCommand("InsertImage",true);
                }
            }
        } 
        return button;
    } 

	
	function set_textbox()
	{
		textarea.value = richtextarea.contentWindow.document.body.innerHTML;
	}
	
	var richtextarea = document.createElement('iframe');
	richtextarea.setAttribute("width", "550");
	richtextarea.setAttribute("height", "200");
	richtextarea.setAttribute("class", "editor");
	
	/*mai jos se creeaza div pentru butoanele cu optiuni*/
	var bar = document.createElement('div');
	bar.setAttribute('class','bara_optiuni');
	
	textarea.parentNode.appendChild(bar);
	bar.appendChild(format_button('<b>B</b>','bold'));
	bar.appendChild(format_button('<i>I</i>','italic'));
	bar.appendChild(format_button('<u>U</u>','underline'));	
	bar.appendChild(url_button('URL', 'url'));	
	bar.appendChild(url_button('IMG', 'image'));	
	textarea.parentNode.appendChild(richtextarea);
	textarea.style.display = "none";
	
	
	richtextarea.contentWindow.document.designMode = "on";
	richtextarea.contentWindow.onload = function()	
	{
		richtextarea.contentWindow.document.designMode = "on";
		richtextarea.contentWindow.document.body.innerHTML = textarea.value;
	}
		
	textarea.form.attachEvent('onsubmit', set_textbox);
}
			
function loadTextboxes()
{
	var textareas = document.getElementsByTagName('textarea');
		
	for(var i=0; i < textareas.length; i++)
	{
		if (textareas.item(i).className == "textarea")
		{
			textarea(textareas.item(i));
			
		}
	}				
}

window.attachEvent('onload',loadTextboxes);
