function FreeFlowAction(uid) {
	this.uid = uid;
	this.listElem = document.getElementById(uid+"_list");
}
FreeFlowAction.prototype = new DowWidget;

FreeFlowAction.prototype.postInit = function() {
	var match = window.location.hash.match(/^#_index_(\d+)$/);
	if (match) {
		var blockIndex = match[1];
		
		var selectedElem = this.getChildElementItem(this.listElem  , "widget_DowBlock" , blockIndex);
		if (selectedElem) {
			selectedElem.scrollIntoView();
		}
	}
}


FreeFlowAction.prototype.sendFreeFlow = function () {
	var action = this.createEmptyAction("setGenericXML");
	action.setAttribute("what_key","freeFlow");
	var doc = action.ownerDocument;
	
	var widgets = this.getAllSubWidgets(this.listElem , "DIV" , "widget_RichTextWidget");
	for (var ii=0; ii<widgets.length ; ii++) {
		var richWidget = widgets[ii];
		
		var curBlock = doc.createElement("dow_block");
		action.appendChild(curBlock);

		var blockWidget = this.getFirstParentWidget (richWidget.getWidgetElement() , "DIV" , "widget_DowBlock");
		blockWidget.appendXMLData(curBlock);
		richWidget.appendXMLData(curBlock);
	}
	
	this.sendAction(action.ownerDocument);
}

FreeFlowAction.prototype.onWidgetModified = function (widget) {
	this.sendFreeFlow(); //overkill?!
}

FreeFlowAction.prototype.on_dataModified = function (who) {
	this.sendFreeFlow();
}

FreeFlowAction.prototype.on_willselect = function(who) {
	this.sendEventDown(this,"unselect");
	this.selected = who;
}

FreeFlowAction.prototype.on_hasselected = function(who) {

}

FreeFlowAction.prototype.on_editAction = function(who) {
	var blockWidget = this.getFirstParentWidget(who.getWidgetElement() , "DIV" , "widget_DowBlock");
	if (blockWidget) {
		var richTextList = this.getAllSubWidgets(blockWidget.getWidgetElement() , "DIV" , "widget_RichTextWidget");
		if (richTextList.length == 1) {
			richTextList[0].do_edit();
		}
	}
}

FreeFlowAction.prototype.on_deleteAction = function(who) {
	var blockWidget = this.getFirstParentWidget(who.getWidgetElement() , "DIV" , "widget_DowBlock");
	if (blockWidget) {
		var blockElem = blockWidget.getWidgetElement();
		this.listElem.removeChild(blockElem);
	}
	this.sendFreeFlow();
}

FreeFlowAction.prototype.on_add_item = function() {
	var templateElem = document.getElementById(this.uid + "_default");
	var newBlockElem = this.cloneSubtree(templateElem);
	this.listElem.appendChild(newBlockElem);
	
	registerAllSubWidgets(newBlockElem);
	this.sendFreeFlow();
}




function UPControlPanel() {
}
UPControlPanel.prototype = new DowWidget;

UPControlPanel.prototype.postInit = function () {
	this.toggleElem = document.getElementById(this.uid+"_toggle");
	this.toggleIconElem = document.getElementById(this.uid+"_toggle_icon");
	this.toggleTextAddElem = document.getElementById(this.uid+"_toggle_text_add");
	this.toggleTextRemoveElem = document.getElementById(this.uid+"_toggle_text_remove");
	this.bindEvent(this.toggleElem,"click","toggleAction"); 
}

UPControlPanel.prototype.toggleAction = function (event) {
	this.params.rank=this.params.rank ? 0 : 1;
	this.doRanking(this.params.rank);
	this.updateToggle();
}

UPControlPanel.prototype.doRanking = function(value) {
	var action = this.createEmptyAction("ranking");
	var doc = action.ownerDocument;
	var rankingElem = doc.createElement("rank");
	rankingElem.setAttribute("value",value);
	action.appendChild(rankingElem);
	this.sendAction(action.ownerDocument);
}

UPControlPanel.prototype.updateToggle = function () {
	if (this.toggleIconElem) {
		if (this.params.rank) {
			this.unsetElementClass(this.toggleIconElem , "up_favorite_icon_off");
			this.setElementClass(this.toggleIconElem , "up_favorite_icon_on");
		} else {
			this.setElementClass(this.toggleIconElem , "up_favorite_icon_off");
			this.unsetElementClass(this.toggleIconElem , "up_favorite_icon_on");
		}
	}
	
	if (this.toggleTextAddElem && this.toggleTextRemoveElem) {
		if (this.params.rank) {
			this.toggleTextAddElem.style.display="none";
			this.toggleTextRemoveElem.style.display="inline";
		} else {
			this.toggleTextAddElem.style.display="inline";
			this.toggleTextRemoveElem.style.display="none";
		}
	}
}
//*********** rich ********

function RichTextWidget() {
}
RichTextWidget.prototype = new DowWidget;

RichTextWidget.prototype.postInit = function () {
	this.divElement = this.getWidgetElement();
	this.dataElem = document.getElementById(this.uid+"_data");
}


RichTextWidget.prototype.getData = function () {
	return this.dataElem.innerHTML;
}

RichTextWidget.prototype.setData = function (text) {
	this.dataElem.innerHTML = text;
	this.end_edit();
	
	this.sendEventUp(this,"dataModified");
}

RichTextWidget.prototype.appendXMLData = function (root) {
	var doc = root.ownerDocument;
	var captionElem = doc.createElement("rich_text");
	
	//var curNode = this.divElement.firstChild;
	var curNode = this.dataElem.firstChild;
	while (curNode) {
		this.parseContent(captionElem,curNode);
		curNode = curNode.nextSibling;
	}
	root.appendChild(captionElem);
}

RichTextWidget.prototype.processStyle = function (htmlNode , xmlNode) {
	var style = htmlNode.style;
	
	if (style.fontWeight) {
		xmlNode.setAttribute("font_weight",style.fontWeight);
	}
	if (style.textDecoration) {
		xmlNode.setAttribute("text_decoration",style.textDecoration);
	}
	if (style.fontStyle) {
		xmlNode.setAttribute("font_style",style.fontStyle);
	}
	if (style.fontSize) {
		xmlNode.setAttribute("font_size",style.fontSize);
	}
	if (style.color) {
		xmlNode.setAttribute("color",style.color);
	}
}

RichTextWidget.prototype.processChildren = function (htmlNode , xmlNode) {
	var curChild = htmlNode.firstChild;
	while (curChild) {
		this.parseContent(xmlNode,curChild);
		curChild = curChild.nextSibling;
	}
}

RichTextWidget.prototype.parseContent = function (xmlRoot , htmlNode) {
	var doc = xmlRoot.ownerDocument;
	//alert("nodeName: "+htmlNode.nodeName);
	
	switch (htmlNode.nodeType) {
		case Node.ELEMENT_NODE: {
			if (htmlNode.nodeName == "SPAN") {
				var xmlNode = doc.createElement('span_text');
				this.processStyle(htmlNode,xmlNode);
				if (xmlNode.attributes.length) {
					this.processChildren(htmlNode,xmlNode);
					xmlRoot.appendChild(xmlNode);
				} else { //no attributes, no need for span_text
					this.processChildren(htmlNode,xmlRoot);
				}
			} else if (htmlNode.nodeName == "P") {
				var xmlNode = doc.createElement('span_text');
				this.processStyle(htmlNode,xmlNode);
				this.processChildren(htmlNode,xmlNode);
				xmlRoot.appendChild(xmlNode);
				xmlRoot.appendChild(doc.createElement('crlf'));
			} else if (htmlNode.nodeName == "STRONG") {
				var xmlNode = doc.createElement('span_text');
				xmlNode.setAttribute("font_weight" , "bold");
				this.processChildren(htmlNode,xmlNode);
				xmlRoot.appendChild(xmlNode);
			} else if (htmlNode.nodeName == "FONT") {
				var xmlNode = doc.createElement('span_text');
				var color = htmlNode.getAttribute("COLOR");
				if (color) {
					xmlNode.setAttribute("color" , color);
				}
				var size = htmlNode.getAttribute("SIZE");
				if (size) {
					var sizeStr = null;
					switch(size) {
						case "1": sizeStr="8pt"; break;
						case "2": sizeStr="10pt"; break;
						case "3": sizeStr="12pt"; break;
						case "4": sizeStr="14pt"; break;
						case "5": sizeStr="18pt"; break;
						case "6": sizeStr="24pt"; break;
						case "7": sizeStr="36pt"; break;
					}
					if (sizeStr) {
						xmlNode.setAttribute("font_size" , sizeStr);
					}
				}
				this.processChildren(htmlNode,xmlNode);
				xmlRoot.appendChild(xmlNode);
			} else if (htmlNode.nodeName == "IMG") {
				var xmlNode = doc.createElement('image');
				var src1 = htmlNode.getAttribute("SRC"); //partial
				var src2 = htmlNode.src; //full
				//regexp
				
				xmlNode.setAttribute("src" , src1 );
				xmlRoot.appendChild(xmlNode);
			} else if (htmlNode.nodeName == "A") {
				var xmlNode = doc.createElement('link');
				xmlNode.setAttribute("href",htmlNode.getAttribute("HREF")); //partial
				//xmlNode.setAttribute("href",htmlNode.href); //full
				this.processChildren(htmlNode,xmlNode);
				xmlRoot.appendChild(xmlNode);
			} else if (htmlNode.nodeName == "BR") {
				xmlRoot.appendChild(doc.createElement('crlf'));
			} else {
				var xmlNode = doc.createElement(htmlNode.nodeName);
				
				var attributes = htmlNode.attributes;
				var ii;
				for (ii=0; ii<attributes.length ; ii++) {
					var attr = attributes.item(ii);
					if (attr.specified) {
						xmlNode.setAttribute(attr.name , attr.value);
					}
				}
				
				this.processChildren(htmlNode,xmlNode);
				/*
				var curChild = htmlNode.firstChild;
				while (curChild) {
					this.parseContent(xmlNode,curChild);
					curChild = curChild.nextSibling;
				}
				xmlRoot.appendChild(xmlNode);
				*/
			}
		} break;
		case Node.TEXT_NODE: {
			//alert("text["+htmlNode.nodeValue+"]");
			xmlRoot.appendChild(doc.createTextNode(htmlNode.nodeValue));
		} break;
	}
}


/*

RichTextWidget.prototype.dblclickAction = function (event) {
	this.do_edit();
}


RichTextWidget.prototype.contextmenuAction = function (event) {
	if (!this.disableContext) {
		event.preventDefault();
		gContextMenu.clear();
		gContextMenu.addMenu("Edit" , this , "do_edit" );
		gContextMenu.addMenu("Set Red" , this , "set_color" , "red");
		gContextMenu.addMenu("Set Black" , this , "set_color" , "black");
		gContextMenu.addMenu("Default" , this , "do_default" );
		gContextMenu.popup(event);
	}
	this.disableContext = false;
}

RichTextWidget.prototype.clickAction = function (event) {
	if (!this.hasElementClass(this.divElement, "selected")) {
		this.sendEventUp(this,"willselect");
		this.setElementClass(this.divElement , "selected");
		window.gSelection.set(this);
		//document.designMode = "On";
		//this.divElement.contentEditable = true;
		this.sendEventUp(this,"hasselected");
	}
}

RichTextWidget.prototype.on_unselect = function (who) {
	//document.designMode = "Off";
	//this.divElement.contentEditable = false;
	this.unsetElementClass(this.divElement , "selected");
}
*/

/*
RichTextWidget.prototype.do_default = function () {
	this.disableContext = true;
}

RichTextWidget.prototype.setRangeStyle = function (selRange , styleName , styleValue) {
	var frag = this.getRangeFragment(selRange);
	
	var span = document.createElement("span");
	span.style[styleName] = styleValue;
	if (frag.innerHTML) { //IE6
		span.innerHTML = frag.innerHTML;
	} else {
		span.appendChild(frag);
	}

	
	var newFrag = document.createDocumentFragment();
	if (span.outerHTML) { //IE6
		newFrag.innerHTML = span.outerHTML;
	} else {
		newFrag.appendChild(span);
	}

	//alert("setRangeStyle: "+styleName);
	this.setRangeFragment(selRange,newFrag);
}

RichTextWidget.prototype.do_bold = function (selRange) {
	this.setRangeStyle(selRange,"fontWeight","bold");
	this.widgetModified(this);
}

RichTextWidget.prototype.do_underline = function (selRange) {
	this.setRangeStyle(selRange,"textDecoration","underline");
	this.widgetModified(this);
}

RichTextWidget.prototype.do_italic = function (selRange) {
	this.setRangeStyle(selRange,"fontStyle","italic");
	this.widgetModified(this);
}

RichTextWidget.prototype.set_color = function (color) {
	var selRange = this.getSelectionRange();
	this.setRangeStyle(selRange,"color",color);
	this.widgetModified(this);
}
*/

RichTextWidget.prototype.do_edit = function () {
	this.end_edit();
	
	var baseElem =this.getWidgetElement();
	var pos = this.getElementPosition(baseElem);
	var size = this.getElementSize(baseElem);
	
	var toolHeight = 40;
	var templateEditor = gWidgets["rich_text_editor"];
	var myEditorElem = templateEditor.cloneWidget();
	myEditorElem.style.position="static";
	baseElem.appendChild(myEditorElem);
	var myEditorId = myEditorElem.id;
	
	//registerAllSubWidgets(myEditorElem);
	this.myEditorElem=myEditorElem;
	
	if (gWidgets[myEditorId].attachEditor(this, 0 , 0 , size.width , size.height)) {
		this.divElement.style.width = size.width + "px";
		this.divElement.style.height = (size.height + toolHeight) +"px";
		this.dataElem.style.display = "none";
	}
}

RichTextWidget.prototype.end_edit = function () {
	if (this.myEditorElem) {
		var baseElem =this.getWidgetElement();
		baseElem.removeChild(this.myEditorElem);
		this.myEditorElem = null;
	}
	this.divElement.style.width = "";
	this.divElement.style.height = "";
	this.dataElem.style.display = "";
}

//***********

function RichEditorWidget() {
}
RichEditorWidget.prototype = new DowWidget;

RichEditorWidget.prototype.postInit = function() {
	this.editorElem = this.getWidgetElement();
	this.frameContainerElem = document.getElementById(this.uid+"_frame_container");
	this.menubarElem = document.getElementById(this.uid+"_menubar");
	this.colorButtonElem = document.getElementById(this.uid+"_menubar_color_button");
	this.colorListElem = document.getElementById(this.uid+"_menubar_color_grid");

	this.uploadButtonElem = document.getElementById(this.uid+"_menubar_upload_button");
	this.uploadListElem = document.getElementById(this.uid+"_menubar_upload_list");
	
	this.sizeButtonElem = document.getElementById(this.uid+"_menubar_size_button");
	this.sizeListElem = document.getElementById(this.uid+"_menubar_size_list");
	
	this.imageButton = document.getElementById(this.uid+"_menubar_upload_image");
	this.pdfButton = document.getElementById(this.uid+"_menubar_upload_pdf");
	this.docButton = document.getElementById(this.uid+"_menubar_upload_doc");
	this.xlsButton = document.getElementById(this.uid+"_menubar_upload_xls");
	this.statusText = document.getElementById(this.uid+"_menubar_upload_status");

	//NO iframe in strict HTML
	this.frameElem = document.getElementById(this.uid+"_frame");
	if (!this.frameElem) {
		this.frameElem = document.createElement("iframe");
		this.frameElem.id=this.uid+"_frame";
		this.frameElem.className="multiline_editor";
		this.frameElem.name="multiline_editor";
		this.frameContainerElem.appendChild(this.frameElem);
	}
	
	//this.bindEvent(this.frameElem,"beforedeactivate","beforedeactivateAction"); //NO NO NO!!!!
	this.bindEvent(this.colorListElem,"click","colorAction");
	this.bindEvent(this.sizeListElem,"click","sizeAction");
	this.bindEvent(this.uploadListElem,"click","insertAction");
}

RichEditorWidget.prototype.getUploadWidget = function() {
	return gWidgets[this.uid+"_menubar_upload"+"_file_upload"];
}

RichEditorWidget.prototype.getData = function() {
	return this.frameElem.contentWindow.document.body.innerHTML;
}

RichEditorWidget.prototype.beforedeactivateAction = function(event) { //IE6
	event.preventDefault();
}

RichEditorWidget.prototype.attachEditor = function (widget,left,top , width , height) {
	this.hideAllPopups();
	
	if (widget) {	
		if (this.ownerWidget && this.ownerWidget==widget) { //already open do nothing
			//this.attachEditor(null);
			return false;
		}
		if (this.ownerWidget) { //closes OTHER open editors
			this.attachEditor(null);
		}

		this.editorElem.style.display = "block";

		var html = widget.getData();

		this.editorElem.style.left=left+"px";
		this.editorElem.style.top=top+"px";
		this.editorElem.style.width=width+"px";
		//this.editorElem.style.height=height+"px";
		this.setElementClass(this.editorElem , "editing");
		this.editorElem.style.display = "block";
		
		// CSS IE6 hack -> height: expression( this.scrollHeight < 334 ? "333px" : "auto" );
		if (height<101)  height=100; //IE6 hack
		this.frameElem.style.height=height+"px";
		
		var iFrameWindow = this.frameElem.contentWindow;
		var iFrameDocument = iFrameWindow.document;
		
		var cssData = "body {border: none ; margin: 0 ;  font-family: Arial,Verdana,Tahoma,'Myriad Web',Syntax,sans-serif; font-size: 12px; }\n";
		cssData += "a {text-decoration:none; font-weight:bold; color:#153970;}\n";
		cssData += "a:hover {text-decoration: underline}\n";
		cssData += "a>img {border: 0}\n";
		
		iFrameDocument.open();
		iFrameDocument.writeln('<html><head><title>rich editor</title><style type="text/css">'+cssData+'</style></head><body>');
		//if (html == "") html="default text";
		iFrameDocument.write(html);
		iFrameDocument.writeln('</body></html>');
		iFrameDocument.close();


		var iFrameBody = iFrameDocument.body;

		iFrameDocument.designMode = "On";
		
/*
		if(iFrameBody.contentEditable) {
			iFrameBody.contentEditable = true;
		} else { //not for IE6 
			iFrameDocument.designMode = "On";
		}
*/
/*
		iFrameWindow.focus();
		if (iFrameWindow.getSelection) {
			var iFrameSelection = iFrameWindow.getSelection();
			if (iFrameSelection && iFrameSelection.selectAllChildren) {
				iFrameSelection.selectAllChildren(iFrameBody);
				iFrameSelection.collapseToEnd();
			}
		}
*/
		
	} else {	
		this.unsetElementClass(this.editorElem , "editing");
		//this.editorElem.style.display = "none";
		this.editorElem.style.display ="";
	}
	this.ownerWidget = widget;
	return true;
}


RichEditorWidget.prototype.userModified = function () {
	var iHeight = this.editorElem.contentDocument.body.offsetHeight;
	this.editorElem.style.height=iHeight+"px";
//alert("userModified");
}



RichEditorWidget.prototype.on_italic = function () {
	this.frameElem.contentWindow.document.execCommand("italic", false, null);
}

RichEditorWidget.prototype.on_bold = function () {
	this.frameElem.contentWindow.document.execCommand("bold", false, null);
}

/*
RichEditorWidget.prototype.on_underline = function () {
	this.frameElem.contentWindow.document.execCommand("underline", false, null);
}
*/

RichEditorWidget.prototype.hideAllPopups = function(){
	this.close_color();
	this.close_size();
	this.close_upload();
}




RichEditorWidget.prototype.open_color = function () {
	this.colorButtonElem.src = "/up/icons/text_color_on.png";
	this.colorButtonElem.onmouseover= function() {this.src='/up/icons/text_color_hover.png'};
	this.colorButtonElem.onmouseout= function() {this.src='/up/icons/text_color_on.png'};
	this.colorListElem.style.display = "";
}
 
RichEditorWidget.prototype.close_color = function () {
	this.colorButtonElem.src = "/up/icons/text_color.png";
	this.colorButtonElem.onmouseover= function() {this.src='/up/icons/text_color_hover.png'};
	this.colorButtonElem.onmouseout= function() {this.src='/up/icons/text_color.png'};
	this.colorListElem.style.display = "none";
}

RichEditorWidget.prototype.on_color = function () {
	if (this.colorListElem.style.display != 'none') {
		this.close_color();
	} else {
		this.hideAllPopups();
		this.open_color();
	}
}

RichEditorWidget.prototype.open_size = function () {
	this.sizeButtonElem.src = "/up/icons/text_size_on.png";
	this.sizeButtonElem.onmouseover= function() {this.src='/up/icons/text_size_hover.png'};
	this.sizeButtonElem.onmouseout= function() {this.src='/up/icons/text_size_on.png'};
	this.sizeListElem.style.display = "";
}
 
RichEditorWidget.prototype.close_size = function () {
	this.sizeButtonElem.src = "/up/icons/text_size.png";
	this.sizeButtonElem.onmouseover= function() {this.src='/up/icons/text_size_hover.png'};
	this.sizeButtonElem.onmouseout= function() {this.src='/up/icons/text_size.png'};
	this.sizeListElem.style.display = "none";
}

RichEditorWidget.prototype.on_size = function () {
	if (this.sizeListElem.style.display != 'none') {
		this.close_size();
	} else {
		this.hideAllPopups();
		this.open_size();
	}
}

RichEditorWidget.prototype.open_upload = function () {
	this.uploadButtonElem.src = "/up/icons/text_upload_on.png";
	this.uploadButtonElem.onmouseover= function() {this.src='/up/icons/text_upload_hover.png'};
	this.uploadButtonElem.onmouseout= function() {this.src='/up/icons/text_upload_on.png'};
	this.uploadListElem.style.display = "";
}
 
RichEditorWidget.prototype.close_upload = function () {
	this.uploadButtonElem.src = "/up/icons/text_upload.png";
	this.uploadButtonElem.onmouseover= function() {this.src='/up/icons/text_upload_hover.png'};
	this.uploadButtonElem.onmouseout= function() {this.src='/up/icons/text_upload.png'};
	this.uploadListElem.style.display = "none";
}

RichEditorWidget.prototype.on_upload = function () {
	if (this.uploadListElem.style.display != 'none') {
		this.close_upload();
	} else {
		this.hideAllPopups();
		this.open_upload();
	}
}



RichEditorWidget.prototype.sizeAction = function (event) {
	var actionName = this.getElementActionName(event.target);
	if (actionName == "size_item") {
		//this.setSelectionStyle(this.frameElem.contentWindow , "fontSize" , event.target.style.fontSize);
		var cssSize = event.target.style.fontSize;
		var size;
		switch (cssSize) {
			case "8pt": size=1; break;
			case "10pt": size=2; break;
			case "12pt": size=3; break;
			case "14pt": size=4; break;
			case "18pt": size=5; break;
			case "24pt": size=6; break;
			case "36pt": size=7; break;
		}
		this.frameElem.contentWindow.document.execCommand("FontSize", false, size);
	}
}




RichEditorWidget.prototype.colorAction = function (event) {
	var actionName = this.getElementActionName(event.target);
	if (actionName == "color_cell") {
		var color = event.target.style.backgroundColor;
		this.frameElem.contentWindow.document.execCommand("forecolor", false, color);
	}
}

RichEditorWidget.prototype.on_html = function () {
	var html = this.getSelectionHTML2(this.frameElem.contentWindow);
	//var html = this.getSelectionHTML(this.frameElem.contentWindow);
	alert("html: "+html);
}

RichEditorWidget.prototype.checkForInsertion = function() {
	var frameDoc = this.frameElem.contentWindow.document;
	var ieSelection = frameDoc.selection;
	if (ieSelection) {
		if (ieSelection.type == "None") {
			var ieRange = frameDoc.body.createTextRange();
			ieRange.collapse(true);
			ieRange.select();		
		}
	} else if (this.frameElem.contentWindow.getSelection) {
		var selObj = this.frameElem.contentWindow.getSelection(); 
		if (selObj.rangeCount == 0) {
			selObj.collapse(frameDoc.body,0);
		}
	}
}

RichEditorWidget.prototype.checkForSelection = function(aStr) {
	var html = this.getSelectionHTML(this.frameElem.contentWindow);
	if (!html) {
		var textNode = this.frameElem.contentWindow.document.createTextNode(aStr);
		var spanNode = this.frameElem.contentWindow.document.createElement("SPAN");
		spanNode.appendChild(textNode);
		
		this.frameElem.contentWindow.document.body.appendChild(spanNode);
		this.setSelectionToElement(spanNode,this.frameElem.contentWindow);
	}
}

RichEditorWidget.prototype.on_link = function () {
	var urlStr = prompt("Enter the URL", "http://www.daysofwonder.com/");
	if (urlStr) {
		this.checkForSelection(urlStr);
		this.frameElem.contentWindow.document.execCommand("CreateLink", false, urlStr); //true NOT in Mozilla/ Safari
	}
}

RichEditorWidget.prototype.insertAction = function (event) {
//alert("insertAction");
	var actionName = this.getElementActionName(event.target);
	if (actionName == "insert_image") {
		this.on_insert_image();
		event.stopPropagation();
	} else if (actionName == "insert_pdf") {
		this.on_insert_file("pdf");
		event.stopPropagation();
	} else if (actionName == "insert_doc") {
		this.on_insert_file("doc");
		event.stopPropagation();
	} else if (actionName == "insert_xls") {
		this.on_insert_file("xls");
		event.stopPropagation();
	} 
}


RichEditorWidget.prototype.off_all_inserts = function () {
	var uploadWidget = this.getUploadWidget();
	uploadWidget.stopEditing();
	this.unsetElementClass(this.imageButton, "on");
	this.unsetElementClass(this.pdfButton, "on");
	this.unsetElementClass(this.docButton, "on");
	this.unsetElementClass(this.xlsButton, "on");
}

RichEditorWidget.prototype.on_insert_file = function (type) {
	var button=null;
	switch (type) {
		case 'pdf': button=this.pdfButton; break;
		case 'doc': button=this.docButton; break;
		case 'xls': button=this.xlsButton; break;
	}
	if (!button) return;
	
	var isOn = this.hasElementClass(button, "on");
	this.off_all_inserts();
	if (!isOn) {
		var uploadWidget = this.getUploadWidget();
		uploadWidget.setFileType(type);
		uploadWidget.setCallback(this,"insertFileLink");
		uploadWidget.startEditing(this);
		this.setElementClass(button, "on");
	}
}

RichEditorWidget.prototype.on_insert_image = function () {
	var isOn = this.hasElementClass(this.imageButton, "on");
	this.off_all_inserts();
	if (!isOn) {
		var uploadWidget = this.getUploadWidget();
		uploadWidget.setFileType("image");
		uploadWidget.setCallback(this,"insertImageLink");
		uploadWidget.startEditing(this);
		this.setElementClass(this.imageButton, "on");
	}
}

RichEditorWidget.prototype.uploadError = function (errorCode , name) {
	if (errorCode == 1 || errorCode==2) {
		this.setTextValue(this.statusText , "Error: File is too large (larger than 500KB).");
	} else {
		this.setTextValue(this.statusText , "error: "+errorCode+" for "+name);
	}
}

RichEditorWidget.prototype.uploading = function () {
	this.setTextValue(this.statusText , "Uploading file, please wait...");
}

RichEditorWidget.prototype.insertFileLink = function(file_id,fileUrl,fileType) {
	//this.frameElem.contentWindow.document.execCommand("CreateLink", false, fileUrl);
	
	var doc = this.frameElem.contentWindow.document;
	var aElem = doc.createElement("a");
	aElem.href= fileUrl;
	var imgElem = doc.createElement("img");
	imgElem.alt=fileType;
	switch (fileType) {
		case 'pdf': imgElem.src = "/lang/all/images/pdf.png"; break;
		case 'doc': imgElem.src = "/lang/all/images/doc.png"; break;
		case 'xls': imgElem.src = "/lang/all/images/xls.png"; break;
	}
	aElem.appendChild(imgElem);
	
	this.checkForInsertion();
	this.insertElementAfterSelection(this.frameElem.contentWindow , aElem)
	this.setTextValue(this.statusText , "PDF document uploaded.");
}


RichEditorWidget.prototype.insertImageLink = function(file_id,fileUrl) {
	var fullSrc = static_host + fileUrl;
	this.checkForInsertion();
	var success = this.frameElem.contentWindow.document.execCommand("InsertImage", false, fullSrc);
	this.setTextValue(this.statusText , "Image uploaded.");
	if (!success) alert("Error: execCommand(InsertImage)");
}


RichEditorWidget.prototype.on_exit = function () {
	if (this.ownerWidget) {
		this.ownerWidget.setData(this.getData());
	}
	this.attachEditor(null);
}

RichEditorWidget.prototype.on_cancel = function () {
	if (this.ownerWidget) {
		this.ownerWidget.end_edit();
	}
	this.attachEditor(null);
}

RichEditorWidget.prototype.on_save = function () {
	if (this.ownerWidget) {
		this.ownerWidget.setData(this.getData());
	}
	this.attachEditor(null); //close on save !!!
}

RichEditorWidget.prototype.setImageData = function(fileUrl, zoom_url , file_id) {
	//var fullSrc = "http://" + window.location.host + zoom_url;
	var fullSrc = static_host + zoom_url;
	this.frameElem.contentWindow.document.execCommand("InsertImage", false, fullSrc);
}
