// 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
$.fn.tinymce = function(options) 
{
    return this.each(function() 
    {
        preString = "<div class='jqHTML_frame' style='width:"+$(this).css("width")+"px;height:" + ($(this).css("height")+20) + "px;'><div>";
        postString = "</div><div class='jqHTML_link' style='float:right' onclick=\"toogleEditorMode('" + this.id + "');\">HTML</div></div>";
        $(this).wrap(preString + postString);
        alert(this.id + '\n' + $(this).html() + '\n');
        alert(this.id + '\nWidth:' + $(this).css("width") + '\nHeight:' + $(this).css("height"));
    });
}

// this is where we decide the toggle of 'on' (true) or 'off' (false)
// init an array to keep each id's state seperate
var tinyMCEmode = new Array();
// this is called by the click method
function toogleEditorMode(sEditorID) 
{
    if(tinyMCEmode[sEditorID]) 
    {
        try 
        {
            tinyMCE.removeMCEControl(tinyMCE.getEditorId(sEditorID));
            tinyMCEmode[sEditorID] = false;
        } 
        catch(e) 
        {
            alert( "REMOVE:" + sEditorID + ':\n' + e.message);
        }
    } 
    else 
    {
        try 
        {
            tinyMCE.addMCEControl(document.getElementById(sEditorID), sEditorID);
            tinyMCEmode[sEditorID] = true;
        } 
        catch(e) 
        {
            alert( "ADD:" + sEditorID + ':\n' + e.message);
        }
    }    
}

function removeAllMCE()
{
    for (var i in tinyMCEmode)
    {
        if(tinyMCEmode[i])
        {
            tinyMCE.removeMCEControl(tinyMCE.getEditorId(i));
            tinyMCEmode[i] = false;
        }
    }
    initMCE();
}

function initMCE()
{        
	
	tinyMCE_GZ.init({
		//plugins : 'advhr,advimage,advlink,autosave,bbcode,contextmenu,directionality,emotions,fullscreen,iespell,inlinepopups,insertdatetime,layer,media,nonbreaking,noneditable,pagebreak,paste,preview,print,save,searchreplace,style,table,template,visualchars,xhtmlxtras',
		plugins : 'media,paste',
		themes : 'advanced',
		languages : 'en',
		disk_cache : true,
		debug : false
	});
	
    tinyMCE.init({ 
	   mode : "textareas",
       editor_selector : "wysiwyg",
	   theme : "advanced",
       plugins : "media,paste",
	   relative_urls : false,
       theme_advanced_layout_manager : "SimpleLayout",
       theme_advanced_disable: "hr,",
       theme_advanced_buttons1: "justifyleft,justifycenter,justifyright,justifyfull,separator,bold,italic,underline,separator,image,link,media,separator,bullist,numlist,charmap,pasteword,cleanup",
       theme_advanced_buttons2: "",
       theme_advanced_buttons3: "",
       theme_advanced_toolbar_location : "top",
       theme_advanced_toolbar_align : "left"/*,
	   valid_elements : ""
	+"a[href|name|title|target|type],"
	//+"abbr[class|id|lang|title],"
	//+"acronym[class|id|lang|title],"
	//+"big[class|id|lang|title],"
	+"blockquote,br,center,"
	//+"code[class|id|lang|title],"
	+"div[align<center?justify?left?right|style],"
	//+"dl[class|id|lang|style|title],"
	//+"dt[class|id|lang|style|title],"
	+"em/i[class|id|lang|style|title],"
	+"embed[type|src|width|height],"
	+"h1,h2,h3,h4,h5,h6,"
	//+"hr,"
	+"img[alt|height|name|src|title|width],"
	//+"ins[cite|class|id|lang|style|title],"
	+"li,ol,p[align<center?justify?left?right],"
	+"object[classid|codebase|width|height],param[name|value],"
	//+"pre/listing/plaintext/xmp[align|class|id|lang|style|title|width],"
	//+"small[class|id|lang|style|title],"
	+"span,strike,strong/b,sub,sup,"
	//+"table[align<center?left?right|bgcolor|border|cellpadding|cellspacing|class"
	//  +"|height|id|lang|rules|style|summary|title|width],"
	//+"tbody[align<center?char?justify?left?right|char|class|charoff|id"
	//  +"|lang|style|title|valign<baseline?bottom?middle?top],"
	//+"td[abbr|align<center?char?justify?left?right|axis|bgcolor|char|charoff|class"
	//  +"|colspan|dir<ltr?rtl|headers|height|id|lang|nowrap<nowrap|rowspan|scope<col?colgroup?row?rowgroup"
	//  +"|style|title|valign<baseline?bottom?middle?top|width],"
	//+"th[abbr|align<center?char?justify?left?right|axis|bgcolor|char|charoff|class"
	//  +"|colspan|dir<ltr?rtl|headers|height|id|lang|nowrap<nowrap|rowspan|scope<col?colgroup?row?rowgroup"
	//  +"|style|title|valign<baseline?bottom?middle?top|width],"
	//+"tr[abbr|align<center?char?justify?left?right|bgcolor|char|charoff|class"
	//  +"|rowspan|dir<ltr?rtl|id|lang|style"
	//  +"|title|valign<baseline?bottom?middle?top],"
	+"u, ul[class|compact<compact|type|style]"*/
	});
}

initMCE();
