	function updateRightBlocks(){
		
		if( relVidStatus == "open" ){
			$("a#rel-vid-toggle").text("скрыть");
			if( $("div#rel-vids:visible").size() == 0 ) { $("div#rel-vids").slideDown(); }
		}else{
			$("a#rel-vid-toggle").text("показать");
			if( $("div#rel-vids:visible").size() != 0 ) { $("div#rel-vids").slideUp(); }			
		}
		
		if( userVidStatus == "open" ){
			$("a#user-vid-toggle").text("скрыть");
			if( $("div#user-vids:visible").size() == 0 ) { $("div#user-vids").slideDown(); }
		}else{
			$("a#user-vid-toggle").text("показать");
			if( $("div#user-vids:visible").size() != 0 ) { $("div#user-vids").slideUp(); }			
		}
	
	}
	function goTo(where) {
		document.location.replace(where);
		return false;
	}
	function clickHandler( id ){

		alert( id );
		return false;

	}
	function processResponse( res ){
		if( res._SENDID == "add_comment" ){
			if( res.status == "failure" ){
				$(".asMes").empty();
				$(".asMes").prepend("<div class=\"ajaxErr\">" + res.curError + "</div>");
			}else{				
				$(".asMes").empty();
				$(".asMes").prepend("<div class=\"ajaxSuccess\">" + msg_comment_added + "</div>");
				$("textarea.quote").val("").attr('disabled' , 'disabled');
				$(".replyForm").hide();
				if( curPage == res.page ){				
					$('#comments').trigger("get_comments");				
				}else{
					curPage = res.page;
					$('#comments').trigger("get_comments");
					pageToGo = curPage;
					containerId = "p-" + pageToGo;
					updatePaginator( pageToGo , containerId , 1 );
				}

			}

		}
		if( res._SENDID == "add_vote" ){

			if( res.status == "failure" ){

				$("#wait").hide();
				$("#addVote").show();
				$("td.vote").prepend("<div class=\"err\">" + msg_vote_error + "</div>");

			}else{

				$("#wait").hide();
				if( res.yourRate == null ){
					$("#addVote").show();
					$("#wait").hide();
				}else{
					$("td.vote").append("<div class=\"success\">" + msg_vote_added + " " +  noun_your_vote + " " + res.yourRate + "</div>");
				}
				$("#rate").addClass("r" + res.curRateDescrete);
				$("#curRateMon").empty();
				$("#curRateMon").append( res.curRate );
				$("#votesCount").empty();
				$("#votesCount").append( res.votesCount );

			}

		} 

	}
	function setNoticeVote( formData, jqForm, options ){
		$("#addVote").hide();
		if( $("#wait").length == 0 ){
			$("td.vote").append("<div id=\"wait\">" + msg_ajax_data_transmitting + "</div>");
		}else{
			$("#wait").show();
		}
	}
	//-----------------------------------------------------------------
	function setNoticeComment( formData, jqForm, options ){
		$(".asMes").append("<div id=\"wait\">" + msg_ajax_data_transmitting + "</div>");
	}
	//-----------------------------------------------------------------
	function updatePaginator( pageToGo , containerId , checkCapacity ){

		var prevPageNum = $("#p-comments span:has('span')").find('span').text();
		$("#p-comments span:has('span')").empty().append('<a href="/watch?v=' + token + '">' + prevPageNum.toString() + '</a>');

		if( checkCapacity == 0 ){

			$("#" + containerId).empty().append('<span>' + pageToGo + '</span>');

		}else{

			if( $("#" + containerId).length ){

				$("#" + containerId).empty().append('<span>' + pageToGo + '</span>');

			}else{

				$("#p-comments .nbg").parents("span").before('<span class="' + containerId + '"><span>' + pageToGo + '</span></span>');

			}

			forcePage = 0;

		}

		curPage = pageToGo;

		$('#comments').trigger("get_comments");

		return false;

	}

	$( document ).ready(function() {		
		updateRightBlocks();		
		$("a#rel-vid-toggle").bind("click", function(){			
			if( relVidStatus == "open" ){
				relVidStatus = "closed";
			}else{
				relVidStatus = "open";
			}			
			updateRightBlocks();			
			return false;
		
		});
		
		$("a#user-vid-toggle").bind("click", function(){
			
			if( userVidStatus == "open" ){
				userVidStatus = "closed";
			}else{
				userVidStatus = "open";
			}
			
			updateRightBlocks();
			
			return false;
		
		});
		var opVote = {
			beforeSubmit:	setNoticeVote,
			url:			'/async',
			success:		processResponse,
			dataType:		'json',
			clearForm:		true
		};

		var opComment = {
			beforeSubmit:	setNoticeComment,
			url:			'/async',
			success:		processResponse,
			dataType:		'json',
			clearForm:		true
		};
		$('#addComment').ajaxForm( opComment );
		$('#addVote').ajaxForm( opVote );

		if( $('.video-info #cut').length >= 1 ) { $('.video-info #full').hide(); }

		$('.user-info a.more').bind("click" , function(){

			if( $('.video-info #full:visible').length ) { 
				$('.video-info #cut').show();
				$('.video-info #full').hide();
				$(this).empty().append("информация о видео");
			} else {
				$('.video-info #cut').hide();
				$('.video-info #full').show();
				$(this).empty().append("скрыть подробности");
			}
			return false;

		});

		$(".replyForm").hide();

		$(".reply-link a.reply").live("click" , function(){

			quoteText = $(this).parent().prev(".comment-text").text();
			$(".replyForm textarea").val("");
			$(".replyForm textarea").val( quoteText );
			$(".replyForm input[name='reply_comment_id']").val( $(this).attr("name") );
			$(".replyForm textarea").removeAttr("disabled");

			$(".replyForm").show();
			
			goTo("#add-entity");

			return false;

		});
		
		$(".reply-link a.block").live("click" , function(){
			
			id = $(this).attr("name");
			dataObj = {
				'_SENDID': 'block_comment',
				'UID': uid,
				'_H': h,
				'commentId': id
			};
			
			$.post('/async', dataObj , function( response ){					
				
				if( response.status == "ok" ) {
					
					$('#comments').trigger("get_comments");
					
				}
				return false;				
				
			} , 'json');

			return false;

		});
		
		$(".reply-link a.deblock").live("click" , function(){
			
			id = $(this).attr("name");
			
			dataObj = {

				'_SENDID': 'deblock_comment',
				'UID': uid,
				'_H': h,
				'commentId': id

			};
			
			$.post('/async', dataObj , function( response ){					
				
				if( response.status == "ok" ) {					
					$('#comments').trigger("get_comments");					
				}
				return false;				
				
			} , 'json');

			return false;

		});

		$("#p-comments a:not(.nbg)").live("click" , function(){

			pageToGo    = $(this).text();
			containerId = $(this).parents("span").attr("id");

			updatePaginator( pageToGo , containerId , 0 );
			return false;

		});

		$('#comments').bind( "get_comments" , function(event , msg){
			
			dataObj = {
				'_SENDID'    : 'get_comments',
				'_CUR_TOKEN' : token,
				'page'       : 0
			};

			$("#asyncComments").empty().append('<div class="wait">Загружаются данные</div>');

			dataObj.page = curPage;

			$("#comments").empty();

			$.post("/async" , dataObj , function( res ){
				
				if( res.status != "ok" ){

					$("#asyncComments").empty().append('<div class="err">Во время загрузки данных возникли ошибки!</div>');

				}else{

					var curComments = "";

					$.each(res.comments , function(index , value){

						newComment = '';
						newComment += '<div class="username"><a class="b" href="#profile?u=' + value.user_id + '">' + value.user_name + '</a></div>';
						newComment += '<div class="datetime">' + value.posted_datetime + '</div>';
						newComment += '<div class="spacer"></div>';

						if( value.parent_comment_id != 0 ){

							newComment += '<blockquote>';
							newComment += '<div class="quote-indent"><div class="quote-content">';
							newComment += '<div class="caption"><a href="/guide/profile?u=' + value.parent_comment.user_id + '">' + value.parent_comment.name + '</a> ' + verb_wrote + ' ' + value.parent_comment.posted_datetime + '</div>';
							
							if( value.parent_comment.is_banned == 1 ){
								
								newComment += '<p class="banned">Комментарий заблокирован!</p>';
								
							}else{
								
								newComment += value.parent_comment.quote;
															
							}						
							
							newComment += '</div></div>';
							newComment += '</blockquote>';

						}

						if( value.is_banned == 1 ){
							
							newComment += '<p class="banned">Комментарий заблокирован!</p>';

						}else{
						
							newComment += '<div class="comment-text">' + value.text + '</div>';
						
						}
						
						newComment += '<div class="reply-link">';
						
						if( value.is_banned == 1 ){						
						}else{
							newComment += '<a class="reply" name="' + index + '" href="#' + index + '">' + verb_reply + '</a>';							
						}
						
						if( own_video ) {
						
							newComment += '&nbsp;&nbsp;&mdash;&nbsp;&nbsp;<strong>Действия:</strong> ';
					
							if( value.is_banned == 0 ){							
								newComment += '<a style="background:none;padding-left:0px;" name="' + index + '" class="block" href="#">заблокировать</a>';							
							}else{							
								newComment += '<a style="background:none;padding-left:0px;" name="' + index + '" class="deblock" href="#">разблокировать</a>';							
							}
						}
						
						newComment += '</div>';						
						
						curComments += '<div class="comment"><div class="comment-indent"><div class="comment-content">' + newComment + '</div></div></div>';

					});

					$("#comments").empty().append( curComments );
					$("#asyncComments").empty();
					window.location.href = '#recent-comments';

				}

				return false;

			} , "json");

			return false;

		});

		$("#add-va").hide();

		$("#choose-add-va-mode").bind("click" , function(){

			curChoice = $("input[name=addMethod]:checked").val();

			switch( curChoice ){

				case "list": window.location.href = "/guide/room?c=choose_va&v=" + $(this).prev("input[name=_CUR_TOKEN]").val(); break;
				case "load": window.location.href = "/guide/add_video?pv=" + $(this).prev("input[name=_CUR_TOKEN]").val(); break;

			}

			return false;

		});
		
		$(".add-comment-link a#go").bind("click" , function(){
			textComments = $(this).text();
			textVA = $("span#add-va-toggle span").text();
			$("span#add-va-toggle").empty().append("<a href=\"#\">" + textVA + "</a>");
			$("span#add-comment-toggle").empty().append("<span>" + textComments + "</span>");

			$("#add-va").hide();
			$("#add-comment").show();

		});
		
		$("span#add-comment-toggle a").live("click" , function(){

			textComments = $(this).text();
			textVA = $("span#add-va-toggle span").text();
			$("span#add-va-toggle").empty().append("<a href=\"#\">" + textVA + "</a>");
			$("span#add-comment-toggle").empty().append("<span>" + textComments + "</span>");

			$("#add-va").hide();
			$("#add-comment").show();

			return false;

		});
		
		$(".add-video-answer-link a#go").bind("click" , function(){
			textVA = $(this).text();
			textComments = $("span#add-comment-toggle span").text();
			$("span#add-comment-toggle").empty().append("<a href=\"#\">" + textComments + "</a>");
			$("span#add-va-toggle").empty().append("<span>" + textVA + "</span>");
			$("#add-va").show();
			$("#add-comment").hide();
		});
		
		$("span#add-va-toggle a").live("click" , function(){
			textVA = $(this).text();
			textComments = $("span#add-comment-toggle span").text();
			$("span#add-comment-toggle").empty().append("<a href=\"#\">" + textComments + "</a>");
			$("span#add-va-toggle").empty().append("<span>" + textVA + "</span>");
			$("#add-va").show();
			$("#add-comment").hide();
			return false;
		});
		
		$("#add-to-fav a").live("click", function(){
			dataObj = {
				'_SENDID': 'add_to_fav',
				'UID': uid,
				'_H': h,
				'token': token
			};			
			$.post('/async', dataObj , function( response ){					
				
				if( response.status == "ok" ) {					
					$("#add-to-fav").empty().append("<strong>Добавлено!</strong>");
					$("h1").prepend('<a href="/room?c=favs"><img src="/img/icon-fav.jpg" valign="top" width="16" height="20" alt="Показать избранное" title="Показать избранное" /></a>&nbsp;');
				}
				if( response.status == "failure" ) {				
						
				}
				return false;				
				
			} , 'json');

			return false;
		});

	});
