//********************************
//** Load the language file | 加载语言文件
//********************************
tinyMCEPopup.requireLangPack();

//********************************
//**The actions in plugin dialogue   |    该插件对话框中的行为
//********************************
var TinyCodeDialog = {

	//--------------------------------
	//-- Initialization (what happen when this plugin button clicked)   |   初始化 （该插件按钮被按下之后）
	//--------------------------------
	init : function() 
	{
		//--------------------------------
		//-- Get arguments passed from plugin to window.  |   获取传递过来的参数
		//--------------------------------
		var editor_content = tinyMCEPopup.getWindowArg('editor_content');
		var editor_options = tinyMCEPopup.getWindowArg('editor_options');

		//--------------------------------
		//-- Check to see if any content was passed to window   |   看看是不是真的有待编辑内容传递过来
		//--------------------------------
		if(editor_content != undefined) 
		{
			//--------------------------------
			//-- select form and place snippet code into code editor window (meanwhile, unescape < and >)  |    如果真有，则放进代码窗口 （同时将 TinyMCE 强行转化的 &lt; &gt; 转换回 < 和 >）
			//--------------------------------
			var f = document.forms[0];
			document.getElementById("tinycode_code").value=editor_content.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/\uFF1C/g,'<').replace(/\uFF1E/g,'>');
			
			//--------------------------------
			//-- Check for brush option and auto select choosen language  |   检查 brush 参数，选中该语言
			//--------------------------------
			if(editor_options['brush']) 
			{
				for(var i = 0; i < f.tinycode_language.options.length; i++) 
				{
					if(f.tinycode_language.options[i].value == editor_options['brush']) 
					{
						f.tinycode_language.selectedIndex = i;
						break;
					}
				}
			}
  
  			if(editor_options['first-line']) 
			{
				f.tinycode_firstline.value = editor_options['first-line'];
			}

			/*
			if(editor_options['gutter'] && editor_options['gutter'] == 'false') 
			{
				f.tinycode_nogutter.checked = 'true';
			}
			
			if(editor_options['light'] && editor_options['light'] == 'true') 
			{
				f.tinycode_light.checked = 'true';
			}
			
			if(editor_options['collapse'] && editor_options['collapse'] == 'true') 
			{
				f.tinycode_collapse.checked = 'true';
			}
			
			if(editor_options['fontsize']) 
			{
				f.tinycode_fontsize.value = editor_options['fontsize'];
			}
			
			if(editor_options['highlight']) 
			{
				f.tinycode_highlight.value = editor_options['highlight'].replace(/[\[\]']+/g,'');
			}
			
			if(editor_options['html-script'] && editor_options['html-script'] == 'true') 
			{
				f.tinycode_html_script.checked = 'true';
			}
			
			if(editor_options['toolbar'] && editor_options['toolbar'] == 'false') 
			{
				f.tinycode_hide_toolbar.checked = 'true';
			}
			*/
		}
	},

	insert : function() 
	{
		var f = document.forms[0], textarea_output, options = '', replace_element, hasSibling;

		//--------------------------------
		//-- Get replacement argument to determine return type  (insert or replace)   |    是插入还是替换
		//--------------------------------
		replace_element = tinyMCEPopup.getWindowArg('replace_content');

		//--------------------------------
		//-- For insert situation (not replace situation) , if no code just return.   |   对于添加情形（非修改情形）如果没有代码返回，则不动作
		//--------------------------------
		/*
		if(f.tinycode_code.value == '' && replace_element==false) 
		{
			tinyMCEPopup.close();
			return false;
		}
		*/

		//--------------------------------
		//-- Whether there is further elements after current code snipet   |    当前代码片段后面是否还有内容，如果没有，则在代码片段后插入 <p>&nbsp;</p> 否则 <pre>...</pre> 会困住当前光标无法进入下面的内容
		//--------------------------------
		hasSibling = tinyMCEPopup.getWindowArg('hasSibling');

		//--------------------------------
		//-- Get first line no parameter    |    获取第一行行号参数
		//--------------------------------
		if(f.tinycode_firstline.value != '') 
		{
			var linenumber = parseInt(f.tinycode_firstline.value);
			options += 'first-line: ' + linenumber + '; ';
		}


		/*
		if(f.tinycode_nogutter.checked) 
		{
			options += 'gutter: false; ';
		}
		
		if(f.tinycode_light.checked) 
		{
			options += 'light: true; ';
		}
		
		if(f.tinycode_collapse.checked) 
		{
			options += 'collapse: true; ';
		}
		
		if(f.tinycode_fontsize.value != '') 
		{
			var fontsize=parseInt(f.tinycode_fontsize.value);
			options += 'fontsize: ' + fontsize + '; ';
		}
		
		if(f.tinycode_highlight.value != '') 
		{
			options += 'highlight: [' + f.tinycode_highlight.value + ']; ';
		}
		
		if(f.tinycode_html_script.checked) 
		{
			options += 'html-script: true; ';
		}
		
		if(f.tinycode_hide_toolbar.checked) 
		{
			options += 'toolbar: false; ';
		}
		*/

		//--------------------------------
		//-- Create returned code  |    构造返回的代码
		//--------------------------------
		if (f.tinycode_code.value == '')
		{
			textarea_output = '';
		}
		else
		{
			textarea_output = '<pre class="brush: ';
			textarea_output += f.tinycode_language.value + '; ' + options + '">';
			//textarea_output +=  tinyMCEPopup.editor.dom.encode(f.tinycode_code.value);
			textarea_output += f.tinycode_code.value.replace(/</g,'\uFF1C').replace(/>/g,'\uFF1E');
			textarea_output += '</pre> '; 	/* note space at the end, had a bug it was inserting twice? */
			
			if (hasSibling==false)
			{
				textarea_output += '<p>&nbsp;</p>';
			}
		}
		
		//--------------------------------
		//-- Check for replace_element option value and return output accordingly   |    按替换还是插入这个选项，进行替换/插入
		//--------------------------------
		if(replace_element == false) 
		{
			tinyMCEPopup.editor.execCommand('mceInsertContent', false, textarea_output);
		} 
		else 
		{
			tinyMCEPopup.editor.execCommand('mceReplaceContent', false, textarea_output);
		}
		
		//--------------------------------
		//-- Close popup window   |   结束
		//--------------------------------
		tinyMCEPopup.close();
	}
};

//********************************
//**Initialization  |  初始化
//********************************
tinyMCEPopup.onInit.add(TinyCodeDialog.init, TinyCodeDialog);