/*************************************************************************************
Class WSPlainEditor:
A standalone html-editor (does not depend on other editors like tinyMCE etc).
*************************************************************************************/
function WSPlainEditor(ed) {
	this.editor = ed;
	this.id = ed.id;
}	
//-----------------------------------------------------------------------
WSPlainEditor.prototype.init = function(content) {
	if (! this.tryEnableDesignMode()) {
		alert("Failed to enable designMode");
		return false;
	}
	var doc = this.getDoc(); //this.editor.contentWindow.document;
	if (doc != null) {
		if (content == null) {
			var content = "<html><head></head><body></body></html>"
		}
		try {
			doc.open();
			doc.writeln(content);
			doc.close();
			return true;
		}
		catch(error) {
			alert(error);
			console.log(error);
		}
	}
	return false;
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.tryEnableDesignMode = function() {
	try {
		var doc = this.getDoc(); //this.editor.contentWindow.document
		if (doc.contentEditable != null) {
			doc.contentEditable = "true";
			return true;
		}
		else if (doc.designMode != null) {
			doc.designMode = "on";
			return true;
		}
	}
	catch (error) {
		alert(error);
		console.log(error);
	}
	return false;
}
//-----------------------------------------------------------------------		
/*
WSPlainEditor.prototype.disableDesignMode = function(iframe, submit) {
	var content = iframe.contentWindow.document.getElementsByTagName("body")[0].innerHTML;
	if(submit==true)
		var textarea = $('<input type="hidden" />');
	else
		var textarea = $('<textarea cols="40" rows="10"></textarea>');
	textarea.val(content);
	t = textarea.get(0);
	if(iframe.className)
		t.className = iframe.className;
	if(iframe.id)
		t.id = iframe.id;
	if(iframe.title)
		t.name = iframe.title;
	$(iframe).before(textarea);
	if(submit!=true)
		$(iframe).remove();
	return textarea;
}
*/
//-----------------------------------------------------------------------
WSPlainEditor.prototype.tryDisableDesignMode = function() {
	var strContent = this.getContent();
	alert("strContent\n" + strContent);
	try {
		var doc = this.getDoc(); //this.editor.contentWindow.document
		if (doc.contentEditable != null) {
			doc.contentEditable = "false";
			return true;
		}
		else if (doc.designMode != null) {
			doc.designMode = "off";
			return true;
		}
	}
	catch (error) {
		alert(error);
		console.log(error);
	}
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.setSelectionRange = function(input, selectionStart, selectionEnd) {
	if (input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
	else if (input.setSelectionRange) {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
	}
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.setCaretTo = function(obj, pos) { 
    if (obj.createTextRange) { 
        /* Create a TextRange, set the internal pointer to
           a specified position and show the cursor at this
           position
        */ 
        var range = obj.createTextRange(); 
        range.move("character", pos); 
        range.select(); 
    } else if (obj.selectionStart) { 
        /* Gecko is a little bit shorter on that. Simply
           focus the element and set the selection to a
           specified position
        */ 
        obj.focus(); 
        obj.setSelectionRange(pos, pos); 
    } 
} 
//-----------------------------------------------------------------------
//rteName = “Name of your editor IFRAME”;
WSPlainEditor.prototype.rteInsertHTML = function(html) {
	if (document.all) {
		var oRng = this.getDoc().selection.createRange();
		oRng.pasteHTML(html);
		oRng.collapse(false);
		oRng.select();
	}
	else {
		this.getDoc().execCommand("insertHTM L", false, html);
	}
} 
//-----------------------------------------------------------------------
WSPlainEditor.prototype.getInstance = function() {
	return this.editor;
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.getWin = function() {
	return this.editor.contentWindow;
	//return this.editor.contentWindow.window;
}
//-----------------------------------------------------------------------		
WSPlainEditor.prototype.getDoc = function() {
	return this.getWin().document;
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.getBody = function() {
	return this.getDoc().body;
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.clearContent = function() {
	this.getBody().innerHTML = "";
	this.getBody().innerText = "";
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.setFocus = function() {
	
	//this.getBody().focus();
	//this.getWin().focus();
	//var body = this.getBody();
	//this.setCaretTo(body,100);
	//this.setSelectionRange(body,210,220);
	//this.rteInsertHTML("<B>hello world</B>");
	//var range = document.getElementById('iframe_id').contentWindow.getSelection().getRangeAt(0);
	//var range = this.getWin().getSelection().getRangeAt(0);
	//alert("range: startOffset : " + range.startOffset + ", endOffset = " + range.endOffset);
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.getHeader = function() {
	//alert("this.editor.contentWindow.document.body = " + this.editor.contentWindow.document.body);
	var elm = this.getDoc().getElementsByTagName('head');
	//alert("elm.item(0) = " + elm.item(0) + ", collection=" + elm + " (size: "+elm.length+")");
	return elm.item(0);
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.getContent = function() {
	return this.getBody().innerHTML;
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.setContent = function(sContent) {
	this.getBody().innerHTML = sContent;
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.addCSS = function(css_file) {
	var elm = this.getDoc().createElement('link');
	elm.type = "text/css";
	elm.rel = "stylesheet";
	elm.href = css_file;
	this.getHeader().appendChild(elm);
}
//-----------------------------------------------------------------------
WSPlainEditor.prototype.addJScript = function(js_file) {
	var elm = this.getDoc().createElement("s" + "cript");
	elm.type = 'text/javascript';
	//elm.onload=scriptLoaded;
	elm.src = js_file;
	this.getHeader().appendChild(elm);

	/* //another option - not tested yet
	var elm = this.getDoc().createElement("span");
	elm.innerHTML = '<s' + 'cript type="text/javascript" src="'+ js_file + '">' + '</' + 'script>';
	this.getHeader().appendChild(elm);
	*/
}
//-----------------------------------------------------------------------
