$(document).ready(function() {
	$(".xtabber .tabcontent>div").not("#tab1").hide(); 
	$(".xtabber .tab a").click(function () {
		var xone = $(this).attr("ref");
		$(".xtabber .tabcontent>div").not(xone).hide(); 
		$(".xtabber .tabcontent>div"+xone).show(); 
	});	

	$(".ytabber ul").hide(); 
	$(".ytabber ul:first").show();
	$(".ytabber h3 a").click(function () {
		var yone = $(this).attr("ref");
		$(".ytabber ul").not(yone).hide("fast"); 
		$(".ytabber ul"+yone).slideDown("slow"); 
	});

	$("a[href^='http://myfairland.net']").attr("rel","inlinks");
	$("a[href^='#']").attr("rel","inlinks");
	$("a[ref]").attr("rel","no");
	$("a[rel!='inlinks'][rel!='no']").attr("title","点击在新窗口打开此链接").click(function(){window.open(this.href);return false;});
	$("a[rel='inlinks']").click(function(){
		var postLoad='<div id="postload" class="load">正在加载...</div>';
		$("#header").after(postLoad);
		$("#postload").hide().slideDown("slow");
	});
		
	$("li.comment").hover(
		function(){
			$(this).find("div.reply").html("<a>回复</a>").fadeIn();
		},
		function(){
			$("div.reply").fadeOut();
	});
	$("input#submit").after("<a id='cancel-reply'>还是不说了</a>");
	$("#cancel-reply").hide();
	$("div.reply").click(function(){
        var replyTo = $(this).parent().find("cite").text(), thisOne = $(this).parent();	
		$("div#respond").insertAfter(thisOne).fadeIn();
		$("div#respond>h3").html("回复 "+replyTo);
		var commentVal = $("textarea#comment").focus().val();
        replyTo = "<a href='#"+thisOne.parent().attr("id")+"'>"+replyTo+"</a>";
		$("textarea#comment").focus().val(commentVal+"@"+replyTo+":\n");
		$("#cancel-reply").show();
	});
	$("#cancel-reply").click(function(){
		$(this).hide();
		$("div#respond").insertAfter($("div.comment")).fadeIn();	
		$("div#respond>h3").html("发表评论");	
	});
	
	$("#comment").focus(function(){
		var isCtrl = false;
		$("#comment").keyup(function(key){if(key.which == 17) isCtrl=false;}).keydown(function (key) {
			if(key.which == 17) isCtrl=true;
			if (key.which == 13 && isCtrl == true){
				$("#commentform").submit();
				return false;
			}
		});
	});

	if ($('#commentform').length) {
	    $('#commentform').submit(function(){ 
	    	jQuery.ajax({
	            url: 'http://myfairland.net/wp-content/themes/green-box/comments-ajax.php',
	            data: $('#commentform').serialize(), 
	            type: 'POST',
	            beforeSend: function() {
	        		$('#respond').insertAfter($("div.comment"));
					$("div#respond>h3").html("发表评论");	
	            	var comLoad='<div id="commentload" class="load">正在提交评论...</div>'; 
	            	$('#tab1>.commentlist').after(comLoad);
	            	$('#commentload').hide().slideDown();
	            },
	            error: function(request) {
	            	$('#commentload').hide();
	            	var comError='<div id="commenterror"></div>';
	            	$('#tab1>.commentlist').after(comError);
	            	$('#commenterror').hide().show("slow").html(request.responseText); 
	            },
	        	success: function(data) {
	        		$('textarea').each(function(){
	        			this.value='';
	        		});
	        		$('#commenterror').hide().html();
	        		$('#tab1>.commentlist').append(data);
	        		$('#tab1>.commentlist li:last').hide().slideDown("slow");
	        		$('#commentload').slideUp("slow");
	        		$('#commentform:input').attr('disabled', true);
	        		setTimeout(function() {
                		$('#commentform:input').removeAttr('disabled');
                	}, 10000);
	        	}
        	});
       		return false;
   		});
   	}

	$("h3#blogroll").bind("click", function(){
		$("h3#blogroll").unbind("click");
		$.get("http://myfairland.net/wp-content/mycache/blogroll", function(data){
			var num = $(data).length;
			var arr = new Array;
			for (i=0;i<num;i++){arr[i]=i+1};
			arr.sort(function(){ return 0.5 - Math.random();});
			for(i=0;i<10;i++){
				var li = $(data).filter("li").eq(arr[i]);
				$("ul.blogroll").append(li);
			}
			$("ul.blogroll").append("<a href='http://myfairland.net/links/' style='float:right'>全部链接</a>");
		});
	});
});
