$(document).ready(function(){
    $(".commenting").find("input").focus(function() {
    	if (!$(this).data("focused") ||
    		$(this).val()=="your name*" ||
    		$(this).val()=="your email*" ||
    		$(this).val()=="your website") {
	    	$(this).data("focused", true);
	    	$(this).val("");
	    }
    });
    $(".commenting").find("textarea").focus(function() {
    	if (!$(this).data("focused") || $(this).val()=="your comment*") {
	    	$(this).data("focused", true);
	    	$(this).val("");
	    }
    });
});

function initForm(moduleid) {
	$("#nameInput").val("your name*");
	$("#emailInput").val("your email*");
	$("#urlInput").val("your website");
	$("#textInput").val("your comment*");
}

var tee;
function submitComment(moduleid, notification) {
	$("#submitCommentButton_"+moduleid).css({'background-image':'url("./usercomments/assets/processing.gif")'});
    // envoi des données au handler
    $.ajax({
       type: "POST",
       url: "./usercomments/exec.comment.xhr.php5",
       async: false,
       data: $("#commentingForm_"+moduleid).serialize()+"&notification="+notification,
       success: function(msg) {
         if (msg.indexOf("congrats") != -1) {
         	$("#submitCommentButton_"+moduleid).css({'background-image':'url("./usercomments/assets/success.gif")'});
         	updateCommentsList(moduleid); //reload the comments
         	initForm(moduleid);
			tee = setTimeout("clearResults();", 3000);
         } else {
			$("#submitCommentButton_"+moduleid).css({'background-image':'url("./usercomments/assets/failure.gif")'});
	        /*$("#userComments_"+moduleid).prepend('<div class="commentingResult" id="commentingResult_'+moduleid+'"></div>');
	        $("#commentingResult_"+moduleid)
	         	.click(function() {clearResults();})
	         	.html(msg).animate({opacity:.97},150,"linear",function(){}
	        );*/
			tee = setTimeout("clearResults();", 5000);
         
         }
       }
    });
}
function clearResults() {
	clearTimeout(tee);
	/*$(".commentingResult").animate({opacity:0}, 300, "linear", function() {$(this).remove();});*/
	$(".submitCommentButton").css({'background-image':'url("./usercomments/assets/submit.gif")'});
}

function updateComments(moduleid, notification) {
	$("#userComments_"+moduleid).html("<img src='./usercomments/assets/loading.gif' alt='Processing...' style='position:relative; top:40%; left:220px;' />");
	$.ajax({
       type: "POST",
       url: "./usercomments/body.usercomments.xhr.php5",
       async: false,
       data: "moduleid="+moduleid+"&notification="+notification,
       success: function(msg) {
			$("#userComments_"+moduleid).html(msg);
       }
    });
}
function updateCommentsList(moduleid, notification) {
	$("#commentsList_"+moduleid).html("<img src='./usercomments/assets/loading.gif' alt='Processing...' style='position:relative; top:50%; left:180px;' />");
	$.ajax({
       type: "POST",
       url: "./usercomments/body.commentslist.xhr.php5",
       async: false,
       data: "moduleid="+moduleid+"&notification="+notification,
       success: function(msg) {
			$("#commentsList_"+moduleid).html(msg);
       }
    });
}

function loginAdmin(moduleid, notification) {
	$.ajax({
       type: "POST",
       url: "./usercomments/body.admin.xhr.php5",
       async: false,
       data: "moduleid="+moduleid+"&notification="+notification+"&"+$("#loginForm_"+moduleid).serialize(),
       success: function(msg) {
			$("#userComments_"+moduleid).html(msg);
			$("form#loginForm_"+moduleid+" :input:visible:first").focus();
       }
    });
}
function logoutAdmin(moduleid, notification) {
	$("#userComments_"+moduleid).html("<img src='./usercomments/assets/loading.gif' alt='Processing...' style='position:relative; top:40%; left:220px;' />");
	$.ajax({
       type: "POST",
       url: "./usercomments/body.admin.xhr.php5",
       async: false,
       data: "moduleid="+moduleid+"&notification="+notification+"&logout=true",
       success: function(msg) {
			$("#userComments_"+moduleid).html(msg);
       }
    });
}
function resetPass(moduleid, notification) {
	$("#login_"+moduleid).html("<img src='./usercomments/assets/loading.gif' alt='Processing...' style='position:relative; top:40%; left:180px;' />");
	$.ajax({
       type: "POST",
       url: "./usercomments/body.admin.xhr.php5",
       async: false,
       data: "moduleid="+moduleid+"&notification="+notification+"&resetpass=true",
       success: function(msg) {
			$("#userComments_"+moduleid).html(msg);
			$("form#loginForm_"+moduleid+" :input:visible:first").focus();
       }
    });
}

function validateComment(moduleid, comid) {
	$("#validateComment_"+moduleid+"_"+comid).css({'background-image':'url("./usercomments/assets/processing_small.gif")'});
	$.ajax({
       type: "POST",
       url: "./usercomments/exec.validate.xhr.php5",
       async: false,
       data: "moduleid="+moduleid+"&comid="+comid,
       success: function(msg) {
			$("#validateComment_"+moduleid+"_"+comid).css({'background-image':'url("./usercomments/assets/'+msg+'.png")'});
			$("#validateComment_"+moduleid+"_"+comid).html("");
       }
    });
}

function webmasterComment(moduleid, comid) {
	$("#webmasterComment_"+moduleid+"_"+comid).css({'background-image':'url("./usercomments/assets/processing_small.gif")'});
	$.ajax({
       type: "POST",
       url: "./usercomments/exec.webmaster.xhr.php5",
       async: false,
       data: "moduleid="+moduleid+"&comid="+comid,
       success: function(msg) {
			$("#webmasterComment_"+moduleid+"_"+comid).css({'background-image':'url("./usercomments/assets/'+msg+'.png")'});
			$("#webmasterComment_"+moduleid+"_"+comid).html("");
       }
    });
}

function deleteComment(moduleid, comid) {
	$("#deleteComment_"+moduleid+"_"+comid).css({'background-image':'url("./usercomments/assets/processing_small.gif")'});
	$.ajax({
       type: "POST",
       url: "./usercomments/exec.delete.xhr.php5",
       async: false,
       data: "moduleid="+moduleid+"&comid="+comid,
       success: function(msg) {
			$("#comment_"+moduleid+"_"+comid).animate({opacity:0}, 300, "linear", function() {$(this).remove();});
       }
    });
}

