function videoComment(hnd, videoId, pages, rows, commentsCnt){
	this.hnd = hnd;
	this.videoId = videoId;
	this.pages = pages;
	this.rows = rows;
	this.commentsCnt = commentsCnt;
	 
	this.currentPage = 0;
	this.order = 'normal';
	
	this.loadData = function(page){
		
		/* Init the params and get the data */
		var con = new rJSON();
		var params = {};
		params.module = 'video_comments';
		params.id = this.videoId;
		params.limit = 5;
		params.offset = page * params.limit;
		params.order = this.order;
		var res = con.getData('exec.php', params);
		if(res.error){
			return false;
		}
		this.commentsCnt = res.comments_cnt;
		this.pages = res.pages;
		this.currentPage = page;
		
		/* Show the new comments */
		var el;
		for(var i = 0; i < res.comments.length; i++){
			el = document.getElementById("j_videoCommentsCaption_" + this.hnd + "_" + i);
			el.innerHTML = "<p><a href=\"user_profile.php?username=" +  encodeURIComponent(res.comments[i].username) + "\">" + res.comments[i].username + "</a>" + " | " + res.comments[i].tstamp + "</p>";
			el = document.getElementById("j_videoCommentsBody_" + this.hnd + "_" + i);
			el.innerHTML = res.comments[i].comment;
			el = document.getElementById("j_videoCommentsContainer_" + this.hnd + "_" + i);
			el.style.display = 'inline';
		}
		if(i < this.rows - 1){
			for(; i < this.rows; i++){
				el = document.getElementById("j_videoCommentsContainer_" + this.hnd + "_" + i);
				el.style.display = 'none';
			}
		}
		
		/* Make the pagination */
		var links = "";
		var linksEnd = "";
		if(this.pages < 6){
			var rangeStart = 0;
			var rangeEnd = this.pages;
		}else if(this.currentPage > this.pages - 3){
			links = "<li><a href=\"#\" onclick=\"videoComments[" + this.hnd + "].loadData(0); return false;\">1</a> ... </li>\n";
			var rangeStart = this.pages - 5;
			var rangeEnd = this.pages;
		}else if(this.currentPage < 3){
			linksEnd = "<li> ... <a href=\"#\" onclick=\"videoComments[" + this.hnd + "].loadData(" + (this.pages - 1) + "); return false;\">" + this.pages + "</a></li>\n";
			var rangeStart = 0;
			var rangeEnd = 5;
		}else{
			links = "<li><a href=\"#\" onclick=\"videoComments[" + this.hnd + "].loadData(0); return false;\">1</a> ... </li>\n";
			if(this.currentPage < this.pages - 3){
				linksEnd = " ... " + this.pages;
			}
			var rangeStart = this.currentPage - 2;
			var rangeEnd = this.currentPage + 3;
		}
		for(i = rangeStart; i < rangeEnd; i++){
			if(this.currentPage == i){
				links += "<li><b>" + (i + 1) + "</b></li>\n";
			}else{
				links += "<li><a href=\"#\" onclick=\"videoComments[" + this.hnd + "].loadData(" + i + "); return false;\">" + (i + 1) + "</a></li>\n";
			}
		}
		el = document.getElementById("j_videoCommentsPages_" + this.hnd);
		el.innerHTML = links + linksEnd;
		
		return true;
	}
	
	this.switchOrder = function(){
		if(this.order == 'normal'){
			this.order = 'reverse';
			document.getElementById("j_switchOrderLabel_" + hnd).innerHTML = "Възходящ ред";
			document.getElementById("j_switchOrderLabel_" + hnd).title = "Покажи коментарите във възходящ ред";
		}else{
			this.order = 'normal';
			document.getElementById("j_switchOrderLabel_" + hnd).innerHTML = "Низходящ ред";
			document.getElementById("j_switchOrderLabel_" + hnd).title = "Покажи коментарите в низходящ ред";
		}
		this.loadData(0);
	}
	
	this.saveComment = function(){
		var con = new rJSON();
		var params = {};
		params.module = 'video_comments';
		params.video_id = this.videoId;
		params.comment = document.getElementById("j_videoCommentsComment_" + this.hnd).value;
		params.cmd = 'add';
		//params.rnd = Math.random();
		if(params.comment == ''){
			return true;
		}
		var res = con.getData("exec.php", params);
		if(res.error){
			alert("Възникна грешка! Моля, опитайте по-късно!");
		}else{
			if(this.order == 'normal'){
				this.loadData(0);
			}else{
				if(commentsCnt % 5 == 0){
					this.loadData(pages);
				}else{
					this.loadData(pages - 1);
				}
			}
			document.getElementById("j_videoCommentsStatus_" + hnd).style.display = 'block';
			document.getElementById("j_videoCommentsNoCommentsLabel_" + hnd).style.display = 'none';
		}
		document.getElementById("j_videoCommentsComment_" + this.hnd).value = '';
		return true;
	}
	
	this.nextPage = function(){
		if(this.currentPage < this.pages - 1){
			this.loadData(this.currentPage + 1);
		}
	}
	this.prevPage = function(){
		if(this.currentPage > 0){
			this.loadData(this.currentPage - 1);
		}
	}
}
		
