/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/


function CallBackManagedLink(sender, args) {
	if (args) {
		theEditor.pasteHtml(args);
	}
}

function CallBackManagedDocument(sender, returnValue) {
	if (returnValue) {
		theEditor.pasteHtml(returnValue);
	}
}

function CallBackManagedImage(sender, returnValue) {
	if (returnValue) {
		theEditor.pasteHtml(returnValue);
	}
}


function CallBackManagedLinkSelection(sender, returnValue) {
	if (returnValue) {
		theEditor.pasteHtml(returnValue + theEditor.getSelectionHtml() + '</a>');
	}
}

function CallBackManagedDocumentSelection(sender, returnValue) {
	if (returnValue) {
		theEditor.pasteHtml(returnValue + theEditor.getSelectionHtml() + '</a>');
	}
}

function CallBackManagedImageSelection(sender, returnValue) {
	if (returnValue) {
		theEditor.pasteHtml(returnValue + theEditor.getSelectionHtml() + '</a>');
	}
}


	if (typeof(RadEditorCommandList) != "undefined")
{
	var theEditor = null;
	RadEditorCommandList["Managed Link"] = function(commandName, editor, oTool) {

		theEditor = editor;
		var params = window.top.document.location.href.substring(window.top.document.location.href.indexOf("?"));


		var Clean = editor.getSelectionHtml().replace("&nbsp;", "");
		Clean = Clean.replace("<p>", "");
		Clean = Clean.replace("</p>", "");
		Clean = Clean.replace(" ", "");
		Clean = Clean.replace("\r","");
		Clean = Clean.replace("\n", "");


		if (Clean.length > 0) {
					var url = "/cognition/admin/pickers/radpagepicker.aspx"+params+"&selection=y&ajaxrad=y";
					editor.showExternalDialog(url
					, null
					, 600
					, 400
					, CallBackManagedLinkSelection
					, null
					, "Managed Link");
		}
		else
		{
			editor.showExternalDialog("/cognition/admin/pickers/radpagepicker.aspx"+params+ "&ajaxrad=y"
					, null
					, 600
					, 400
					, CallBackManagedLink
					, null
					, "Managed Link");
		}













	}


	RadEditorCommandList["Managed Document"] = function(commandName, editor, oTool) {

		theEditor = editor;
		var params = window.top.document.location.href.substring(window.top.document.location.href.indexOf("?"));


		var Clean = editor.getSelectionHtml().replace("&nbsp;", "");
		Clean = Clean.replace("<p>", "");
		Clean = Clean.replace("</p>", "");
		Clean = Clean.replace(" ", "");
		Clean = Clean.replace("\r","");
		Clean = Clean.replace("\n", "");


		if (Clean.length>0) {

			editor.showExternalDialog("/cognition/admin/DocumentManager/DocumentBrowse.aspx"+params+"&selection=y&startid=KB&AjaxRAD=y"
					, null
					, 660
					, 440
					, CallBackManagedDocumentSelection
					, null
					, "Managed Document");
		}
		else
		{

			editor.showExternalDialog("/cognition/admin/DocumentManager/DocumentBrowse.aspx"+params + "&startid=KB&AjaxRAD=y"
					, null
					, 660
					, 440
					, CallBackManagedDocument
					, null
					, "Managed Document");
		}

	}


	RadEditorCommandList["Managed Image"] = function(commandName, editor, oTool) {

		theEditor = editor;
		var params = window.top.document.location.href.substring(window.top.document.location.href.indexOf("?"));

		if (!editor.getSelectionHtml() || true) {
			editor.showExternalDialog("/cognition/admin/photogallery/pickers/RADGalleryList.aspx"+params+'&RAD=y'
					, null
					, 550
					, 600
					, CallBackManagedImage
					, null
					, "Managed Image2");
		}
		else if (editor.getSelectionHtml() == "\r\n<P>&nbsp;</P>")
		{
			editor.showExternalDialog("/cognition/admin/photogallery/pickers/RADGalleryList.aspx"+params+'&RAD=y'
					, null
					, 550
					, 600
					, CallBackManagedImage
					, null
					, "Managed Image2");
		}
		else {	


					var url = "/cognition/admin/photogallery/pickers/RADGalleryList.aspx"+params+"&selection=y&RAD=y'";
				//	alert("#"+editor.getSelectionHtml()+"#");
					editor.showExternalDialog(url
					, null
					, 550
					, 600
					, CallBackManagedImage
					, null
					, "Managed Image2");

		}






	};







	
	// removes <p> tags from table cells
	RadEditorCommandList["Strip"] = function(commandName, editor, oTool) {
		
		// mozilla
		if(window.getSelection)
		{
			editorHtml = editor.getSelectionHtml();
			tableRegExp = /^<(\s?)table/ ;
			if (editorHtml.match(tableRegExp))
			{
				var newHtml = editorHtml.replace(/<(\/{0,1})p(.*?)(\/{0,1})\>/gi, '');
				newHtml = newHtml.replace(/(<(?:\/{0,1})(?:td|TD|th|TH|tr|TR|table|TABLE))(?:.*?)((?:\/{0,1})\>)/gi, '$1$2');
				editor.PasteHtml(newHtml);
			}
			else
			{
				alert('Please select a table using the node inspector');	
			}
		}
		
		//  internet explorer
		else if (editor.Document.selection)
		{
			// if the selection is a control
			if ("Control" == editor.Document.selection.type)
			{
				// get the control
				var cr = editor.Document.selection.createRange();
				// if the control is a table
				if (cr(0).tagName == 'TABLE')
				{

					// get the table attributes
					tableAttributes = cr(0).attributes;
					// for each of the table attributes
					for (var t=0; t < tableAttributes.length; t++)
					{
						// remove it
						cr(0).removeAttribute(tableAttributes[t].name);
					}
					// get each of the table cells
					tableCells = cr(0).cells;
					// for each of the cells
					for(var i=0; i < tableCells.length; i++)	
					{
						// get the cell attributes
						cellAttributes = tableCells[i].attributes;
						// for each of the cells attributes
						for (var j=0; j < cellAttributes.length; j++)
						{
							// remove the attribute
							tableCells[i].removeAttribute(cellAttributes[j].name);
						}
						
					}
					
					// for each of the cells
					for(var k=0; k < cr(0).cells.length; k++)
					{
						// remove the paragraphs
						cr(0).cells[k].innerHTML = cr(0).cells[k].innerHTML.replace(/<(\/{0,1})p(.*?)(\/{0,1})\>/gi, '');
					}
				}
				else
				{
					alert('Please select a table using the node inspector');
				}
			}
			else
			{
				alert('Please select a table using the node inspector');
			}
		}
	}
}