var tinyMCEInitialized = false;

/*plugins : 'paste,fullscreen,sparkbb',*/
/*tinyMCE_GZ.init({
	plugins : 'fullscreen,sparkbb',
	themes : 'advanced',
	languages : 'en',
	disk_cache : false,
	debug : false
});*/
/* sparkbb_youtube,*/
/*plugins: "fullscreen, paste, sparkbb, " + plugins,
 			paste_create_paragraphs : false,
			paste_create_linebreaks : false,
			paste_use_dialog : true,
			paste_auto_cleanup_on_paste : true,
			paste_convert_middot_lists : false,
			paste_unindented_list_class : "unindentedList",
			paste_convert_headers_to_strong : true,
			paste_insert_word_content_callback : "convertWord",*/
/*function initTinyMCE(plugins, buttons){
	if(!tinyMCEInitialized){
		tinyMCE.init({
			mode : "none",
			theme : "advanced",
			plugins: "fullscreen, sparkbb,  " + plugins,
			theme_advanced_buttons1 : "fontsizeselect,fontselect,forecolor,bold,italic,underline,separator,justifyleft,justifycenter,justifyright, justifyfull,separator,code",
			theme_advanced_buttons2 : "bullist,numlist,outdent,indent,separator,undo,redo,separator,cut,copy,paste,separator,link,unlink,selectall,separator,fullscreen,image,sparkbb_quote,sparkbb_spoiler,sparkbb_media,sparkbb_extend,sparkbb_shorten",
			theme_advanced_buttons3 : buttons,
			verify_html : false,
			force_br_newlines: true,
			force_p_newlines: false,
			convert_newlines_to_brs: true,
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_path_location : "none",
			fullscreen_new_window : false,
			convert_fonts_to_spans : true,
			button_tile_map : true,
			entity_encoding: "raw",
			extended_valid_elements : "a[name|href|target|title|onclick],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],q",
			oninit : "refreshTinyMCE"
		});
	}
	tinyMCEInitialized=true;
}*/

var htmlAreaEnabled = false;

function focusTinyMCE(){
	if(document.getElementById('textEditor'))
		tinyMCE.execCommand('mceFocus',false,'textEditor'); 
}
function refreshTinyMCE(){
	if(tinyMCE && tinyMCE.activeEditor.getContent()){
		tinyMCE.activeEditor.setContent(tinyMCE.activeEditor.getContent());
	}
	setTinyMCE();
	htmlAreaEnabled=true;
	if(document.getElementById('useAdvEditor'))
		document.getElementById('useAdvEditor').checked = 'checked';
	unsetBB();
}

function unsetTinyMCE(){
	if(htmlAreaEnabled && document.getElementById('textEditor')){
		tinyMCE.execCommand('mceRemoveControl',false,'textEditor');
		htmlAreaEnabled=false;
		setBB();
	}	
}

function setTinyMCE(){
	if(!htmlAreaEnabled && document.getElementById('textEditor')){
		tinyMCE.execCommand('mceAddControl',false,'textEditor');
		htmlAreaEnabled=true;
		unsetBB();
	}
}

function setBB(){
	if(document.getElementById('bbEditor'))
		document.getElementById('bbEditor').style.display = 'block';
}

function unsetBB(){
	if(document.getElementById('bbEditor'))
		document.getElementById('bbEditor').style.display = 'none';
}


// AJAX Preview functionality
function advEditor_handleSuccess(o){
	div = document.getElementById('divPreview');			
	if(o.responseText !== undefined){
//		div.style.height=0;
		div.innerHTML = o.responseText; 
		document.getElementById('divPreviewLoading').style.display="none";
		var myAnim = new YAHOO.util.Anim('divPreview', {
  			opacity: { from:0, to: 1} 
			}, 0.5);
		document.getElementById('divPreview').style.display="block";
		myAnim.animate();
	}
}

function advEditor_handleFailure(o){
	div = document.getElementById('divPreview'); 
   	div.innerHTML = "An error occured while trying to load the preview. Please try again later.";
	document.getElementById('divPreviewLoading').style.display="none";
	var myAnim = new YAHOO.util.Anim('divPreview', {
 			opacity: { from:0, to: 1} 
		}, 0.5);
	myAnim.animate();
} 

function doPreview(form, name, aBID){
	var callback =
	{
	  success: advEditor_handleSuccess,
	  failure: advEditor_handleFailure
	};
	if(htmlAreaEnabled)
		tinyMCE.triggerSave();
	YAHOO.util.Connect.setForm(form);
	var postData = "TextAreaName="+name+"&aBID="+aBID; 
	div = document.getElementById('divPreview');			
	document.getElementById('divPreviewLoading').style.display="block";
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/preview.spark', callback, postData); 
}
