var comments = '';
var total_comments = '';
var total_comments_2 = '';
var timeout = 2500;

function deleteComment(comment_id){
	if(confirm('Are you sure you want to delete this comment?') == true){
	new Ajax.Request('../actions/delete_comment.php?comment_id=' + comment_id, {onComplete:function(e){
		response = e.responseText;
		
		$('comment_response').update(response);
		
		$('comment_' + comment_id).remove();
		
		setTimeout('clearResponse()', timeout);
	}});
	}
}

function evaluteSurvey(){
	$('conclusion').show();
	
	$('survey_form').request({onComplete:function(e){
		$('survey_result').update(e.responseText);
	}});
}

function postComment(id, comment_type, child_id){
	$('comment_form').request({
		parameters: {article_id:id,
					 type:comment_type,
					 video_id:child_id},
		onComplete:function(e){
			$('comment_response').update(e.responseText);
			$('comment_text').value = '';
			
			setTimeout('clearResponse()', timeout);
			//updateComments(id, comment_type, video_id, false);
		}	
	});
}

function clearResponse(){
	$('comment_response').update('');
}

function updateComments(article_id, type, video_id, period){
	period = typeof(period) != 'undefined' ? period : true;

	if(period == true){
		comments = new Ajax.PeriodicalUpdater('comment_container','../actions/comments.php?article_id=' + article_id + '&video_id=' + video_id + '&type=' + type, {frequency: 30});
	}
		else{
			comments = new Ajax.Updater('comment_container','../actions/comments.php?article_id=' + article_id + '&video_id=' + video_id + '&type=' + type);
		}
	
	
	//total_comments = new Ajax.PeriodicalUpdater('total_comments','../actions/total_hesaid_comments.php?article_id=' + article_id + '&video_id=' + video_id);
}

function hesaidComments(article_id, video_id){	
	comments = new Ajax.PeriodicalUpdater('hesaid_comments','../actions/hesaid_comments.php?article_id=' + article_id + '&video_id=' + video_id, {frequency: 30});
	
	total_comments = new Ajax.PeriodicalUpdater('total_comments','../actions/total_hesaid_comments.php?article_id=' + article_id + '&video_id=' + video_id);
	//total_comments_2 = new Ajax.PeriodicalUpdater('total_comments_2','../actions/total_hesaid_comments.php?article_id=' + article_id + '&video_id=' + video_id);
}

function stopComments(){
	comments.stop();
	total_comments.stop();
	//total_comments_2.stop();
}

function updateTitle(set_to){
	$('comment_title').update(set_to);
}
function changeCurrentVideo(article_id, change_to){
	video_id = change_to.getValue();
	
	if(video_id !== ''){
		stopComments();
		
		$('comment_form').attributes['onsubmit'].value = 'postComment(\'' + article_id  + '\',\'hesaid\',\'' + video_id + '\'); return false;';
		
		hesaidComments(article_id, video_id);
	}
}