/**
 * Rich-text.js
 * @fileOverview This file is responsible for adding rich-text ability to a page
 * @author LBi - http://www.lbi.com/en
 * @requires jQuery Core 1.3.2 - http://www.jquery.com/
 * @requires jQuery Tiny MCE 3.2.6 - http://tinymce.moxiecode.com/
 */

/*jslint eqeqeq: true, undef: true */
/*global $, jQuery, window, document, swfobject, confirm, prompt, tinyMCE  */


/**
 * @namespace Root namespace for holding all objects created for BT Tradespace.
 */
BTT = window.BTT || {};



/**
 * Object that stores functions for Rich Text editing
 * @namespace Object that stores functions for Rich Text editing
 * @member BTT
 */
BTT.RichText = {

	applyRichText: function(){

		var richTextClass = "JS_richText";
		//var pathToRichTextJS = '/assets/plugins/jquery.tinymce/jquery.tinymce.js'; // causing AJAX to fail, (cannot find tinymce variable)
		
		var pathToRichTextJS = BTT.assets + '/plugins/jquery.tinymce/tiny_mce.js';
			//pathToRichTextCSS = BTT.assets + '/css/rich-text.css';
		
		$('textarea.' + richTextClass).each(function(i){
			
			$(this).tinymce({
				theme : "advanced",
				skin : "tradespace",
				script_url : pathToRichTextJS,
				//content_css : pathToRichTextCSS,

				//plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
				plugins : "paste",
				
				//relative urls turned off
				relative_urls : false,
				remove_script_host : false,

                // nb. MCE has no out-of-the-box UI for dl, code, q, cite elements.
                inline_styles: false,
                valid_elements: "a[href],p,h2,h3,h4,h5,h6,ul,ol,li,dl,dt,dd,blockquote,pre,em/i,strong/b,u,sub,sup",
				theme_advanced_blockformats : "p,h2,h3,h4,h5,h6,blockquote,dt,dd,pre",
				theme_advanced_buttons1: "bold, italic, underline, |, bullist, numlist, |, link, unlink, |, formatselect",
				theme_advanced_buttons2: "",
				theme_advanced_buttons3: "",
				theme_advanced_buttons4: "",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				
				// Validate the richtext updates with jQuery validation routines
				setup : function(richTextArea) {
					 var timer;
					 
					 function focusTextarea() {
						 // get 'this' instance of the richtext editor
						 var plainTextArea = $('#' + $(richTextArea).attr('id'));
						 tinyMCE.triggerSave();					
						 plainTextArea.valid();
					 }
					 
					 // delay the number of times we validate the updates in the editor
					 richTextArea.onKeyUp.add(function(){
						 clearTimeout(timer);
						 timer = setTimeout(focusTextarea, 500);
					 });
				 }
			});
		});
	},

	init: function(){
		BTT.RichText.applyRichText();
	}

};




/* Initialise page when the DOM is ready
-------------------------------- */
$(document).ready(function () {
	BTT.RichText.init();
});



