//author:fantasy
//create date: 2008-9-14

/*
 * 增加某项
 *
 */
function addItems() {
	var num = $(".the_item").length+1;
	$(".the_item:last").after('<tr class="the_item"><th>选项 '+num+'：</th><td><input class="f-text" maxlength="50" name="choice[]" type="text"></td></tr>');
}


/*
 * 截止日期
 *
 */
$(function(){
	$("#jiezhi").toggle(function(){
		$(this).parent().next().show();
	},function(){
		$(this).parent().next().hide();
	});

	$("#fri_see").click(function(){
		if($(this).is(":checked")){
			$(this).val(1);
		}else{
			$(this).val(0);
		}
	});

	//$(".vote_per").animate( { width: "90%"}, 2000);

});

/*
 * 投票
 *
 */
function vote(voteId,myId,view) {
	var message = $("#message_"+voteId).val();
	var choice  = $("#choice_"+voteId).val();
	var choiceId  = $("#choice_id_"+voteId).val();
	var uid = $("#page_uid").val();
	if(!MID){
		alert("需要登录后才能投票！");
		return false;
	}
	if(!choiceId){
		alert("请至少选择一项!");
		return;
	}

	$.post(APP+"/Vote/vote",{voteId:voteId,message:message,choice:choice,choiceId:choiceId},function(txt){
		if(txt){
			if(view){
				location.href = APP+"/Vote/view/id/"+voteId+"/uid/"+uid;
			}else{
				$("#vote_success_"+voteId).html('<p>投票成功。<a target="_blank" href='+APP+'/Vote/view/id/'+voteId+'/uid/'+myId+'>点击查看结果</a></p>');
				var num = $("#vote-digg-"+voteId).text();
				var new_num = parseInt(num)+1;
				$("#vote-digg-"+voteId).text(new_num);	
			}

		}else{
			alert('投票失败，请稍候再试！');
		}
	});
}

/*
 * 在View页投票
 *
 */
function vote_view(voteId,myId) {
	var message = $("#message_"+voteId).val();
	var choice  = $("#choice_"+voteId).val();
	var choiceId  = $("#choice_id_"+voteId).val();

	if(!choiceId){
		alert("请至少选择一项!");
		return;
	}

	$.post(APP+"/Vote/vote",{voteId:voteId,message:message,choice:choice,choiceId:choiceId},function(txt){
		if(txt){
			location.href = APP+"/Vote/view";
		}else{
			alert('投票失败，请稍候再试！');
		}
	});
}

/*
 * 单选某个
 *
 */
function single_check(_this,voteId,choiceId) {
	var con = $(_this).next().text();
	$("#choice_"+voteId).val(con);
	$("#choice_id_"+voteId).val(choiceId-1);
}

function view_single_check(_this,voteId,choiceId) {
	//var con = $(_this).next().text();
	var con = $("#vote-option-"+choiceId).text();
	//alert(con);
	$("#choice_"+voteId).val(con);
	$("#choice_id_"+voteId).val(choiceId-1);
}

/*
 * 多选某个
 *
 */
function many_check(_this,voteId,choiceId) {
	var con			 = 	$(_this).next().text();
	var yuan_con	 = 	$("#choice_"+voteId).val();
	var new_con 	 =  yuan_con?yuan_con+"||"+con:con;
	$("#choice_"+voteId).val(new_con);
	
	var num = choiceId-1;
	var yuan_num = $("#choice_id_"+voteId).val();
	var new_num = yuan_num?yuan_num+"-"+num:num;
	$("#choice_id_"+voteId).val(new_num);
}

function view_many_check(_this,voteId,choiceId) {
	if($(_this).is(":checked")){
		var con = $("#vote-option-"+choiceId).text();
		var yuan_con	 = 	$("#choice_"+voteId).val();
		var new_con 	 =  yuan_con?yuan_con+"||"+con:con;
		$("#choice_"+voteId).val(new_con);
		
		var num = choiceId-1;
		var yuan_num = $("#choice_id_"+voteId).val();
		var new_num = yuan_num?yuan_num+"-"+num:num;
		$("#choice_id_"+voteId).val(new_num);	
	}else{
		var con = $("#vote-option-"+choiceId).text();
		var yuan_con	 = 	$("#choice_"+voteId).val();
		var yuan_arr	 =  yuan_con.split('||');
		var yuan_length  =  yuan_arr.length;

		for(var i=0;i<yuan_length;i++){
			if(con == yuan_arr[i]){
				yuan_arr[i] = '';
			}
		}
			
		var new_con = yuan_arr[0];
		for(var i=1;i<yuan_length;i++){
			if(yuan_arr[i] != ''){
				new_con += "||"+ yuan_arr[i];
			}
		}


		$("#choice_"+voteId).val(new_con);
		
		//更新num
		var num = choiceId-1;
		var yuan_num = $("#choice_id_"+voteId).val();

		var num_arr	 =  yuan_num.split('-');
		var num_length  =  num_arr.length;

		for(var i=0;i<num_length;i++){
			if(num == num_arr[i]){
				num_arr[i] = '';
			}
		}
			
		var new_num = num_arr[0];
		for(var i=1;i<num_length;i++){
			if(num_arr[i] != ''){
				new_num += "-"+ num_arr[i];
			}
		}

		$("#choice_id_"+voteId).val(new_num);	
	}

}

/*
 * 点击修改你的评论
 *
 */
function modify_message(){	
	var con = $("#the_tp_message").text();
	$("#tp_message").hide();
	$("#hid_area").show();	
	$("#new_tp_message").val(con);
}

/*
 * 点击取消修改
 *
 */
function cancel_modify(){
	$("#hid_area").hide();	
	$("#tp_message").show();
}


/*
 * 发送你修改的评论
 *
 */
function send_tp_message(id){
	var mes = $("#new_tp_message").val();
	$.post(APP+"/Vote/modifMessage",{id:id,message:mes},function(txt){
		if(txt){
			$("#hid_area").hide();	
			$("#tp_message").show();
			$("#the_tp_message").text(mes);
		}else{
			alert('修改失败,请稍后再试!');
		}
	});
}






