$(document).scroll(function(){

	/* Соседние статьи при прокрутке держим всегда на виду */
	if( $( '.Siblings' ).length == 1 ){

		var eSiblings = $( '.Siblings' );
		var iTop = eSiblings.offset().top + 20;

		if( !eSiblings.attr( 'rel' ) ){
			eSiblings.attr( 'rel', eSiblings.offset().top - 20 );
		} else {
			iTop = eSiblings.attr( 'rel' );
		}

		if( $(document).scrollTop() >= iTop ){
			eSiblings.addClass( 'Fixed' );
		} else {
			eSiblings.removeClass( 'Fixed' );
		}

	}

})


$(document).ready(function(){



	$( 'input[type=text]' ).makeFocus();
	$( '.Tool' ).makeHover();


	/* Подсвечичвание кавычек вокруг ссылки */
	$( 'a' ).each(function(){
		$( this ).parent().html(function(){
			return $( this ).html().replace( /«(<a\s.+?)(>)(.+?)(<\/a>)»/gi, '$1 class="NoU"$2«<u>$3</u>»$4' );
		});
	});


	/* При просмотре статьи убираем навигацию */
	if( $( '.Siblings' ).length == 1 ){
		$( '#NavigationTree' ).remove();
	}



	/* При просмотре статьи убираем навигацию */
	if( $( '#Countdown' ).length == 1 ){
		var iCountdown = Number( $( '#Countdown' ).html() );
		if( $( '#TopHeader' ).attr( 'rel' ) == 'ru' ){
			var sData = $.NumDecline( iCountdown, 'день', 'дня', 'дней' );
		} else {
			var sData = iCountdown > 1 ? 'days' : 'day';
		}
		$( '#Countdown' ).append( ' ' + sData );
	}



	/* Фоторамка на главной и в истории */
	$( '#Frame' ).children( 'ul' ).teaser({
		speed: 2000
		, timeout: 6000
		, scroller: true
		, type: 'sequence'
	});



	/* Метки в новостях */
	$( '.Labels' ).children().each(function(){
		var eParent = $( this ).parent().parent().children( 'p:first' );
		$( this ).parent().remove();
		$( this ).prependTo( eParent );
//		$( this ).after( '<span>&rarr;</span>' );
	});



	/* Календарь в новостях и дневнике фестиваля */
	$( '#Calendar' ).find( '.Year' )
		.makeHover()
		.click(function(){
			if( !$( this ).parent().is( '.Selected' ) ){
				$( this ).parent().parent().parent()
					.find( '.Year' ).not( this ).next().slideUp().parent().removeClass( 'Selected' );

				$( this ).parent()
					.addClass( 'Selected' )
					.find( 'ul' ).slideDown();
			}
		});



	/* Форма обратной связи */
	$( 'input.Text, textarea' ).each(function(){
		var t = $( this );
		if( t.prev().is( 'label' ) ){
			t.focus( __focus ).blur( __blur ).bind( 'fade', __fade );
			t.closest( 'form' ).find( 'input.Text, textarea' ).each(function(){
				if( $( '#Feedback' ).find( 'h3' ).length > 0 && $( this ).val() == '' ){
					$( this ).trigger( 'focus' );
					return false;
				}
			})
			if( t.val() ) t.trigger( 'fade' );
		}

		function __fade(){
			$( this ).prev().animate({
				opacity: 0
			}, 0 );
		}

		function __focus(){
			$( this ).prev().animate({
				marginTop: -10
			}, 'fast' );
		}

		function __blur(){
			if( $( this ).val() == '' ){
				// Если значение пустое, то опускаем label и делаем видимым
				$( this ).prev().animate({
					opacity: 1
					, marginTop: 24
				}, 'fast', function(){
					if( $.browser.msie ) $( this ).css( 'filter', '' );
				});
			} else {
				// Если значение не пустое, то скрываем label
				$( this ).prev().animate({
					opacity: 0
				}, 'fast' );
			}
		}

	});

	$( '#Feedback' ).find( '.Close' ).click(function(){
		var e = $( this ).parent();
		$( this ).remove();
		e.animate({
			opacity: 0
			, height: 0
			, marginTop: 0
			, marginBottom: 0
			, paddingTop: 0
			, paddingBottom: 0
		}, 'medium', function(){
			$( this ).remove();
		})
		return false;
	})



	/*
		<h3><span class="Tool">Свернуть/развернуть</span></h3>
		<div></div>
	*/
	$( 'h3 .Tool' ).each(function(){
		$( this ).parent().next().data( '__height', $( this ).parent().next().height() );
	});

	$( 'h3 .Tool' ).not( '.Selected' ).each(function(){
		$( this ).parent().next().css({
			opacity: 0
			, height: 0
		});
	});

	$( 'h3' ).delegate( '.Tool', 'click', function(){
		if( !$( this ).is( '.Selected' ) ){

			$( 'h3' ).find( '.Selected' )
				.removeClass( 'Selected' )
				.parent().next().animate({
					height: 0
					, opacity: 0
				}, {
					duration: 'normal'
					, queue: false
				});

			$( this )
				.addClass( 'Selected' )
				.parent().next()
					.animate({
						opacity: 1
					}, {
						duration: 'fast'
						, queue: false
						, complete: function(){
							if( $.browser.msie ) $( this ).css( 'filter', '' );
						}
					})
					.animate({
						height: $( this ).parent().next().data( '__height' )
					}, {
						duration: 'normal'
						, queue: false
					});

		}
	});



	/* Фотографии */
	$( '.Photos a' ).lightbox();
	$.Lightbox.construct({
		'speed': 200,
		'show_linkback': false,
		'keys': {
			close: 'q',
			prev: 'z',
			next: 'x'
		},
		'opacity': 0.7,
		text: {
			image: 'Фотография',
			of: 'из',
			close: 'Закрыть',
			closeInfo: 'Клик вне картинки завершит просмотр.'
		},
		files: {
			js: {
				colorBlend: '/f/1/jQuery.colorBlend.pack.js'
			},
			css: {
				lightbox:	'/f/1/jquery.lightbox.css'
			},
			images: {
				prev:		'/f/1/i/lightbox/prev.gif',
				next:		'/f/1/i/lightbox/next.gif',
				blank:		'/f/1/i/lightbox/blank.gif',
				loading:	'/f/1/i/lightbox/loading.gif'
			}
		}
	});


});
