/**
 * @fileoverview Ucren Desktop 2
 * @author <a href="dron@qq.com">dron</a>
 * @version 2.0
 * @create-date 2008-8-23
 */

var hidden = Ucren.get("hidden");

var DesktopCont = Ucren.get("desktopcont");
DesktopCont.reset = function(){ this.resizeTo(BODY.width, BODY.height - 40);};

var TaskCont = Ucren.get("taskcont");
TaskCont.reset = function(){ this.setTop(BODY.height - 40); };

var StartBtn = Ucren.get("startbtn");
StartBtn.on({
	mouseover: function(){ this.className = "startbtnon"; },
	mouseout: function(){ this.className = ""; },
	mousedown: function(e){ this.className = "startbtndown"; Ucren.Event(e).cancel();},
	mouseup: function(){ this.className = "startbtnon"; StartMenu.expand() === false && StartMenu.collapse(); }
});

var ShowTime = Ucren.get("showtime");
ShowTime.refreshTime = function(){ var d = new Date(), s = [d.getHours(), d.getMinutes(), d.getSeconds()].join(":").replace(/\b(\d)\b/g, "0$1"); this.setHtml(s); };

var DeskTip = Ucren.get("desktip");
DeskTip.reset = function(){ this.setTop(BODY.height - 56); };

var TaskPanel = Ucren.get("taskpanel");
TaskPanel.reset = function(){
	this.setWidth(BODY.width - 200);
	this.task && this.task.setWidth(BODY.width - 200);
}
TaskPanel.bindControl = function(){
	this.task = new Ucren.WindowTaskBar({ container: this });
	this.task.render();
}
TaskPanel.add = function(w){
	this.task.addTask(w);
}

var StartMenu = {
	data: [
		{ name: "程序(P)", iconPosition: 0, handler: function(){ Ucren.alert("程序"); } },
		{ name: "文档(D)", iconPosition: 1, handler: function(){ Ucren.alert("文档"); } },
		{ name: "设置(S)", iconPosition: 2, handler: function(){ Ucren.alert("设置"); } },
		{ name: "搜索(C)", iconPosition: 3, handler: function(){ Ucren.alert("搜索"); } },
		{ name: "帮助和支持", iconPosition: 4, handler: function(){ Ucren.alert("帮助和支持"); } },
		{ name: "运行(R)...", iconPosition: 5, handler: function(){ Ucren.alert("运行"); } },
		{ name: "注销 Ucren(L)...", iconPosition: 6, handler: function(){ Ucren.alert("注销"); } },
		{ name: "退出 Ucren(U)...", iconPosition: 7, handler: function(){ Ucren.alert("退出"); } }
	],
	expand: function(){
		if(this.isExpand)return false;
		BODY.refresh();
		var eff = this.eff;
		eff.setDisplay(true);
		eff.flashTo({
			uniformity: true,
			timeout: 1,
			left: 6,
			top: BODY.height - this.data.length * 36 - 43,
			width: 184, height: this.data.length * 36 + 6,
			callback: function(){
				this.show();
				this.isExpand = true;
				eff.setDisplay(false);
			}.createDelegate(this)
		});
	},
	collapse: function(){
		if(!this.isExpand)return false;
		BODY.refresh();
		var eff = this.eff;
		this.hide();
		eff.setDisplay(true);
		eff.flashTo({
			uniformity: true,
			left: 0,
			top: BODY.height - 36,
			width: 0, height: 0,
			callback: function(){
				this.isExpand = false;
				eff.setDisplay(false);
			}.createDelegate(this)
		});
	},
	show: function(){
		Ucren.get("startMenu").setDisplay(true);
		Ucren.get("startMenu").topMost();
	},
	hide: function(){
		Ucren.get("startMenu").setDisplay(false);
	},
	synTop: function(){
		if(!this.rendered)return ;
		var div = Ucren.get("startMenu");
		var h = BODY.height - this.data.length * 36 - 43;
		div.setTop(h);
		var eff = this.eff;
		if(this.isExpand){
			eff.moveTo(6, h);
		}else{
			eff.moveTo(0, BODY.height - 36);
		}
	},
	render: function(){
		this.rendered = true;
		var inner = Ucren.get("startMenu-inner");
		var itemTemp = Ucren.String(
			"<table cellspacing='0' cellpadding='0' height='36'>",
				"<tr>",
					"<td width='5'></td>",
					"<td width='32' align='center'><img src='@{blank}' style='background-position: 0 -@{iconPosition}px;' class='icon' alt='' /></td>",
					"<td>&nbsp;@{text}</td>",
				"</tr>",
			"</table>"
		);

		var blank = Ucren.appPath + "resource/blank.gif";
		for(var i = 0, d, l = this.data.length; i < l; i ++){
			d = this.data[i];
			if(i == l - 2){
				var line = inner.createElement("div");
				line.setClass("item-line");
			}
			var idv = inner.createElement("div");
			idv.setClass("item");
			idv.setHtml(itemTemp.replace({ iconPosition: d.iconPosition * 24, text: d.name, blank: blank }));
			idv.on({
				mouseover: function(){ this.className = "item itemon"; },
				mouseout: function(){ this.className = "item"; },
				mousedown: function(e){
					Ucren.Event(e).cancel();
					this.handler && this.handler();
					StartMenu.collapse();
				}.createDelegate(d)
			});
		}

		this.renderEff();
	},
	renderEff: function(){
		var eff = this.eff = BODY.createElement("div");
		eff.setClass("startmenueff ucren-dottedborder");
		eff.resizeTo(0, 0);
	}
};

var DesktopIcons = {
	data: [
		{ name: "我的文档", iconPosition: 0, handler: function(){ Ucren.alert("我的文档"); } },
		{ name: "我的电脑", iconPosition: 1, handler: function(){ Module.load("MyComputer"); } },
		{ name: "网上邻居", iconPosition: 2, handler: function(){ Ucren.alert("网上邻居"); } },
		{ name: "回收站", iconPosition: 3, handler: function(){ Ucren.alert("回收站"); } },
		{ name: "Ucren QQ", iconPosition: 4, handler: function(){ Module.load("QQ"); } },
		{ name: "示例窗口1", iconPosition: 5, handler: function(){ Module.messages.SimpleWindowNum = 1; Module.load("SimpleWindow"); } },
		{ name: "示例窗口2", iconPosition: 5, handler: function(){ Module.messages.SimpleWindowNum = 2; Module.load("SimpleWindow"); } },
		{ name: "示例窗口3", iconPosition: 5, handler: function(){ Module.messages.SimpleWindowNum = 3; Module.load("SimpleWindow"); } },
		{ name: "示例窗口4", iconPosition: 5, handler: function(){ Module.messages.SimpleWindowNum = 4; Module.load("SimpleWindow"); } },
		{ name: "显示设置", iconPosition: 6, handler: function(){ Module.load("SetDisplay"); } }
	],
	render: function(){
		this.group = new Ucren.DesktopIconGroup({
			container: DesktopCont,
			iconsImage: "images/desktopicons.gif",
			data: this.data
		});
		this.group.render();
	}
};

var Module = {
	list: {}, // 已加载模块列表
	messages: {}, // 用于传递给模块的参数存放平台
	load: function(name){
		var l = this.list;
		if(l[name])return l[name].touch && l[name].touch();
		Ucren.showLoading("loading...");
		Ucren.remoteCallScript("modules/" + name + ".js", true);
	}
};

Ucren.onReady(function(){
	StartMenu.render();

	Ucren.addEvent(window, "resize", function(){
		BODY.refresh();
		TaskCont.reset();
		DeskTip.reset();
		TaskPanel.reset();
		DesktopCont.reset();
		StartMenu.synTop();
	})();

	DesktopIcons.render();
	TaskPanel.bindControl();

	BODY.on({ mousedown: StartMenu.collapse.createDelegate(StartMenu) });

	TaskCont.setDisplay(true);
	DeskTip.setDisplay(true);

	Ucren.loadingImage(["images/startmenubg2.gif", "images/taskbtn.gif"]);

	ShowTime.refreshTime.defer(1000, ShowTime, true);
	var d = new Date;
	ShowTime.set("title", d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日");

	var bodymenudata = [
		{ text: "返回(B)", disabled: true },
		{ text: "前进(O)", disabled: true },
		{ type: "line" },
		{ text: "背景另存为(S)...", disabled: true },
		{ text: "设置为背景(G)", disabled: true },
		{ text: "复制背景(C)", disabled: true },
		{ type: "line" },
		{ text: "全选(A)", disabled: true },
		{ text: "粘贴(P)", disabled: true },
		{ type: "line" },
		{ text: "创建快捷方式(T)", handler: function(){ Ucren.alert("test"); }},
		{ text: "添加到收藏夹(F)", handler: function(){ Ucren.alert("test"); }},
		{ text: "查看源文件(V)", handler: function(){ Ucren.alert("test"); }},
		{ type: "line" },
		{ text: "打印(I)...", handler: function(){ Ucren.alert("test"); }},
		{ text: "刷新(R)", handler: function(){ Ucren.alert("test"); }},
		{ type: "line" },
		{ text: "属性(P)", handler: function(){ Module.load("SetDisplay"); }}
	];

	var bodymenu = new Ucren.ContextMenu({
		data: bodymenudata
	});
	bodymenu.bind(BODY);

	$lc = true;
});