$(document).ready(function() {
	checkUnreads();
});

function VDM(id) {

	$('#vote'+id).css('display','none');
	$.get("./votevalid.php?eid="+id,function(html) {
		if (html == 'NA') {
			$('#vote'+id).html('voce ja votou ...');
		} else {
			$('#vote'+id).html('Obrigado por confirmar! ('+html+')');
			$('#votebf'+id).html('Obrigado por classificar!');
		}
	});
	$('#vote'+id).fadeIn();

}

function VDM_BF(id) {

	$('#votebf'+id).css('display','none');
	$.get("./votedeserve.php?eid="+id,function(html) {
		if (html == 'NA') {
			$('#votebf'+id).html('voce ja votou ...');
		} else {
			$('#votebf'+id).html('Obrigado por classificar! ('+html+')');
			$('#vote'+id).html('Obrigado por confirmar! ');
		}
	});
	$('#votebf'+id).fadeIn();

}
function VDMCOMMENT(id) {

	$('#vote'+id).css('display','none');
	$.get("./commentabuse.php?cid="+id,function(html) {
		if (html == 'NA') {
			$('#vote'+id).html('voce reportou um abuso...');
		} else {
			$('#vote'+id).html('abuso reportado');
		}
	});
	$('#vote'+id).fadeIn();

}
function signaler(type,id) {

	$('#signaler'+id).css('display','none');
	$.get("/apps/moderer.php?type="+type+"&id="+id,function(html) {
		$('#signaler'+id).html('abus signal');
	});
	$('#signaler'+id).fadeIn();

}

function cut(input) {

	if (input.value.length >= 300) {
		input.value = input.value.substring(0,300);
	}

	var reste = 300 - input.value.length;
 	var print =  reste;
	
	$(".ok #compteur").html(input.value.length+'/300');

}

function submitToggle() {

	if ($('#submit').css('display') == 'none') {
		$('#submit').slideDown();
	} else {
		$('#submit').slideUp();
	}

}

function plusToggle(id) {

	if ($('#plus'+id).css('display') == 'none') {
		$('#plus'+id).slideDown();
	} else {
		$('#plus'+id).slideUp();
	}

}

function plusTogglef(id) {
	resetallbox(id);
	if ($('#plusf'+id).css('display') == 'none') {
		$('#plusf'+id).slideDown();
	} else {
		$('#plusf'+id).slideUp();
	}

}

function resetallbox(id){
	$('#plusf'+id).slideUp();
	$('#pluss'+id).slideUp();
	$('#plusr'+id).slideUp();
}
function plusToggles(id) {
	resetallbox(id);
	if ($('#pluss'+id).css('display') == 'none') {
		$('#pluss'+id).slideDown();
	} else {
		$('#pluss'+id).slideUp();
	}

}

function plusToggler(id) {
	resetallbox(id);
	if ($('#plusr'+id).css('display') == 'none') {
		$('#plusr'+id).slideDown();
	} else {
		$('#plusr'+id).slideUp();
	}

}

function Favori(action,id) {

	$.get("./favourite.php?action="+action+"&id="+id,function() {
		if (action == 'add') {
			$('#plus'+id+' .plus_bouton#favori').attr('OnClick',"Favori('delete','"+id+"');");
			$('#plus'+id+' .plus_bouton#favori .label').html('Added to favorite');
		} else {
			$('#plus'+id+' .plus_bouton#favori').attr('OnClick',"Favori('add','"+id+"');");
			$('#plus'+id+' .plus_bouton#favori .label').html('Removed from Favorite');
		}
	});

}

function plusContent(type,id) {

	if (type == 'ami') {
	
		if ($('#plus'+id+' .content').attr('id') != '' && $('#plus'+id+' .content').attr('id') != 'ami') {
			$('#plus'+id+' .content').attr('id','');
			$('#plus'+id+' .content').slideUp();
		}
	
		if ($('#plus'+id+' .content').css('display') == 'none') {
		
			$('#plus'+id+' .content').html('<div class="form">'+
			'<div class="champ"><label>E-mail friend:</label><input type="text" name="mail" /></div>'+
			'<div class="champ"><label><i>Message:</i> :</label><input type="text" name="message" />'+
			'<input type="submit" class="submit_ami" value="Envoyer" onclick="plusSubmit(\'ami\',\''+id+'\');" /></div>'+
			'</div>');
			$('#plus'+id+' .content').attr('id','ami');
			$('#plus'+id+' .content').slideDown();
		
		} else {
		
			$('#plus'+id+' .content').attr('id','');
			$('#plus'+id+' .content').slideUp();
		
		}
	
	} else if (type == 'dejavu') {

		if ($('#plus'+id+' .content').attr('id') != '' && $('#plus'+id+' .content').attr('id') != 'dejavu') {
			$('#plus'+id+' .content').attr('id','');
			$('#plus'+id+' .content').slideUp();
		}

		if ($('#plus'+id+' .content').css('display') == 'none') {

			$('#plus'+id+' .content').html('<div class="form">'+
			'<div class="champ"><label># Your message :</label><input type="text" name="num_article" />'+
			'<input type="submit" class="submit_ami" value="Envoyer" onclick="plusSubmit(\'dejavu\',\''+id+'\');" /></div>'+
			'</div>');
			$('#plus'+id+' .content').attr('id','dejavu');
			$('#plus'+id+' .content').slideDown();

		} else {

			$('#plus'+id+' .content').attr('id','');
			$('#plus'+id+' .content').slideUp();

		}

	}

}


function submitreportabuse(id){

	var commentatorname = $('#commentatorname').val() ;
	var email = $('#email').val() ;
	var commentdescription = $("textarea[@name=commentdescription]").val()
	var security_code = $('#security_code').val() ;
	
	if (commentatorname == ''){
		alert("Coloque o seu nome ou pseudonimo");
		return false;
	}

	if (commentdescription == ''){
		alert("Enter abuse message");
		return false;
	}
	if (security_code == ''){
		alert("Enter code");
		return false;
	}
	
	else{
		
		$.get('reportabuse.php?commentatorname='+commentatorname+'&email='+email+'&commentdescription='+commentdescription+'&security_code='+security_code+'&eid='+id,function(html) {
		
		//alert(html);
		
		$('#plusf_rp'+id).html('<div class="content_result">'+html+'</div>');
		
		$('#plusf_rp'+id).slideDown();
		
		setTimeout("$('#plusf_rp"+id+"').slideUp()",1000);
						
		setTimeout('resetallbox('+id+')',2000);
		
			});
																									 
	}
}


function sendtofriend(id){

	var femail = $('#femail').val() ;
	var message = $("textarea[@name=message]").val()
	var security_codef = $('#security_codef').val() ;
	
	if ( (femail.indexOf("@") == -1) ||(femail.indexOf(" ") >= 0) || (femail.indexOf(".") == -1) ) {	
 		alert("Por favor coloque o Seu Email sem qualquer espaço."); 
		return (false);
	}
	
	if (message == ''){
		alert("Enter message");
		return false;
	}
	if (security_codef == ''){
		alert("Enter code");
		return false;
	}
	
	else{
		
		$.get('sendtofriend.php?femail='+femail+'&message='+message+'&security_codef='+security_codef+'&eid='+id,function(html) {
		
		//alert(html);
		
		$('#plusf_sf'+id).html('<div class="content_result">'+html+'</div>');
		
		$('#plusf_sf'+id).slideDown();
		
		setTimeout("$('#plusf_sf"+id+"').slideUp()",1000);
						
		setTimeout('resetallbox('+id+')',2000);
		
			});
																									 
	}
}


function commentsubmit(id){

	var name_cmment = $('#name_cmment').val() ; 
	var sexo = $('#sexo').val() ;
	var email_comment = $('#email_comment').val() ;
	var message_comment = $("textarea[@name=message_comment]").val()
	var security_codecomment = $('#security_codecomment').val() ;
	
	if (name_cmment == ''){
		alert("Coloque o seu nome ou um nome alternativo");
		return false;
	}

	
	
	if (message_comment == ''){
		alert("Coloque o seu comentario");
		return false;
	}
	if (security_codecomment == ''){
		alert("Coloque o codigo anti-spam");
		return false;
	}
	
	else{
		
		$.get('commentsubmition.php?sexo='+sexo+'&name_cmment='+name_cmment+'&email_comment='+email_comment+'&security_codecomment='+security_codecomment+'&message_comment='+message_comment+'&eid='+id,function(html) {
		
		
		$('#plusf_cs'+id).html('<div class="content_result">'+html+'</div>');
		
		
		$('#plusf_cs'+id).slideDown();
		
		//if(html!='Code mismatch'){
		setTimeout("$('#plusf_cs"+id+"').slideUp()",1000);
		//}else{
						
		setTimeout('resetallbox('+id+')',2000);
		//}
		
			});
																									 
	}
}


function votesubmit(id){

	var votevalue = $('#votevalue').val() ;
	
		
		$.get('vote.php?votevalue='+votevalue+'&eid='+id,function(html) {
		
		//alert(html);
		
		$('#plusv'+id).html('<div class="content_result">'+html+'</div>');	
		
				
			});
	
}


function favourite(id){

	
		$.get('favourite.php?eid='+id,function(html) {
		
		//alert(html);
		
		$('#plusv'+id).html('<div class="content_result">'+html+'</div>');	
		
						
			});
	
}

function seguro(id){

	
		$.get('safe.php?eid='+id,function(html) {
		
		//alert(html);
		
		$('#plusv'+id).html('<div class="content_result">'+html+'</div>');	
		
						
			});
	
}

function adulto(id){

	
		$.get('adulto.php?eid='+id,function(html) {
		
		//alert(html);
		
		$('#plusv'+id).html('<div class="content_result">'+html+'</div>');	
		
						
			});
	
}

function subscription(id){

	
		$.get('subscription.php?eid='+id,function(html) {
		
		//alert(html);
		
		$('#plusv'+id).html('<div class="content_result">'+html+'</div>');	
		
				
			});
	
}




function plusSubmit(type,id) {

	if (type == 'ami') {
	
		$('#plus'+id+' .content').slideUp(function() {
		
			mail = $('#plus'+id+' .content input[@name=mail]').attr('value');
			message = $('#plus'+id+' .content input[@name=message]').attr('value');
			$.get('sendtofriend.php?id='+id+'&mail='+mail+'&message='+message,function(html) {
		
				$('#plus'+id+' .content').html('<div class="content_result">'+html+'</div>');
				$('#plus'+id+' .content').slideDown();
				$('#plus'+id+' .content').attr('id','');
			
			});
		
		});
	
	} else if (type == 'dejavu') {
	
		$('#plus'+id+' .content').slideUp(function() {

			num_article = $('#plus'+id+' .content input[@name=num_article]').attr('value');
			$.get('reportduplicate.php?id='+id+'&original='+num_article,function(html) {
			
				$('#plus'+id+' .content').html('<div class="content_result">'+html+'</div>');
				$('#plus'+id+' .content').slideDown();
				$('#plus'+id+' .content').attr('id','');
			
			});

		});
	
	}
	
	setTimeout("$('#plus"+id+" .content').slideUp();",2500);

}

function displayMail() {

	$('#submit .input_mail').html('E-mail  : <input type="text" name="email">');

}

function toggleSuiviComms(type,id) {

	$.get("/apps/togglesuivi.php?type="+type+"&id="+id,function(html) {
		if (html == '1') {			
			$('span.suivicomm').fadeOut( function() { $('span.suivicomm').html('<a href="javascript:toggleSuiviComms(\''+type+'\',\''+id+'\');">Dsactiver le suivi</a>'); $('span.suivicomm').fadeIn(); });
		} else {
			$('span.suivicomm').fadeOut( function() { $('span.suivicomm').html('<a href="javascript:toggleSuiviComms(\''+type+'\',\''+id+'\');">Activer le suivi</a>'); $('span.suivicomm').fadeIn(); });
		}
	});

}

function checkUnreads() {

	unread = false;
	$.get("/apps/unreads.php",function(xml) {
	
		$('root item',xml).each(function() {
			id = $(this).attr('id');
			name = $(this).attr('name');
			nb = $(this).text();
			if (nb != '0') {
				$('#box_categories li#'+id).html('<strong><a href="/'+id+'">'+name+' ('+nb+')</a></strong>');
				unread = true;
			}
		});
		
		if (unread == true) {
			$('#box_categories ul').append('<li class="tout"><a href="/lu">Marquer tout comme lu</a></li>');
		}
	
	});

}

function compte_SuiviListe() {

	if ($('#suivi').css('display') == 'none') {

		$.get("/apps/suivi.php?action=display",function(html) {
	
			$('#suivi').html(html);
			compte_SuiviEvent();
			$('#suivi').slideDown();
	
		});
	
	} else {
	
		$('#suivi').slideUp();
	
	}

}

function compte_SuiviEvent() {

	$('#suivi input[@type=checkbox]').click(function() {

		nombre = parseInt($('span#suivi_nb').text());
		id = $(this).attr('id').replace('vdm','');
		if ($(this).is(':checked') == true) {
			nombre++;
			$.get("/apps/togglesuivi.php?type=vdm&id="+id);
		} else {
			nombre--;
			$.get("/apps/togglesuivi.php?type=vdm&id="+id);
		}
		$('span#suivi_nb').html(nombre);

	});

}

function displayValidInfos() {

	$('#valid_no_infos').parent('div.champ').css('height','108px');
	$('#valid_no_infos').parent('div.champ').find('label').css('height','80px');
	$('#valid_no_infos').html('Sans valider votre adresse, vous ne pouvez pas laisser de commentaires sur le blog ou les VDM. Si votre adresse est bonne, <a href="javascript:sendValid();">cliquez ici pour recevoir</a> le mail de validation. Si elle est incorrecte, modifiez-la, vous recevrez un e-mail dans l\'instant.');

}

function sendValid() {

	$('#valid_no_infos').slideUp(function() {
		$('#valid_no_infos').parent('div.champ').css('height','68px');
		$('#valid_no_infos').parent('div.champ').find('label').css('height','40px');
		$.get("/apps/valid_send.php",function() {
			$('#valid_no_infos').html('Nous venons de vous envoyer un e-mail pour valider votre adresse e-mail, vous devriez le recevoir dans quelques minutes.');
			$('#valid_no_infos').slideDown();
		});
	});

}

function compte_FormatNL() {

	$.get("/apps/format_nl.php",function(html) {
		if (html == 'Texte') {
			$('#format_nl').html('Texte');
			$('#formater_nl').html('changer en HTML');
		} else {
			$('#format_nl').html('HTML');
			$('#formater_nl').html('changer en texte');
		}
	});

}

function afCountdown() {

	if ($('#alerte_af span#secondes')) {
		nombre = parseInt($('#alerte_af span#secondes').html());
		if (nombre > 0) {
			nombre = nombre-1;
			if (nombre == 0) nombre = '0';
			$('#alerte_af span#secondes').html(nombre);
			if (nombre == 1) $('#alerte_af span#secondes_txt').html('seconde');
			setTimeout('afCountdown();',1000);
		} else {
			$('#alerte_af').html('Vous pouvez  nouveau commenter ;)');
		}
	}

}

function afCountdownComment(id) {

	if ($('#commentEdit'+id+' span#secondes')) {
		nombre = parseInt($('#commentEdit'+id+' span#secondes').html());
		if (nombre > 0) {
			nombre = nombre-1;
			if (nombre == 0) {
				nombre = '0';
				$('#commentEdit'+id).fadeOut();
			}
			$('#commentEdit'+id+' span#secondes').html(nombre);
			if (nombre == 1) 
				$('#commentEdit'+id+' span#secondes_txt').html('seconde');
			setTimeout('afCountdownComment('+id+');',1000);
		}			
	}

}


function LafonClose() {
	$.get("/apps/lafon_capping.php",function(html) {
	if(html == "ok")
		$('#capping_lafon').slideUp();
		
	});
}
