/* <script> */
// if console is not defined, e.g., Firebug console is not enabled or Non-Firefox browser  
if (typeof console == 'undefined') {  
    var console = {};  
    console.log = function(msg) {  
        return;  
    };  
}

var wysiwyg_mode; // 'on' or 'off'

/* ################################################################ */
	function wysiwyg_init( selector, class_or_id, for_comments ) {
/* ################################################################ */

		wysiwyg_init_tinymce(selector, class_or_id, for_comments);

		$(document).ready(function(){
			if ( selector.toLowerCase() == 'id' ) {
				// USE TEXTAREA_ID AS SELECTOR
				$('#'+class_or_id).autogrow('disable'); // otherwise this conflicts with TinyMCE
				tinyMCE.execCommand('mceAddControl', false, class_or_id);
			} else {
				// USE TEXTAREA_CLASS AS SELECTOR
				$('textarea.'+class_or_id).each(function(i){
					$(this).autogrow('disable'); // otherwise this conflicts with TinyMCE
					if ( $(this).attr('id').length == 0  ) {
						$(this).attr({ id : 'wysiwyg-'+i}); // only add an id if DNE
					}
					tinyMCE.execCommand('mceAddControl', false, this.id);
//					$('#'+this.id).before("<div id='wysiwyg_code_quicktags'>quicktags</div>");
				});
			}
	
			wysiwyg_make_toggle_links(selector, class_or_id);
			
			$('.toggleWYSIWYG').click(function(){
				toggleWYSIWYG(this, tinyMCE); //txtareaID, linkID);
			});
	
		});
	}
	

/* ################################################################ */
	function wysiwyg_init_tinymce( selector, class_or_id, for_comments ) {
/* ################################################################ */

		if (class_or_id == null) return;
		if ( for_comments ) {
			callback = 'wysiwyg_onchange_comment';
		} else {
			callback = 'wysiwyg_onchange';
		}
			
			
		if ( selector.toLowerCase() == 'id' ) {
			console.log('using ID: '+class_or_id);
			// USE TEXTAREA_ID AS SELECTOR
			tinyMCE.init({
				mode : 'none',
				elements : class_or_id,
				onchange_callback : callback,
				width : "100%",
content_css : "http://www.malandragem.net/blog/themes/stardust/stylesheets/screen.css",
theme : "advanced",
theme_advanced_buttons1 : "bold,italic,underline,|,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,|,bullist,numlist,blockquote,link,unlink,undo,redo,|,formatselect",
theme_advanced_buttons2 : "",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
theme_advanced_resize_horizontal : false,
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]",
convert_urls : false			});


			$(document).ready(function(){
				$('#body').change(function(){
					tinyMCE.updateContent('body');
				});

			});

	
		} else {
			console.log('using TEXTAREA: '+class_or_id);
			// USE TEXTAREA_CLASS AS SELECTOR
			tinyMCE.init({
				mode : 'none', // 'textareas'
				editor_selector : class_or_id,
				onchange_callback : callback,
				width : "100%",
content_css : "http://www.malandragem.net/blog/themes/stardust/stylesheets/screen.css",
theme : "advanced",
theme_advanced_buttons1 : "bold,italic,underline,|,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,|,bullist,numlist,blockquote,link,unlink,undo,redo,|,formatselect",
theme_advanced_buttons2 : "",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
theme_advanced_resize_horizontal : false,
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]",
convert_urls : false			});

			// when textarea contents change (in CODE EDIT mode), push contents back to TinyMCE
//			$('textarea.'+class_or_id).change( tinyMCE.activeEditor.setContent( $(this).val() ) );
		}
		
		
	} // end function
	
/* ################################################################ */
	function wysiwyg_onchange(inst) {
/* ################################################################ */

		if ( typeof unplucked_enabled !== "undefined") {
			 if (unplucked_enabled == true )
				setConfirmUnload;
		}
		// pass value to textarea so that the PREVIEW button works correctly
		$("#"+inst.editorId).val(tinyMCE.activeEditor.getContent());
	}

/* ################################################################ */
	function wysiwyg_onchange_comment() {
/* ################################################################ */
		// pass value to textarea so that the PREVIEW button works correctly
		$("textarea.wide").val(tinyMCE.activeEditor.getContent());
	}
	
	
/* ################################################################ */
	function wysiwyg_make_toggle_links(which, class_or_id) {
/* ################################################################ */
		// select either by class or by id
		// for each textarea, add a link immediately above.

		if (class_or_id == null) return;
		
		if ( which.toLowerCase() == 'id' ) {
			// USE TEXTAREA_ID AS SELECTOR
			
			if ( class_or_id.indexOf(',') > 0 ) {
				// multiple IDs (comma-delimited).
				// split at commas and act on each separately

				idAr = class_or_id.split(',');
				
				for ( key in idAr ) {
					// loop through IDs
					//console.log('idAr: '+idAr[key]);
					$('textarea#'+idAr[key]).each(function(){ wysiwyg_make_toggle_link(this) });
				}
			} else {
				// single ID
				$('textarea#'+class_or_id).each(function(){ wysiwyg_make_toggle_link(this) });
			}
		} else {
			// USE TEXTAREA_CLASS AS SELECTOR
			$('textarea.'+class_or_id).each(function(){ wysiwyg_make_toggle_link(this) });

		}
		
	} // end function
	
/* ################################################################ */
	function wysiwyg_make_toggle_link(e) {
/* ################################################################ */
		// adds link to toggle WYSIWYG mode
		// also adjusts CSS for textarea label to collapse vertical gap
		// also sets a 'toggled-on' class to show that wysiwyg mode is enabled
		
		var theId = e.id;
		var theName = e.name;

		// make the link
		$(e).before( "<ul id='toggleWYSIWYG'>" +
					 "		<li><a href='javascript:;' class='toggleWYSIWYG' id='toggle-off-"+theId+"'><pre>Code</pre></a></li>" +
					 "		<li><a href='javascript:;' class='toggleWYSIWYG selected' id='toggle-on-"+theId+"'>WYSIWYG</a></li>" +
					 "</ul><br style='clear:both;width:100%;'>" );
//		$(e).before( "<a href='javascript:;' class='toggleWYSIWYG toggled-on' id='toggle-"+theId+"'>&raquo; Code View</a><br style='clear:both;width:100%;'>" );

		// fix the textarea's label style
		$('label[for="'+theId+'"]').addClass('labelBeforeToggleWYSIWYG'); // apply class to the textarea's label to collapse vertical gap

	} // end function


/* ################################################################ */
	function toggleWYSIWYG(linkEl, tiny) { //textareaId, LinkID) {
/* ################################################################ */
//console.log(linkEl);

		linkID = $(linkEl).attr('id');
		if ( linkID.substr(0,10) == 'toggle-on-' ) {
			textareaID = linkID.substr( 10 ); // strip 'toggle-on-'
		} else {
			textareaID = linkID.substr( 11 ); // strip 'toggle-off-'
		}

//console.log( linkEl.id );
//console.log( textareaID );

		// already selected ??
		if ( $('#'+linkID).hasClass('selected') ) {
			console.log('already selected...');
			return false;
		}

		clickedId = linkID; //$('#'+LinkID);
				
		if ( clickedId.indexOf('toggle-on-') != -1 ) {
			// TOGGLE WYSIWYG ON
console.log('toggle wysiwyg ON');
			otherId = clickedId.replace('toggle-on-', 'toggle-off-');
			// hide code wrapper div (for quick-tag buttons)
			wysiwyg_mode = 'on';
//			$('#wysiwyg_code_quicktags').show();
		} else {
			// TOGGLE WYSIWYG OFF
console.log('toggle wysiwyg OFF');
			otherId = clickedId.replace('toggle-off-', 'toggle-on-');
			// show code wrapper div (for quick-tag buttons)
			wysiwyg_mode = 'off';
//			$('#wysiwyg_code_quicktags').hide();
//			$('#'+textareaID).autogrow('enable');
		}

/*
		if (wysiwyg_mode == 'on') {
			$('#'+textareaID).autogrow('disable');
		} else {
			$('#'+textareaID).autogrow('enable');
		}
*/		
		$('#'+linkID).addClass('selected');
		$('#'+otherId).removeClass('selected');
		$('#'+otherId).addClass('shouldbeoff');
		tinyMCE.execCommand('mceToggleEditor', false, textareaID);
//		tiny.execCommand('mceToggleEditor', false, 'caption');
		$('#'+linkID).blur();

	} // end function 
