/**
 * $Id: editor_plugin_src.js 126 2006-10-22 16:19:55Z spocke $
 *
 * @author Moxiecode
 * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
 */

/* Import plugin specific language pack */
tinyMCE.importPluginLanguagePack('su3modeswitch');

var TinyMCE_su3modeswitchPlugin = {
	getInfo : function() {
		return {
			longname : 'Modeswitch',
			author : 'Moxiecode Systems AB',
			authorurl : 'http://tinymce.moxiecode.com',
			infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_save.html',
			version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
		};
	},

	/**
	* Simple ja advanced ovat tasot joita käytetään. simplellä piilotetaan valitut napit ja advanced näyttää ne
	*/
	initInstance : function(inst) {
		var mode = tinyMCE.getParam("su3_modeswitch", "advanced");

		inst.addShortcut('ctrl', 'm', 'lang_modeswitch_desc', 'su3modeswitch');

		var buttons = tinyMCE.getParam("su3_modeswitch_buttons", "", true, ',');

		if (buttons.length == 0)
			return false;

		if (mode == 'simple'){
			this.hideButtons(buttons);
		}
	},

	/**
	 * Returns the HTML contents of the save control.
	 */
	getControlHTML : function(cn) {
		switch (cn) {
			case "su3modeswitch":

			return tinyMCE.getButtonHTML(cn, 'lang_modeswitch_desc', '{$pluginurl}/images/modeswitch.gif', 'su3modeswitch');


		}

		return "";
	},

	/**
	 * Hides some buttons
	 */
	execCommand : function(editor_id, element, command, user_interface, value) {
		// Handle commands
		switch (command) {
			case "su3modeswitch":
				tinyMCE.isNotDirty = true;

				var buttons = tinyMCE.getParam("su3_modeswitch_buttons", "", true, ',');

				if (TinyMCE_Engine.su3modeswitch == 'simple'){
					this.showButtons(buttons);
				}
				else{
					this.hideButtons(buttons);
				}


				return true;
		}
		// Pass to next handler in chain
		return false;
	},

	/**
	 * Varsinainen buttoneiden piilotus suoritetaan
	 */
	hideButtons : function(buttons) {

		i = 0;
		while (document.getElementById('mce_editor_'+i+'_su3save')){
			j = 0;
			while (buttons[j]){
				if (document.getElementById('mce_editor_'+i+'_'+buttons[j]))
					document.getElementById('mce_editor_'+i+'_'+buttons[j]).style.display = 'none';
				j++;
			}
			tinyMCE.switchClass('mce_editor_'+i+'_su3modeswitch', 'mceButtonCustom');
			i++;
		}

		var classElements = new Array();
		classElements = this.getElementsByClass('mceSeparatorLine');

		for(i=0; i<classElements.length; i++) {
			classElements[i].style.display = 'none';
		}


		TinyMCE_Engine.su3modeswitch = 'simple';

		return "";
	},

	/**
	 * Varsinainen buttoneiden palauttaminen suoritetaan
	 */
	showButtons : function(buttons) {

		i = 0;
		while (document.getElementById('mce_editor_'+i+'_su3save')){
			j = 0;
			while (buttons[j]){
				if (document.getElementById('mce_editor_'+i+'_'+buttons[j]))
					document.getElementById('mce_editor_'+i+'_'+buttons[j]).style.display = 'inline';

				j++;
			}
			tinyMCE.switchClass('mce_editor_'+i+'_su3modeswitch', 'mceButtonCustom');
			i++;
		}

		var classElements = new Array();
		classElements = this.getElementsByClass('mceSeparatorLine');

		for(i=0; i<classElements.length; i++) {
			classElements[i].style.display = 'inline';
		}

		TinyMCE_Engine.su3modeswitch = 'advanced';

		return "";
	},

	/* Hakee elementit luokan mukaan */
	getElementsByClass : function(className) {
	var all = document.all ? document.all : document.getElementsByTagName('*');
		var elements = new Array();

		for (var e = 0; e < all.length; e++)
			if (all[e].className == className)
		    	elements[elements.length] = all[e];

		return elements;
	},

	handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
		if (tinyMCE.getParam("fullscreen_is_enabled")) {
			tinyMCE.switchClass(editor_id + '_modeswitch', 'mceButtonDisabled');
			return true;
		}

		if (tinyMCE.getParam("save_enablewhendirty")) {
			var inst = tinyMCE.getInstanceById(editor_id);

			if (inst.isDirty()) {
				tinyMCE.switchClass(editor_id + '_modeswitch', 'mceButtonNormal');
				return true;
			}

			tinyMCE.switchClass(editor_id + '_modeswitch', 'mceButtonDisabled');
		}

		return true;
	}
};

tinyMCE.addPlugin("su3modeswitch", TinyMCE_su3modeswitchPlugin);
