/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function CallBackManagedLink(returnValue) {
	if (returnValue) {
		theEditor.PasteHtml(returnValue);
	        theEditor.Fire("ToggleScreenMode");
	        theEditor.Fire("ToggleScreenMode");
	}
}

function CallBackManagedResource(returnValue) {
	if (returnValue) {
		theEditor.PasteHtml(returnValue);
	        theEditor.Fire("ToggleScreenMode");
	        theEditor.Fire("ToggleScreenMode");
	}
}

function CallBackManagedImage(returnValue) {
	if (returnValue) {
		theEditor.PasteHtml(returnValue);
	        theEditor.Fire("ToggleScreenMode");
	        theEditor.Fire("ToggleScreenMode");
	}
}


function CallBackManagedLinkSelection(returnValue) {
	if (returnValue) {
		theEditor.PasteHtml(returnValue + theEditor.GetSelectionHtml() + '</a>');
	        theEditor.Fire("ToggleScreenMode");
	        theEditor.Fire("ToggleScreenMode");
	}
}

function CallBackManagedResourceSelection(returnValue) {
	if (returnValue) {
		theEditor.PasteHtml(returnValue + theEditor.GetSelectionHtml() + '</a>');
	        theEditor.Fire("ToggleScreenMode");
	        theEditor.Fire("ToggleScreenMode");
	}
}

function CallBackManagedImageSelection(returnValue) {
	if (returnValue) {
		theEditor.PasteHtml(returnValue + theEditor.GetSelectionHtml() + '</a>');
	        theEditor.Fire("ToggleScreenMode");
	        theEditor.Fire("ToggleScreenMode");
	}
}




	if (typeof(RadEditorCommandList) != "undefined")
{
	var theEditor = null;
	RadEditorCommandList["Save"] = function(commandName, editor, oTool) { editor.Submit(); }
	RadEditorCommandList["Cancel"] = function(commandName, editor, oTool) { editor.CancelEdit(); }
	RadEditorCommandList["ManagedLink"] = function(commandName, editor, oTool) {

		theEditor = editor;
		var params = window.top.document.location.href.substring(window.top.document.location.href.indexOf("?"));

		if (!editor.GetSelectionHtml()) {
			editor.ShowDialog("/cognition/admin/pickers/radpagepicker.aspx"+params
					, null
					, 600
					, 400
					, CallBackManagedLink
					, null
					, "Managed Link");
		}
		else if (editor.GetSelectionHtml() == "\r\n<P>&nbsp;</P>")
		{
			editor.ShowDialog("/cognition/admin/pickers/radpagepicker.aspx"+params
					, null
					, 600
					, 400
					, CallBackManagedLink
					, null
					, "Managed Link");
		}
		else {	


					var url = "/cognition/admin/pickers/radpagepicker.aspx"+params+"&selection=y";
				//	alert("#"+editor.GetSelectionHtml()+"#");
					editor.ShowDialog(url
					, null
					, 600
					, 400
					, CallBackManagedLinkSelection
					, null
					, "Managed Link");

		}












	}


	RadEditorCommandList["ManagedResource"] = function(commandName, editor, oTool) {

		theEditor = editor;
		var params = window.top.document.location.href.substring(window.top.document.location.href.indexOf("?"));

		if (!editor.GetSelectionHtml()) {
			editor.ShowDialog("/cognition/admin/pickers/RADresourcepicker.aspx"+params
					, null
					, 600
					, 400
					, CallBackManagedResource
					, null
					, "Managed Resource");
		}
		else if (editor.GetSelectionHtml() == "\r\n<P>&nbsp;</P>")
		{
			editor.ShowDialog("/cognition/admin/pickers/RADresourcepicker.aspx"+params
					, null
					, 600
					, 400
					, CallBackManagedResource
					, null
					, "Managed Resource");
		}
		else {	


					var url = "/cognition/admin/pickers/RADResourcepicker.aspx"+params+"&selection=y";
				//	alert("#"+editor.GetSelectionHtml()+"#");
					editor.ShowDialog(url
					, null
					, 600
					, 400
					, CallBackManagedResourceSelection
					, null
					, "Managed Resource");

		}

	}


	RadEditorCommandList["ManagedImage"] = function(commandName, editor, oTool) {

		theEditor = editor;
		var params = window.top.document.location.href.substring(window.top.document.location.href.indexOf("?"));

		if (!editor.GetSelectionHtml()) {
			editor.ShowDialog("/cognition/admin/photogallery/pickers/RADGalleryList.aspx"+params
					, null
					, 630
					, 500
					, CallBackManagedImage
					, null
					, "Managed Image2");
		}
		else if (editor.GetSelectionHtml() == "\r\n<P>&nbsp;</P>")
		{
			editor.ShowDialog("/cognition/admin/photogallery/pickers/RADGalleryList.aspx"+params
					, null
					, 630
					, 500
					, CallBackManagedImage
					, null
					, "Managed Image2");
		}
		else {	


					var url = "/cognition/admin/photogallery/pickers/RADGalleryList.aspx"+params+"&selection=y";
				//	alert("#"+editor.GetSelectionHtml()+"#");
					editor.ShowDialog(url
					, null
					, 630
					, 500
					, 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');
			}
		}
	}
}