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

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

	//--------------------------------
	//-- Initialization (what happen when this plugin button clicked)   |   初始化 （该插件按钮被按下之后）
	//--------------------------------
	init : function() 
	{
		//--------------------------------
		//-- Get the content of current node passed from plugin to window.  |   获取传递过来的当前节点的内容（以便从中解析引用代码）
		//--------------------------------
		var current_node_content = tinyMCEPopup.getWindowArg('current_node_content');

		//--------------------------------
		//-- Check to see if any content was passed to window   |   看看是不是真的有待编辑内容传递过来
		//--------------------------------
		if (current_node_content != undefined)
		{
			//--------------------------------
			//-- Resolve all reference codes   |   解析所有引用代码
			//--------------------------------
			var arrayMatch = current_node_content.match(/\[%[pfr][\s\S]+?%\]/gi);
			
			if(arrayMatch != undefined && arrayMatch != null) 
			{
				if (arrayMatch.length>1)
				{
					for (var i=1; i<=arrayMatch.length-1; i++)
					{
						//--------------------------------
						//-- For 1+ reference codes, create more entries   |   多个引用代码，动态创建多个 entry
						//--------------------------------
						document.getElementById('more_reference_code_entry').innerHTML += '<div class="ReferenceCode"><input class="mceFocus ReferenceCode" name="reference_code" id="reference_code_' + i + '" type="textfield" /></div><div class="ReferenceCode" id="srcbrowsercontainer_' + i + '">&nbsp;</div>';
						document.getElementById('srcbrowsercontainer_' + i).innerHTML = getBrowserHTML('srcbrowser_' + i,'reference_code_' + i,'reference','theme_advanced_image');		
					}
				}	

				if (arrayMatch.length>0)
				{
					for (var i=0; i<=arrayMatch.length-1; i++)
					{
						//--------------------------------
						//-- put reference code to  reference_code input box |    将引用代码放入输入框
						//--------------------------------
						document.getElementById('reference_code_' + i).value=arrayMatch[i];
					}
				}				
			}			
		}
	},

	insert : function() 
	{
		//--------------------------------
		//-- Local var  |    本地变量
		//--------------------------------
		var f = document.forms[0], replace_content;

		//--------------------------------
		//-- Get array of reference code entry controls  |    获取所有引用代码输入框（数组）
		//--------------------------------
		var return_reference_code = '';
		
		if (f.reference_code.length>0)
		{
			for (var i=0; i<=f.reference_code.length-1; i++)
			{
				return_reference_code += f.reference_code[i].value;
			}
		}
		else
		{
			return_reference_code = f.reference_code.value;
		}

		//--------------------------------
		//-- Insert or Replace   |    是插入还是替换
		//--------------------------------
		replace_content = tinyMCEPopup.getWindowArg('replace_content');

		//--------------------------------
		//-- Check for replace_content option and return output accordingly   |    按替换还是插入这个选项，进行替换/插入
		//--------------------------------
		if(replace_content == false) 
		{
			tinyMCEPopup.editor.execCommand('mceInsertContent', false, return_reference_code);
		} 
		else 
		{
			//--------------------------------
			//-- Get the content of current node passed from plugin to window.  |   获取传递过来的当前节点的内容（以便从中解析引用代码）
			//--------------------------------
			var current_node_content = tinyMCEPopup.getWindowArg('current_node_content');

			//--------------------------------
			//-- Replace all reference codes with new code  |   用新代码替换旧引用代码
			//--------------------------------
			var matchCounter = -1;
			
			return_reference_code = current_node_content.replace(/\[%[pfr][\s\S]+?%\]/gi,function(m)
			{			
				//--------------------------------
				//-- Match counter ++   |   Match 计数器++
				//--------------------------------
				matchCounter++;
				
				//--------------------------------
				//-- Current reference code to replace the match   |   当前引用代码取代对应的旧引用代码
				//--------------------------------
				if (f.reference_code.length>0)
				{
					return f.reference_code[matchCounter].value;
				}
				else
				{
					return f.reference_code.value;
				}					
			});		
				
			//--------------------------------
			//-- Return   |   返回结果
			//--------------------------------
			tinyMCEPopup.editor.execCommand('mceReplaceContent', false, return_reference_code);
		}
		
		//--------------------------------
		//-- Close popup window   |   结束
		//--------------------------------
		tinyMCEPopup.close();
	}
};

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