jq(document).ready(function(){
	jq('form#commentform').submit(function(){
		clearTimeout(commenter.formTimer);
		this.isLoading = true;
		jq.ajax({
			beforeSend: commenter.loading,
			complete: commenter.complete,
			error: commenter.error,
			success: commenter.success,
			cache: false,
			data: jq('#commentform').serialize(),
			type: 'POST',
			dataType: 'html',
			url: commenter.processURL		
		});
		return false;
	});
	jq('#writeComment h3').click(commenter.formToggle);
	if(jq('form#commentform').length > 0){
		commenter.formEmpty(); // disabling the submit button while there is no message.		
	}

	//open the editor if page called on page load
	if (window.location.hash == '#writeComment') {
		jq('#commentform').slideDown();
	}

	commenter.hideComments(5);
});

var commenter = {
	formToggle: function(){
		if(document.getElementById('commentform').style.display == 'none'){
			jq('#commentform').slideDown();			
		} else {
			jq('#commentform').slideUp();						
		}
	},
	success: function(data){
		jq('#commentlist').prepend(data);
		jq('form#commentform textarea').removeAttr('value');
		jq('.noComment').hide();
		notifyer.dismissNotice();
		jq('#commentform').slideUp();
		jq('#commentlist li:first').slideDown();
		jq('#commentlist li:first').colorBlend([{
			cycles:1, 
			fromColor:"#FFF6BF", 
			toColor:"white", 
			param:"background-color", 
			isFade:false,
			fps: 100 // bigger is slower
		}]);
	},
	error: function(response){
		notifyer.displayNotice('error', response.responseText);
		jq('#commentform input').removeAttr('disabled');
		jq('#commentform textarea').removeAttr('disabled');
	},
	loading: function(state){
		notifyer.dismissNotice();
		jq('#loading').show();
		jq('#commentform input').attr('disabled', 'disabled');
		jq('#commentform textarea').attr('disabled', 'disabled');

	},
	complete: function(){
		this.isLoading = false;
		commenter.formEmpty();
		jq('#loading').hide();
		jq('#commentform input').removeAttr('disabled');
		jq('#commentform textarea').removeAttr('disabled');
		
	},
	formEmpty: function(){ // TODO - check name, email, and website for completeness before making available the submit button
		jq('form#commentform #submit').attr('disabled', 'disabled');
		if(jq('form#commentform textarea').attr('value').length > 0){
			jq('form#commentform #submit').removeAttr('disabled');
		} else {
			jq('form#commentform #submit').attr('disabled', 'disabled');
		}
		if (this.isLoading == true) {
			console.log('timimg');			
		};
		this.formTimer = setTimeout('commenter.formEmpty()', 10);
	},
	scrollToComment: function(loc){
		var divOffset = jq(document).offset().top;
		var trackOffset = jq(loc).offset().top;
		var pScroll = trackOffset - divOffset;
		jq(document).animate({scrollTop: '+=' + pScroll + 'px'});
	}, 
	hideComments: function(display){
		var comments = jq('#commentlist li');
		var remainingComments = comments.length - display;
		if (comments.length > display && window.location.hash.substring(0,8) != '#comment') {
			//if there are many comments and they didn't follow a link to a specific comment
			for(var i=0; i < comments.length; i++){
				if(i > (display - 1)){
					jq(comments[i]).hide();
				} // end if i > display
			} // end for loop
			moreLink = '<a id="moreComments" class="more">' + sprintf(pluralise('See one remaining comment', 'See %s remaining comments', remainingComments), remainingComments) + '</a>';
			jq('#commentlist').after(moreLink);
			jq('#moreComments').click(commenter.showComments);
		} // end comments.length
	},
	showComments: function(){
		jq('#commentlist li:hidden').each(function(){ jq(this).slideDown() });
		jq('#moreComments').remove();
	},
	isLoading: false, 
	formTimer: null, 
	processURL: templatePath + '/comments-ajax.php' // set by php in the html page
}

