if (typeof(RadEditorCommandList) == "undefined")
{
	RadEditorCommandList = new Array();
}


RadEditorCommandList["MOSSRightToLeft"] = function(commandName, editor, oTool)
{
    Telerk_MOSS_SetDirectionOfSelection(editor,"rtl");
}

RadEditorCommandList["MOSSLeftToRight"] = function(commandName, editor, oTool)
{
    Telerk_MOSS_SetDirectionOfSelection(editor,"ltr");
}

RadEditorCommandList["MOSSHelp"] = function(commandName, editor, oTool)
{
    HelpWindowKey('WCMHTMLEditor');
}

RadEditorCommandList["ToggleSource"] = function(commandName, editor, oTool)
{
    //todo: call the source dialog as the MOSS editor does
    editor.SetMode(2);
}

RadEditorCommandList["MOSSInsertTable"] = function(commandName, editor, oTool)
{
    //todo: change the dialog to use the moss default (after firefox is fixed)
    editor.Fire("TableWizard");
}

RadEditorCommandList["ImageManager"] = function (commandName, editor, oTool)
{
	var callBackFn = (oTool && "function" == typeof(oTool) ? oTool : RadEditorNamespace.radEditorCreateImage );
	var argument = {};
	argument.InternalParameters = editor.GetDialogInternalParameters(commandName);
	
	editor.ShowDialog(editor.GetDialogUrl("SharePoint" + commandName) +
		"&ImageFilters=" + editor.ImagesFilters + GetDialogArguments(editor, commandName)
		, argument
		, 400
		, 300
		, callBackFn
		, null
		, editor.Localization[commandName]);
	return false;
};

RadEditorCommandList["TemplateManager"] = function (commandName, editor, oTool)
{
/*	var callBackFn = (oTool && "function" == typeof(oTool) ? oTool : RadEditorNamespace.radEditorInsertTemplate);
	
	var argument = {};
	argument.InternalParameters = editor.GetDialogInternalParameters(commandName);
	
	editor.ShowDialog(editor.GetDialogUrl("SharePoint" + commandName) +
		"&TemplateFilters=" + editor.TemplateFilters + GetDialogArguments(editor, commandName)
		, argument
		, 400
		, 300
		, callBackFn
		, null
		, editor.Localization[commandName.replace(/\//ig,'')]);
*/
    MOSSReusableText(editor);
	return false;
};

//helper functions
var g_strRTE2ReusableFragmentCommonToken="__publishingReusableFragment";

function MOSSReusableText(editor)
{   
    var strBaseElementID = "RadEContentIframe" + editor.Id;
    var params = editor.GetDialogParameters("MOSSParams");
	var docEditor=editor.GetDocument();
	if (docEditor==null) return;
	RTE_RestoreSelection(strBaseElementID);
	var configObj=new AssetPickerConfig("");
	configObj.ClientID=strBaseElementID;
	configObj.DefaultAssetLocation="";
	configObj.CurrentWebBaseUrl=params["CurrentWebBaseUrl"];
	configObj.AllowExternalUrls=params["AllowExternalUrls"];
	var bInserting=false;
	var elemToReplace=null;
	var fragmentId=null;
	var elemViewToEdit=RTE_GetNearestContainingElementOfType(strBaseElementID, "SPAN");
	if ((elemViewToEdit !=null) && (elemViewToEdit.id==g_strRTE2ReusableFragmentCommonToken))
	{
		fragmentId=elemViewToEdit.getAttribute("fragmentid");
	}
	var dialogInput=new Object();
	dialogInput.fragmentId=fragmentId;
	var callback=function(dialogOutput)
	{
		if ((dialogOutput !=null) && (dialogOutput.fragmentView !=null))
		{
			var selection=editor.GetDocument().selection;
			if (selection !=null)
			{
				if (selection.type=="Control")
				{
					var controlRange=selection.createRange();
					if (controlRange !=null)
					{
						controlRange.execCommand("Delete");
					}
				}
				if (selection.type !="Control")
				{
					var textRange=selection.createRange();
					if (textRange !=null)
					{
					//class=ms-reusableTextView 
						editor.PasteHtml(dialogOutput.fragmentView);
					}
				}
			}
		}
	};
	
	var dialogUrl=params["CurrentWebBaseUrl"]+"/_layouts/ReusableTextPicker.aspx";
	commonShowModalDialog(dialogUrl, "dialogHeight:600px; dialogWidth:800px; center:yes; resizable:yes; scroll:no; status:no;", callback, dialogInput);
	//RTE_GiveEditorFocus(strBaseElementID);
}

if (typeof(RadEditor) == "undefined")
{
	RadEditor = function () {};
}

RadEditor.EncodeUrl = function(url)
{
	if (window.encodeURIComponent)
	{
		return encodeURIComponent(url);
	}
	
	if (window.escape)
	{
		return escape(url);	
	}
	
	return url;
}

function GetDialogArguments(editor, commandName)
{
	var args = editor.GetDialogParameters(commandName);
	var argStr = "";
	for (var item in args)
	{
		argStr += "&" + item + "=" + RadEditor.EncodeUrl(args[item]);
	}
	return argStr;
}

function Telerk_MOSS_SetDirectionOfSelection(editor, strDirection)
{
    var rngSelection=editor.GetSelection();
	var strTagNames;
	strTagNames="|H1|H2|H3|H4|H5|H6|P|PRE|TD|DIV|BLOCKQUOTE|DT|DD|TABLE|HR|IMG|BODY|TR|UL|OL|";
	if (rngSelection.GetParentElement())
	{
		var elemSelectionParent=rngSelection.GetParentElement();
		while ((elemSelectionParent !=null) && (strTagNames.indexOf("|"+elemSelectionParent.tagName+"|")==-1))
		{
			elemSelectionParent=elemSelectionParent.parentElement;
		}
		if (elemSelectionParent)
		{
			Telerik_MOSS_SetDirectionOnElement(elemSelectionParent, strDirection);
		}
	}
}

function Telerik_MOSS_SetDirectionOnElement(element, strDirection)
{
	if ("ltr"==strDirection)
	{
		element.dir="ltr";
		element.align="left";
	}
	else if ("rtl"==strDirection)
	{
		element.dir="rtl";
		element.align="right";
	}
}
















RadEditorCommandList["MOSSLinkManager"] = function(commandName, editor, oTool)
{
    var docEditor = editor.GetContentArea().document;
	var configObj=new AssetPickerConfig("");
    //TODO: what is this used for?
	configObj.ClientID=editor.Id;
    //get the default parameters 
	//configObj.DefaultAssetLocation=RTE2_GetDialogParameter(strBaseElementID, "DefaultAssetLocation");
	//configObj.DefaultAssetImageLocation=RTE2_GetDialogParameter(strBaseElementID, "DefaultAssetImageLocation");
	//configObj.CurrentWebBaseUrl=RTE2_GetDialogParameter(strBaseElementID, "CurrentWebBaseUrl");
	//configObj.AllowExternalUrls=RTE2_ShouldAllowExternalUrls(strBaseElementID);
    //
	var bInserting=false;
	var elemToReplace=null;
	var linkAsset=new LinkAsset("");
	var innerLinkHTML=null;
	linkAsset.ManageLinkDisplayText=false;
    
    //figure out how to get new or old hyperlink
	var elemLink=null;//RTE_GetNearestContainingElementOfType(strBaseElementID, "A");
	if( !elemLink )
	{
		bInserting=true;
		elemLink=docEditor.createElement("A");
		if (elemLink==null) {    return; }
		var selectionType=docEditor.selection.type;
		if (selectionType==null) {return;}
		if (selectionType=="Control")
		{
			elemToReplace=RTE_GetCurrentElement(strBaseElementID);
			if (elemToReplace==null) {return;}
		}
	}
	else
	{
		innerLinkHTML=elemLink.innerHTML;
	}
	configObj.ReturnCallback=function(newAssetUrl, newAssetText, currentConfig, dialogReturnedData)
	{
		if (dialogReturnedData)
		{
			if(innerLinkHTML && innerLinkHTML !="")
			{
				elemLink.innerHTML=innerLinkHTML;
			}
			else if(elemLink.innerHTML=="")
			{
				if(linkAsset.AssetText && linkAsset.AssetText !="")
				{
					elemLink.innerText=linkAsset.AssetText
				}
				else
				{
					elemLink.innerText=linkAsset.AssetUrl;
				}
			}
			if (elemToReplace !=null)
			{
				elemToReplace.applyElement(elemLink);
				linkAsset.FixupIconsAndEmptyAnchors(elemLink)
				return;
			}
			else if (bInserting)
			{
				var selection=docEditor.selection;
				if (selection !=null)
				{
					var textRange=selection.createRange();
					if (textRange !=null)
					{
						var text=textRange.text;
						if (text && text !="" && !text.match(/^\s+$/))
						{
							textRange.execCommand("Unlink");
							elemLink.innerHTML=textRange.htmlText;
							linkAsset.FixupIconsAndEmptyAnchors(elemLink)
						}
						textRange.pasteHTML(elemLink.outerHTML);
					}
				}
			}
			else
			{
				linkAsset.FixupIconsAndEmptyAnchors(elemLink)
			}
		}
	}
	linkAsset.LaunchModalDialogOnElement(configObj, elemLink, true, !bInserting );
}