$(function() {
	//搜索框
	var keyWord = $("input#s");
	if (keyWord.val().length >= 1 ) keyWord.prev("label").hide();
	keyWord.focus(function(){ keyWord.prev("label").hide(); })
	keyWord.blur(function(){
		if (keyWord.val() == "") {
			keyWord.val("");
			keyWord.prev("label").show();
		}
	});

	//主菜单下拉
	$("#nav li li").has('ul').children("a").append(" &raquo;");
	$("#nav li").has('ul').hover(
		function() {
			if ($.browser.msie) $(this).children("ul").fadeIn(200);
			else $(this).children("ul").slideDown(200);
		},
		function() {
			if ($.browser.msie) $(this).children("ul").fadeOut(200);
			else $(this).children("ul").slideUp(200);
		}
	);

	//赞助我
	$("#sponsor").html($("#sponsor-me").attr("title"));
	$("#sponsor-me").attr("title","");
	$("#sponsor-me").hover(
		function() {
			$("#sponsor").slideDown(200);
			$("#sponsor").attr("style","display:block;");//这一行是为了解决 jQuery IE8 bug
		},
		function() { $("#sponsor").fadeOut(200); }
	);

	//列表隔行换色
	$(".list li:odd").addClass("odd");

	//初始化选项卡
	$("#choice ul:first").idTabs();
	$("#side-cmt ul:first").idTabs();
	$("#column-nav ul:first").idTabs();

	//首页相册文字
	$("#home-album li a").hover(
		function() {
			$(this).children("span").slideDown(200);
		},
		function() {
			$(this).children("span").slideUp(200);
		}
	);

	//鼠标经过tips效果
	$("#container a").not("#home-album li a").mouseover(function(tips){
		this.tipsTxt = this.title;
		this.tipsTxt = (this.tipsTxt.length>100?this.tipsTxt.toString().substring(0,100)+"...":this.tipsTxt);
			if (this.tipsTxt){
				this.tipsUrl = this.href;
				this.title = '';
				var tips = '<div id="tips"><p>' + this.tipsTxt + '</p><p><em>' + this.tipsUrl + '</em></p></div>';
				$('body').append(tips);
				$('#tips').css({"opacity":"0.8"})
			}
		}).mouseout(function(){
			this.title=this.tipsTxt;
			$('#tips').remove();
		}).mousemove(function(tip){ $('#tips').css({"top":(tip.pageY+22)+"px","left":(tip.pageX-10)+"px"});
	});
	//鼠标经过tips效果结束

	//彩色tag云
	tagColor(".tags a");	

	//新窗口打开
	$(function() { $("a[rel*='external']").attr("target","_blank") });

	//滑动到顶部
	$('#to-top').click(function() { $("html,body,#wrap").animate({ scrollTop: 0 }, 1000); });
	var showDistance = 50;//距离顶端多少距离开始显示to-top
	$(window).scroll(function(){
		if($(this).scrollTop() < showDistance){
			$("#to-top").fadeOut("fast");
		}else{
			$("#to-top").fadeIn("fast");
		}
	});

});

	//Ctrl+Enter回复
$(document).keypress(function(e){
	if(e.ctrlKey && e.which == 13 || e.which == 10) {
		$("#submit").click();
	}
})

//图片渐显
function TsLazyload(templateUrl) {
	if(window.XMLHttpRequest){
		$(function() {
			$("img").not("#slide-pic img, #respond-form img, #side-cmtli-2 img, .gallery img").lazyload({
				placeholder: templateUrl,
				effect: "fadeIn"
			});
		});
	}
}

//闪动联系我
function TsFlicker(getObject,times) {
	if (times == null) times = 5;
	for (i = 0; i<times; i++) {
		$("#" + getObject).fadeOut(200);
		$("#" + getObject).fadeIn(50);
	}
}

//彩色tag
function tagColor(getObject) {
	var colors = ["#0cf","#06f","#900","#990","#f36","#639","#60f","#0c0","#f90","#c66","#6cc","#f0f","#00f","#333"];
	var tags = $(getObject);
	var tagCount = tags.length;
	for ( i=0; i<tagCount; i++ ) {
		n = Math.floor( Math.random() * colors.length );
    	tags.eq(i).css({"color":colors[n]});
	}
}

//评论表情
function ts_smilie(tag) {
	var myField;
	tag = ' ' + tag + ' ';
		if (document.getElementById('comment') && document.getElementById('comment').type == 'textarea') {
		myField = document.getElementById('comment');
	} else {
		return false;
	}
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = tag;
		myField.focus();
	}
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		myField.value = myField.value.substring(0, startPos)
						+ tag
						+ myField.value.substring(endPos, myField.value.length);
		cursorPos += tag.length;
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
	}
	else {
		myField.value += tag;
		myField.focus();
	}
}
