/**
 * @author Jonny
 */


function simple_text_Editor(id, place_Id, defaultText, textcolor, bgcolor, width, height){
	
	this.id = id;
	this.place_Id = place_Id;
	this.defaultText = defaultText;
	this.bgcolor = bgcolor;
	this.heigth = height;
	if(this.heigth == null){
		this.heigth = "100"
	}
	this.width = width;
	if(this.width == null){
		this.width = "250";
	}
	if(this.bgcolor == null){
		this.bgcolor = "#ffffff";
	}
	if (textcolor == null) {
		this.textcolor = "#000000";
	}else{
		this.textcolor = textcolor;
	}
	
	//gets the html
	this.getHTML = function(){
		frame = document.getElementById(this.textarea.id).contentWindow || document.getElementById(this.textarea.id).contentDocument;
		text = frame.document.body.innerHTML;
		text = text.replace(/&amp;/gi, "%26");
		text = text.replace(/&/gi, "%26");
		return text;
	}
	
	this.delete_  = function(){
		document.getElementById(this.place_Id).innerHTML = "";
	}

	//creates the buttons
	this.create = function(){
		this.div = document.createElement("div");
		this.div.name = this.div.id = this.id + "div";
		
		document.getElementById(this.place_Id).appendChild(this.div)
		
		this.def();
		
		//make a bold button
		this.bold = document.createElement("input");
		this.bold.type = "button";
		this.bold.name = this.bold.id = this.id + "bold";
		this.bold.value = "B";
		this.bold.onclick = this.boldclick;
	
		document.getElementById(this.div.id).appendChild(this.bold);
		document.getElementById(this.bold.id).value = "B";
		
		//makes an italic button
		this.italic = document.createElement("input");
		this.italic.type = "button";
		this.italic.name = this.italic.id = this.id + "italic";
		this.italic.value = "i";
		this.italic.onclick = this.italicclick;
	
		document.getElementById(this.div.id).appendChild(this.italic);
		document.getElementById(this.italic.id).value = "i";
		
		//makes an italic button
		this.underline = document.createElement("input");
		this.underline.type = "button";
		this.underline.name = this.underline.id = this.id + "underline";
		this.underline.value = "u";
		this.underline.onclick = this.underlineclick;
	
		document.getElementById(this.div.id).appendChild(this.underline);
		document.getElementById(this.underline.id).value = "u";
		
		//creates a span
		this.span3 = document.createElement("span");
		this.span3.name = this.span3.id = this.id + "span3";
		
		document.getElementById(this.div.id).appendChild(this.span3);
		document.getElementById(this.span3.id).value = "&nbsp;| ";
		
		//makes an link button
		this.link = document.createElement("input");
		this.link.type = "button";
		this.link.name = this.link.id = this.id + "link";
		this.link.onclick = this.linkclick;
	
		document.getElementById(this.div.id).appendChild(this.link);
		document.getElementById(this.link.id).value = "Link";
		
		//makes an email button
		this.email = document.createElement("input");
		this.email.type="button";
		this.email.name = this.email.id = this.id + "email";
		this.email.onclick = this.emailclick;
	
		document.getElementById(this.div.id).appendChild(this.email);
		document.getElementById(this.email.id).value = "email";
		
	}
	//creates the iframe
	this.def = function()
	{
		this.textarea = document.createElement("iframe");
		this.textarea.name = this.textarea.id = this.id + "text";
		this.textarea.width = this.width;
		this.textarea.height = this.heigth;

		if (this.textarea.addEventListener){
			this.textarea.addEventListener("load",function(e){this.contentWindow.document.designMode = "on";}, false);
		} else if (this.textarea.attachEvent){
			this.textarea.attachEvent("load", function(e){this.contentWindow.document.designMode = "on";});
		}
		
		document.getElementById(this.place_Id).appendChild(this.textarea);
		
		if (window.frames[this.id + "text"].document) {
			window.frames[this.id + "text"].document.designMode = "on";
			window.frames[this.id + "text"].document.open();
			window.frames[this.id + "text"].document.write('<head><style type="text/css">body{ font-family:Tahoma; font-size:13px; color: ' + this.textcolor +'}</style> </head>');
			window.frames[this.id + "text"].document.close();
			window.frames[this.id + "text"].document.body.bgColor = this.bgcolor;
			window.frames[this.id + "text"].document.body.innerHTML = this.defaultText;
		}
		window.frames[this.id + "text"].focus();



	}
	
	//event listeners
	
	//make font bolb
	this.boldclick = function (e){
		id = this.id.replace("bold", "");
		window.frames[id + "text"].document.execCommand("bold","","");
		window.frames[id + "text"].focus();
	}
	
	this.underlineclick = function (e){
		id = this.id.replace("underline", "");
		window.frames[id + "text"].document.execCommand("underline","","");
		window.frames[id + "text"].focus();
	}
	
	this.italicclick = function (e){
		id = this.id.replace("italic", "");
		window.frames[id + "text"].document.execCommand("italic","","");
		window.frames[id + "text"].focus();
	}
	
	this.linkclick = function (e){
		id = this.id.replace("link", "");
		if((url = prompt("http://")) != null){
			The_Link = "http://" + url;
			The_Link = "javascript:void window.open('"+The_Link+"');";
			window.frames[id + "text"].document.execCommand("CreateLink","", The_Link);
		}
		window.frames[id + "text"].focus();
	}
	this.emailclick = function (e){
		id = this.id.replace("email", "");
		if((url = prompt("mailto:")) != null){
			The_Link = "mailto:" + url;
			window.frames[id + "text"].document.execCommand("CreateLink","", The_Link);
		}
		window.frames[id + "text"].focus();
	}
}




