// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


var shown = {"action_1": false, "action_2": false, "action_3": false};
var action_to_instance = {"action_1": "message_body_1", "action_2": "message_body_2", "action_3": "message_body_3"};

function toggle_action(action) {
  $$('.actions').each( function(element) { element.hide(); } );
 Effect.toggle(action, 'blind', { duration: 0.2, afterFinish: function() {
			if (!shown[action]) new nicEditor({iconsPath : '/images/nicEditorIcons.gif', buttonList: ['bold', 'italic', 'underline', 'strikeThrough', 'color', 'ol', 'ul', 'fontSize', 'fontFamily', 'fontFormat']}).panelInstance(action_to_instance[action]);
			shown[action] = true;
			$(action).scrollIntoView(true);
  		}
	});
  return false;
}

// enable rich text editor on all .richtext elements
function enable_nic_editor() {

	  $$('.richtext').each( function(element) { if (element.id) {
			new nicEditor({iconsPath : '/images/nicEditorIcons.gif', buttonList: ['bold', 'italic', 'underline', 'strikeThrough', 'color', 'ol', 'ul', 'fontSize', 'fontFamily', 'fontFormat']}).panelInstance(element.id);
		}
	 });
}

function enable_nic(name) {

	  $$(name).each( function(element) { if (element.id) {
			new nicEditor({iconsPath : '/images/nicEditorIcons.gif', buttonList: ['bold', 'italic', 'underline', 'strikeThrough', 'color', 'ol', 'ul', 'fontSize', 'fontFamily', 'fontFormat']}).panelInstance(element.id);
		}
	 });
}


var _ni= null;
function do_edit_textblock(ide){
	Element.show(ide+'_edit_div');
	Element.hide(ide)
	_ni = new nicEditor({iconsPath : '/images/nicEditorIcons.gif', buttonList: ['bold', 'italic', 'underline', 'strikeThrough', 'color', 'ol', 'ul', 'fontSize', 'fontFamily', 'fontFormat']}).panelInstance(ide+"_edit_");
}


function do_edit_feedback(ide){
	Element.show(ide+'_edit_div');
	_ni = new nicEditor({iconsPath : '/images/nicEditorIcons.gif', buttonList: ['bold', 'italic', 'underline', 'strikeThrough', 'color', 'ol', 'ul', 'fontSize', 'fontFamily', 'fontFormat']}).panelInstance(ide+"_edit_");
}

function do_cancel_feedback(ide){
	Element.hide(ide+'_edit_div');
}



function do_save_textblock(ide) {
	new Ajax.Request("/admin/save_textblock", {
	  method: 'post',
	  postBody: 'name='+ide+'&body='+encodeURIComponent(_ni.nicInstances[0].getContent()),
	  onSuccess: function(transport) {
		Element.hide(ide+'_edit_div');
		Element.show(ide)
	  }
	} );
}
Event.observe(window, 'load', enable_nic_editor );
