/**
 *
 * A comment class to manage adding/replying
 * to comments.
 *
 **/
var Comments = (function(){
	
	var constructor = function(){
		include_js('javascript/library/jquery/ui/ui.core.min.js');
		include_js('javascript/library/jquery/ui/ui.slider.min.js');
		include_js('javascript/library/jquery/plugins/jquery.autogrow.js');
		include_js('javascript/library/jquery/plugins/jquery.validate.min.js');
	}
	
	constructor.currently_open_box = -1;
	
	/**
	 * Show a reply container
	 */
	constructor.prototype.show_comment_box = function(parent_comment_id){
		var self = this;
		if(this.currently_open_box >-1){
			this.cancel_comment(this.currently_open_box);
		}
		$('#recaptcha_container_'+parent_comment_id).html($('#recaptcha_widget_div').html());
		$('#comment_textarea_'+parent_comment_id).autogrow();
		$('#comment_container_'+parent_comment_id).slideDown(100, function(){
			self.initialize_validation(parent_comment_id);
			$('#comment_form_'+parent_comment_id+' input[name=commenter_name]').focus();

			self.currently_open_box = parent_comment_id;
		});
	}
	
	/**
	 * The function that will be called when a reply is canceled.
	 */
	constructor.prototype.cancel_comment = function(el){
		this.currently_open_box = -1;
		$(el).parent().parent().slideUp();
	}

	constructor.prototype.initialize_validation = function(parent_comment_id){
		$('#comment_form_'+parent_comment_id).validate({
			rules: {
				commenter_name: {
					required:true
				},
				commenter_email_address: {
					required:true,
					email:true
				},
				commenter_website: {
					url:true
				},
				comment_text: {
					required:true
				},
				recaptcha_response_field: {
					required:true
				}
			}
		});
	}
	
	return constructor;
		
})();