//*******************************************************************
//** BGImage is a TinyMCE plugin to insert and edit background image of certain elements
//** Author: COMSHARP
//** http://www.comsharp.com
//*******************************************************************
//** BGImage 是一个用来在 TinyMCE 中插入和编辑 HTML 元素背景图片的插件
//** 作者：锐商软件
//** http://www.comsharp.com
//*******************************************************************

(function() 
{
	//--------------------------------
	//- Load the language file | 加载语言文件 ( bgimage 就是该插件的名字 ）
	//--------------------------------
	tinymce.PluginManager.requireLangPack('bgimage');
	
	//********************************
	//** Plugin Initialization | 插件初始化
	//********************************
	tinymce.create('tinymce.plugins.bgimage', 
	{
		//--------------------------------
		//-- Initialization : Executed after the creation of the plugin  |  初始化： 插件被加载后执行该部分初始化代码
		//--------------------------------
		//-- @param ed  {tinymce.Editor} - Editor instance that the plugin is initialized in.      |  参数 ed {tinymce.Editor} - 该插件所在的 TinyMCE 编辑器实例
		//-- @param url {string}         - string url Absolute URL to where the plugin is located. |  参数 url {string}        - 该插件所在的绝对路径
		//--------------------------------
		init : function(ed, url) 
		{
			//--------------------------------
			//-- Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('mceBGImage'), ie when the tool button of this plugin clicked  |  注册 mceBGImage 命令，也就是点击了该插件按钮后执行的命令
			//--------------------------------
			ed.addCommand('mceBGImage', function() 
			{
				//--------------------------------
				//-- Get current node in cursor position  |   获取当前光标所在的位置的节点对象
				//--------------------------------
				var currentNode = ed.selection.getNode();

				//--------------------------------
				//-- get all parents of current selected node to find the node which is entitled for background image  |   获取当前光标所在的位置的节点的所有（各级）父节点，找到其中适合设置 background image 属性的节点
				//-- Remove the body / html entry   |   排除 body 和 html 项
				//--------------------------------
				var parentNodes=ed.dom.getParents(currentNode,null);
				parentNodes.splice(parentNodes.length-2,2);
				
				//--------------------------------
				//-- Open plugin dialogue window   |   打开插件窗口
				//--------------------------------
				ed.windowManager.open(
				{
					//--------------------------------
					//-- Plugin window path and property   |   插件窗口路径和属性
					//--------------------------------
					file : url + '/dialog.htm',
					width : 500,
					height : 380,
					inline : 1
				}, 
				{
					//--------------------------------
					//-- Plugin more property   |   插件更多属性 （background style 内容）
					//--------------------------------
					plugin_url : url, 
					current_node : currentNode,
					parent_nodes : parentNodes
				});					
			});

			//--------------------------------
			//-- Register plugin  button   |    注册插件按钮 （描述，命令名，图标）
			//--------------------------------
			ed.addButton('bgimage', 
			{
				title : 'bgimage.desc',
				cmd : 'mceBGImage',
				image : url + '/img/bgimage.gif'
			});

			//--------------------------------
			//-- Context menu   |   右键菜单
			//--------------------------------
			if(ed.plugins.contextmenu != undefined)
			{
				ed.plugins.contextmenu.onContextMenu.add(function(sender, menu)
				{					
					//--------------------------------
					//-- Define sub menu   |   定义右键子菜单
					//--------------------------------
					var sub_menu = menu.add(
					{
						title : 'BGImage - Set',
						icon : url + '/img/bgimage.gif',
						
						onclick : function() 
						{ 
							//--------------------------------
							//-- Get current selected node |  当前被选中的内容节点 （光标所在节点）
							//--------------------------------
							var currentNode = ed.selection.getNode();

							//--------------------------------
							//-- get all parents of current selected node to find the node which is entitled for background image  |   获取当前光标所在的位置的节点的所有（各级）父节点，找到其中适合设置 background image 属性的节点
							//-- Remove the body / html entry   |   排除 body 和 html 项
							//--------------------------------
							var parentNodes=ed.dom.getParents(currentNode,null);
							parentNodes.splice(parentNodes.length-2,2);
							
							//--------------------------------
							//-- Open the plugin dialogue window when this context menu clicked   |    右键菜单点中后，打开插件对话框窗口
							//--------------------------------
							ed.windowManager.open(
							{
								file : url + '/dialog.htm',
								width : 500,
								height : 380,
								inline : 1
							}, 
							{
								plugin_url : url, 
								current_node : currentNode,
								parent_nodes : parentNodes
							});
						}
					});					
				});
			};
		},

		//--------------------------------
		//-- Creates control instances based in the incomming name. This method is normally not
		//-- needed since the addButton method of the tinymce.Editor class is a more easy way of adding buttons
		//-- but you sometimes need to create more complex controls like listboxes, split buttons etc then this
		//-- method can be used to create those.
		//--------------------------------
		//-- @param {String} n Name of the control to create.
		//-- @param {tinymce.ControlManager} cm Control manager to use inorder to create new control.
		//-- @return {tinymce.ui.Control} New control instance or null if no control was created.
		//--------------------------------
		createControl : function(n, cm) 
		{
			return null;
		},

		//--------------------------------
		//-- Returns information about the plugin as a name/value array.
		//-- The current keys are longname, author, authorurl, infourl and version.
		//--------------------------------
		//-- @return {Object} Name/value array containing information about the plugin.
		//--------------------------------
		getInfo : function() 
		{
			return {
				longname : 'TinyMCE Background Image Plugin',
				author : 'xxx',
				authorurl : 'http://www.xxx.com',
				infourl : 'http://www.xxx.com',
				version : "1.0"
			};
		}
		
	});

	//********************************
	//** Register plugin   |    注册插件
	//********************************
	tinymce.PluginManager.add('bgimage', tinymce.plugins.bgimage);
	
})();


