$(document).ready(function(){
	
	$('#verify').one('click', function(){
		var verify = $('<img>').attr({'src':'/Verify', 'id':'verifyimg', 'title':'看不清楚?点击刷新'}).css({'margin-left':'1em','cursor':'pointer','vertical-align':'bottom'});
		$(this).next('span').text('看不清楚点击图片刷新');
		$(this).after(verify);
	});

	$('#verifyimg').live('click', function(){
		$(this).attr('src', '/Verify/show/temp/'+Math.random());
	});
	
	$('#sendPost').click(function(){
		if ($('#subject').val() == '') {
			$(this).next('span').remove().end().after("<span style='margin-left:2em;color:red'>回复标题不能为空</span>");
			return false;
		}
				
		if ($('#content').val().length =='' || $('#content').val().length < 10) {
			$(this).next('span').remove().end().after("<span style='margin-left:2em;color:red'>回复内容不能为空或者少于10个字符</span>");
			$('#content').focus();
			return false;
		}
		
		if ($('#verify').length && $('#verify').val() == '') {
			$(this).next('span').remove().end().after("<span style='margin-left:2em;color:red'>请输入验证码</span>");
			$('#verify').click();
			return false;
		}
/*			
		$(this).attr('disabled', true);
		$(this).next('span').remove().end().after("<span style='margin-left:2em;color:#000000'>问题提交中, 请稍等...</span>");
		
		$.ajax({
			type: "POST",
			url: $('#postForm').attr('action'),
			data: $("#postForm").ajaxdata(),
			timeout: 10000,
			cache: false,
			dataType: 'json',
			success: function(json){
				if (json.status == 1) {				
					$('#sendPost').next('span').remove().end().after("<span style='color:#009900;margin-left:2em;font-weight:bold;height:20px;line-height:20px'>"+json.info+"</span>");
					setTimeout(function(){$.reload();}, 1000);
					$('#sendPost').attr('disabled', false);
				}else{
					$('#sendPost').next('span').remove().end().after("<span style='color:red;margin-left:2em;font-weight:bold;height:20px;line-height:20px'>"+json.info+"</span>");
					$('#sendPost').attr('disabled', false);
				}
			},
			error: function(){
				$('#sendPost').next('span').remove().end().after("<span style='color:red;margin-left:2em;font-weight:bold;height:20px;line-height:20px'>系统或者网络有问题, 请稍候再试。</span>");
				$(this).attr('disabled', false);
			}
		});	*/
		//return false;
	});	

	$('#thread-inform').click(function(){
		$('.boxy-wrapper').remove();
		var thread = $(this).parents('.thread').eq(0);
		var tid = thread.attr('id').substr(7);
		var informHandler = function(){
			$.get('/Thread/inform/tid/'+tid, function(json){
				if (json.status) {
					jQuery.popMsg('成功举报该主题!',1,1);
				}else {
					jQuery.popMsg('系统错误, 操作失败!',0,1);
				}
			});
		}
		new Boxy.confirm('<b>[举报主题]</b>“ '+thread.find('h1').text().substr(6)+'”', informHandler);			
	});
	
	$('#thread-reply').click(function(){
		window.location.hash = 'reply';
		$('#content').focus();
	});
	
	$('#thread-manage').click(function(){
		$('#manage').toggle();
	});
	
	$('#thread-digest').click(function(){
		var thread = $(this).parents('.thread').eq(0);
		var tid = thread.attr('id').substr(7);
		var win = $.boxy('主题：“'+thread.find('h1').text().substr(6)+'”');
		$('#boxy-body').append("操作：<input type='radio' name='digest' value='1'/>设置精华 <input type='radio' name='digest' value='0'/>取消精华");
		$("#boxy input[value='"+$(this).attr('digest')+"']").attr('checked', true);
		$('#operate').click(function(){
			$.get('/Thread/setdigest/tid/'+tid+'/digest/'+$("#boxy input:checked").val(), function(json){
				if (json.status) {
					win.remove();
					$.popMsg('操作成功',1,1);
					$.reload();
				}else {
					win.remove();
					$.popMsg('系统错误, 操作失败!',0,1);
				}				
			});
		});
	});
	
	$('#thread-order').click(function(){
		var thread = $(this).parents('.thread').eq(0);
		var tid = thread.attr('id').substr(7);
		var win = $.boxy('主题：“'+thread.find('h1').text().substr(6)+'”');
		$('#boxy-body').append("置顶：<input type='radio' name='uptime' value='86400'/>一天 <input type='radio' name='uptime' value='259200'/>三天 <input type='radio' name='uptime' value='604800'/>一周 <input type='radio' name='uptime' value='2592000'/>一月 <input type='radio' name='uptime' value='7776000'/>三月 <input type='radio' name='uptime' value='0'/>取消");		
		$("#boxy input[value='"+$(this).attr('uptime')+"']").attr('checked', true);
		$('#operate').click(function(){
			$.get('/Thread/uptime/tid/'+tid+'/uptime/'+$("#boxy input:checked").val(), function(json){
				if (json.status) {
					win.remove();
					$.popMsg('操作成功',1,1);
					$.reload();
				}else {
					win.remove();
					$.popMsg('系统错误, 操作失败',0,1);
				}				
			});
		});		
	});

	$('#thread-close').click(function(){
		var thread = $(this).parents('.thread').eq(0);
		var tid = thread.attr('id').substr(7);
		var win = $.boxy('主题：“'+thread.find('h1').text().substr(6)+'”');
		$('#boxy-body').append("操作：<input type='radio' name='closed' value='0'/>打开回复 <input type='radio' name='closed' value='1'/>关闭回复");		
		$("#boxy input[value='"+$(this).attr('closed')+"']").attr('checked', true);
		$('#operate').click(function(){
			$.get('/Thread/closed/tid/'+tid+'/closed/'+$("#boxy input:checked").val(), function(json){
				if (json.status) {
					win.remove();
					$.popMsg('操作成功',1,1);
					$.reload();
				}else {
					win.remove();
					$.popMsg('系统错误, 操作失败',0,1);
				}				
			});
		});		
	});
	
	$('#thread-resolve').click(function(){
		$('.boxy-wrapper').remove();
		var thread = $(this).parents('.thread').eq(0);
		var tid = thread.attr('id').substr(7);
		var deleteHandler = function(){
			$.getJSON('/Thread/resolve/tid/'+tid, function(json){
				if (json.status) {
					$.reload();
				}else {
					$.popMsg('系统错误, 操作失败',0,1);
				}
			});
		}
		new Boxy.confirm('<b>[问题解决]</b> “'+thread.find('h1').text().substr(6)+'” 已经得到解决？', deleteHandler);			
	});
	
	$('#thread-delete').click(function(){
		$('.boxy-wrapper').remove();
		var thread = $(this).parents('.thread').eq(0);
		var tid = thread.attr('id').substr(7);
		var deleteHandler = function(){
			$.getJSON('/Thread/delete/tid/'+tid, function(json){
				if (json.status) {
					$.reload();
				}else {
					$.popMsg('系统错误, 删除失败',0,1);
				}
			});
		}
		new Boxy.confirm('<b>[删除主题]</b> “'+thread.find('h1').text().substr(6)+'”', deleteHandler);
	});

	$('#thread-highlight').click(function(){
		var thread = $(this).parents('.thread').eq(0);
		var tid = thread.attr('id').substr(7);
		var win = $.boxy('主题：“'+thread.find('h1').text().substr(6)+'”');
		$('#boxy-body').append("操作：<input type='radio' name='highlight' value='1'/><b class='highlight1'>高亮一</b> <input type='radio' name='highlight' value='2'/><b class='highlight2'>高亮二</b> <input type='radio' name='highlight' value='3'/><b class='highlight3'>高亮三</b> <input type='radio' name='highlight' value='0'/>无");		
		$("#boxy input[value='"+$(this).attr('highlight')+"']").attr('checked', true);
		$('#operate').click(function(){
			$.get('/Thread/highlight/tid/'+tid+'/highlight/'+$("#boxy input:checked").val(), function(json){
				if (json.status) {
					win.remove();
					$.popMsg('操作成功',1,1);
					$.reload();
				}else {
					win.remove();
					$.popMsg('系统错误, 操作失败',0,1);
				}				
			});
		});		
	});

	$('#thread-move').click(function(){
		var thread = $(this).parents('.thread').eq(0);
		var tid = thread.attr('id').substr(7);
		var win = $.boxy('移动主题“'+thread.find('h1').text().substr(6)+'”');
		$('#boxy-body').append("<p>请选择栏目</p><div style='float:left;width:150px' id='forum'><select size='10' style='width:120px'></select></div><div style='float:right;width:150px' id='sub-forum'><select size='10' style='width:120px'></select></div><div style='clear:both'></div>");
		$.get('/Server/forums', function(json){
			if (json.status) {
				var forum = [];
				var data = json.data;
				$.each(data, function(i,f){
					if (f.fup == 0) {
						forum.push({'fid':f.fid, 'name':f.name});
						delete data[f.fid];
					};		
				});
				$.each(forum, function(i,f){
					var sub = [];
					$.each(data, function(k,v){
						if (v.fup == f.fid) {
							sub.push({'fid':v.fid, 'name':v.name, 'top':f.name});
							delete data[v.fid];
						}
					});
					$("<option value='"+f.fid+"'>"+f.name+"</option>").data('sub', sub).appendTo("#forum select");
				});
				$("#forum select").change(function(){
					$("#sub-forum select").empty();
					$.each($('option:selected', $(this)).data('sub'), function(i,f){
						$("#sub-forum select").append("<option value='"+f.fid+"'>"+f.name+"</option>");
					});
				});
			}else {
				win.setContent("<p style='padding:20px;color:red'>栏目信息加载出错</p>");
			}
		});
		$('#operate').click(function(){
			var fid = $("#sub-forum select").val();
			if (!fid) {
				$('#operate').parent().nextAll().remove().end().after("<span style='color:red;margin-left:1em'>请选择子栏目</span>");
				return false;
			}
			$.get('/Thread/move/tid/'+tid+'/fid/'+fid, function(json){
				if (json.status) {
					win.remove();
					$.popMsg('操作成功',1,1);
					$.reload();
				}else {
					win.remove();
					$.popMsg('系统错误, 操作失败',0,1);
				}				
			});
		});		
	});

	$('#thread-edit').click(function(){
		var thread = $(this).parents('.thread').eq(0);
		var tid = thread.attr('id').substr(7);
		var content = thread.find('.thread-content').eq(0);
		var tempHtml = content.html();
		$.get('/Thread/content/tid/'+tid, function(json){
			if (json.status) {
				var editor = $("<textarea class='editor-content' cols='100' rows='10' name='editor-content'></textarea>");
				editor.val(json.info);
				content.empty().append(editor);
				editor.focus();
				$('.editor-content').blur(function(){
					if ($(this).val() != json.info) {
						$.post('/Thread/edit', {'ajax':'1', 'tid':tid, 'content':$(this).val()}, function(json){
							if (json.status) {
								$.popMsg(json.info,1,1);
								content.empty().html(editor.nl2p().replace('<p>[quote]', '<div class="quote"><p>').replace('[/quote]</p>', '</p></div>'));
							} else {
								$.popMsg(json.info,0,1);
								content.empty().html(tempHtml);
							}
						});
					}else {
						content.empty().html(tempHtml);
					}
				});					
			}else {
				$.popMsg(json.info,0,1);
			}
		});
	});

	$('.op-inform').click(function(){
		$('.boxy-wrapper').remove();
		var pid = $(this).parents('.post').attr('id').substr(5);
		var post = $('#post_'+pid);
		var informHandler = function(){
			$.get('/Post/inform/pid/'+pid, function(json){
				if (json.status) {
					jQuery.popMsg('成功举报该帖子',1,1);
				}else {
					jQuery.popMsg('系统错误, 操作失败',0,1);
				}
			});
		}
		new Boxy.confirm('<b>[举报帖子]</b> “'+post.find('h2').text().substr(3)+'”', informHandler);		
	});
	
	$('.op-delete').click(function(){
		$('.boxy-wrapper').remove();
		var pid = $(this).parents('.post').attr('id').substr(5);
		var post = $('#post_'+pid);
		var deleteHandler = function(){
			$.getJSON('/Post/delete/pid/'+pid, function(json){
				if (json.status) {
					jQuery.reload();
				}else {
					jQuery.popMsg('系统错误, 删除失败!',0,1);
				}
			});
		}
		new Boxy.confirm('<b>[删除回复]</b> “'+post.find('h2').text().substr(3)+'”', deleteHandler);
	});
	
	$('.op-edit').click(function(){
		var pid = $(this).parents('.post').attr('id').substr(5);
		var content = $(this).parent().parent().prev('.post-content').eq(0);
		var tempHtml = content.html();
		var tempText = '';
		$.get('/Post/content/pid/'+pid, function(json){
			if (json.status) {
				tempText = json.info;
				var editor = $("<textarea class='editor-content' cols='100' rows='6' name='editor-content'></textarea>");
				editor.val(tempText);
				content.empty().append(editor);
				editor.focus();
				
				$('.editor-content').blur(function(){
					if ($(this).val() != tempText) {
						$.post('/Post/edit', {'ajax':'1', 'pid':pid, 'content':$(this).val()}, function(json){
							if (json.status) {
								$.popMsg(json.info,1,1);
								content.empty().html(editor.nl2p().replace('<p>[quote]', '<div class="quote"><p>').replace('[/quote]</p>', '</p></div>'));
							} else {
								$.popMsg(json.info,0,1);
								content.empty().html(tempHtml);
							}
						});
					}else {
						content.empty().html(tempHtml);
					}
				});				
			}else {
				$.popMsg(json.info,0,1);
			}
		});		
	});
	
	$('.op-quote').click(function(){
		var pid = $(this).parents('.post').attr('id').substr(5);
		var post = $('#post_'+pid);
		var quote = post.find('.post-content').eq(0);
		var floor = post.find('.post-header span').text();
		var user = post.find('.post-info span').eq(0).text().substr(3);
		$('#content').val("[quote]"+floor+user+"\n"+quote.children('p').p2nl().substr(0,100)+"..[/quote]\n").end().focus();
		window.location.hash = 'reply';
	});
	
	$('.op-reply').click(function(){
		var pid = $(this).parents('.post').attr('id').substr(5);
		var post = $('#post_'+pid);		
		var floor = post.find('.post-header span').text();
		var title = post.find('.post-header h2').text().substr(3);
		$('#subject').val(floor+title);
		window.location.hash = 'reply';
	});
	
});