var anim_content = ["&#9658;","&#9660;","&#9668;","&#9650;"];
var anim_control = 0;

function startAnim(anim){
	if (anim_control > 3){anim_control = 0;}
	anim.html(anim_content[anim_control]);
	anim_control++;
}

function viewComment(id,fokey) {
  var link = $("#comment"+id+"link");
  var open = $("#comment"+id+"open");
  var anim = $("#anim"+id);

  if (open.html() == "") {
		var animId = 0;
		animId = setInterval (function(){startAnim(anim)}, 250);

		$.ajax({
			type:'GET',
			url:'gen.pl?p=comments:view&fokey='+fokey,
	   	success:function(data){
				open.html(data);
				link.hide();
				open.show();
				
				clearInterval (animId);
				anim_control = 0;
				anim.html("&#9658;");
			}
		});

  }else{
		link.hide();
		open.show();
  }
}

function closeComment(id) {
	$("#comment"+id+"open").hide();
	$("#comment"+id+"link").show();
}

function moderateComment(fokey){
	var TimeStamp = new Date().getTime();
	jConfirm('Aguarde, por favor...', 'pontuar comentário');
	$('#popup_cancel').hide();
	$('#popup_ok').hide();
	$('#popup_cancel').val('   Fechar   ');
	$('#popup_ok').val('   OK   ');
	$.ajax({
		type:'GET',
		url:'gen.pl?p=moderate&fokey='+fokey+'&ts='+TimeStamp,
   	success:function(data){
   		$('#popup_message').html(data);
   		$('#popup_cancel').show();
		}
	});
}

function voteComment(fokey,reason){
	var TimeStamp = new Date().getTime();
	$("#popup_cancel").trigger('click');
	jAlert('A enviar...', 'pontuar comentário');
	$('#popup_ok').hide();
	$.ajax({
		type:'GET',
		url:'gen.pl?p=moderate&fokey='+fokey+'&op=moderate&reason='+reason+'&ts='+TimeStamp,
   	success:function(data){
   		$('#popup_message').html(data);
   		$('#popup_ok').show();
		}
	});
}

function markAbuse(fokey){
	var TimeStamp = new Date().getTime();
	jConfirm('Aguarde, por favor...', 'reportar abusivo',function(clickedok) { clickedok?voteCommentAbuse(fokey,'suspect'):null;  }  );
	$('#popup_cancel').hide();
	$('#popup_ok').hide();
	$('#popup_message').load('gen.pl?p=abuse&fokey='+fokey+'&ts='+TimeStamp,
		function() {
			$('#popup_cancel').show();
		}
		
	); 
}


function voteCommentAbuse(fokey,reason){
	var TimeStamp = new Date().getTime();
	$("#popup_cancel").trigger('click');
	jAlert('A enviar...', 'reportar abusivo');
	$('#popup_ok').hide();
	$.ajax({
		type:'GET',
		url:'gen.pl?p=abuse&fokey='+fokey+'&op=mark&abusive='+reason+'&ts='+TimeStamp,
   	success:function(data){
   		$('#popup_message').html(data);
   		$('#popup_ok').show();
		}
	});
}

function makeCommentVisible(fokey,oid){
	var TimeStamp = new Date().getTime();
	
	var oldHide = $.alerts._hide;   $.alerts._hide = altAlertHide;
	
	jConfirm('Quer tornar este comentário visível?', 'Aviso',
		function(clickedok) {
			$.alerts._hide = oldHide;
			if(!clickedok)return;			
			$.ajax({
				type:'GET',
				url:'gen.pl?p=comments&op=mkvisible&fokey='+fokey+'&ts='+TimeStamp,
				success:function(rsp){
					if(jQuery.trim(rsp) == 'OK'){
						jAlert('Este comentário ficará visível para os outros utilizadores.', 'Sucesso');
						jQuery('#mkvisible_'+oid).hide();
					}else if(jQuery.trim(rsp) == 'NOPRIV'){
						jAlert('No tem permissões para efectuar este pedido.', 'Erro');
					}else{
						jAlert('Estamos com dificuldades em submeter o seu pedido. Por favor tente mais tarde.', 'ERRO');
					}
				}
			});
			
		} 
	);
	
}

function altAlertHide(){
	$("#popup_message").html('<img style="width: 32px; height: 32px;" alt="Em processamento..." src="/i/icons/ic_loginLoading.gif">&nbsp;Em processamento...');
	$("#popup_panel").hide();
}
