$(function(){

	// add comment form show
	var $comment_form = $('#comment_form');
	$comment_form.hide();
	$('.addcomment').click(function(){
		$('.addcomment').show();
		if ($(this).attr('id', 'new_comment')) {
	   		$comment_form.find('input[name="comment_parent"]').val(0);
			$(this).hide().closest('div').after($comment_form);
	    } else {
			var $comment_id = $(this).closest('.comment').attr('id').replace('comment_', '');
	   		$comment_form.find('input[name="comment_parent"]').val($comment_id);
	   		$(this).hide().closest('div').after($comment_form);
	    }
	    $comment_form.find('textarea').val('');
	    $comment_form.show().find('textarea').focus();
	    reinitialiseJSP();
	    return false;
	});

	// cancel comment	
	$('form .reset').click(function(){
		$comment_form.hide();
		$('.addcomment').show();
		reinitialiseJSP();
	});

	// comment deleting button
	$('.comment_delete').hover(
		function(){
			innerimg = $(this).find('b').html();
			$(this).find('b').prepend('Удалить? ');
			$(this).find('b img').css('margin-left', '3px');
		},
		function(){
			$(this).find('b').html(innerimg);
			$(this).find('b img').css('margin-left', '-3px');
		}
	);
	
	// 'show_all' link underline type
	$('a.show_all').each(function(){
		if($(this).attr('href') == '#') {
			$(this).addClass('ajax');
		}
	});
	
	// show all comments button
	$('.show_more a').click(function(){
		var link = $('.show_more').html();
		var last = $('.comment').not('.answer').last();
		var last_id = last.attr('id').replace('comment_','');
		$.ajax({
			url: '',
			data: 'id=' + last_id,
			beforeSend: function(xhr) {
				$('.show_more').html('<img src="images/loading-w.gif" alt="loading...">');
			},
			success: function(data){
				//$('.wall').append(data);
				//$('.show_more').html(link);
			}
		});
		return false;
	});
	
	var allmedals_link = $('#medals a.show_all').text();
	$('#medals a.show_all').click(function(){
		var allmedals = $('#medals .hidden');
		if (allmedals.is(':hidden')) {
			allmedals.stop().slideDown();
			$(this).text('Скрыть пока не полученные');	
		} else {
			allmedals.stop().slideUp();
			$(this).text(allmedals_link);
		}
		return false;
	});
	
	if ($('ul.medals').length) {
		var m_total = $('ul.medals').width();
		var m_width = $('ul.medals li').width();
		var m_count = $('ul.medals li').length;
		if ((m_width * m_count) < m_total) m_total = m_width * m_count;
		m_width = Math.floor(m_total / m_count);
		$('ul.medals li').each(function(i){
			$(this).css({
				'width' : m_width,
				'z-index': i + 100
			});
		});
		$('ul.medals li').hover(
			function(){
				$(this).addClass('big');
			},
			function(){
				$(this).removeClass('big');
			}
		);
	}
	
	loadJSP();
	$('.jScrollPane .comment').first().css('padding-top', 0);
	
	// rounded corners
	var browser = $.browser;
	$('.rounded').each(function(){
		if ((browser.opera && browser.version < 10.5) || (browser.msie && browser.version < 10)) {
			$(this).append('<q class="tl"></q><q class="tx"></q><q class="tr"></q><q class="ly"></q><q class="ry"></q><q class="bl"></q><q class="bx"></q><q class="br"></q><q class="c"></q>');
			$(this).addClass('old_browser');
		}
	});
	
	$('img.r').each(function(){
		var src = $(this).attr('src');
		if (browser.opera || (browser.msie && browser.version < 9)) {
			$(this).replaceWith('<div class="r" style="background-image:url(' + src + ');" />');
		}
	});

	if ($('.jslider').length) {
		$('.jslider').jcarousel({
			wrap: 'circular',
			scroll: 1
		});
	}
	
	$('.popup .bg').live('click', function(){
		$(this).parent().addClass('hide');
	});
	
/*
	$('#choose_file').live('click', function(){
		$('#file_selector').click();
	});
	$('#file_selector').live('change', function(){
		$('#selected_file').text($(this).val());
	});
*/
	
	$('#login_form').submit(function(){
		$('#noreg_login').removeClass('focused');
		if ($('#login').val() == '' && $('#password').val() == '') {
			$('#noreg_login').addClass('focused');
			return false;
		}
	});
	
});

function showPopup(type, id) {
	if ($('.popup').length) {
		if (type != '') {
			$('.' + type).show();
		} else {
			$('.popup').show();
		}
		//alert(document.height);
		if ($('.popup').hasClass('popup-big')) {
			$('body, html').scrollTop(0);
			$('.wrapper').addClass('with-popup').css('height', document.height);
		}
/*
		if ($('.popup .in').height() > $(window).height()) {
			$('body').scrollTop(0);
			$('.popup').css('position', 'absolute');
			$('.wrapper').addClass('with-popup').css('height', document.height);
		}
*/
		loadJSP();
	}
}

function hidePopup() {
	if ($('.popup').length) {
		$('.popup').hide();
		$('.wrapper').removeClass('with-popup').css('height', 'auto');
	}
}

function loadJSP() {
	if ($('.jScrollPane').length) {
		$('.jScrollPane').jScrollPane({
			showArrows: true
		});
		if ($('.jScrollPane').parent().hasClass('comments')) {
			var cnt_h = $('.jspPane').height();
			$('.jScrollPane').css('height', cnt_h);
			reinitialiseJSP();
		}
	}
}
function reinitialiseJSP() {
	if ($('.jScrollPane').length) {
		var w = $('.jspPane').width();
		$('.jScrollPane').jScrollPane({
			showArrows: true,
			contentWidth: w
		});
	}
}
