$(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') {
								$.get("/apps/getip.php",function(html) {
									if(html != '')
										$('#resultsubmitsh').html('<p class=\"pasgood\">Your IP adress has been banned. For more informations, please contact contact@fmylife.com with this IP :'+html+'</p>');
									else
										$('#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\">URLs are forbidden.</p>');
							} else if(retour == 'errmail') {
								$('#resultsubmitsh').html('<p class=\"pasgood\">E-mail addresses are forbidden.</p>');
							}else {
								$('#resultsubmitsh').html('<p class=\"good\">Thanks for sharing your FWM 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('('+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('Sorry to hear that! ('+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 AbusDesc(id) {
	$('#abusdesc').css('display','none');
	$.get("/apps/account_abus_desc.php?id="+id,function(html) {
		$('#abusdesc').html('Abuse reported');
	});
	$('#abusdesc').fadeIn();
}

function ClosePub(){
	$.get("/apps/megaban_close.php",function(html) {
		$('#pub_megaban').slideUp();
	});
}

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;
}

function limitChars(textid, max, min, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > max)
	{
		$('#' + infodiv).html('You cannot write more then '+max+' characters!');
		$('#'+textid).val(text.substr(0,max));
		$('#tdomf_form4_send').attr("disabled", "true");
		return false;
	}
	else
	{
if(textlength < min)
	{
		$('#' + infodiv).html('not enough buddy keep typing');
		$('#tdomf_form4_send').attr("disabled", "true");
	} else {
		$('#' + infodiv).html('You have '+ (max - textlength) +' characters left.');
		$('#'+textid).val(text.substr(0,max));
		$('#tdomf_form4_send').removeAttr('disabled');
	}
		return true;
	}
}
function minlength(textid, min, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength < min)
	{
		$('#' + infodiv).html('not enough buddy keep typing');
		$('#tdomf_form4_send').attr("disabled", "true");
		return false;
	}
	else
	{
		$('#tdomf_form4_send').removeAttr('disabled');
		return true;
	}
}
$(function(){
 	$('#content_content').keyup(function(){
 		limitChars('content_content',300,50,'charlimitinfo');
 	})
});
$(function(){
 	$('#tdomf_form4_send').click(function(){
		minlength('content_content',50,'charlimitinfo');
 	})
});
$(document).ready(function() {
		$('#tdomf_form4_send').attr("disabled", "true");
});

///MODERATE///



function VDMmod(id) {
	$.get("/apps/vote_mod.php",{"id":id},function(xml) {
		$('.post .contain').slideUp(function() {
				if ($('content',xml).length > 0) {
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none')
					$('.post .contain .billet').html($('content',xml).text());
					$('.post .contain').slideDown("fast");
					var nextid = $('id',xml).text()
						$.get("/apps/postfoot.php",{"id":nextid},function(html) {
							$('.post .postfoot').css('height','20px');
							$('.post .postfoot').html(html).fadeIn("fast");
						});
				} else {
					$('.post .contain .billet').css('height','25px');
					$('.post .contain .billet').css('padding-top','20px');
					$('.post .contain .billet').css('text-align','center');
					$('.post .contain .billet').html('There are no more FWMs to moderate. <a href="/category/unmoderated/">Click here</a> to refresh the page.');
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none');
					$('.post .contain').slideDown("fast");
				}
			});
		});
}

function VDM_BFmod(id) {
	$.get("/apps/vote_bf_mod.php",{"id":id},function(xml) {
		$('.post .contain').slideUp(function() {
				if ($('content',xml).length > 0) {
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none')
					$('.post .contain .billet').html($('content',xml).text());
					$('.post .contain').slideDown();
					var nextid = $('id',xml).text()
						$.get("/apps/postfoot.php",{"id":nextid},function(html) {
							$('.post .postfoot').css('height','20px');
							$('.post .postfoot').html(html).fadeIn("fast");
						});
				} else {
					$('.post .contain .billet').css('height','25px');
					$('.post .contain .billet').css('padding-top','30px');
					$('.post .contain .billet').css('text-align','center');
					$('.post .contain .billet').html('There are no more FWMs to moderate. <a href="/category/unmoderated/">Click here</a> to refresh the page.');
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none');
					$('.post .contain').slideDown("fast");
				}
			});
		});
}

function approve(id) {
	$.get("http://fwhyme.com/approve.php",{"id":id},function(xml) {
		$('.post .contain').slideUp(function() {
				if ($('content',xml).length > 0) {
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none')
					$('.post .contain .billet').html($('content',xml).text());
					$('.post .contain').slideDown("fast");
					var nextid = $('id',xml).text()
						$.get("/apps/postfoot.php",{"id":nextid},function(html) {
							$('.post .postfoot').css('height','20px');
							$('.post .postfoot').html(html).fadeIn("fast");
						});
				} else {
					$('.post .contain .billet').css('height','25px');
					$('.post .contain .billet').css('padding-top','30px');
					$('.post .contain .billet').css('text-align','center');
					$('.post .contain .billet').html('There are no more FWMs to moderate. <a href="/category/unmoderated/">Click here</a> to refresh the page.');
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none');
					$('.post .contain').slideDown("fast");
				}
			});
		});
}

function fail(id) {
	$.get("http://fwhyme.com/fail.php",{"id":id},function(xml) {
		$('.post .contain').slideUp(function() {
				if ($('content',xml).length > 0) {
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none')
					$('.post .contain .billet').html($('content',xml).text());
					$('.post .contain').slideDown();
					var nextid = $('id',xml).text()
						$.get("/apps/postfoot.php",{"id":nextid},function(html) {
							$('.post .postfoot').css('height','20px');
							$('.post .postfoot').html(html).fadeIn("slow");
						});
				} else {
					$('.post .contain .billet').css('height','25px');
					$('.post .contain .billet').css('padding-top','30px');
					$('.post .contain .billet').css('text-align','center');
					$('.post .contain .billet').html('There are no more FWMs to moderate. <a href="/category/unmoderated/">Click here</a> to refresh the page.');
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none');
					$('.post .contain').slideDown();
				}
			});
		});
}
function deletefwm(id) {
	$.get("/apps/delete.php",{"id":id},function(xml) {
		$('.post .contain').slideUp(function() {
				if ($('content',xml).length > 0) {
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none')
					$('.post .contain .billet').html($('content',xml).text());
					$('.post .contain').slideDown();
					var nextid = $('id',xml).text()
						$.get("/apps/postfoot.php",{"id":nextid},function(html) {
							$('.post .postfoot').css('height','20px');
							$('.post .postfoot').html(html).fadeIn("slow");
						});
				} else {
					$('.post .contain .billet').css('height','25px');
					$('.post .contain .billet').css('padding-top','30px');
					$('.post .contain .billet').css('text-align','center');
					$('.post .contain .billet').html('There are no more FWMs to moderate. <a href="/category/unmoderated/">Click here</a> to refresh the page.');
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none');
					$('.post .contain').slideDown();
				}
			});
		});
}

function nextfwm(id) {
	$.get("/apps/next.php",{"id":id},function(xml) {
		$('.post .contain').slideUp(function() {
				if ($('content',xml).length > 0) {
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none')
					$('.post .contain .billet').html($('content',xml).text());
					$('.post .contain').slideDown();
					var nextid = $('id',xml).text()
						$.get("/apps/postfoot.php",{"id":nextid},function(html) {
							$('.post .postfoot').css('height','20px');
							$('.post .postfoot').html(html).fadeIn("slow");
						});
				} else {
					$('.post .contain .billet').css('height','25px');
					$('.post .contain .billet').css('padding-top','30px');
					$('.post .contain .billet').css('text-align','center');
					$('.post .contain .billet').html('There are no more FWMs to moderate. <a href="/category/unmoderated/">Click here</a> to refresh the page.');
					$('.post .postfoot').css('display','none');
					$('.post .sharethis').css('display','none');
					$('.post .contain').slideDown();
				}
			});
		});
}