
//公共投票入口， 2010-7-8, 2010-7-19

/* 使用方法举例：
function vote_commont(id, ud){
	common_vote("programe_comment", id, "感谢您的参与！投票已成功，请勿重复投票！", "program", ud, id, "program");
}
function common_vote_callback(channel,ud,id,item_type){
	if(ud==1){
		$("#commont_up_"+id).html(parseInt($("#commont_up_"+id).html())+1);
	}else{
		$("#commont_down_"+id).html(parseInt($("#commont_down_"+id).html())+1);
	}
}
*/

/**
 * cookie_item: cookie key
 * cookie_id: cookie value
 * note: 重复顶踩的提示信息
 * channel: 频道
 * up_down: 顶踩 1：顶；2：踩
 * item_id: intval, for Rating.class.php
 * item_type: string, for Rating.class.php
 */
 
function common_vote(cookie_item, cookie_id, note, channel, up_down, item_id, item_type){
    if(get_vote_status_rating(cookie_item, cookie_id) == false){
		alert(note);
		return false;
    }
	window.open("rating.php?channel="+channel+"&item_id="+cookie_id+"&up_down="+up_down);
}
 
/*
function common_vote(cookie_item, cookie_id, note, channel, up_down, item_id, item_type){
    if(get_vote_status_rating(cookie_item, cookie_id) == false){
		alert(note);
		return false;
    }

    $.post("rating.php?channel="+channel+"&up_down="+up_down, { id:item_id, type:item_type },
    function(data){		
		if(data ==0 ){
			alert("投票失败！");
		}else if(data ==1 ){
			common_vote_callback(channel,up_down,item_id,item_type);
		}else if(data ==2 ){
			alert(note); //重复顶踩的提示信息
		}else if(data ==3 ){
			alert("\n"+note+"\n "); //重复顶踩的提示信息,ip限制
		}else if(data ==-2 ){ //need login
                    login(function(){common_vote(cookie_item, cookie_id, note, channel, up_down, item_id, item_type);});
                }else{
			alert(data);
		}
    });
}
*/
/**
 * Get vote history status
 * item   : vote object
 * id     : vote id of item
 * return : true indicate operating success; false indicate you had voted.
 * Usage  : if(get_vote_status('questions',id) == false){ alert('您已经投过票了！'); return false; }
 */
function get_vote_status_rating(item, id){
	var First=false;
	var item_str = $.cookie(item);

	if(item_str == null || $.trim(item_str) == ''){
		First=true;
	}else{
		//alert(item_str.length); 
		if(item_str.length>4000) $.cookie(item, '', { expires: 10, path: '/'}); //add 2010-3-8

		if(item_str.indexOf('_'+id+'_') != -1){
			return false;
		}
	}

	if(First == true) $.cookie(item, '_' +id+ '_' , { expires: 10, path: '/'});
	else $.cookie(item, item_str + id + '_' , { expires: 10, path: '/'});

	return true;
}
