var QEditor = {
	timeout: [],
	init: function(event, id) {
		if (id == null) {
			$$(".wysiwyg").each(function(item) {
				QEditor.init(null, item);
			});
		} else {
			var item = $(id);
			var iframe = QEditor.createIFrame(item);
			QEditor.timeout[item.name] = setInterval(function() {
				if (QEditor.doc(iframe.id).body.innerHTML != "") {
					clearTimeout(QEditor.timeout[item.name]);
				} else {
					QEditor.doc(iframe.id).body.innerHTML = "loading";
					return;
				}
				QEditor.loadContents(item, iframe);
				QEditor.makeEditable(iframe);
				QEditor.syncContents(iframe, item);
				QEditor.hideElement(item);
				if (item.hasClassName("wysiwyg_simple") && $$("."+item.name+"_toolbar").length > 0) {
					QSimpleToolbar.init($$("."+item.name+"_toolbar")[0], $(item.name));
				}
				if (item.hasClassName("wysiwyg_advanced") && $$("."+item.name+"_toolbar").length > 1) {
					QAdvancedToolbar.init($$("."+item.name+"_toolbar")[1], $(item.name));
				}
				iframe.style.display = "block";
			}, 10);
		}
	},
	
	createIFrame: function(item) {
		var iframe = document.createElement("iframe");
		iframe.id = item.name;
		if (item.style.width != null) {
			iframe.style.width = item.style.width;
		}
		item.parentNode.insertBefore(iframe, item);
		iframe.style.display = "none";
		
		return iframe;
	},
	
	loadContents: function(item, iframe) {
		var str = item.value.replace(/&lt;/gi, "<").replace(/&gt;/gi, ">").replace(/&amp;/gi, "&").replace(/&quot;/gi, "\"");
		QEditor.doc(iframe.id).body.innerHTML = QEditor.filterHTML(str);
		try {
			if (typeof(item.getAttribute("style")) == "object") {
				var styles = item.getAttribute("style");
				QEditor.doc(iframe.id).body.setAttribute("style", styles.cssText + ";width:auto;");
			} else {
				QEditor.doc(iframe.id).body.setAttribute("style", item.getAttribute("style")+";width:auto;");
			}
			iframe.setAttribute("style", item.getAttribute("style"));
		} catch (e) {};

		try {
	        var styleTag = QEditor.doc(iframe.id).createElement('style');
	        styleTag.type = 'text/css';
	        styleTag.appendChild(QEditor.doc(iframe.id).createTextNode('a { color:#e20000; text-decoration:underline; }'));
			QEditor.doc(iframe.id).getElementsByTagName('head')[0].appendChild(styleTag);
		} catch (e) {}  
	},
	
	makeEditable: function(iframe, editable) {
		if (!document.all) {
			try {
				QEditor.doc(iframe.id).designMode = (editable==null||editable?"On":"Off");
			} catch (e) {
				setTimeout(function() {
					QEditor.makeEditable(iframe, editable);
				}, 250);
			}
		}
		QEditor.doc(iframe.id).body.contentEditable = (editable==null||editable);
	},
	
	hideElement: function(item) {
		item.style.position = "absolute";
		item.style.left = "-10000px";
	},
	
	syncContents: function(iframe, item) {
		window.setInterval(function() {
			if (QEditor.doc(iframe.id) != null && !iframe.sourceView) {
				item.value = QEditor.unfilterHTML(QEditor.doc(iframe.id).body.innerHTML);
			} else if (QEditor.doc(iframe.id) != null && iframe.sourceView) {
				QEditor.doc(iframe.id).body.innerHTML = QEditor.filterHTML(item.value);
			}
		}, 100); 
	},
	
	/// functions to be called from external scripts
	doc: function(name) {
		var oIframe = $(name);
		if (!oIframe) return null; 
   		if (oIframe.contentDocument) return oIframe.contentDocument;
   			else if (oIframe.contentWindow) return oIframe.contentWindow.document;
   			else if (oIframe.document) return oIframe.document;
   			else return null;
	}, 
	
	exec: function(name, instr, value) {
		QEditor.doc(name).execCommand(instr, false, value);
	},
		
	insertHTML: function(name, html) {
		QEditor.doc(name).execCommand("inserthtml", false, html);
	},
	
	surroundSelection: function(obj, element) {
		if (document.selection) {	/* for internet explorer */
			if (QEditor.doc(obj.id).selection) {
				var rng = QEditor.doc(obj.id).selection.createRange();
				if (rng) {
					var newContent = element.outerHTML.replace(/<\/([a-z0-9_-])+>/i, rng.text+'<\/'+element.nodeName+'>');
					rng.pasteHTML(newContent);
				}
			}
		} else {
			var range = document.getElementById(obj.id).contentWindow.getSelection().getRangeAt(0);
			if (element.innerHTML.length > 0 && range.toString() == "") {
				range.insertNode(element);
			} else {
				range.surroundContents(element);
			}
		}
	},
 
	switchView: function(iframe, bSource) {
		if (bSource) {
			iframe.hide();
			document.getElementsByName(iframe.id)[0].style.position = "relative";
			document.getElementsByName(iframe.id)[0].style.left = "0px";
			iframe.sourceView = true;
			QEditor.makeEditable(iframe, false);
			document.getElementsByName(iframe.id)[0].focus();
		} else {
			iframe.show();
			document.getElementsByName(iframe.id)[0].style.position = "absolute";
			document.getElementsByName(iframe.id)[0].style.left = "-10000px";
			QEditor.makeEditable(iframe, true);
			iframe.sourceView = false;
		}
	},
	
	filterHTML: function(string) {
		// should be used to detect object and embed tags (which are not displayed by default in some browsers)
		// and replace them with something that can be resized (for example a special kind of image)
		return string;
	},
	
	unfilterHTML: function(string) {
		// should be used to find special kinds of images created with filterHTML and replace them back
		// into object and embed tags.
		return string;
	}
};

var QSimpleToolbar = {
	buttons: [{
		name: 'bold',
		title: 'bold',
		img: 'templates/main/bold.gif',
		func: function() {
			QEditor.exec(this, "Bold", null);
		}
	}, {
		name: 'italic',
		title: 'italic',
		img: 'templates/main/italic.gif',
		func: function() {
			QEditor.exec(this, "Italic", null);
		}
	}, {
		name: 'underline',
		title: 'underline',
		img: 'templates/main/underline.gif',
		func: function() {
			QEditor.exec(this, "Underline", null);
		}
	}, {
		name: 'superscript',
		title: 'superscript',
		img: 'templates/main/superscript.gif',
		func: function() {
			QEditor.exec(this, "Superscript", null);
		}
	}, {
		name: 'subscript',
		title: 'subscript',
		img: 'templates/main/subscript.gif',
		func: function() {
			QEditor.exec(this, "Subscript", null);
		}
	}, {
		name: 'justifyleft',
		title: 'align left',
		img: 'templates/main/left_justified.gif',
		func: function() {
			QEditor.exec(this, "justifyleft", null);
		}
	}, {
		name: 'justifycenter',
		title: 'center',
		img: 'templates/main/center_justified.gif',
		func: function() {
			QEditor.exec(this, "justifycenter", null);
		}
	}, {
		name: 'justifyright',
		title: 'align right',
		img: 'templates/main/right_justified.gif',
		func: function() {
			QEditor.exec(this, "justifyright", null);
		}
	}, {
		name: 'justifyfull',
		title: 'justify',
		img: 'templates/main/justify.gif',
		func: function() {
			QEditor.exec(this, "justifyfull", null);
		}
	}, {
		name: 'paragraph',
		title: 'make paragraph',
		img: 'templates/main/paragraph.gif',
		func: function() {
			var n=QEditor.doc(this.id).createElement("P");
			QEditor.surroundSelection(this, n);
		}
	}, {
		name: 'insertorderedlist',
		title: 'insert ordered list',
		img: 'templates/main/orderedlist.gif',
		func: function() {
			QEditor.exec(this, "insertorderedlist", null);		
		}
	}, {
		name: 'insertunorderedlist',
		title: 'insert unordered list',
		img: 'templates/main/unorderedlist.gif',
		func: function() {
			QEditor.exec(this, "insertunorderedlist", null);		
		}
	}, {
		name: 'createlink',
		title: 'create link',
		img: 'templates/main/url.gif',
		func: function() {
			QEditor.exec(this, "createlink", prompt("Please enter the url for the hyperlink:"));		
		}
	}],
	
	elements: [],
	
	init: function(target, field) {
		for (var i = 0; i < QSimpleToolbar.buttons.length; i++) {
			if (!target.hasClassName("-"+QSimpleToolbar.buttons[i].name)) {
				QSimpleToolbar.renderButton(target, QSimpleToolbar.buttons[i].img, QSimpleToolbar.buttons[i].func.bind($(field)), QSimpleToolbar.buttons[i].title);
			} 
		}
		for (var i = 0; i < QSimpleToolbar.elements.length; i++) {
			if (!target.hasClassName("-"+QSimpleToolbar.elements[i].name)) {
				target.appendChild(QSimpleToolbar.elements[i].element);
			}
		}
	},
	
	renderButton: function(target, img, func, title) {
		var button = document.createElement("button");
		var image = document.createElement("img");
		image.src = img;
		image.alt = title;
		image.title = title;
		image.border= "0";
		button.setAttribute("type", "button");
		button.title = title;
		button.appendChild(image);
		button.onclick = function() {
			try {
				func();
			} catch (e) {};
			return false;
		};
		target.appendChild(button);
	},
	
	addButton: function(name, img, func, title) {
		var button = document.createElement("button");
		var image = document.createElement("img");
		image.src = img;
		image.alt = title;
		image.title = title;
		image.border= "0";
		button.setAttribute("type", "button");
		button.title = title;
		button.appendChild(image);
		button.onclick = function() {
			try {
				func();
			} catch (e) {};				
			return false;
		};
		QSimpleToolbar.elements.push({name: name, element:button});
	}, 
		
	addElement: function(name, element) {
		QSimpleToolbar.elements.push({name: name, element: element});
	}
};

var QAdvancedToolbar = {

	elements: [],
	
	init: function(target, field) {
		if (!target.hasClassName("-fontfamily")) {
			var select = document.createElement("select");
			select.size = 1;
			select.onclick = function() {
				QEditor.exec(field, "fontname", this.value);
			};
			select.title = "font size";
			select.options[select.length] = new Option("Arial", "Arial");
    		select.options[select.length] = new Option("Arial Black", "Arial Black");
    		select.options[select.length] = new Option('Courier New', 'Courier New'); 
    		select.options[select.length] = new Option('Times New Roman', 'Times New Roman');
    		select.options[select.length] = new Option('Verdana', 'Verdana');
			target.appendChild(select);
		}
		if (!target.hasClassName("-fontsize")) {
			var select = document.createElement("select");
			select.size = 1;
			select.onclick = function() {
				QEditor.exec(field, "fontsize", this.value);
			};
			select.title = "font size";
			select.options[select.length] = new Option("8pt (1)", "1");
			select.options[select.length] = new Option("10pt (2)", "2", true, true);
			select.options[select.length] = new Option("12pt (3)", "3");
			select.options[select.length] = new Option("14pt (4)", "4"); 
			select.options[select.length] = new Option("16pt (5)", "5"); 
			select.options[select.length] = new Option("18pt (6)", "6"); 
			select.options[select.length] = new Option("20pt (7)", "7"); 
			target.appendChild(select);
		}
		for (var i = 0; i < QAdvancedToolbar.elements.length; i++) {
			if (!target.hasClassName("-"+QAdvancedToolbar.elements[i].name)) {
				var clone = QAdvancedToolbar.elements[i].element.cloneNode(true);
				clone.link = QAdvancedToolbar.elements[i];
				clone.field = field;
				clone.onclick = function() {
					try {
						var func = this.link.event;
						func(this.field);
					} catch (e) {};
					return false;
				}.bind(clone);
				target.appendChild(clone);
			}
		}
	},
	
	addButton: function(name, img, func, title) {
		var button = document.createElement("button");
		var image = document.createElement("img");
		image.src = img;
		image.alt = title;
		image.title = title;
		image.border= "0";
		button.title = title;
		button.setAttribute("type", "button");
		button.appendChild(image);
		QAdvancedToolbar.elements.push({name: name, element:button, event:func});
	}, 
		
	addElement: function(name, element) {
		QAdvancedToolbar.elements.push({name: name, element: element});
	}
};

addLoadEvent(QEditor.init);