﻿/**
 *
 */

var delayFuncs = [];
var doDelayFuncs = function(){
	var fn;
	if(delayFuncs.length){
		fn = delayFuncs.shift();
		fn.call();
	}
};
doDelayFuncs = doDelayFuncs.infrequently(200);

var layout = {
	initMenuBar: function(){
		var menubar = new Ucren.MenuBar({
			container: "menubar",
			data: [
				{ text: "文件(F)", subMenuData: [
					{ text: "新建(N)", subMenuData: [
						{ text: "标准文本", handler: function(){ window.alert("hello"); } },
						{ text: "HTML 网页" },
						{ text: "浏览器窗口" },
						{ type: "separator" },
						{ text: "配置模板" },
						{ text: "其它" }
					] },
					{ text: "打开(O)..." },
					{ text: "关闭(C)" },
					{ text: "全部关闭(E)" },
					{ type: "separator" },
					{ text: "保存(S)" },
					{ text: "全部保存(L)" },
					{ text: "另存为(A)..." },
					{ type: "separator" },
					{ text: "打印(P)..." },
					{ text: "直接打印" },
					{ text: "打印预览(V)" },
					{ text: "打印设置(R)..." },
					{ text: "全部打印..." },
					{ type: "separator" },
					{ text: "FTP(F)", subMenuData: [
						{ text: "FTP 上载(F)..." },
						{ text: "批量上载(M)..." },
						{ text: "全部上载(L)..." },
						{ type: "separator" },
						{ text: "打开远程文件(O)..." },
						{ text: "另存为远程文件(A)..." },
						{ type: "separator" },
						{ text: "FTP 设置(T)..." },
						{ text: "断开连接(D)..." }
					] },
					{ text: "其它(T)", subMenuData: [
						{ text: "打开标头文件(O)" },
						{ text: "打开所选内容(E)" },
						{ text: "跳到文件(J)" },
						{ type: "separator" },
						{ text: "重命名(R)" },
						{ text: "锁定文件(L)" },
						{ text: "发送邮件(S)..." },
						{ type: "separator" },
						{ text: "删除文件(D)" },
						{ type: "separator" },
						{ text: "属性(P)..." }
					] },
					{ text: "最近的文件(A)", subMenuData: [
						{ text: "1 D:\\demo\\test.html" },
						{ text: "2 D:\\demo\\test.css" },
						{ text: "3 D:\\demo\\boot.js" }
					] },
					{ type: "separator" },
					{ text: "保存和退出" },
					{ text: "退出(X)" }
				] },
				{ text: "编辑(E)", subMenuData: [
					{ text: "撤消(U)" },
					{ text: "重做(R)" },
					{ type: "separator" },
					{ text: "剪切(T)" },
					{ text: "复制(C)" },
					{ text: "粘贴(P)" },
					{ text: "删除(D)", subMenuData: [
						{ text: "删除所选内容(D)" },
						{ text: "删除单词(W)" },
						{ text: "删除行(L)" },
						{ text: "删除至单词结尾(E)" },
						{ text: "删除至行结尾(N)" },
						{ text: "反向删除单词(O)" }
					] },
					{ text: "选择(S)", subMenuData: [
						{ text: "选择单词" },
						{ text: "选择行" },
						{ text: "全部选定(A)" },
						{ text: "列选择(C)" },
						{ type: "separator" },
						{ text: "开始/结束选择(B)" },
						{ text: "开始/结束列选择(E)" }
					] },
					{ text: "剪贴板(O)", subMenuData: [
						{ text: "剪切附加(T)" },
						{ text: "复制附加(C)" },
						{ type: "separator" },
						{ text: "剪切标记的行(U)" },
						{ text: "复制标记的行(O)" },
						{ text: "删除标记的行(D)" },
						{ type: "separator" },
						{ text: "粘贴为列(P)" },
						{ text: "复制为 HTML(H)" },
						{ text: "复制 URL(R)" }
					] },
					{ type: "separator" },
					{ text: "格式(F)", subMenuData: [
						{ text: "剪裁行尾空格(T)" },
						{ text: "制表符转为空格(A)" },
						{ text: "空格转为制表符(S)" },
						{ text: "行首空格转为制表符(L)" },
						{ type: "separator" },
						{ text: "合并行(J)" },
						{ text: "拆分行(P)" },
						{ text: "重排格式(M)" },
						{ text: "增大缩排(I)" },
						{ text: "减小缩排(D)" },
						{ type: "separator" },
						{ text: "行注释(O)" },
						{ text: "行注释缩排(N)" },
						{ text: "取消行注释(U)" },
						{ text: "填充所选内容(F)" },
						{ type: "separator" },
						{ text: "左边对齐(E)" },
						{ text: "居中对齐(C)" },
						{ text: "右边对齐(R)" }
					] },
					{ text: "大小写转换(N)", subMenuData: [
						{ text: "大写字母(U)" },
						{ text: "小写字母(L)" },
						{ text: "大小写互换(I)" },
						{ text: "词首字母大写(C)" }
					] },
					{ text: "插入(I)", subMenuData: [
						{ text: "搜索素材文本(S)" },
						{ text: "搜索文档(E)" },
						{ text: "搜索所有打开文件(A)" },
						{ text: "插入文件(I)..." },
						{ text: "分页符(P)" },
						{ type: "separator" },
						{ text: "日期(D)" },
						{ text: "长格式日期(L)" },
						{ text: "时间(T)" },
						{ text: "长格式时间(O)" },
						{ text: "自定义日期/时间(C)..." }
					] },
					{ text: "HTML(H)", subMenuData: [
						{ text: "去除 HTML 标签(H)" },
						{ text: "ANSI 转为 HTML 实体(A)" },
						{ text: "HTML 实体转为 ANSI(T)" }
					] },
					{ text: "其它(E)", subMenuData: [
						{ text: "转置(T)" },
						{ text: "重复复制行(L)" },
						{ text: "重复复制字符(C)" },
						{ type: "separator" },
						{ text: "向上移动(U)" },
						{ text: "向下移动(D)" },
						{ type: "separator" },
						{ text: "字符代码(A)" },
						{ text: "锁定插入模式(I)" },
						{ text: "允许拖放(O)" }
					] },
					{ type: "separator" },
					{ text: "计算字符和单词数目(A)..." },
					{ text: "允许虚拟空格(V)" }
				] },
				{ text: "视图(V)", subMenuData: [
					{ text: "工具栏/视图(T)", subMenuData: [
						{ text: "工具栏(T)" },
						{ text: "用户工具栏(U)" },
						{ text: "HTML 工具栏(H)" },
						{ text: "状态栏(S)" },
						{ type: "separator" },
						{ text: "素材文本窗口(C)" },
						{ text: "目录窗口(R)" },
						{ text: "输出窗口(O)" },
						{ text: "文档选择器(D)" },
						{ type: "separator" },
						{ text: "文档选择器选项(M)..." },
						{ text: "转到素材文件/目录(G)" },
						{ text: "转到输出窗口(W)" }
					] },
					{ type: "separator" },
					{ text: "屏幕字体(E)", subMenuData: [
						{ text: "默认 (Courier New, 9)" },
						{ text: "自定义 (Arial, 9)" },
						{ text: "自定义 (Verdana, 8)" },
						{ text: "自定义 (Times New Roman, 9)" },
						{ text: "自定义 (MS Sans Serif, 8)" },
						{ text: "自定义 (Terminal, 10)" },
						{ type: "separator" },
						{ text: "设置显示字体(S)..." }
					] },
					{ text: "打印机字体(P)", subMenuData: [
						{ text: "对应屏幕字体(M)" },
						{ text: "设置打印机字体(S)..." }
					] },
					{ type: "separator" },
					{ text: "制表符和空格(A)" },
					{ text: "换行符(L)" },
					{ text: "URL突出显示(U)" },
					{ text: "语法突出显示(S)" },
					{ text: "匹配括号(M)" },
					{ type: "separator" },
					{ text: "光标指示符(I)" },
					{ text: "标尺(R)" },
					{ text: "行号(N)" },
					{ text: "代码折叠(O)", subMenuData: [
						{ text: "使用代码折叠(F)" },
						{ type: "separator" },
						{ text: "折叠(C)" },
						{ text: "展开(E)" },
						{ text: "切换折叠(T)" },
						{ type: "separator" },
						{ text: "折叠一层(S)" },
						{ text: "展开一层(N)" },
						{ type: "separator" },
						{ text: "全部折叠(O)" },
						{ text: "全部展开(X)" }
					] },
					{ type: "separator" },
					{ text: "全屏显示(F)" },
					{ text: "在浏览器中查看(B)" },
					{ text: "在浏览器 2 中查看" },
					{ text: "用户界面", subMenuData: [
						{ text: "英文(E)" },
						{ text: "中文(C)" }
					] }
				] },
				{ text: "搜索(S)", subMenuData: [
					{ text: "查找(F)..." },
					{ text: "替换(E)..." },
					{ text: "查找下一个(N)" },
					{ text: "查找上一个(P)" },
					{ text: "查找下一个单词" },
					{ text: "查找上一个单词" },
					{ text: "在文件中查找(I)..." },
					{ type: "separator" },
					{ text: "转到(G)", subMenuData: [
						{ text: "转到行(L)..." },
						{ text: "转到列(C)..." },
						{ text: "转到 URL(U)" }
					] },
					{ text: "标记(K)", subMenuData: [
						{ text: "切换标记(T)" },
						{ text: "清除所有标记(C)" },
						{ text: "下一个标记(X)" },
						{ text: "上一个标记(R)" }
					] },
					{ type: "separator" },
					{ text: "匹配括号(M)" },
					{ text: "选择括号(S)" },
					{ text: "匹配标签(T)" },
					{ text: "选择标签(A)" },
					{ type: "separator" },
					{ text: "函数列表(L)" },
					{ text: "下一个函数(X)" },
					{ text: "上一个函数(V)" }
				] },
				{ text: "文档(D)", subMenuData: [
					{ text: "自动换行(W)" },
					{ text: "自动缩排(I)" },
					{ text: "自动完成(M)" },
					{ type: "separator" },
					{ text: "自动换行选项(O)..." },
					{ text: "制表符/缩排(T)..." },
					{ text: "函数模式(N)..." },
					{ text: "列标记(U)", subMenuData: [
						{ text: "设置列标记(S)..." },
						{ type: "separator" },
						{ text: "显示列标记(C)" },
						{ text: "下一个列标记(N)" },
						{ text: "上一个列标记(P)" }
					] },
					{ type: "separator" },
					{ text: "文件换行格式 (CR/LR)(C)", subMenuData: [
						{ text: "更改文件换行格式(C)..." },
						{ text: "批量更改文件换行格式(F)..." }
					] },
					{ text: "文件类型(L)", subMenuData: [
						{ text: "更改文件类型(C)..." },
						{ text: "刷新 STX/ACP(S)" }
					] },
					{ text: "文件编码方式(D)", subMenuData: [
						{ text: "更改文件编码方式(C)..." },
						{ text: "批量更改文件编码方式(F)..." }
					] },
					{ type: "separator" },
					{ text: "监视剪贴板(A)" },
					{ text: "设置只读(R)" },
					{ text: "重新载入(L)" },
					{ text: "重新载入为(A)..." },
					{ type: "separator" },
					{ text: "固定设置值(P)..." }
				] },
				{ text: "项目(P)", subMenuData: [
					{ text: "选项项目", subMenuData: [
						{ text: "默认" },
						{ text: "ucren-lite" }
					] },
					{ text: "管理项目", subMenuData: [
						{ text: "添加到项目(A)" },
						{ text: "从项目删除(R)" },
						{ text: "添加所有到项目(L)" },
						{ type: "separator" },
						{ text: "载入项目(A)" },
						{ text: "关闭项目(C)" },
						{ type: "separator" },
						{ text: "下一个项目" },
						{ text: "上一个项目" }
					] },
					{ text: "编辑项目(E)..." },
					{ type: "separator" },
					{ text: "boot.js" },
					{ text: "interface.css" },
					{ text: "public.css" }
				] },
				{ text: "工具(T)", subMenuData: [
					{ text: "首选项(P)..." },
					{ text: "设置目录(I)..." },
					{ type: "separator" },
					{ text: "拼写检查(S)", subMenuData: [
						{ text: "文档(D)" },
						{ text: "从当前位置(P)" },
						{ text: "当前所选内容(S)" },
						{ text: "当前单词(W)" }
					] },
					{ text: "录制(R)", subMenuData: [
						{ text: "录制击键(R)..." },
						{ text: "导出录制(E)..." },
						{ text: "导入录制(I)..." },
						{ text: "播放多次(M)..." },
						{ type: "separator" },
						{ text: "播放录制 1(1)" },
						{ text: "播放录制 2(2)" },
						{ text: "播放录制 3(3)" },
						{ text: "播放录制 4(4)" },
						{ text: "播放录制 5(5)" },
						{ text: "播放录制 6(6)" },
						{ text: "播放录制 7(7)" },
						{ text: "播放录制 8(8)" },
						{ text: "播放录制 9(9)" },
						{ text: "播放录制 10(0)" }
					] },
					{ text: "排序(O)..." },
					{ text: "求和(M)..." },
					{ type: "separator" },
					{ text: "配置用户工具(C)..." },
					{ text: "用户工具组(G)", subMenuData: [
						{ text: "Group 1" },
						{ text: "Group 2" },
						{ text: "Group 3" },
						{ text: "Group 4" },
						{ text: "Group 5" },
						{ text: "Group 6" },
						{ text: "Group 7" },
						{ text: "Group 8" },
						{ text: "Group 9" },
						{ text: "Group 10" }
					] },
					{ type: "separator" },
					{ text: "(1) Ucren 文本编码工具" },
					{ text: "(2) JS 压缩与格式化" },
					{ text: "(3) 打开手册" }
				] },
				{ text: "浏览器(B)", subMenuData: [
					{ text: "编辑源文件(E)" },
					{ type: "separator" },
					{ text: "后退(B)" },
					{ text: "前进(F)" },
					{ text: "停止(T)" },
					{ text: "刷新(R)" },
					{ type: "separator" },
					{ text: "字体(N)", subMenuData: [
						{ text: "最大(G)" },
						{ text: "较大(L)" },
						{ text: "中(M)" },
						{ text: "较大(S)" },
						{ text: "最小(A)" }
					] },
					{ type: "separator" },
					{ text: "主页(H)" },
					{ text: "搜索(S)" },
					{ text: "收藏夹", subMenuData: [
						{ text: "编辑收藏夹" }
					] },
					{ type: "separator" },
					{ text: "选项(O)..." }
				] },
				{ text: "窗口(W)", subMenuData: [
					{ text: "下一个(N)" },
					{ text: "上一个(P)" },
					{ text: "最后访问(V)" },
					{ text: "查找窗口(F)" },
					{ type: "separator" },
					{ text: "排列(A)", subMenuData: [
						{ text: "层叠窗口(C)" },
						{ text: "横向平铺窗口(H)" },
						{ text: "纵向平铺窗口(T)" },
						{ text: "排列图标(A)" },
						{ type: "separator" },
						{ text: "全部最小化(M)" },
						{ text: "全部还原(R)" }
					] },
					{ text: "分隔器(S)", subMenuData: [
						{ text: "拆分(S)" },
						{ text: "下一个窗格(E)" },
						{ text: "上一个窗格(V)" }
					] },
					{ text: "其它(T)", subMenuData: [
						{ text: "总在最前面(T)" },
						{ text: "透明(R)" },
						{ text: "保存窗口宽度(S)" },
						{ text: "拉伸(E)" }
					] },
					{ text: "全部关闭(E)" },
					{ type: "separator" },
					{ text: "1 boot.js" },
					{ text: "2 interface.css" },
					{ text: "3 public.css" },
					{ text: "窗口列表(W)..." }
				] },
				{ text: "帮助(H)", subMenuData: [
					{ text: "帮助主题(H)" },
					{ text: "上下文帮助(C)" },
					{ text: "全部快捷键(K)" },
					{ type: "separator" },
					{ text: "反馈和支持(F)" },
					{ text: "检查最新版本(L)" },
					{ type: "separator" },
					{ text: "关于 EditPlus(D)" }
				] }
			]
		});
		menubar.render();
	}(),

	initToolbar: function(){
		var toolbar = new Ucren.Toolbar({
			container: "toolbar",
			data: [
				{ type: "groupStart" },
				{ icon: "images/new.gif", text: "新建" },
				{ icon: "images/open.gif", text: "打开" },
				{ icon: "images/save.gif", text: "保存" },
				{ icon: "images/saveall.gif", text: "全部保存" },
				{ type: "separator" },
				{ icon: "images/printview.gif", text: "打印预览" },
				{ icon: "images/print.gif", text: "打印" },
				{ icon: "images/transliterate.gif", text: "拼写检查" },
				{ icon: "images/newhtml.gif", text: "新建 HTMl 网页" },
				{ type: "separator" },
				{ icon: "images/cut.gif", text: "剪切", disabled: true },
				{ icon: "images/copy.gif", text: "复制", disabled: true },
				{ icon: "images/paste.gif", text: "粘贴" },
				{ icon: "images/delete.gif", text: "删除" },
				{ type: "separator" },
				{ icon: "images/prev.gif", text: "撤消 " },
				{ icon: "images/next.gif", text: "重做", disabled: true },
				{ type: "separator" },
				{ icon: "images/find.gif", text: "查找" },
				{ icon: "images/replace.gif", text: "替换" },
				{ icon: "images/findinfile.gif", text: "在文件中查找" },
				{ icon: "images/gotoline.gif", text: "转到行" },
				{ type: "separator" },
				{ icon: "images/font.gif", text: "屏幕字体" },
				{ icon: "images/autonewline.gif", text: "自动换行", type: "record", value: true },
				{ icon: "images/autoindent.gif", text: "自动缩排", type: "record", value: true },
				{ icon: "images/linenumber.gif", text: "行号", type: "record", value: true },
				{ icon: "images/options.gif", text: "首选项" },
				{ type: "separator" },
				{ icon: "images/fodder.gif", text: "素材文本窗口", type: "record", value: false },
				{ icon: "images/folders.gif", text: "目录窗口", type: "record", value: false },
				{ icon: "images/output.gif", text: "输出窗口", type: "record", value: true },
				{ icon: "images/fullscreen.gif", text: "全屏显示", type: "record", value: false },
				{ type: "separator" },
				{ icon: "images/help.gif", text: "帮助", type: "record", value: false },
				{ type: "groupEnd" }
			]
		});
		toolbar.render();

		document.getElementById("toolbar-end").style.display = "block";
	}(),

	initMenus: function(){
		var icons = document.getElementById("icons");
		var datas = new Ucren.DataStack({
			fields: ["text", "dofunc"],
			records: [
				{ text: "2010 不去的地方", dofunc: null },
				{ text: "2010 不做的事情", dofunc: null },
				{ text: "2010 不看的书", dofunc: null },
				{ text: "2010 不拍的照片", dofunc: null },
				{ text: "2010 不记的事本", dofunc: null },
				{ text: "其它不干的事", dofunc: null },
				{ text: "其它不干的事", dofunc: null },
				{ text: "其它不干的事", dofunc: null }
			]
		});

		var getBackgroundAndCover = function(el){
			var divs = el.getElementsByTagName("div");
			return [divs[0], divs[1], divs[2]];
		};

		var colors = new Ucren.BasicColor({
			startColor: "#3b6ea5",
			endColor: "#aa0000",
			maxIndex: 10
		});
		colors.init();

		datas.each(function(record, index){
			var iconItem = document.createElement("div");
				iconItem.className = "icon-item";
				iconItem.id = "icon-item-" + index;
				iconItem.innerHTML = "<div class='background'></div><div class='cover'></div><div class='cover2'></div>";
				icons.appendChild(iconItem);

			var iconItemChilds = getBackgroundAndCover(iconItem);
			var background = iconItemChilds[0];
			var cover = iconItemChilds[1];
				cover = Ucren.Element(cover);
				cover.html(record.text);
			var cover2 = iconItemChilds[2];
				cover2 = Ucren.Element(cover2);

			var slippage = new Ucren.BasicSlippage({
				container: background,
				width: 191,
				height: 73,
				image: "images/menu.jpg",
				length: 12,
				enableAnimation: true,
				animationInterval: Ucren.isIe ? 20 : 30
			});
			slippage.render();
			slippage.on("change", function(number){
				if(number < 11){
					cover.left(- (15 - number));
					cover.dom.style.color = colors.getColorByIndex(number);
				}
			});

			cover2.addEvents({
				mouseover: function(){
					delayFuncs.push(slippage.animationTo.saturate(slippage, 10));
					doDelayFuncs();
				},

				mouseout: function(){
					delayFuncs.push(slippage.animationTo.saturate(slippage, 0));
					doDelayFuncs();
				},

				mousedown: function(){
					delayFuncs.push(slippage.animationTo.saturate(slippage, 11));
					doDelayFuncs();
				},

				mouseup: function(){
					delayFuncs.push(slippage.animationTo.saturate(slippage, 10));
					doDelayFuncs();
				}
			});
		});
	}()
};