var CommentsClass = new Class.create();
CommentsClass.prototype = {

	initialize: function(commentDiv, commentForm, container) {
		this.commentDiv = commentDiv;
		this.commentForm = document.forms[commentForm];
		this.container = container;
		this.delimiter = 'cbr5DFsd3r23fF2WgwergwegWWEsCVGsDFsaAQER';
		this.current_page = 1;
		this.pages = 1;
		this.commentvalidator  = new Validator(commentForm);
		this.commentvalidator.addValidation("name", "req", "Please enter your Name");
		this.commentvalidator.addValidation("name", "alfa");
	 	this.commentvalidator.addValidation("email", "req", "Please enter your Email");
		this.commentvalidator.addValidation("email", "email", "Please enter valid Email address");
		this.commentvalidator.addValidation("header", "req", "Please enter your Subject");
		this.commentvalidator.addValidation("header", "alfa");
		this.commentvalidator.addValidation("message_body", "req", "Please enter your Comment");
		this.commentvalidator.addValidation("message_body", "alfa");
	},

	checkPagination: function() {
		if(this.current_page > this.pages)
			this.current_page = this.pages;
		if(this.current_page < 1)
			this.current_page = 1;
	},

	showMainDiv: function() {
		$('comments_next_arrow').style.visibility = 'hidden';
		$('comments_prev_arrow').style.visibility = 'hidden';
		this.checkPagination();
		if(this.current_page < this.pages)
		 	$('comments_next_arrow').style.visibility = 'visible';
		if(this.current_page > 1)
		 	$('comments_prev_arrow').style.visibility = 'visible';
	},

	fillAndShowCommentsDiv: function(id) {
		this.segment_id = id;
		//this.commentForm.name.value = ''; 
		//this.commentForm.email.value = ''; 
		//this.commentForm.location.value = '';
		this.commentForm.header.value = ''; 
		this.commentForm.message_body.value = ''; 
		this.commentForm.action = 'comments.php?id='+id;
		//Effect.DropOut(this.commentDiv);
		$('mainDiv').style.cursor = 'progress';
		$(this.commentDiv).style.cursor = 'progress';
		
		var parent_obj = this; 
		var update_complete = false;
		new Ajax.Request('comments.php?segment_id='+id+'&page='+parent_obj.current_page+'&info', {
			onSuccess: function(transport) {
				var segmInfoArr = transport.responseText.split(parent_obj.delimiter);
				$('comment_program_title').innerHTML = segmInfoArr[0]; 
				$('comment_program_desc').innerHTML = segmInfoArr[1];
				parent_obj.pages = segmInfoArr[2]; 
				if(update_complete) { 
					parent_obj.showMainDiv();
					$('mainDiv').style.cursor = 'default';
					$(parent_obj.commentDiv).style.cursor = 'default';
					Effect.BlindDown(parent_obj.commentDiv, {duration:0.5});
					//$(parent_obj.commentDiv).style.display = 'block';
				}
				update_complete = true;
			}
		});

		new Ajax.Request('comments.php?segment_id='+id+'&page='+parent_obj.current_page+'&comments', {
			onSuccess: function(transport) {
				$(parent_obj.container).innerHTML = transport.responseText;
				if(update_complete) {
					parent_obj.showMainDiv();
					$('mainDiv').style.cursor = 'default';
					$(parent_obj.commentDiv).style.cursor = 'default';
					Effect.BlindDown(parent_obj.commentDiv, {duration:0.5});
					//$(parent_obj.commentDiv).style.display = 'block';
				}
				update_complete = true;
			}
		});
	},

	prevPage: function() {
		this.current_page--;
		this.checkPagination();
		this.renewCommentList();
	},

	nextPage: function() {
		this.current_page++;
		this.checkPagination();
		this.renewCommentList();
	},

	renewCommentList: function() {
		//Effect.DropOut(this.commentDiv);
		$('mainDiv').style.cursor = 'progress';
		$(this.commentDiv).style.cursor = 'progress';
		var parent_obj = this; 
		new Ajax.Request('comments.php?segment_id='+parent_obj.segment_id+'&page='+parent_obj.current_page+'&comments', {
			onSuccess: function(transport) {
				$(parent_obj.container).innerHTML = transport.responseText;
				parent_obj.showMainDiv();
				$('mainDiv').style.cursor = 'default';
				$(parent_obj.commentDiv).style.cursor = 'default';
			}
		});
	},

	AddComment: function() {
		if(!this.commentvalidator.formobj.onsubmit())
			return;
		var SendArray = new Array();
		SendArray['name'] = this.commentForm.name.value; 
		SendArray['email'] = this.commentForm.email.value; 
		SendArray['location'] = this.commentForm.location.value;
		SendArray['notify_me'] = this.commentForm.notify_me.checked;
		SendArray['header'] = this.commentForm.header.value; 
		SendArray['message_body'] = this.commentForm.message_body.value; 
		SendArray['commentsOnRoad'] = this.commentForm.commentsOnRoad.value;
        SendArray['captcha_code'] = this.commentForm.captcha_code.value; 
		 
		$('mainDiv').style.cursor = 'progress';
		$(this.commentDiv).style.cursor = 'progress';
		
		var parent_obj = this; 
		new Ajax.Request('comments.php?id='+this.segment_id, {
			method: 'post',
			parameters: SendArray, 
			onSuccess: function(transport) {
				alert(transport.responseText);
                Effect.DropOut(parent_obj.commentDiv);
				$('mainDiv').style.cursor = 'default';
				$(parent_obj.commentDiv).style.cursor = 'default';
                if(!transport.responseText.match("Wrong"))
                {
				    //parent_obj.commentForm.name.value = ''; 
				    //parent_obj.commentForm.email.value = ''; 
				    //parent_obj.commentForm.location.value = '';
				    //parent_obj.commentForm.notify_me.checked = false;
				    parent_obj.commentForm.header.value = ''; 
                    parent_obj.commentForm.message_body.value = ''; 
                    parent_obj.commentForm.captcha_code.value = ''; 
                    for(i=0; i<6; i++)
                        $('comments_hide_row_'+i).style.display='none';
                }
			}
		});
	}
    
}
