(function($) {
	
	var tinyMCE_count = 1;
	
	var tinyMCE_config_full = {
		mode: 'none',
		theme: 'advanced',
		theme_advanced_toolbar_location: 'top',
		theme_advanced_toolbar_align: 'left',
		theme_advanced_statusbar_location : 'bottom',
//		plugins : 'safari,spellchecker,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,imagemanager,filemanager',
		plugins: 'safari,inlinepopups,style,layer,table,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,'+ 
		         'searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras',
		theme_advanced_buttons1: 'bold,italic,underline,strikethrough,|,sub,sup,|,justifyleft,justifycenter,justifyright,justifyfull,|,forecolor,backcolor,|,styleselect,formatselect,fontsizeselect,|,preview,fullscreen',
		theme_advanced_buttons2: 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,|,help,|,code,styleprops,cleanup,visualchars,nonbreaking,template,pagebreak',
		theme_advanced_buttons3: 'tablecontrols,|,removeformat,visualaid,|,charmap,emotions,iespell,media,advhr,insertdate,inserttime',
		theme_advanced_buttons4: '',
		theme_advanced_resizing: true,
		file_browser_callback: 'fileBrowserCallBack',
	    relative_urls: false, 
		width: '100%'
	};
	
	var tinyMCE_config_mini = {
		mode: 'none',
		theme: 'simple',
		file_browser_callback: 'fileBrowserCallBack',
	    relative_urls: false 
	};
	
	var tinyMCE_config_flash = {
		mode: 'none',
		theme: 'advanced',
		theme_advanced_toolbar_location: 'top',
		theme_advanced_toolbar_align: 'left',
		theme_advanced_statusbar_location : 'bottom',
		plugins: 'safari,inlinepopups,layer,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,'+ 
                 'searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras',
		theme_advanced_buttons1: 'bold,italic,underline,strikethrough,|,sub,sup,|,justifyleft,justifycenter,justifyright,justifyfull,|,forecolor,backcolor,|,formatselect,fontsizeselect,|,preview,fullscreen',
		theme_advanced_buttons2: 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,|,help,|,code,styleprops,cleanup,visualchars,nonbreaking,template,pagebreak',
		theme_advanced_buttons3: 'removeformat,visualaid,|,charmap,emotions,iespell,media,advhr,insertdate,inserttime',
		theme_advanced_buttons4: '',
		theme_advanced_resizing: true,
		width: '100%',
//		cleanup_callback: 'flashCleanup',
		file_browser_callback: 'fileBrowserCallBack',
	    relative_urls: false, 
		inline_styles: false
	};
	
	$.tinymcegz = function() {
		tinyMCE_GZ.init({
			plugins: tinyMCE_config_full.plugins,
			themes: 'simple,advanced',
			languages: 'en',
			disk_cache: true,
			debug: false
		});
	}
	
	$.fn.tinymce = function(options) {
		return this.each(function() {
			// this should init tinyMCE and plugins used in all configurations
			if(!tinyMCE.settings) {
				tinyMCE_config_full.document_base_url = DIR_WS_SITE;
				tinyMCE_config_full.content_css = DIR_WS_SITE + 'includes/css/htmlarea_content.css';
				tinyMCE.init(tinyMCE_config_full);
			}
			
			if(options && options.flash) {
				tinyMCE_config_flash.document_base_url = DIR_WS_SITE;
				tinyMCE_config_flash.content_css = DIR_WS_SITE + 'includes/css/htmlarea_content.css';
				tinyMCE.settings = tinyMCE_config_flash;
			} else if(options && options.mini) {
				tinyMCE_config_mini.document_base_url = DIR_WS_SITE;
				tinyMCE_config_mini.content_css = DIR_WS_SITE + 'includes/css/htmlarea_content.css';
				tinyMCE.settings = tinyMCE_config_mini;
			} else {
				tinyMCE_config_full.document_base_url = DIR_WS_SITE;
				tinyMCE_config_full.content_css = DIR_WS_SITE + 'includes/css/htmlarea_content.css';
				tinyMCE.settings = tinyMCE_config_full;
			}
			
			tinyMCE.settings.content_css = DIR_WS_SITE + 'includes/css/htmlarea_content.css';
			
			if(!this.id) {
				this.id = '_tinymce_ctrl_'+tinyMCE_count;
				tinyMCE_count++;
			}
			tinyMCE.execCommand('mceAddControl', false, this.id);
		});
	}
	
	$.fn.commit = function(options) {
		return this.each(function() {
			var ed = tinymce.EditorManager.get(this.id);
			if(ed) ed.save();
		});
	}
	
	$.fn.untinymce = function(options) {
		return this.each(function() {
			var ed = tinymce.EditorManager.get(this.id);
			if(ed) ed.remove();
		});
	}
	
	$.fn.tinymcehide = function() {
		return this.each(function() {
			var ed = tinymce.EditorManager.get(this.id);
			if(ed)
				$('span#' + this.id + '_parent').hide();
		});
	}
	
	$.fn.tinymceshow = function() {
		return this.each(function() {
			var ed = tinymce.EditorManager.get(this.id);
			if(ed)
				$('span#' + this.id + '_parent').show();
		});
	}
	
	$.fn.tinymcetoggle = function() {
		return this.each(function() {
			var ed = tinymce.EditorManager.get(this.id);
			if(ed)
				$('span#' + this.id + '_parent').toggle();
		});
	}
	
	function flashCleanup(type, html) {
		switch(type) {
			case "get_from_editor":
				return html
					.replace(/<(\/?)strong/gi, '<$1b')
					.replace(/<(\/?)em/gi, '<$1i')
					.replace(/<(\/?)span/gi, '<$1b');
			case "insert_to_editor":
				break;
			case "submit_content":
				break;
			case "get_from_editor_dom":
				break;
			case "insert_to_editor_dom":
				break;
			case "setup_content_dom":
				break;
			case "submit_content_dom":
				break;
		}
	
		return html;
	}
})(jQuery);

function fileBrowserCallBack(field_name, url, type, win) {
	var connector = DIR_WS_SITE + 'tiny_mce/file_manager/file_manager.php';
	
	switch (type) {
		case "image":
			connector += "?type=img";
			break;
		case "media":
			connector += "?type=media";
			break;
		case "flash": //for older versions of tinymce
			connector += "?type=media";
			break;
		case "file":
			connector += "?type=files";
			break;
	}

	tinyMCE.activeEditor.windowManager.open({
		file: connector,
		title: 'File Browser',
		width: 700,
		height: 500,
		resizable: 'yes',
		inline: 'yes',
		close_previous: 'no'
	}, {
		window: win,
		input: field_name
	});
	
	return false;
}


