/*
	作者：戴铭
*/
$(document).ready(function() {
	//初始化
	var loadinghtml = '<div style="margin:20px;"><img src="img/plugin/mine/loading.gif"/>读取中...</div>';
	//如果采用ajax话的初始化
	$(".the_root").removeAttr('href').attr('class','the_root nolinklink');
	
	//发评论
	$("#addreply").click(function() {
		$("#addreply_form").show();
	});
	
	//ajax方式控制中间列表
	$(".the_root").click(function() {
		var midid = $(this).parent().attr('id');
		$("#mid .block_con").html(loadinghtml);
		$.getJSON('index.php?action=plugin&v=wave&idroot=' + midid + '&ajax=juststr&str=showmid', function(json) {
			if($("#mid").attr('id') == undefined) {
				$("#bigright").html('<div id="mid"></div>');
			}
			$("#mid").html(json['html']);
		});
	});
	
	//切换发内容类型
	var $switchnewposttype = $("#switchnewposttype");
	$switchnewposttype.children("input").click(function() {
		$switchnewposttype.children("input").removeAttr("checked");
		$(this).attr("checked", "checked");
		//发投票
		if($(this).val() == 'addpoll') {
			$.getJSON("index.php?action=plugin&v=wave&mid=" + $("#usenav").text() + "&ajax=juststr&str=poll", function(json) {
				$(".addnewpost").html(json['html']);
				$(".addnewpost").children("form").attr("action", json['path']);
			});
		}
		//发文章
		if($(this).val() == 'addarticle') {
			$.getJSON("index.php?action=plugin&v=wave&ajax=juststr&str=article", function(json) {
				$(".addnewpost").html(json['html']);
			});
		}
		//剧本
		if($(this).val() == 'addgamescript') {
			$.getJSON("index.php?action=plugin&v=wave&ajax=juststr&str=gamescript", function(json) {
				$(".addnewpost").html(json['html']);
			});
		}
	});
	
	//添加相关的内容
	$(".add_leaves").click(function() {
		if($(".add_leaves_bt").text() == '添加') {
			return;
		}
		$(this).parent().parent().append('<div id="addwikitools"><input id="leaves_name" type="text"/><a class="add_leaves_bt nolinklink">添加</a></div>');
		$("#addwikitools").children(".add_leaves_bt").click(function() {
			var $id_root = $(this).parent().parent().children(".id_root").val();
			$.getJSON('index.php?action=plugin&v=wave&idroot=' + $id_root + '&ajax=juststr&str=add_leaves&leaves=' + $("#leaves_name").val(),function(json) {
				$("#addwikitools").remove();
			});
		});
	});

	//读取相关的内容
	$li_more_leaves = $(".li_more_leaves");
	$li_more_leaves.click(function() {
		if($li_more_leaves.children(".listleaves").length) {
			$li_more_leaves.children(".listleaves").remove();
			//return;
		}

		var $id_root = $(this).children(".id_root").val();
		//ajax获取数据
		$.getJSON('index.php?action=plugin&v=wave&idroot=' + $id_root + '&ajax=juststr&str=more_leaves',function(json) {
			if($(".li_more_leaves").children(".listleaves").length) {
				return;
			}
			var moreleaveshtml = '';
			for(var leavesid in json) {
				moreleaveshtml += '<li id="' + leavesid + '"><a class="nolinklink delwikileaves">x</a> <a class="the_root nolinklink">' + json[leavesid] + '</a></li>';
			}
			moreleaveshtml = '<ul>' + moreleaveshtml + '</ul>';
			$("#" + $id_root).append('<div class="listleaves">' + moreleaveshtml + '</div>');
			//ajax方式控制中间列表
			$(".the_root").click(function() {
				var midid = $(this).parent().attr('id');
				$("#mid .block_con").html(loadinghtml);
				$.getJSON('index.php?action=plugin&v=wave&idroot=' + midid + '&ajax=juststr&str=showmid', function(json) {
					if($("#mid").attr('id') == undefined) {
						$("#bigright").html('<div id="mid"></div>');
					}
					$("#mid").html(json['html']);
				});
			});
			
			//删除子导航
			$(".delwikileaves").click(function() {
				var leavesid = $(this).parent().attr('id');
				$.getJSON('index.php?action=plugin&v=wave&idleaves=' + leavesid + '&ajax=juststr&str=delleaves&idroot=' + $id_root, function(json) {
					$("#" + leavesid).remove();
				});
			});
		});
		
	});
	
	//查看单个内容
	$(".fullviewonepost").wBox({title:'',isFrame:true,iframeWH:{width:950,height:450}});
});
