//Wrote by Alexander Burzev ( Александр Бурцев)
//Remade by Roman Bunyak  

// massive of object's elements
var textAreaSelectionObjects = [];
// get object's elements
function getTextAreaSelection(id) { // get object synhe
		if (typeof(textAreaSelectionObjects[id]) == "undefined") {
			textAreaSelectionObjects[id] = new textAreaSelectionHelper(id);
		}
		return textAreaSelectionObjects[id];
	}
// designer uses id 
function textAreaSelectionHelper(id) {
		var obj = document.getElementById(id);
		this.target = obj;
//create property carretHandler
   this.target.carretHandler = this;
//Add hadler of events
   this.target.onchange = _textareaSaver;
		this.target.onclick = _textareaSaver;
		this.target.onkeyup = _textareaSaver;
		this.target.onfocus = _textareaSaver;
		if(!document.selection) this.target.onSelect = _textareaSaver;
//Property for save cursor's position    
   this.start=-1;
		this.end=-1;
		this.scroll=-1;
		this.iesel=null;
	}
//write methods into prototype
textAreaSelectionHelper.prototype = {
//get accentuation of text
    getSelectedText : function() {
			return this.iesel? this.iesel.text: (this.start>=0&&this.end>this.start)? this.target.value.substring(this.start,this.end): "";
		},
// Set text accentuation before - text
// and after text, if it need - secondtag
    setSelectedText : function(text, secondtag) {
			if (this.iesel) {
				if (typeof(secondtag) == "string") {
					var l = this.iesel.text.length;
					this.iesel.text = text + this.iesel.text + secondtag;
					this.iesel.moveEnd("character", -secondtag.length);
					this.iesel.moveStart("character", -l);   
				} else {
					this.iesel.text = text;
				}
				this.iesel.select();
			} else if (this.start >= 0 && this.end >= this.start) {
				var left = this.target.value.substring(0, this.start);
				var right = this.target.value.substr(this.end);
				var scont = this.target.value.substring(this.start, this.end);
				if (typeof(secondtag) == "string") {
					this.target.value = left + text + scont + secondtag + right;
					this.end = this.target.selectionEnd=this.start+text.length+scont.length;
					this.start = this.target.selectionStart = this.start + text.length;    
				} else {
					this.target.value = left + text + right;
					this.end = this.target.selectionEnd = this.start + text.length;
					this.start = this.target.selectionStart = this.start + text.length;
				}
				this.target.scrollTop = this.scroll;
				this.target.focus();
			} else {
				this.target.value += text + ((typeof(secondtag) == "string") ? secondtag: "");
				if (this.scroll >= 0) this.target.scrollTop = this.scroll;
			}
		},
		getText : function() {
			return this.target.value;
		},
		setText : function(text) {
			this.target.value = text;
		}
	}
	//Save info about accentuation and position of scroll
	function _textareaSaver() {
		if(document.selection) {
			this.carretHandler.iesel = document.selection.createRange().duplicate();
		} else if(typeof(this.selectionStart) != "undefined") {
			this.carretHandler.start = this.selectionStart;
			this.carretHandler.end = this.selectionEnd;
			this.carretHandler.scroll = this.scrollTop;
		} else {
			this.carretHandler.start = this.carretHandler.end = -1;
		}
	}
//client's functions
function setBold(id) {
		getTextAreaSelection(id).setSelectedText('<b>', '</b>');
	}
	function setItalic(id) {
		getTextAreaSelection(id).setSelectedText('<i>', '</i>');
	}
	function setUnderline(id) {
		getTextAreaSelection(id).setSelectedText('<u>', '</u>');
	}
	function setAlignRight(id) {
		getTextAreaSelection(id).setSelectedText('<div align="right">', '</div>');
	}
	function setAlignLeft(id) {
		getTextAreaSelection(id).setSelectedText('<div align="left">', '</div>');
	}
	function setAlignCenter(id) {
		getTextAreaSelection(id).setSelectedText('<div align="center">', '</div>');
	}
	function setAlignJustify(id) {
		getTextAreaSelection(id).setSelectedText('<div align="justify">', '</div>');
	}

function disp_url(id)
{
    var name=prompt("Введіть URL","http://");
    if (name!=null && name!="" && name != "http://")
    {
        this.setLink(id, name );
    }
}

function setLink(id, url)
{
    var a = '<a href="'+url+'" target="_blank" nofollow>';
    getTextAreaSelection(id).setSelectedText( a, '</a>');
}

function disp_img(id, count)
{
    if ( count > 0 )
    {
        var url=prompt("Enter name image file","");
        //var noimg = url.substr( url.length-4, url.length );
        if (url!=null && url!="" )//&& url != "http://" && noimg == ".jpg")
        {
            //var num = url.substr( 0, url.length-4 );
            //var d = num.substr( num.length-1, num.length );
            var d = 'a';
            var description = prompt("Enter description of image", "");
            if (description != null && description != "" )//&& d!= "a")
            {
                var align = prompt("Enter align of image: right, left");
                if ( align != null && align != "")
                this.setImg(id, url, description, align, d );
            }
            else
            {
                var align = "";
                this.setImg(id, url, description, align, d );
            }
        }
        else
        {
            alert("Error: incorrect file name of file type!");
        }
    }
    else
        alert("Your Images Gallery is empty!");  
}

function setImg(id, url, description, align, d )
{
    if ( d == 'a' )
    {var a = '<div align="center"><img src="'+url+'" alt="'+description+'" border="0"></img></div>';}
    else
    {var a = '<table cellpadding=0 cellspacing=0 border=0 align="'+align+'" style="padding:5px;"><tr><td><img src="'+url+'" alt="'+description+'" border="0"></img></td></tr></table>';}
    
    getTextAreaSelection(id).setSelectedText( a );
}

function disp_video(id)
{
    var url=prompt("Enter url of Google or YouTube Video","");
    if (url!=null && url!="" )
    {
        this.setTube(id, url );
    }
}

function setTube( id, url)
{
    var video = '<div style="padding:5px" align="center"><embed src="'+url+'" type="application/x-shockwave-flash" width="425" height="344"></embed></div>';
    getTextAreaSelection(id).setSelectedText( video );
}

