/* load more comments */
var comm_page = 0;
	
function loadComments( action ) {
	
	if( action == 'more' ) comm_page += 1;
	else comm_page -= 1;
	
	ShowLoading('');
	$.get(dle_root + "engine/modules/dlel/comments_load.php", { page: comm_page, skin: dle_skin }, function(data){
		HideLoading('');
		$('#load-comments').slideUp(200);
		setTimeout(function() {
			$('#load-comments').html(data).slideDown(200);
		},200);
		
		if( comm_page > 3 ) {
			$('.unvis').show();
			$('.vis').hide();
		} else {
			$('.unvis').hide();
			$('.vis').show();
		}
		
		if( comm_page > 0 ) $('.unvisback').show();
		else $('.unvisback').hide();
		
	});
	
	return false;	
}

/* error report */
function errorReport( sel_text ) {
	var b = {};
	
	b[dle_act_lang[3]] = function() { 
		$(this).dialog('close');						
	};
	
	b[dle_p_send] = function() { 
		if ( $('#bug_text').val().length < 1) {
			$('#bug_text').addClass('ui-state-error');
		} else if ( $('#bug_link').val().length < 1) {
			$('#bug_link').addClass('ui-state-error');
		} else if ( $('#bug_comment').val().length < 1) {
			$('#bug_comment').addClass('ui-state-error');
		} else {
			var response = $('#bug_text').val();
			var link = $('#bug_link').val();
			var comment = $('#bug_comment').val();
			$(this).dialog('close');
			$('#dlepopup').remove();
			$.post(dle_root + 'engine/ajax/report.php', { link: link, text: response, comment: comment, action: 'submit', skin: dle_skin },
				function(data){
					if (data == 'ok') { DLEalert(dle_p_send_ok, dle_info); } else { DLEalert(data, dle_info); }
				});
		}
	};
	
	$("#dlepopup").remove();

	$('body').append("<div id='dlepopup' title='Уведомление об ошибке' style='display:none'><label for='bug_link'><strong>Ссылка на страницу с ошибкой</strong>:</label><br /><input type='text' name='bug_link' id='bug_link' class='ui-widget-content ui-corner-all' style='width:80%; padding: .4em;' value='' disabled /><b onclick='$(\"#bug_link\").removeAttr(\"disabled\").focus();$(this).remove();' style='cursor: pointer; padding-left: 5px; text-decoration: underline;'>Изменить</b><br /><label for='bug_text'><strong>Текст, содержащий ошибку</strong>:</label><br /><textarea name='bug_text' id='bug_text' class='ui-widget-content ui-corner-all' style='width:97%;height:100px; padding: .4em;'></textarea><br /><label for='bug_comment'><strong>Комментарий к ошибке</strong>:</label><br /><textarea name='bug_comment' id='bug_comment' class='ui-widget-content ui-corner-all' style='width:97%;height:60px; padding: .4em;'></textarea></div>");
	$('#bug_text').val(sel_text);
	$('#bug_link').val(location.href);

	$('#dlepopup').dialog({
		autoOpen: true,
		show: 'fade',
		hide: 'fade',
		buttons: b,
		width: 450
	});
	
	return false;
}

/* post rate */
function ratePost( id, action ) {
	ShowLoading('');
	$.get(dle_root + "engine/modules/dlel/post_rate.php", { id: id, action: action, skin: dle_skin }, function(data){
		HideLoading('');
		RunAjaxJS('post-vote-layer', data);
		setTimeout(function() {
			$('#post-vote-layer').html(data);
		},100);
	});
}

function showRatePost( id ) {
	ShowLoading('');
	$.get(dle_root + "engine/modules/dlel/post_showrate.php", { id: id, skin: dle_skin }, function(data){
		HideLoading('');
		RunAjaxJS('show-rate-post', data);
		setTimeout(function() {
			$('#show-rate-post').html(data).slideDown();
		},100);
	});
}

/* comments rate */
function rateCom( id, action ) {
	ShowLoading('');
	$.get(dle_root + "engine/modules/dlel/comments_rate.php", { id: id, action: action, skin: dle_skin }, function(data){
		HideLoading('');
		RunAjaxJS('com-rate-' + id, data);
		setTimeout(function() {
			$('#com-rate-' + id).html(data);
		},100);
	});
}

/*
function showRateCom( id ) {
	ShowLoading('');
	$.get(dle_root + "engine/modules/dlel/comments_showrate.php", { id: id, skin: dle_skin }, function(data){
		HideLoading('');
		RunAjaxJS('rate-minussum-' + id, data);
		setTimeout(function() {
			$('#rate-minussum-' + id).html(data);
		},100);
	});
}
*/

/* users rate */
function rateUser( id, action ) {
	ShowLoading('');
	$.get(dle_root + "engine/modules/dlel/users_rate.php", { id: id, action: action, skin: dle_skin }, function(data){
		HideLoading('');
		RunAjaxJS('user-rate-' + id, data);
		setTimeout(function() {
			$('#user-rate-' + id).html(data);
			$('.user_do_rate').remove();
		},100);
	});
}

/* comments go to and back */
function comGoTo( obj, id_parent ) {
	var save_com_back = obj.rel;
	$("html"+( ! $.browser.opera ? ",body" : "")).animate({scrollTop: $('#comment-id-' + id_parent).position().top}, 400);
	$('#comment-id-' + id_parent).find('.c_answer_back').css({visibility: 'visible'}).attr('rel',save_com_back);
	return false;
}

function comGoBack( obj ) {
	obj.style.visibility = 'hidden';
	var get_com_back = obj.rel;
	$("html"+( ! $.browser.opera ? ",body" : "")).animate({scrollTop: $('#comment-id-' + get_com_back).position().top}, 400);
	return false;
}

$(document).ready(function(){
	
	var loc = window.location.hash;
	var loc_comment = loc.match(/\#comment-id-\d+/);
	
	if(loc_comment) $('html,body').stop(true,true).animate({scrollTop: $(loc).offset().top},1100);

	
	$('.com_rate').CreateBubblePopup({
		position: 'top',
		align: 'center',
		innerHtml: '<img src="/templates/rnns/images/bubble/loading-bubble.gif" /> Загрузка...',
		innerHtmlStyle: { color:'#FFFFFF', 'text-align':'center' },
		themeName: 'all-black',
		themePath: '/templates/rnns/images/bubble'
	});
	
	$('.com_rate').mouseover(function(){
		var ratevar = $(this);
		
		$.get(dle_root + "engine/modules/dlel/comments_showrate.php", { id: ratevar.attr('id'), skin: dle_skin }, function(data){
			ratevar.SetBubblePopupInnerHtml(data, true);
		});
	});
	
	$('#rotator').dlelRotator();
	$('#slide_day').slideDay();
	
	/* error report action */
	$(document).keypress(function(e){
		if((e.ctrlKey) && ((e.keyCode==10)||(e.keyCode==13))) {
			
			var sel_text = '';
			
			if(navigator.appName == 'Microsoft Internet Explorer') {
				if(document.selection.createRange()) {
					var range = document.selection.createRange();
					sel_text = range.text;
				}
			} else {
				if (window.getSelection()) {
					sel_text = window.getSelection();
				} else if(document.getSelection()) {
					sel_text = document.getSelection();
				}
			}
			
			errorReport(sel_text);
		}
	});
	
	/* login action */
	$('#cabinet a.login').click(function(){
		parent_id = $(this).parent();
		if( ! parent_id.hasClass('cabinet_active') ) {
			parent_id.addClass('cabinet_active');
			parent_id.find('.account_info').slideToggle(700);	
		} else {
			parent_id.find('.account_info').slideToggle(700,function(){
				parent_id.removeClass('cabinet_active');
			});
		}		
		
		return false;
	});
	
	/* top nav */
	var menuselected = $('#sections_navigation a.selected').attr('rel');
	if( menuselected ) {
		$('#sub_sections_navigation ul').css('display','none');
		$('#sub_sections_navigation ul.'+menuselected).css('display','block');
	}
	
	$('#sections_navigation a').hover(function(){
		var id = $(this).attr('rel');
		$('#sections_navigation a').removeClass('selected');
		$(this).addClass('selected');
		$('#sub_sections_navigation ul').css('display','none');
		if( id ) $('#sub_sections_navigation ul.'+id).css('display','block');
	});

	$('#general_navigation li:last-child,#sub_sections_navigation li:last-child,#external_navigation li:last-child,#footer_navigation li:last-child,.posts div.post_preview:last-child,.top_news div.post_preview:last-child,#content div.block:last-child,#extra_content div.block:last-child,.block_content div.post_preview:last-child').addClass('last');
	
	$('.parent_wrap').each(function(){
		$(this).find('.sep_line:first').css({border:0});
	});

	/* adding comment */
	var com_form = $('#reply_form_0').html(); // saving default comments form
	
	function reloadCaptcha() {
		if( $('#dle-captcha').html() !== null ) reload();
	}
	
	$('.comment_reply, .defcomlink').live('click',function(){
		var id = $(this).attr('rel');
		$('.reply_form').hide().empty();
		//$('.comment_frame').hide();
		$('#dle-ajax-comments-'+id).show();
		$('#reply_form_'+id).show().append(com_form);
		$('#replyto').val(id);
		reloadCaptcha();
		$("html"+( ! $.browser.opera ? ",body" : "")).animate({scrollTop: $('#comment-id-' + id).offset().top}, 400);
		return false;
	});
	
	$('.defcomlink').click(function(){
		$('#reply_form_0').show();
	});
	
	/* top link */
	$(window).scroll(function() {
		if($(this).scrollTop() > 300) $('#top_link').fadeIn();
		else $('#top_link').fadeOut('fast');
	});
	$('#top_link').click(function() {
		$("html:not(:animated)"+( ! $.browser.opera ? ",body:not(:animated)" : "")).animate({scrollTop:0},400);
	});
	
});
