// JavaScript Document
$.fn.extend({
	Scroll:function(opt,callback){
		//参数初始化
		if(!opt) var opt={};
		var _this=this.eq(0).find("ul:first");
		var lineH=_this.find("li:first").height(), //获取行高

		line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), //每次滚动的行数，默认为一屏，即父容器高度
		speed=opt.speed?parseInt(opt.speed,10):500, //卷动速度，数值越大，速度越慢（毫秒）
		timer=opt.timer?parseInt(opt.timer,10):3000; //滚动的时间间隔（毫秒）

		if(line==0) line=1;
		var upHeight=0-line*lineH;
		//滚动函数
		scrollUp=function(){
				_this.animate({
						marginTop:upHeight
				},speed,function(){
						for(i=1;i<=line;i++){
								_this.find("li:first").appendTo(_this);
						}
						_this.css({marginTop:0});
				});
		}
		//鼠标事件绑定
		_this.hover(function(){
				clearInterval(timerID);
		},function(){
				timerID=setInterval("scrollUp()",timer);
		}).mouseout();
	}
});

$(function(){
	$("#submit").click(function(){
		if($("#arcmsg").val()==''){
			alert("您的评论内容不能为空哦 :)");
			return false;
		}
		$.post("/comment/dopost",{aid:article_id,msg:$("#arcmsg").val()});

		$("#comment_new").html('<span class="myself">我</span>'+$("#arcmsg").val()).show();
		$("#arcmsg").addClass('arcmsg').val("");
		$("#submit").attr('disabled','disabled');

		return false;
	});

	$("#index-feed-list").Scroll({line:4,speed:1000,timer:5000});
	
	$("#smallfont").click(function(){
		ChangeFontSize("small");
	});
	$("#bigfont").click(function(){
		ChangeFontSize("big");
	});
	$("#btnsearch").click(function(){
		dosearch();
	});

	$("#keywords").val("搜索爱情散文").focus(function(){if($(this).val()=="搜索爱情散文") $(this).val("");}).blur(function(){if($(this).val()=='') $(this).val("搜索爱情散文");});
});

//判断当前用户是否登录
function CheckLogin(){
	$.get("http://www.sanwen.net/public/checklogin/",function(user){
		if(user.data){
			userLogined(user);
		}else{
			$("#welcome").slideDown();
		}
		
	},'json');
}

//如果已登录
function userLogined(user){
	$("#welcome").html('<div id="new-wel"><span class="right"><a href="http://u.sanwen.net/'+user.data.uid+'.html" target="_blank">我的空间主页</a> <a href="http://u.sanwen.net/manage/">管理中心</a> <a href="/public/doexit/">退出</a></span>你好 <strong>'+user.data.penname+'</strong> 欢迎回来！<a href="http://u.sanwen.net/manage/message">短消息</a> <a href="http://u.sanwen.net/manage/favorite">收藏夹</a> <a href="http://u.sanwen.net/manage/articleadd">发表文章</a> <a href="http://www.sanwen.net/shortcut.php"><font color=red>创建桌面快捷方式</font></a></div>').slideDown();
}

function dosearch(){
	var keywords = $("#keywords").val();
	window.open("http://www.sanwen.net/search/"+keywords);
}

function ChangeFontSize(action){
	if(action=='small'){
		$("#article_content").css('font-size',14);
		$("#smallfont").css('color','gray');
		$("#bigfont").css('color','#336699');
	}else if(action=='big'){
		$("#article_content").css('font-size',16);
		$("#bigfont").css('color','gray');
		$("#smallfont").css('color','#336699');
	}
}

function fontsize(size){
	$("#article_content").css('font-size',size);
}

function DiggPost(action){
	var saveid = GetCookie('diggid');
	if(saveid != null)
	{
		var saveids = saveid.split(',');
		var hasid = false;
		saveid = '';
		j = 1;
		for(i=saveids.length-1;i>=0;i--)
		{
			if(saveids[i]==article_id && hasid) continue;
			else {
				if(saveids[i]==article_id && !hasid) hasid = true;
				saveid += (saveid=='' ? saveids[i] : ','+saveids[i]);
				j++;
				if(j==20 && hasid) break;
				if(j==19 && !hasid) break;
			}
		}
		if(hasid) { alert("您已经顶过该帖，请不要重复顶帖！"); return; }
		else saveid += ','+article_id;
		SetCookie('diggid',saveid,1);
	}
	else
	{
		SetCookie('diggid',article_id,1);
	}
	
	$.post("/public/digg/",{"aid":article_id,'action':action},function(msg){
		$("#btn_good").html("被顶"+msg.data.goodpost+"次");
		$("#btn_bad").html("被踩"+msg.data.badpost+"次");
	},"json");
}

function GetCount(aid){
	$.get("/public/getcount/",{'aid':aid}, function(result){
	  $("#article_click").html(result.data.click);
	  $("#article_comment").html(result.data.comment);
	},"json");
}

function LoadCommets(aid,page){
	//$.get("http://www.sanwen.net/plus/feedback_ajax.php?dopost=getlist&aid="+aid+"&page="+page, function(data){
	//  $("#comment_list").html(data);
	//});
	$.get("http://www.sanwen.net/comment/getlist/aid/"+aid, function(data){
	  $("#comment_list").html(data);
	});
}

function LoadUnion(){
	$.get("/union_code.html",function(data){
		$("#union").html(data);				  
	});
}

//读写cookie函数
function GetCookie(c_name)
{
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf(c_name + "=")
		if (c_start != -1)
		{
			c_start = c_start + c_name.length + 1;
			c_end   = document.cookie.indexOf(";",c_start);
			if (c_end == -1)
			{
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return null
}

function SetCookie(c_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function doZoom(size) { document.getElementById('article_content').style.fontSize = size + 'px'; return false;}

//复制网址
function copylink(){
   var clipBoardContent='';
   clipBoardContent+=document.title;
   clipBoardContent+=window.location;
   window.clipboardData.setData("Text",clipBoardContent);
   alert("复制成功，请粘贴到你的QQ/MSN上推荐给你的好友");
}