// JavaScript Document
//读写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()); //使设置的有效时间正确。增加toGMTString()
}

function postDigg(ftype,aid)
{
	if (ftype == 'good') {
		var target_obj = $('.dig1 p');
	} else if (ftype == 'bad') {
		var target_obj = $('.dig2 p');
	} else {
		return '';
	}
	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]==aid && hasid) continue;
			else {
				if(saveids[i]==aid && !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 += ','+aid;
		SetCookie('diggid',saveid,1);
	}
	else
	{
		SetCookie('diggid',aid,1);
	}
	
	var js = document.getElementById('remote_js');
	if (js == undefined || js == '') {
		js = document.createElement('SCRIPT');
		document.getElementsByTagName("HEAD")[0].appendChild(js);
		js.id = "remote_js";		
	}
	js.src = "http://www.xkq.com/plus/digg_ajax.php?action="+ftype+"&id="+aid;
	document.getElementsByTagName("HEAD")[0].removeChild(js);
    var i =	Number(target_obj.text()) + 1;
	target_obj.text(i);
}

function digg_good()
{
	var target_obj = $(this).prev();	
	aid = $(this).attr('aid');
	if (checkCookie(aid) == false) {
		return ;
	}
	
	$.getJSON("/plus/digg_ajax.php?action=good&id="+aid, function(data){
		if (data.num != '') {
			target_obj.html(data.num);
		} else {
			return;
		}
	})
}

function digg_bad()
{
	var target_obj = $(this).prev();	
	aid = $(this).attr('aid');
	if (checkCookie(aid) == false) {
		return ;
	}
	
	$.getJSON("/plus/digg_ajax.php?action=bad&id="+aid, function(data){
		if (data.num != '') {
			target_obj.html(data.num);
		} else {
			return;
		}
	})
}

function checkCookie(aid)
{
	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]==aid && hasid) continue;
			else {
				if(saveids[i]==aid && !hasid) hasid = true;
				saveid += (saveid=='' ? saveids[i] : ','+saveids[i]);
				j++;
				if(j==20 && hasid) break;
				if(j==19 && !hasid) break;
			}
		}
		if(hasid) { alert("您已经顶过该帖，请不要重复顶帖 ！"); return false; }
		else saveid += ','+aid;
		SetCookie('diggid',saveid,1);
	}
	else
	{
		SetCookie('diggid',aid,1);
	}
	return true;
}