/**
 *
 * A comment class to manage adding/replying
 * to comments.
 *
 **/
var Comments = (function(){
	
	var constructor = function(){
		
		$.getScript(GBL_BASE_URL+'javascript/library/jquery/plugins/jquery.validate.min.js');
		if(!$.browser.msie){
			$.getScript(GBL_BASE_URL+'javascript/library/jquery/plugins/jquery.autogrow.js');
			$.getScript(GBL_BASE_URL+'javascript/library/jquery/plugins/jquery.scrollTo.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(self.currently_open_box);
		}

		
		if(!$.browser.msie){
			$('#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();
				$.scrollTo('#comment_container_'+parent_comment_id, 500);
				self.currently_open_box = parent_comment_id;
			});
		} else {
			$('#comment_container_'+parent_comment_id).show();
			$('#recaptcha_container_'+parent_comment_id).html($('#recaptcha_widget_div').html());
			self.initialize_validation(parent_comment_id);
			$.scrollTo('#comment_container_'+parent_comment_id, 500);
			this.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;
		if(isNaN(el)){
			$(el).parents('[id*=comment_container_]').slideUp();
		} else {
			$('#comment_container_'+el).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;
		
})();