$(document).ready(function() {
	$("#submitsh").submit(function() {
		$(this).ajaxSubmit({
					beforeSubmit: function(before) {
					$('#loadingsubmitsh').css('display','block');
					$("#resultsubmitsh").html('');
					$("#resultsubmitsh").css('display','none');
					},
					success: function(retour) {
						pageTracker._trackEvent("Formulaires", "Envoi anecdote");
						$('#loadingsubmitsh').css('display','none');
							if(retour == 'errban') {
								$('#resultsubmitsh').html('<p class=\"pasgood\">Your IP adress has been banned. For more informations, please contact contact@fmylife.com</p>');
							} else if(retour == 'errshort') {
								$('#resultsubmitsh').html('<p class=\"pasgood\">Your message is too short.</p>');
							} else if(retour == 'errlong') {
								$('#resultsubmitsh').html('<p class=\"pasgood\">Your message is too long. 300 characters maximum!</p>');
							} else if(retour == 'errflood') {
								$('#resultsubmitsh').html('<p class=\"pasgood\">Please don\'t flood the server :)</p>');
							} else if(retour == 'errurl') {
								$('#resultsubmitsh').html('<p class=\"pasgood\">Web adress is prohibed.</p>');
							} else if(retour == 'errmail') {
								$('#resultsubmitsh').html('<p class=\"pasgood\">Email is prohibed.</p>');
							}else {
								$('#resultsubmitsh').html('<p class=\"good\">Thanks for sharing your FML with us. Come back soon to find out if it has been posted! Or not.<br><a href="javascript:submitToggle();">Close this form</a></p>');
								setTimeout("$('#submitsh #update').val('');",5000);
							}	
					$('#resultsubmitsh').hide();
					$('#resultsubmitsh').fadeIn(500);
					setTimeout("$('#resultsubmitsh').fadeOut();",5000);							
					}
			}); 
		return false;
	});		
});

function VDM(id,nb) {

	$('#vote'+id).css('display','none');
	$.post("/apps/vote.php",{"id":id,"nb":nb},function(html) {
		if (html == 'NA') {
			$('#vote'+id).html('dude! already done...').fadeIn();
		} else {
			$('#vote'+id).html('it definitely is! ('+html+')').fadeIn();
		}
	});
}

function VDM_BF(id,nb) {

	$('#votebf'+id).css('display','none');
	$.post("/apps/vote_bf.php",{"id":id,"nb":nb},function(html) {
		if (html == 'NA') {
			$('#votebf'+id).html('dude! already done...').fadeIn();
		} else {
			$('#votebf'+id).html('yeah! way to show \'em ('+html+')').fadeIn();
		}
	});
}

function signaler(type,id) {

	$('#signaler'+id).css('display','none');
	$.get("/apps/moderer.php?type="+type+"&id="+id,function(html) {
		$('#signaler'+id).html('abuse reported');
	});
	$('#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 characters');

}

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 displayMail() {

	$('#submit .input_mail').html('Email (optional) : <input type="text" name="mail">');

}

function Favori(action,id) {

	$.get("/apps/favori.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('Delete from favorites');
		} else {
			$('#plus'+id+' .plus_bouton#favori').attr('OnClick',"Favori('add','"+id+"');");
			$('#plus'+id+' .plus_bouton#favori .label').html('Add to favorites');
		}
	});

}

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+'\');">ON</a>'); $('span.suivicomm').fadeIn(); });
		} else {
			$('span.suivicomm').fadeOut( function() { $('span.suivicomm').html('<a href="javascript:toggleSuiviComms(\''+type+'\',\''+id+'\');">OFF</a>'); $('span.suivicomm').fadeIn(); });
		}
	});

}

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('article','');
		if ($(this).is(':checked') == true) {
			nombre++;
			$.get("/apps/togglesuivi.php?type=article&id="+id);
		} else {
			nombre--;
			$.get("/apps/togglesuivi.php?type=article&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('Without having validated your address, you cannot use all website functions. If your address is correct, <a href="javascript:sendValid();">click here</a> to receive the confirmation email. If it is incorrect, please change it, to receive the confirmation email.');

}



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('We have just sent you an email to validate your email address, you should receive it in a few minutes.');
			$('#valid_no_infos').slideDown();
		});
	});

}

function deleteRequest() {
	$.get("/account/delete/request",function(html) {
		if(html == "ok") {
			$('#deleteinstruction').fadeOut();
			$('#deleterequest').css('display','none');
			$('#deleterequest').html('An email has been sent to you containing all the instructions on how to validate the deletion. You should receive it within a few minutes. You can, if you wish, <a href="javascript:deleteAnnul()">cancel the procedure</a>.');
		}
		$('#deleterequest').fadeIn();
	});	
}

function deleteAnnul() {
	$.get("/account/delete/cancel",function(html) {
		if(html == "ok") {
			$('#deleterequest').css('display','none');
			$('#deleterequest').before('<span id="deleteinstruction">You can delete your account at any time. Once the request has been made via the link below, you\'ll receive a link in an email allowing you to confirm this deletion.<br></span>');
			$('#deleterequest').html('<a href="javascript:deleteRequest();">Request that your account be deleted</a>');
		}
		$('#deleterequest').fadeIn();
	});		
}

function compte_FormatNL() {

	$.get("/apps/format_nl.php",function(html) {
		if (html == 'Texte') {
			$('#format_nl').html('Texte');
			$('#formater_nl').html('change to HTML');
		} else {
			$('#format_nl').html('HTML');
			$('#formater_nl').html('change to text');
		}
	});

}

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('second');
			setTimeout('afCountdown();',1000);
		} else {
			$('#alerte_af').html('Now, you can comment ;)');
		}
	}

}

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('second');
			setTimeout('afCountdownComment('+id+');',1000);
		}			
	}

}
function fbs_click(u,t) {
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}