jQuery.noConflict();
var $_ = jQuery;
$_(document).ready(function() {
	$_("#submitsh").submit(function() {
		$_(this).ajaxSubmit({
					beforeSubmit: function(before) {
					$_('#loadingsubmitsh').css('display','block');
					$_("#resultsubmitsh").html('');
					$_("#resultsubmitsh").css('display','none');
					},
					success: function(retour) {
						$_('#loadingsubmitsh').css('display','none');
							 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 == '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 Advice 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 INT(id,nb) {

	$_('#INT'+nb).css('display','none');
	$_.post("/blog/int_vote.php",{"id":id,"v":nb},function(html) {
			$_('#INT'+nb).html(html).fadeIn();
	});
	return false;
}
function INT2(id,nb) {

	$_('#INT_'+id+'_'+nb).css('display','none');
	$_.post("/blog/int_vote.php",{"id":id,"v":nb},function(html) {
			$_('#INT_'+id+'_'+nb).html(html).fadeIn();
	});
	return false;
}
function callPublish(msg, attachment, action_link) {
		  FB.ui(
            {
                'method': 'stream.publish',
                'message': msg,
                'attachment': attachment
            }
        );
}


function VDM(id,nb) {

	$_('#vote'+id).css('display','none');
	$_.post("/blog/vote.php",{"id":id,"v":nb},function(html) {
		if (html == 'NA') {
			$_('#vote'+id).html('dude! already done...').fadeIn();
		} else {
			$_('#vote'+id).html('I Agree ('+html+')').fadeIn();
		}
	});
}

function VDM_BF(id,nb) {

	$_('#votebf'+id).css('display','none');
	$_.post("/blog/vote.php",{"id":id,"v":nb},function(html) {
		if (html == 'NA') {
			$_('#votebf'+id).html('dude! already done...').fadeIn();
		} else {
			$_('#votebf'+id).html('I Disagree ('+html+')').fadeIn();
		}
	});
}

function VDM_e(id,nb) {

	$_('#vote2'+id).css('display','none');
	$_.post("/blog/vote_e.php",{"id":id,"v":nb},function(html) {
		if (html == 'NA') {
			$_('#vote2'+id).html('dude! already done...').fadeIn();
		} else {
			$_('#vote2'+id).html('I Agree ('+html+')').fadeIn();
		}
	});
}

function VDM_BF_e(id,nb) {

	$_('#votebf2'+id).css('display','none');
	$_.post("/blog/vote_e.php",{"id":id,"v":nb},function(html) {
		if (html == 'NA') {
			$_('#votebf2'+id).html('dude! already done...').fadeIn();
		} else {
			$_('#votebf2'+id).html('I Disagree ('+html+')').fadeIn();
		}
	});
}

function signaler(type,id) {

	$_('#signaler'+id).css('display','none');
	$_.get("/blog/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("/blog/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("/blog/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("/blog/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("/blog/togglesuivi.php?type=article&id="+id);
		} else {
			nombre--;
			$_.get("/blog/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("/blog/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("/blog/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("/blog/account_abus_desc.php?id="+id,function(html) {
		$_('#abusdesc').html('Abuse reported');
	});
	$_('#abusdesc').fadeIn();
}

function ClosePub(){
	$_.get("/blog/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;
}
