var comments = {
	curPage:1,
	videoId : 0,
	addNewComment:function (fm) {
		if (jQuery('#tbComment').val() == "") {
			showError("You must enter your comment message");
			return false;
		}
		var t=this;
		jQuery(fm).ajaxSubmit(
		{
			dataType: 'json',
			success: function (j) {
				if(!checkAccessJson(j))return false;
				if (j.message) {
					showMessage(j.message);
					t.updateComments();
				}
				if (j.error) showError(j.error);
			}
		});
		return false;
	},
	delcomment : function(id){
		if (!confirm('Are you sure you want to remove this comment?'))return false;
		var t=this;
		jQuery.ajax({
			url: "index.php?ACTION=comments&todo=delVideoComment&type=json",
			data:{id:id},
			dataType: 'json',
			success: function(j) {
				if(!checkAccessJson(j))return false;
				if (j.message) {
					showMessage(j.message);
					t.updateComments();
				}
				if (j.error) showError(j.error);
			}
		});
	},
	setPage:function (page){
		this.curPage=page;
		this.updateComments();
	},
	nextPage:function (){
		this.curPage++;
		this.updateComments();
	},
	prevPage:function (){
		this.curPage--;
		this.updateComments();
	},
	updateComments:function(){
		this.getComments();
	},
	setBlockUser:function(id){
		jQuery.ajax({
			url: "index.php?ACTION=comments&todo=setBlockUser&type=json",
			data:{userId:id},
			dataType: 'json',
			success: function(j) {
				if(!checkAccessJson(j))return false;
				if (j.message) {
					showMessage(j.message);
					_this.updateComments();
				}
				if (j.error) showError(j.error);
			}
		});
	},
	reply:function(el,id){
		var str = '<form class="reply" action="" onsubmit="return comments.addReply(this.txt.value,'+id+')">'+
		'<label for="">Reply this comment:</label><textarea cols="45" rows="6" name="txt"></textarea>'+
		'<ul class="repl-ctrl"><li><button type="submit">Post comment</button></li>'+
		'	<li><a href="javascript:void(0)" onclick="comments.hideReplyForm(this)">Hide form</a></li></ul></form>';
		jQuery(el.parentNode.parentNode).after(str);
	},
	hideReplyForm:function(el){
		jQuery(el.parentNode.parentNode.parentNode).remove('form.reply');
	},
	addReply:function(txt,id){
		var t=this;
		jQuery.ajax({
			url: "index.php?ACTION=comments&todo=addVideoReplyComment&type=json",
			cache: false,
			data:{
				replyId: id,
				videoId: t.videoId,
				text: txt
			},
			dataType: 'json',
			success: function(j) {
				if(!checkAccessJson(j))return false;
				if (j.message) {
					showMessage(j.message);
					t.updateComments();
				}
				if (j.error) showError(j.error);
			}
		});
		return false;
	},
	addCtrl : function(html, j) {
		html.push('<h4><a href="#'+j.AUTHOR_ID+'">'+j.COMMENT_AUTHOR+'</a></h4><div class="clear">&nbsp;</div>');
		if (j['MY_ID'] == j['VIDEO_OWNER']){
			html.push('<ul class="ctrl">');
			if(j['AUTHOR_ID']!=j['MY_ID'])html.push('<li class="ban"><a href="javascript:void(0)" onclick="comments.setBlockUser('+j.AUTHOR_ID+')">Block user</a></li>');
			html.push('<li class="rem"><a href="javascript:void(0)" onclick="comments.delcomment('+j.ID+')">Remove message</a></li></ul>');
		}
		html.push('<div class="photo"><img src="'+j.AVATAR+'" alt="'+j.COMMENT_AUTHOR+'"/></div>');
		html.push('<div class="date">'+j.COMMENT_CREATED+'</div>');
		html.push('<div class="comment">'+j.COMMENT_TEXT+'</div>');
		return html;
	},
	getComments:function(){
		var _this = this;
		jQuery.ajax({
			url: "index.php?ACTION=comments&todo=getVideoComments&type=json",
			cache: false,
			data:{
				videoId:_this.videoId,
				page: _this.curPage
			},
			dataType: 'json',
			success: function(j) {
				if(!checkAccessJson(j))return false;
				var html = [];
				var lst = j[0];
				var b=0;
				if(lst.length){
					for(var i=0; i<lst.length; i++){
						while((i)<lst.length && lst[i].IS_REPLY>0) {
							i++;
							if(b==0) { html.push('<ul class="list-inn">'); b=1; }
							html.push('<li class="it-inn"><div class="cont">');
							html.push('<h4><a href="#'+lst[i].AUTHOR_ID+'">'+lst[i].COMMENT_AUTHOR+'</a></h4><div class="clear">&nbsp;</div>');
							_this.addCtrl(html,lst[i]);
							html.push('</div></li>');
						}
						if(b!=1) {
							html.push('<li class="it"><div class="cont">');
							 _this.addCtrl(html,lst[i]);
							html.push('<div class="reply"><a href="javascript:void(0)" onclick="comments.reply(this,'+lst[i].ID+')">Reply</a></div></div>');
						}
						while((i+1)<lst.length && lst[i+1].IS_REPLY>0) {
							i++;
							if(b==0) { html.push('<ul class="list-inn">'); b=1; }
							html.push('<li class="it-inn"><div class="cont">');
							_this.addCtrl(html,lst[i]);
							html.push('</div></li>');
						}
						if(b==1){html.push('</ul>');b=0;}
						html.push('</li>');
					}
				}
				else html.push('<li>No comments</li>');
				jQuery('.comments-list').html(html.join(''));
				/// paging
				html = [];
				var pag = j[1];
				var len= pag.length;
				if(len > 1 && lst.length){
					if(pag[0].CURRENT == 'True') html.push('<li class="prev"><span>Prev</span>');
					else html.push('<li class="prev"><a href="javascript:comments.prevPage()">Prev</a></li>');
					// paging
					for(var i=0; i<len; i++){
						j = pag[i];
						if(j.CURRENT != 'True') html.push('<li><a href="javascript:comments.setPage('+j.PAGE+')">'+j.PAGE+'</a></li>');
						else html.push('<li><strong>'+j.PAGE+'</strong></li>');
					}

					if(pag[len-1].CURRENT == 'True') html.push('<li class="next"><span>Next</span>');
					else html.push('<li class="next"><a href="javascript:comments.nextPage()" >Next</a>');
				}
				jQuery('.pagging >ul.pages').html(html.join(''));
			}
		});
	}
}
