var aTopic, iTopic, aComment, iComment, iMode=2, aEmployee, iEmployee;
	
$(document).ready( onDocumentReady );

function onDocumentReady() {
	$("#file_picture").change(picturePrepare);
	pageInit(showCreate);
}

function showTopic(i) {
	iMode=i;
	var aMode=['announce', 'private', 'public'];
	menuSwitch(aMode[i]);
	loadTopic();
}

function loadTopic() {
	jsonRequest("discuss", "load_topic_list", {mode:iMode}, onTopicResponse, "正在获取讨论话题…请稍候");
}

function onTopicResponse(res) {
	aTopic=res.topic_list;
	
	var html="";
	for (var i=0; i<aTopic.length; i++) {
		var topic=aTopic[i];
		html+=div("div_topic_item",
			(topic.visited?"":div("div_topic_new",""))+
			div("div_topic_title", topic.title)+
			div("div_topic_date", topic.date)+
			div("div_topic_employee_name", topic.employee.name+" ("+topic.employee.title+")")+
			img("img_topic_employee_headimg", topic.employee.headimg)+
			div("div_topic_stats",
				div("div_topic_comment", topic.comment_num)+
				div("div_topic_visit", topic.visit_num)+
				div("div_topic_love"+(topic.loved?"d":""), topic.love_num)),
					"iTopic="+i+"; showComment();");
	}
	$("#div_topic_group_"+iMode).html(html);
}

function promptTopicClose() {
	showConfirm("是否关闭这个讨论话题?", "confirmTopicClose()");
}

function confirmTopicClose() {
	var req={};
	req.topic_uid=aTopic[iTopic].uid;
	jsonRequest("discuss", "do_topic_close", req, onTopicCloseResponse, "正在关闭讨论话题……");
}

function onTopicCloseResponse(res) {
	pageBack();
	loadTopic();
}

function showCreate() {
	if (aPage.length==1) {
		menuSwitch("create");
	} else {
		pageGo("create");
	}
	$("#input_topic_title").val("");
	iMode=iMode || 2;
	setMode(iMode);
	$("#input_topic_title").focus();
	loadEmployeeList();
}

function loadEmployeeList() {
	jsonRequest("discuss", "load_employee_list", {}, onEmployeeListResponse, "正在获取员工名录……请稍候");
}

function onEmployeeListResponse(res) {
	aEmployee=res.employee_list;
	var html="";
	for (var i=0; i<aEmployee.length; i++) {
		var emp=aEmployee[i];
		html+=div("div_employee_item",
			img("img_employee_headimg", emp.headimg)+
			div("div_employee_code", emp.code)+
			div("div_employee_name", emp.name)+
			div("div_employee_title", emp.title)+
			div("div_employee_off","",null,"employee_"+i), "toggleEmployee("+i+")" );
	}
	$("#div_employee_group").html(html);
}

function setMode(i) {
	toggleSwitch("mode", i);
	iMode=i;
	if (i==1) {
		$("#div_employee_group").slideDown();
	} else {
		$("#div_employee_group").slideUp();
	}
}

function toggleEmployee(i) {
	aEmployee[i].selected=!aEmployee[i].selected;
	var b=aEmployee[i].selected?"on":"off";
	$("#employee_"+i).attr("class", "div_employee_"+b);
}

function confirmCreate() {
	var req={};
	req.topic_title=$("#input_topic_title").val();
	if (!req.topic_title) {
		alertMessage("请填写话题标题");
		return;
	}
	req.topic_mode=iMode;
	if (req.topic_mode==1) {
		req.employee_uid_list=[];
		for (var i=0; i<aEmployee.length; i++) {
			if (aEmployee[i].selected) {
				req.employee_uid_list.push(aEmployee[i].uid);
			}
		}
		if (req.employee_uid_list.length==0) {
			alertMessage("请选择话题参与人员");
			return;
		}
	}
	jsonRequest("discuss", "do_topic_add", req, onCreateResponse, "正在发布新话题……请稍候");
}

function onCreateResponse() {
	showTopic(0);
	aPage=["menu", "announce"];
}

function addLove() {
	var req={};
	req.topic_uid=aTopic[iTopic].uid;
	jsonRequest("discuss", "do_love_add", req, onCommentResponse);
}

function showComment() {
	pageGo("comment");
	loadComment();
	$("#div_confirm_say").hide();
}

function loadComment() {
	var req={};
	req.topic_uid=aTopic[iTopic].uid;
	jsonRequest("discuss", "load_comment_list", req, onCommentResponse, "正在获取话题留言……请稍候");
}

function onCommentResponse(res) {
	var html="";
	var topic=res.topic;
	aPicture=[];
	html+=div("div_topic_title", topic.title);
	html+=div("div_topic_date", topic.date);
	html+=div("div_topic_employee_name", topic.employee.name+" ("+topic.employee.title+")");
	html+=img("img_topic_employee_headimg", topic.employee.headimg);
	var employee=topic.employee_num>0?div("div_topic_employee", topic.employee_num, "showTopicEmployee()"):"";
	var close=topic.owner?div("div_topic_close", "关闭", "promptTopicClose()"):"";

	html+=div("div_topic_stats",
			div("div_topic_comment", topic.comment_num)+
			div("div_topic_visit", topic.visit_num)+
			div("div_topic_love"+(topic.loved?"d":""), topic.love_num, "addLove();")+
			employee+close);
	$("#div_comment_header").html(html);
	var html="";
	aComment=res.comment_list;
	for (var i=0; i<aComment.length; i++) {
		var comment=aComment[i];
		var close2=(topic.owner || comment.owner)?div("div_comment_close", "删除", "iComment="+i+"; promptCommentClose()"):"";
		html+=div("div_comment_item",
			div("div_comment_date", comment.date)+
			div("div_comment_name", comment.employee.name+" ("+comment.employee.title+")")+
			img("img_comment_headimg", comment.employee.headimg)+
			div("div_comment_content", comment.content)+ close2+
			htmlPicture(comment.picture_list));
	}
	$("#div_comment_group").html(html);
}

function promptCommentClose() {
	showConfirm("是否删除这条留言?", "confirmCommentClose()");
}

function confirmCommentClose() {
	var req={};
	req.comment_uid=aComment[iComment].uid;
	jsonRequest("discuss", "do_comment_close", req, onCommentCloseResponse, "正在删除留言……");
}

function onCommentCloseResponse(res) {
	loadComment();
}

function htmlPicture(list) {
	var html="", group="";
	if (list && list.length>0) {
		for (var j=0; j<list.length; j++) {
			aPicture.push(list[j]);
			group+=img("img_comment_picture", list[j].pre_name, "showBig("+(aPicture.length-1)+")");
		}
		html=div("div_comment_picture_group", group);
	}
	return html;
}

function showTopicEmployee() {
	pageGo("topic_employee");
	jsonRequest("discuss", "load_topic_employee_list", {topic_uid:aTopic[iTopic].uid}, onTopicEmployeeResponse, "正在获取话题参与人员……请稍候");
}

function onTopicEmployeeResponse(res) {
	aEmployee=res.employee_list;
	var html="";
	for (var i=0; i<aEmployee.length; i++) {
		var emp=aEmployee[i];
		var b=emp.invited?"on":"off";
		html+=div("div_employee_item",
			img("img_employee_headimg", emp.headimg)+
			div("div_employee_code", emp.code)+
			div("div_employee_name", emp.name)+
			div("div_employee_title", emp.title)+
			div("div_employee_"+b,"",null,"invite_"+i), "toggleInvite("+i+")" );
	}
	$("#div_topic_employee_group").html(html);
}

function toggleInvite(i) {
	aEmployee[i].invited=!aEmployee[i].invited;
	b=aEmployee[i].invited?"on":"off";
	$("#invite_"+i).attr("class", "div_employee_"+b);
}

function confirmTopicEmployeeUpdate() {
	var req={};
	req.topic_uid=aTopic[iTopic].uid;
	req.employee_uid_list=[];
	for (var i=0; i<aEmployee.length; i++) {
		if (aEmployee[i].invited) {
			req.employee_uid_list.push(aEmployee[i].uid);
		}
	}
	jsonRequest("discuss", "do_topic_employee_update", req, onTopicEmployeeUpdateResponse);
}

function onTopicEmployeeUpdateResponse(res) {
	alertMessage(res.hint);
	pageBack();
}

function showSay() {
	aPicture=[];
	$("#div_picture_group").empty();	
	$("#input_comment_content").val("");
	pageGo("say");
	$("#input_comment_content").focus();
}

function confirmSay() {
	var req={};
	req.topic_uid=aTopic[iTopic].uid;
	req.comment_content=$("#input_comment_content").val();
	req.picture_num=aPicture.length;
	if (aPicture.length>0) {
		req.picture_uid_list=[];
		for (var i=0; i<aPicture.length; i++) {
			req.picture_uid_list.push(aPicture[i].uid);
		}
	}
	if (!req.comment_content) {
		alertMessage("请填写留言内容");
	} else {	
		jsonRequest("discuss", "do_comment_add", req, onSayResponse, "正在发送留言……请稍候");
	}
}

function onSayResponse(res) {
	pageBack();
	onCommentResponse(res);
}

function pictureReady() {
	var html="";
	for (var i=0; i<aPicture.length; i++) {
		html+=div("div_picture_item", 
			img("img_picture_preview", aPicture[i].pre_name, "showBig("+i+")") );
	}
	$("#div_picture_group").html(html);	
}

function showBig(i) {
	var y=Math.floor((innerHeight-aPicture[i].big_height*innerWidth/aPicture[i].big_width)/2);
	$("#img_picture_big").attr("src", imgURL+aPicture[i].big_name);
	$("#img_picture_big").css({top:y+"px"});
	var name=aPicture[i].src_name;
	$("#div_picture_big_name").html(name);
	var kRaw=Math.floor(aPicture[i].raw_size/1024);
	var kBig=Math.floor(aPicture[i].big_size/1024);
	var info="原图: "+aPicture[i].raw_width+"x"+aPicture[i].raw_height+" ("+kRaw+"K)";
	info+=" &nbsp; 优化: "+aPicture[i].big_width+"x"+aPicture[i].big_height+" ("+kBig+"K)";
	$("#div_picture_big_info").html(info);
	$("#div_picture_big").fadeIn();
}

function hideBig() {
	$("#div_picture_big").fadeOut();
}