KindEditor.plugin('character', function(K) {
	var self = this, name = 'character',currentPageNum = 1;
	var characterArray=new Array('–','—','½','é','°','≈','￥','『','』','﹌','﹋','ˆ','ˇ','︵','︶','︷','︸','①','②','③','④','⑤','⑥','⑦','⑧','⑨','⑩','Ⅰ','Ⅱ','Ⅲ','℃','÷','★','☆');
	self.clickToolbar(name, function() {
		var rows = 5, cols = 9, total = characterArray.length, startNum = 0,
		cells = rows * cols, pages = Math.ceil(total / cells),
		colsHalf = Math.floor(cols / 2),
		wrapperDiv = K('<div class="ke-plugin-emoticons ke-plugin-character"></div>'),
		elements = [],
		menu = self.createMenu({
			name : name,
			beforeRemove : function() {
				removeEvent();
			}
		});
		menu.div.append(wrapperDiv);
		wrapperDiv.parent('.ke-shadow').removeClass('ke-shadow').addClass('new-boxshadow');
		wrapperDiv.append('<h5>特殊字符</h5>');
		function bindCellEvent(cell, j, num) {
			cell.mouseover(function() {
				K(this).addClass('ke-on');
			});
			cell.mouseout(function() {
				K(this).removeClass('ke-on');
			});
			cell.click(function(e) {
				self.insertHtml(cell.text()).hideMenu().focus();
				e.stop();
			});
		}
		function createEmoticonsTable(pageNum, parentDiv) {
			var table = document.createElement('table');
			parentDiv.append(table);
			table.className = 'ke-table';
			table.cellPadding = 0;
			table.cellSpacing = 0;
			table.border = 0;
			var num = (pageNum - 1) * cells + startNum;
			for (var i = 0; i < rows; i++) {
				var row = table.insertRow(i);
				for (var j = 0; j < cols; j++) {
					if (!characterArray[num]) {
						break;
					}
					var cell = K(row.insertCell(j));
					cell.addClass('ke-cell');
					bindCellEvent(cell, j, num);
					var span = K('<span class="ke-char">'+characterArray[num]+'</span>');
					cell.append(span);
					elements.push(cell);
					num++;
				}
			}
			return table;
		}
		var table = createEmoticonsTable(currentPageNum, wrapperDiv);
		function removeEvent() {
			K.each(elements, function() {
				this.unbind();
			});
		}
		var pageDiv;
		function bindPageEvent(el, pageNum) {
			el.click(function(e) {
				removeEvent();
				table.parentNode.removeChild(table);
				pageDiv.remove();
				table = createEmoticonsTable(pageNum, wrapperDiv);
				createPageTable(pageNum);
				currentPageNum = pageNum;
				e.stop();
			});
		}
		function createPageTable(currentPageNum) {
			pageDiv = K('<div class="ke-page"></div>');
			wrapperDiv.append(pageDiv);
			for (var pageNum = 1; pageNum <= pages; pageNum++) {
				if (currentPageNum !== pageNum) {
					var a = K('<a href="javascript:;">[' + pageNum + ']</a>');
					bindPageEvent(a, pageNum);
					pageDiv.append(a);
					elements.push(a);
				} else {
					pageDiv.append(K('@[' + pageNum + ']'));
				}
				pageDiv.append(K('@&nbsp;'));
			}
		}
		createPageTable(currentPageNum);
	});
});
