// tiny MCE -
//    jQuery plugin for accessible, unobtrusive WYSIWYG HTML editing
// v .1
// by Alton Crossley
// http://www.nogahidebootstrap.com/jtinymce/
// Free beer and free speech. Enjoy!
// The cool part is oh-so-simple
if(typeof mceType === "undefined"){
	mceType = "simple";
}

$.fn.tinymce = function(options){
    return this.each(function(){
        // I don't think the style declaration is not really used in this sense, but I left it anyway
        preString = "<div class='jqHTML_frame' style='width:"+$(this).css("width")+"px;height:" + ($(this).css("height")+20) + "px;'>";
        postString = "</div>";
        $(this).wrap(preString + postString);
        // this comes last to avoid IE7 bug
        toggleEditor(this.id);
    });
}
function toggleEditor(id) {
	if (!tinyMCE.get(id))
		tinyMCE.execCommand('mceAddControl', false, id);
	else
		tinyMCE.execCommand('mceRemoveControl', false, id);
}

var initMCE = {
	advanced : function(){
		tinyMCE.init({ mode : "none",
	       theme : "advanced",
	       plugins : "advhr,advlink,style,paste",
	       relative_urls : false,
	       remove_script_host : false,
	       document_base_url : "http://"+document.location.host+"/",
	       theme_advanced_layout_manager : "SimpleLayout",
	       theme_advanced_disable: "hr,",
	       theme_advanced_buttons1: "justifyleft,justifycenter,justifyright,justifyfull,separator,removeformat,separator,charmap,advhr,separator,styleprops,seperator,image,seperator,seperator,pasteword",
	       theme_advanced_buttons2: "styleselect,bold,italic,underline,separator,link,separator,bullist,numlist,outdent,indent,code,",
	       theme_advanced_buttons3: "",
	       theme_advanced_toolbar_location : "top",
	       theme_advanced_toolbar_align : "left",
		   file_browser_callback : "ibrowseCallback",
		   paste_auto_cleanup_on_paste : true,
		   paste_strip_class_attributes: "all",
		   paste_create_paragraphs: false,
		   paste_create_linebreaks: false,
	       content_css : "/includes/style/tinymce-content.css"
		});
		
	},
	simple : function(){
		tinyMCE.init({ mode : "none",
	       theme : "simple",
		   paste_auto_cleanup_on_paste : true,
		   paste_strip_class_attributes: "all",
		   paste_create_paragraphs: false,
		   paste_create_linebreaks: false,
	       content_css : "/includes/style/tinymce-content.css"
		});
	}
}

initMCE[mceType]();

function ibrowseCallback(field_name, url, type, win) {
	
	var host = window.location.host;
	
	url = 'http://' + host + '/modules/admin/includes/javascript/tiny_mce/plugins/ibrowser/index.cfm?editorType=mce&EDITOR_RESOURCE_TYPE=' + type;
	win_ref = win; // not sure why this works for IE, but it does.
	win_x = 700; win_y = 500;
	// adjust these variables to fit your desired window size
	cffmWindow = window.open( url, "", "width="+win_x+",height="+win_y+",left=20,top=20,bgcolor=white,resizable,scrollbars,menubar=0");
	if ( cffmWindow != null ) {
		cffmWindow.focus();
	}
}

