<!--// JavaScript Document
$(document).ready(function () { /**** JQUERY SCRIPTS ****/
//---------------------------------------
// Script 1
// Enables submit button only if there's something in the LongLink field
//---------------------------------------
$('#biginput').keyup(function () {
	var convertLink = $('#convertLink'); // Form button
	if(this.value == "") { convertLink.attr("disabled", "true"); } else { convertLink.removeAttr("disabled"); }
});
$('#biginput').change(function () {
	var convertLink = $('#convertLink'); // Form button
	if(this.value == "") { convertLink.attr("disabled", "true"); } else { convertLink.removeAttr("disabled"); }
});
// End Script 1
//---------------------------------------

//---------------------------------------
// Script 2
// Facebox - Customize checkbox in span "group"
//---------------------------------------
$("input[type='checkbox']").custCheckBox();
  jQuery(document).ready(function($) { $('a[rel*=facebox]').facebox({loading_image : 'library/jquery/facebox/loading.gif',close_image : 'library/jquery/facebox/closelabel.gif'}) })
// END Script 2
//---------------------------------------

//---------------------------------------
// Script 3
// Tag validation logic
//---------------------------------------
	var validateTag = $('#validateTag'); // Element that will receive the return of ajax.
	var convertLink = $('#convertLink'); // Form submit button to disable/enable
	var biginput = $("#biginput"); // Other form field checked when enabling/desabling form button.

	$('#tag').keyup(function () {
		// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
		var t = this;
		// only run the check if the tag has actually changed - also means we skip meta keys
		if (this.value != this.lastValue) {
			// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
			// a particular key, it will only fire when the release the key.
			if (this.timer) clearTimeout(this.timer);
			// show our holding text in the validation message space
			validateTag.removeClass('error').html('<p style=3line-height:16px3><img src="images/ajax-loader.gif" height="16" width="16" align="absmiddle" /> &nbsp;V&eacute;rification en cours...</p>');

			// fire an ajax request in 1/5 of a second
			this.timer = setTimeout(function () {
				$.ajax({
					url: 'ajax_tagcheck.php',
					data: 'action=check_tag&tag=' + t.value,
					dataType: 'json',
					type: 'post',
					success: function (j) {
						// put the 'msg' field from the $resp array from check_tag (php code) in to the validation message
						validateTag.html(j.msg);
						// disables/enables the submit button
						if(j.ok == false) {	convertLink.attr("disabled", "true"); }
						else {
							if(biginput.val() != "") { // if no link provided, do not enable submit button.
								convertLink.removeAttr("disabled");
							}
						}
					}
				});
			}, 200);

			// copy the latest value to avoid sending requests when we don't need to
			this.lastValue = this.value;
		}
	});

// END Script 3
//---------------------------------------

//---------------------------------------
// Script 4
// Sends Quickform thru ajax. PHP returns JSON array.
//---------------------------------------
	convertLink.click(function () {
		$.ajax({
			url: 'ajax_addlink.php',
			data: $("#addLink").serialize(),
			dataType: 'json',
			type: 'post',
			success: function(j) {
				$("#ajax_addlink_return").html(j.msg); // Updates Results element
				$("#Results").fadeIn("slow"); // Shows Results element
				if(j.ok == true) {
					//Updating nextSteps form fields.
					$("#cKey").attr("value", j.Key);
					$("#clink_id").attr("value", j.link_id);
					$("#QuickForm").slideUp(); // Hide the form.
					$("#HowManyLinks").fadeOut();
					$("#HowManyLinks").html(j.HowManyLinks);
					$("#HowManyLinks").fadeIn();
					$("#nextSteps").css('display', 'block');
				}
			},
		});
	return false;
	});

// END Script 4
//---------------------------------------

//---------------------------------------
// Script 5
// Various functions linked to the 'nextsteps'
//---------------------------------------
$("#nextSteps").accordion({ active: false, autoHeight: false, header: "h4" }); // Accordion
$("#LinkComment").elastic(); // make the comment textarea elastic like facebook
$("#LinkComment").keyup(function () { if(this.value == "") { $("#saveLinkComment").attr("disabled", "true"); } else { $("#saveLinkComment").removeAttr("disabled"); } }); // enables/disables the comment submit button.

// END Script 5
//---------------------------------------

//---------------------------------------
// Script 6
// Sends comment thru ajax. PHP returns JSON array.
//---------------------------------------
$("#addComment").submit(function() {
	$.ajax({
			url: 'ajax_addcomment.php',
			data: $("#addComment").serialize(),
			dataType: 'json',
			type: 'post',
			success: function(j) {
				if(j.ok == true) {
					// $("#nextSteps_return").append(j.msg); // adds 'ok' feedback to nextSteps_return
					// $("#CommentSection").fadeOut();
					// $("#h4addcomment").fadeOut();
					$("#h4addcomment").removeClass('comment32');
					$("#h4addcomment").html(j.msg);
					$("#CommentSection").html(j.comment);

				} else {
					$("#CommentSection").append(j.msg); // adds an element CommentSection element
				}
			},
		});
	return false;
});
// END Script 6
//---------------------------------------

//---------------------------------------
// Script 7
// registration form.
//---------------------------------------
// Script 7.1: Calls registrion form
$("#GoToRegister").click( function() {
	$.ajax({
	 url: 'ajax_user-management.php',
	 data: 'mod=regform',
	 dataType: 'json',
	 type: 'get',
			success: function(j) { 
				$("#getLinkSection").html(j.msg);
			}
	});
return false;
});

//	$("#GoToRegister").click( function() {
//		$("#getLinkSection").load("ajax_user-management.php?mod=regfrom");
//			$.ajax({
//			url: 'ajax_user-management.php',
//			data: "mod=regfrom",
//			success: function(j) {
//					$("#getLinkSection").html(j);
//					Recaptcha.create("6LeeYgUAAAAAAD-wIqrytSIL0J1grDWYLyxIUEts", "reg_reCaptcha", { theme: "blackglass", callback: Recaptcha.focus_response_field });
//									}
//			}),
//		});
//


// Script 7.2: Submit registration form

// END Script 7
//---------------------------------------



}); // END DOCUMENT READY (JQUERY SCRIPTS FOR THIS PAGE)

//---------------------------------------
// Script 8
// Simply counts the number of words in the string submitted.
//---------------------------------------

function countwords(inputString, targetId){ 
 var response; var numwords = inputString.split(' ').length;
 if (numwords == 0) { response = "Ecrivez votre commentaire ci-dessus."; } else if(numwords == 1) { response = "1 mot."; } else { response = numwords + " mots"; }
 document.getElementById(targetId).innerHTML = response;
}
//-->
