/* This is where we store custom code for use with the WYSIWYG editor

 TODO - Make these real themes, instead of doing this... */

/*
	generate_wysiwyg_config( differences )

	Builds a wysiwyg config description from the default config 
	specification and an object specifying any additions and changes.

	The difference object overwrites the attribute fully.
*/

function generate_wysiwyg_config(differences) {
	var config = {};
	for ( var i in base_wysiwyg_config_spec ) {
		config[i] = base_wysiwyg_config_spec[i];
	}
	if ( differences != null  ) {
		for ( var i in differences ) {
			config[i] = differences[i];
		}
	}
	return config;
}


/* 
   This is an object describing the default configuration of the wysiwyg 
   editor.
*/

var base_wysiwyg_config_spec = {
	mode : "textareas",
	theme : "advanced",
	content_css : "/myresources/page/page.css",
	relative_urls: false,
	document_base_url : '/',
	width : "100%",
	styles : "none=blank",
	editor_selector : "html",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_path_location : "bottom",					
  	plugins : "table,advimage,insertdatetime,contextmenu,paste,print,miFilesbox,miSearch,miBackgroundColour,iespell,searchreplace,miAbout,miClipArt",
	theme_advanced_buttons1_add : "fontselect,fontsizeselect,iespell,print",
	theme_advanced_buttons2_add : "separator,forecolor,backcolor,miBackgroundColour,separator,miAbout",
	theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
	theme_advanced_buttons3_add_before : "tablecontrols,separator",
	theme_advanced_buttons3_add : "separator,insertdate,inserttime,separator,miFilesbox,miSearch,miClipArt",
	theme_advanced_disable : "styleselect,help",
	theme_advanced_resize_horizontal : true,
	theme_advanced_resizing : true,
	//theme_advanced_fonts:"Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sand;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats",
	extended_valid_elements : "a[name|href|target|title|onclick|real_href],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
	theme_advanced_styles : "Blank=blank",
	table_styles : "Blank=blank",
	table_cell_styles : "Blank=blank",
	table_row_styles : "Blank=blank",
	setupcontent_callback : "initializeEditor"
};

// default wysiwyg configuration
var mi_wysiwyg_default = generate_wysiwyg_config();

// different configuration for the page editor wysiwyg
var page_editor_differences =  {
  	plugins : "table,insertdatetime,contextmenu,paste,print,miFilesbox,miSearch,miBackgroundColour,iespell,searchreplace,miAbout,miPageLink,miClipArt",
	theme_advanced_buttons3_add : "separator,insertdate,inserttime,separator,miFilesbox,miSearch,miClipArt,miPageLink"
};

// portfolio page editor wysiwyg configuration
var mi_wysiwyg_page = generate_wysiwyg_config(page_editor_differences);


function initializeEditor() {
	getWYSIWYGHelper().setPageStyle();
}

// NOTE - ADAM - Not sure if the following note is still true, it was when we used HTMLArea but I haven't tested this since
// NOTE - ADAM - When inserting HTML e.g. a div it is important to make sure something editable appears after it at least an &nbsp. Mozilla will not let you move the cursor out of the div / span unless there is further editable content for it to move in to, this is only a problem if the thing you inserted is the last thing on the page.

function WYSIWYGHelper() {
	
	this.info = 'You have found a WYSIWYGHelper!';
	
	this.setPageStyle = function(style)	{
		// TODO - ADAM - This should perhaps be incorporated in to the editor toolbar as a plugin, but it means the editor plugin has to be given the information about avaialble page styles or provide the page styles itself
		tinyMCE.getInstanceById('mce_editor_0').getWin().document.body.className = (style) ? style : getElementsByClassName(document, 'select', 'mi_wysiwyg_helper_setStyle')[0].value;
	};
	
	this.insertContent = function(jsonParams)	{
		// TODO - This is not really insert content it should simply translate any badgerfish to XML
		eval(jsonParams);
		var html;
		if(jsonParams.a && jsonParams.a.img)	{
			// insert thumb		
			html = '<a href="' + jsonParams.a.$href + '"><img src="' + jsonParams.a.img.$src + '" alt="' + jsonParams.a.img.$alt + '" /></a>';	
			tinyMCE.execCommand("mceInsertContent", false, html);
		} else if (jsonParams.img) {
			// insert image
			html = '<img src="' + jsonParams.img.$src + '" alt="' + jsonParams.img.$alt + '" />';
			tinyMCE.execCommand("mceInsertContent", false, html);
		};
	};
	
	this.insertLink = function(jsonParams)	{
		eval(jsonParams);
		if (jsonParams.a.$href) {
			// surround the selection otherwise insert a new link
			if (tinyMCE.getInstanceById('mce_editor_0').selection.getSelectedHTML()) {
				tinyMCE.execCommand('mceReplaceContent',false,'<a href="' + jsonParams.a.$href + '">{$selection}</a>');	
			} else {
				tinyMCE.execCommand("mceInsertContent", false, '<a href="' + jsonParams.a.$href + '">' + jsonParams.a.$ + '</a>');
			}
		}
	};
	
};


function resizeEditor(sEditor, iX, iY) {
	var oEditor = document.getElementById(sEditor);
	if (oEditor) {
		var iCurrentX = parseInt(oEditor.style.width);
		var iCurrentY = parseInt(oEditor.style.height);
    	oEditor.style.width = eval(iCurrentX+iX);
    	oEditor.style.height = eval(iCurrentY+iY);
	} else {
		alert('Couldn\'t resize editor named: ' + sEditor);
	}
};


function reduceEditor ()	{
	resizeEditor('mce_editor_0', -320, 0);
};

function findWYSIWYGHelper(win)   {
   if (win.WYSIWYGHelperInstance != null) {return win.WYSIWYGHelperInstance;}
   if (win.length > 0) {
        for (var i=0; i<win.length; i++)    {
            var theWYSIWYGHelper = findWYSIWYGHelper(win.frames[i]);
            if (theWYSIWYGHelper != null) {return theWYSIWYGHelper;}
      };
   };
   return null;
};

function getWYSIWYGHelper()   {
	var theWYSIWYGHelper = findWYSIWYGHelper(this.top);
	if (theWYSIWYGHelper == null) {
		if (typeof(this.top.opener) != "undefined") {
			if (this.top.opener != null) {
				theWYSIWYGHelper = findWYSIWYGHelper(this.top.opener.top);
			};
		};
	};
	return theWYSIWYGHelper;
};