shine=function(){
	$("#west #menu #ulmenu li.active")
	.animate({ backgroundColor: "orange" }, 1000)
	.animate({ backgroundColor: "yellow" }, 1000)
	.animate({ backgroundColor: "green" }, 1000)
	.animate({ backgroundColor: "blue" }, 1000)
	.animate({ backgroundColor: "indigo" }, 1000)
	.animate({ backgroundColor: "violet" }, 1000)
	.animate({ backgroundColor: "red" }, 1000);
};
rgb = function(){
	$('#red').animate({"width":Math.round(Math.random()*$(window).width()/2)},"slow");
	$('#green').animate({"width":Math.round(Math.random()*$(window).width()/2)},"slow");
	$('#blue').animate({"width":Math.round(Math.random()*$(window).width()/2)},"slow");
	if ($("#red").width()>$("#green").width()){
		maxColor = "red";
	}else {
		maxColor = "green";
	}
	if ($("#"+ maxColor).width()>$("#blue").width()){
		maxColor = maxColor;
	}else {
		maxColor = "blue";
	}
	$("#north p").css("color",maxColor);
};
//window.setInterval(rgb,1000);

var countdown=function(){
	$("#countdown p").countdown({seconds:60,callback:'countdown()'});
};
/*尽量不*/
var layout=function(){
	$("#north").corner("bl br")
	$("#main").height($("#all").height()-40).width($("#all").width());
	$("#west").height($("#main").height()-5).corner("tr br");
	$("#menu").height($("#west").height()-160-70-50);
	
	var width=$("#main").width()-170-170-20;
	var height=$("#main").height()-15;//#center设置了padding:5px
	$("#center").corner().animate({width:width,height:height},'slow');
	
	$("#content").height($("#center").height()-10).width($("#center").width()-20);
	
	$("#east").height($("#main").height()-5).corner("tl bl");
	
	$("#south").corner('tl tr');
};
var menua=function(id,title){
	var a="<li id='"+id+"' ><a href='#"+id+"' >"+title+"</a></li>";
	return a;
}
var suba=function(id,title){
	var a="<li><a id='"+id+"' href='#"+id+"'>"+title+"</a><ul id='"+id+"_children'></ul></li>";
	return a;
};
var subs=function(path,sub_s){
	$.each(sub_s,function(index,sub){
		$("#ulmenu"+path).append(suba(sub.id,sub.title));
		$("#ulmenu #"+sub.id).click(function(){
			$("#"+sub.id).parent().parent().children().find("ul").each(function(){
				$("#"+$(this)[0].id).slideUp("slow");	
			});
			$("#"+sub.id+"_children").slideDown('slow');
			post(sub.id);
		});
		
		/* 添加subs */
		if(sub.subs){
			subs(path+" #"+sub.id+"_children",sub.subs);
		};
		/*先把subs隐藏*/
		$("#"+sub.id+"_children").each(function(){
			$(this).slideUp("slow");
		});
		
		/* 添加children */
		if(sub.children){
			$.each(sub.children,function(index,child){
				$("#ulmenu #"+sub.id+"_children").append(menua(child.id,child.title));
				$("#"+sub.id+"_children #"+child.id).click(function(){
					post(child.id);	
				});
			});
		};
	});
};

var menu=function(){
	$.get("./json/menu.json",function(menus){
		subs("",menus);
		//$('#ulmenu li a').ahover({moveSpeed:100,hoverEffect:function(){
	      //  $(this).css({opacity:0.99}).animate({opacity:0.5},750).animate({opacity:0.99},750).dequeue();
	        //$(this).queue(arguments.callee);
	    //}});		
	});
};

/* 对＃center #path内容进行更新,并自动做尺寸上的调整,添加事件处理函数 */
/* 在这里处理的都是当前post的上一级,添加的类是parent */
var patha = function(id,title,summary){
	var html = "<li class='parent' id='"+id+"' ><a id='"+id+"' href='#"+id+"' title='"+summary+"'>"+title+"</a></li>";
	return html;
};
/* 在这里要聪明一点,当要生成的path项目已经存在的时候,不要生成,也不要再做尺寸的动画 */
/* 如果是向上跳的话,他的后面的路径不要删除 */
var path = function(path_level,paths){
	$("#path").html("");
	 for(var i = 0 ; i < path_level; i++){
	 	if (paths[i] != undefined) {
	 		$("#path").append(patha(paths[i].id, paths[i].title, paths[i].summary));
		};
	};
	$("#path li.parent a").each(function(){
		if($(this).width() < 80){
			$(this).width(80);
		};
	});

	/* 当点击这里的项目的时候,#north会发生定位上的错误,也不知道是哪里引起的 */
	$("#path li a").each(function(){
		$(this).click(function(){
			post($(this)[0].id);
		});
	});
};
var a = function(id,title,summary){
    var activea_html = "<a id='"+id+"' title='"+ summary + "' >"+title+"</a>";
    return activea_html;
};
var active = function(id,title,summary,path_level){
	var active_html = "";
	if(id != "root"){
		$("#active").html(a(id,title,summary));
		var active_width = $("#center").width()-path_level*140 -50;
		$("#active").animate({width:active_width},"slow");var active_width = null;
		$("#active").css({opacity:0.99}).animate({opacity:0.5},750).animate({opacity:0.99},750).dequeue();
		$(".active").toggleClass("active");
		$("#ulmenu #"+id).toggleClass("active");
	};
};

/* 更新＃center #content内容,并自动做尺寸上的调整 */;
var content = function(content){
	$("#content").html(content);
	
	$("#content h2").each(function(){
		$(this).css("width",($(this).text().realLength()+2).toString()+"em");
	});
	
	if(children_count > 0){
		$("#content").append("<div id='children'></div>");
	};
	$("#top").width($("#center").width()-20);
	
	/* 判断是不是有相关的那个菜单,如果没有的话,切换尺寸 */
	if(relates_count > 0){
		var content_height = $("#center").height()-$("#top").height()-$("#relate").height()-40;
		$("#content").width($("#center").width()-20).animate({"height":content_height},'slow');var content_height=null;	
		$("#relate").width($("#content").width()).animate({height:"show"},'slow');
	}else{
		var content_height = $("#center").height()-$("#top").height()-20;
		$("#content").width($("#center").width()-20).animate({height:content_height},'slow');var content_height=null;
	};
	
	/* 处理content里每个段落的事件处理 */
	$("#content div p").hoverIntent(function(){
	},function(){
	});
};
childa = function(id,title,summary){
	var childa_html ="<div id='"+ id+"'><a class='title'>" + title +"</a>";
	childa_html += "<a class='summary'>"+summary+"</a></div>";
	return childa_html;
};
/*  把子类的相关链接添加到＃center #content里面去 */
var children = function(child_ren){
	$.each(child_ren,function(index,child){
		$("#children").append(childa(child.id,child.title,child.summary))
	});

	$("#children").width($("#content").width()-40);

    $("#children div").each(function(){
        $(this).click(function(){
            post($(this)[0].id);
        });
    });
    $("#children div a.summary").hide("slow");
    
    $("#children div a.title").hoverIntent(function(){
    	/*这里存在问题,如果这个框被float到下一行的话,就会出现很麻烦的问题,一直循环个不停*/
    	$(this).next().show('slow');
    },function(){
    	$(this).next().hide('slow');
    });
};

/* 在#center #rc #relate 里生成相关链接 */
var relatea = function(id,title,summary){
	var relate_html = "<a id='"+ id + "' title='" + summary + "' >" + title +"</a>";
	return relate_html;
};
/* 在这里应该聪明一些,如果没有相关的东西的话,就直接跳到comment() */
var relate = function(relates_count,relates){
	if($("#relate").is(":hidden")){
		$("#relate").animate({height:"show"},'fast');		
	};
	$("#relate").html("");
	$.each(relates,function(index,relate){
		$("#relate").append(relatea(relate.id,relate.title,relate.summary));
	});
	var count=relates.length;
	var width = Math.round(($("#relate").width()-3*(count-1))/count -5);
	$("#relate a").each(function(){
		$(this).animate({width:width},'slow');
		
		$(this).click(function(){
			$(this).siblings().animate({width:140},'slow');
			$(this).animate({width:$("#relate").width()-10},'slow');
			var id=$(this)[0].id;
			$("#ulmenu #"+id).click();
			
			post(id);
		});
	});
};

var posts = new Array();//用来缓存数据的数组
var currentPost = null;//在任何时候都可以取得当前post的数据
/* 为了防止在post()这个方法里面对一个空的数组进行遍历,在这里先要不做任何判断的填充一个数据进去 */
var firstpost = function(id){
	$.getJSON('./json/'+id+'.json',function(post){
		currentPost=post;
		tpost = post;
		posts[0]=tpost;
		render(tpost);
	});
};

var post = function(id){
	/* 如果在posts里找到想要更新的内容,就直接更新它 */
	$.each(posts,function(index,index_post){
		if(id == index_post.id){
			update(index_post);
		};
	});
	/* 如果没有完成更新的话,就去服务器加载这个文件过来 */
	if(currentPost.id !=id){
		$.get('./json/'+id+'json',function(post){
			tpost = post;
			posts[posts.length] = tpost;
			update(tpost);
		});
	};
};
var render = function(post){
	//path(post.path_level,post.path);
	//active(post.id,post.title,post.summary,post.path_level);
	content(post.content);
	//children(post.children);
	//relate(post.relates_count,post.relates);
	currentPost = post;
};
var update = function(post){
	if(currentPost.id != post.id){
		render(post);
	};
}

var searcha = function(id,title,summary){
	var searcha_html = "<li id='"+id +"' >" + id + "</li>";
	searcha_html += "<li id='"+id +"' >" + title + "</li>";
	searcha_html +="<li id='"+id +"' >" + summary + "</li>";
	return searcha_html;
};
var search = function(){
	var searchinput = "#main #east #search #searchform #searchinputbox #searchinput";
	var searchlistbox = "#main #east #search #searchlistbox";
	$.getJSON("./json/allpost.json",function(allpost){
		for (var i =0 ;i< allpost.posts_counts;i++){
			$(searchlistbox + " #searchlist").append(searcha(allpost.posts[i].id,allpost.posts[i].title,allpost.posts[i].summary));
		};
		
		$(searchlistbox +" #searchlist li").each(function(){
			$(this).click(function(){
				post($(this)[0].id);
			});
		});
		
		$(searchinput).liveUpdate(searchlistbox+" #searchlist")
		.focus(function(){
			$(this).val("");
		})
		.focus()
		.blur(function(){
			$(this).val("寻找");
		});
	});
};