var fadeSpeed = 3000;

$(document).ready(function(){
	var authorClicked = false;
	var emailClicked = false;
	var urlClicked = false;
	
	$(".commentAuthor").click(function(){
		$(this).css('color','#000000');
		if (! authorClicked){
			$(this).attr('value','');
			authorClicked = true;
		}
	});
	
	$(".commentEmail").click(function(){
		$(this).css('color','#000000');
		if (! emailClicked){
			$(this).attr('value','');
			emailClicked = true;
		}
	});
	
	$(".commentUrl").click(function(){
		$(this).css('color','#000000');
		if (! urlClicked){
			$(this).attr('value','');
			urlClicked = true;
		}
	});

	ajaxCommentForm('1');


});

function ajaxCommentForm(form){


	//set ajax handler to the comment form
	var options = {target: '#commentsOutput' + form,beforeSubmit:showLoading, success:showResponse};
	function showResponse(){
		$("#ajaxLoading" + form).hide();
 		$("#commentsOutput" + form).fadeIn("slow");
		$("#commentList").load(commentThread,{},function(){
			$("#commentsOutput" + form).fadeTo(fadeSpeed, 75);
			$("#commentText" + form).html('');
			$("#commentText" + form).attr('value','');
		});
	}
	
	function showLoading(){
		$("#ajaxLoading" + form).show();
	}
	
	
	$("#commentSubmit" + form).click(function() {
		$("#realCommentPreview" + form).attr('value','');
		$("#commentsForm" + form).ajaxSubmit(options); 
    
		return false; 
	});
	
	
	//set ajax handler to the comment form
	var previewOptions = {target: '#commentsOutput' + form,beforeSubmit:showLoading, success:showPreview};
	function showPreview(){
		$("#ajaxLoading" + form).hide();
 		$("#commentsOutput" + form).fadeIn("slow");
	}
	
	
	
	$("#commentPreview" + form).click(function() {
		$("#realCommentPreview" + form).attr('value','Preview');
		$("#commentsForm" + form).ajaxSubmit(previewOptions); 
    
		return false; 
	});
	
	$("#commentsForm" + form).submit(function(){
		$("#realCommentPreview" + form).attr('value','Preview');
		$(this).ajaxSubmit(previewOptions); 
    
		return false; 
	});
	
	
}

function cancelReply(){
	$(".replyForm").fadeOut(fadeSpeed, function(){
		//$(".replyForm").empty();
	});
	return false;
}

function mtReplyCommentOnClick(entry_id, parent_id, author) {
    //mtShow('comment-form-reply');
    
    $("#replyForm" + parent_id).load("commentForms/commentForm" + entry_id + ".php", {commentID: parent_id}, function(){
    
 	   ajaxCommentForm(parent_id);
 	   $("#replyForm" + parent_id).append('<p><a href="#" onclick="cancelReply(); return false;" class="cancelReply">Cancel</a></p>');
	 
		// Populate label with new values
		if (author){
			var replyText = 'Replying to '+ author;
		}else {
			var replyText = 'Replying to comment #' + parent_id;
		}
	   
		$("#commentParentID" + parent_id).attr('value',parent_id); 
		$("#commentsHeader" + parent_id).html(replyText);
		$("#commentReply" + parent_id).attr("value",parent_id); 
		$("#commentReply" + parent_id).attr("checked","checked"); 
		
		$("#replyForm" + parent_id).fadeIn(fadeSpeed);
		$("#replyForm" + parent_id + " comment-text").focus();
   });
   
	
	
}

