$(document).ready(function () {
	
	if ($('td.caption').length && $('td.caption').text() == '') $('td.caption').css('visibility', 'hidden');
	
	$('.trigger').delegate('.past', 'click', function() {
		var next = $(this).next();
		var prev = $(this).prev();
		var current = $(this);
		if (next.length) {

			current.removeClass('past').addClass('future');
			next.removeClass('inactive');
			
			if (next.next().length)
				next.addClass('past')
			else
				next.removeClass('past')
			;
			
			var following_index = current.attr('rel');
			if (prev.length) {
				prev.addClass('inactive');
				var old_index = prev.attr('rel');
			}
			
			$('#newsitem'+old_index).hide();
			$('#newsitem'+following_index).show();
			
		}
		return false;
		
	}).delegate('.future', 'click', function() {
		var next = $(this).next();
		var prev = $(this).prev();
		var current = $(this);
		if (next.length) {

			current.removeClass('future').addClass('past');
			prev.removeClass('inactive');
			
			if (prev.prev().length)
				prev.addClass('future')
			else
				prev.removeClass('future')
			;
			
			var following_index = current.attr('rel');
			if (next.length) {
				next.addClass('inactive');
				var old_index = next.attr('rel');
			}
			
			$('#newsitem'+old_index).hide();
			$('#newsitem'+following_index).show();
			
		}
		return false;
		
	}).delegate('a', 'click', function() {
	
		if ($(this).hasClass('.past') || $(this).hasClass('.future')) return false;
		
		var next = $(this).next();
		var prev = $(this).prev();
		var current = $(this);
			
		var following_index = current.attr('rel');
		if (next.length)
			var old_index = next.attr('rel');
		else 
			var old_index = prev.attr('rel');
		
		$('#newsitem'+old_index).hide();
		$('#newsitem'+following_index).show();
			
		return false;
	});
	
	/*
	$('.news-list-trigger').click(function () {
		var els = $(this).parent().children();
		var idx = $(this).attr('rel');
		var byord = null;

		for (var i = 0;i < els.length;i++) {
			if ($(els[i]).attr('rel') == idx) byord = i;
		}

		if ($(this).hasClass('past')) {
			$(this).removeClass('past').addClass('inactive').css('visibility', 'hidden');
			if (els[byord+1] != undefined) $(els[byord+1]).removeClass('inactive').addClass('past').css('visibility', 'visible');
			if (els[byord-1] != undefined) {
				$(els[byord-1]).removeClass('inactive').addClass('future').css('visibility', 'visible');
				var curidx = $(els[byord-1]).attr('rel');
				$('#newsitem'+curidx).hide();
			}
			if (els[byord-2] != undefined) {
				$(els[byord-2]).removeClass('future').addClass('inactive').css('visibility', 'hidden');
			}
		}
		if ($(this).hasClass('future')) {
			$(this).removeClass('future').addClass('inactive').css('visibility', 'hidden');
			if (els[byord-1] != undefined) $(els[byord-1]).removeClass('inactive').addClass('future').css('visibility', 'visible');
			if (els[byord+1] != undefined) {
				$(els[byord+1]).removeClass('inactive').addClass('past').css('visibility', 'visible');
				var curidx = $(els[byord+1]).attr('rel');
				$('#newsitem'+curidx).hide();
			}
			if (els[byord+2] != undefined) {
				$(els[byord+2]).removeClass('past').addClass('inactive').css('visibility', 'hidden');
			}
		}
		$('#newsitem'+idx).show();
		return false;
	});
	*/
	
	$('ul.tape li').each(function () {
		var link = $(this).find('a');

		$(link).click(function () {
			var img = $(this).attr('href');
			var c = $(this).find('span').text();
			$('.photoblock .photo').css('background-image', 'url(' + img + ')');
			$('td.caption p').text(c);
			$('ul.tape li a').each(function () {
				if ($(this).hasClass('active')) $(this).removeClass('active');
			});
			$(this).addClass('active');
			return false;
		});
	});

	$('.next-image').click(function () {
		var els = $('ul.tape li a');
		var active = 0;
		for (var i = 0; i < els.length;i++) {
			if ($(els[i]).hasClass('active')) {
				active = i;
				break;
			}
		}
		var next = active + 1;
		if (active == els.length - 1) next = 0;
		
		$(els[active]).removeClass('active');
		var img = $(els[next]).attr('href');
		var c = $(els[next]).find('span').text();

		$('.photoblock .photo').css('background-image', 'url(' + img + ')');
		$('td.caption p').text(c);
		if (c == '') 
			$('td.caption').css('visibility', 'hidden');
		else 
			$('td.caption').css('visibility', 'visible');
		$(els[next]).addClass('active');
		return false;
	});

	$('.prev-image').click(function () {
		var els = $('ul.tape li a');
		var active = 0;
		for (var i = 0; i < els.length;i++) {
			if ($(els[i]).hasClass('active')) {
				active = i;
				break;
			}
		}
		var next = active - 1;
		if (active == 0) next = els.length - 1;
		
		$(els[active]).removeClass('active');
		var img = $(els[next]).attr('href');
		var c = $(els[next]).find('span').text();

		$('.photoblock .photo').css('background-image', 'url(' + img + ')');
		$('td.caption p').text(c);
		if (c == '') 
			$('td.caption').css('visibility', 'hidden');
		else 
			$('td.caption').css('visibility', 'visible');
		$(els[next]).addClass('active');
		return false;
	});	

	$('ul.large li').each(function () {
		var link = $(this).find('a');

		$(link).click(function () {
			var img = $(this).attr('href');
			var c = $(this).find('span').text();
			$('.photoblock .photo').css('background-image', 'url(' + img + ')');
			$('td.caption p').text(c);
			$('ul.large li a').each(function () {
				if ($(this).hasClass('active')) $(this).removeClass('active');
			});
			$(this).addClass('active');
			return false;
		});
	});

	$('.i-next-image').click(function () {
		var els = $('ul.large li a');
		var active = 0;
		for (var i = 0; i < els.length;i++) {
			if ($(els[i]).hasClass('active')) {
				active = i;
				break;
			}
		}
		var next = active + 1;
		if (active == els.length - 1) next = 0;
		
		$(els[active]).removeClass('active');
		var img = $(els[next]).attr('href');
		var c = $(els[next]).find('span').text();

		$('.photoblock .photo').css('background-image', 'url(' + img + ')');
		$('td.caption p').text(c);
		$(els[next]).addClass('active');
		return false;
	});

	$('.i-prev-image').click(function () {
		var els = $('ul.large li a');
		var active = 0;
		for (var i = 0; i < els.length;i++) {
			if ($(els[i]).hasClass('active')) {
				active = i;
				break;
			}
		}
		var next = active - 1;
		if (active == 0) next = els.length - 1;
		
		$(els[active]).removeClass('active');
		var img = $(els[next]).attr('href');
		var c = $(els[next]).find('span').text();

		$('.photoblock .photo').css('background-image', 'url(' + img + ')');
		$('td.caption p').text(c);
		$(els[next]).addClass('active');
		return false;
	});

	$('#seeend-input').focusin(function() {
		if ($('#seeend-input').val() == 'поиск по сайту') $('#seeend-input').val('');
	});
	$('#seeend-input').focusout(function() {
		if ($.trim($('#seeend-input').val()) == '') $('#seeend-input').val('поиск по сайту');
	});
	$('#seeend-input').keyup(function(e) {
		if(e.which == 13) $('#seeend').submit();
		return false;
	});	
});
