$(document).ready(
function()
{
	var $genLenta = $('.fotoLentaBlock2');
	$genLenta.initLenta = function()
	{
		var $lenta = this;
		var $items = $lenta.find('.block');
		var $scroll = $('#sovetScroll');
		
		var $scrollThumb = $lenta.find('.scroll');
		var $scrollBar = $lenta.find('.scrollBlock');
		var scrollFrameWidth = $scrollBar.width();
		
		var itemCount = $items.length;

		var $imgItems = $lenta.find('.block img').last();
		var margin = 260;
		var itemMargin = 0;
		var imgWidth = $imgItems.outerWidth(true);

		var fullWidth = itemCount * ( imgWidth + itemMargin) + margin;
		var frameWidth = $lenta.innerWidth();
		var extraWidth = fullWidth - frameWidth;

		var $nameBlock = $genLenta.find('.nameBlock');
		
		var largeWidth = 147;
		var largeHeight = 210;

 		var smallWidth = 89;
 		var smallHeight = 122; 		
		
		var smallMarginTop = 0;
		var largeMarginTop = -43;
		
		var maxOpacity = 1.0;
		var minOpacity = 0.55;
		var tOpacityStep = 0.10;
 		
 		var nCount = 2;

 		var shiftW = (largeWidth - smallWidth) / nCount;
 		var shiftH = (largeHeight - smallHeight) / nCount;
 		var shiftM = (largeMarginTop - smallMarginTop) / nCount;
 		
		var $innerLenta = $lenta.find('.lenta');
		window.currentValue = -1;
		
		var lolaIndex = 0;
		$lenta.runLolaRun = function()
		{
			if( lolaIndex >= fullWidth ) lolaIndex = 0;
			$lenta.updatePhoto(lolaIndex+= 40);
			window.setTimeout($lenta.runLolaRun, 20);
		}
		
		$lenta.convertValue = function(_v)
		{
			return fullWidth * _v / itemCount;
		}
		
		$lenta.convertPos = function(_p)
		{
			return itemCount * _p / fullWidth;
		}
		
		$items.click( function()
		{
			var index = $(this).prevAll('.block').length;
			var val = $lenta.convertValue(index);
			$scroll.slider('value', val);
			$lenta.updatePhoto(val, index);
		});
		
	    $lenta.timeout = null;
	    
	    $lenta.updatePhoto = function(newShiftValue, forceNewValue)		 				
	    {
			//$lenta.updatePhotoInv(newShiftValue, true);
			$lenta.updatePhotoInv(newShiftValue, forceNewValue);
		}
		 
		$lenta.updatePhotoInv = function(newShiftValue, forceNewValue)
		{
			var newValue = Math.floor($lenta.convertPos(newShiftValue));
			if(forceNewValue) newValue = forceNewValue;

			var shift = extraWidth * newShiftValue / (fullWidth - 1);
			$innerLenta.stop(true, true).animate({'left': - shift}, 'fast');			
			
			if(window.currentValue == newValue){  return; }
			window.currentValue = newValue;
			
			
			var $candidate = $($items.get(newValue));
//			if(!$candidate.hasClass('cur'))
//			{
				$items.removeClass('cur');			
				$items.removeClass('opacity3');		
				
				$oImage = $items.find('img');
				//$oImage.stop().fadeTo('fast', 0.55);				
				
				$cImage = $candidate.find('img');
				
				
				
				var i = 0;
				var $n = $candidate;
				var $p = $candidate;

				for(i = 1; i < nCount; ++i)
				{
					$n = $n.next('.block');
					$p = $p.prev('.block');
					var $pni = $n.add($p).find('img');
					
					$pni.stop().fadeTo('slow', 1.0 - tOpacityStep * i);					
					$pni.stop().animate( { 'width': largeWidth - shiftW * i, 'height': largeHeight - shiftH * i, 'margin-top': largeMarginTop - shiftM * i, 'opacity': maxOpacity - tOpacityStep * i }, 500 );
					// special effects@@@@!!!
//					$pni.fadeTo('slow', 0.6);
					$oImage = $oImage.not($pni);

				}
				
				$cImage.stop().animate( { 'width': largeWidth, 'height': largeHeight, 'margin-top': largeMarginTop, 'opacity': maxOpacity  }, 500 );
				
				$oImage = $oImage.not($cImage);
				$oImage.stop(false, true).animate( { 'width': smallWidth, 'height': smallHeight, 'margin-top': smallMarginTop, 'opacity': minOpacity  }, 500 );
				
				var $name = $candidate.find('.name');
				$nameBlock.html($name.html());
				$candidate.addClass('cur');
				
//				$candidate.next('.block').addClass('opacity3');
//				$candidate.prev('.block').addClass('opacity3');				
//				$('#info1').text(frameWidth);
//				$('#info2').text(extraWidth);


				if(newValue > itemCount / 2)
				{
					$name.addClass('right');
				}
				
//			}
		}
		
		$lenta.updatePhoto(0);
		$scroll.slider(
			{
				min: 0,
				max: fullWidth - 1,
				value: 0,
				animate: true,
				change: 
				function(event, ui)
				{
//					$('#info1').text(selected);
					$lenta.updatePhoto(ui.value);
				}
				,
				slide: 
				function(event, ui)
				{
//					$('#info2').text(ui.value);
					$lenta.updatePhoto(ui.value);
				}
			}
		);		
	}

    
    var $projectsInner = $('.projectsBlockMain');
    $projectsInner.initLenta = function()
    {
        if(this.length == 0) return;
        var $this = this;
        var $projectsLenta = $this.find('.lenta');
        var $items = $this.find('.block');
        var projectsLentaItemWidth = $items.outerWidth(true);
        var itemCount = $items.length;
        var maxLeft = itemCount - Math.floor($projectsLenta.parent().innerWidth() / projectsLentaItemWidth);
        var $projectsNext = $this.find('.next');
        var $projectsPrev = $this.find('.prev');        

        $this.moveProjects = function(dir)
        {
            var curOffset = $items.filter('.cur').prevAll().length;
            
            curOffset += dir ? 1 : -1;
            
            if( curOffset < 0 )
                curOffset = 0;  

            if( curOffset >= maxLeft )
                curOffset = maxLeft - 1;
                
            curOffset > 0 ? $projectsPrev.show() : $projectsPrev.hide();
            curOffset < maxLeft - 1 ? $projectsNext.show() : $projectsNext.hide();                
            
            $items.removeClass('cur');
            $items.eq(curOffset).addClass('cur');
            
            var offset = - projectsLentaItemWidth * curOffset;
            
            
            var parentWidth = $projectsLenta.parent().innerWidth();
            var width = itemCount * projectsLentaItemWidth;
            var left = $projectsLenta.position().left;

//            if ( ( width - parentWidth + left < projectsLentaItemWidth) && dir) return;

            $projectsLenta.stop().animate({'left': offset});                          
        }

        $this.moveProjects(false);
        
        $projectsNext.click(
        function()
        {
            $this.moveProjects(true);

        }
        )

        $projectsPrev.click(
        function()
        {
            $this.moveProjects(false);

        }
        )

    }

    $projectsInner.initLenta();
    
        
	var $projects = $('.projectsBlock');
	

	$projects.initLenta = function()
	{
        if(this.length == 0) return;
		var $projectsList = $('.projectsList');
		var $projectsLentaBlock = $('.projectsBlock .lentaBlock');
		var $projectsLenta = $('.projectsBlock .lenta');
		var projectsLentaItemWidth = $('.projectsBlock .block').width();
		var itemCount = $projectsLenta.find('.block').length;
		var minHoverItemCount = 3;
		var minItemCount = 5;
		if(itemCount < minItemCount)
		{
			var left = (minItemCount - itemCount) * projectsLentaItemWidth;
			$projectsLenta.css('left', left + 'px');

		}
		
		if( itemCount >= minHoverItemCount )
			$projects.hover(
				function()
				{
					$projectsList.addClass('hover');
				},

				function()
				{
					$projectsList.removeClass('hover');
				}
			)


		$projects.moveProjects = function(dir)
		{
			var parentWidth = $projectsLenta.parent().innerWidth();
			var width = itemCount * projectsLentaItemWidth;
			var left = $projectsLenta.position().left;

			if ( ( width - parentWidth + left < projectsLentaItemWidth) && dir) return;
			if( (-left < projectsLentaItemWidth) && !dir) return;
			
			var offset = (dir ? '-=' : '+=') + projectsLentaItemWidth;
			$projectsLenta.stop().animate({'left': offset});              			
		}

		var $projectsNext = $('.projectsList .next');
		var $projectsPrev = $('.projectsList .prev');
		
		$projectsNext.click(
		function()
		{
			$projects.moveProjects(true);

		}
		);

		$projectsPrev.click(
		function()
		{
			$projects.moveProjects(false);

		}
		);
$projectsLenta.find('.block').click(
function()
{
var $cblock = $(this);
window.location = $(this).find('a').attr('href');
});


	}
	
	var $fotoLenta = $('.fotoLentaBlock'); 
	
	$fotoLenta.initLenta = function()
	{
		var $fotoLenta = this;
		var $scrollLeft = $fotoLenta.find('.prev');
		var $scrollRight = $fotoLenta.find('.next');
		var $scrollBlock = $fotoLenta.find('.scrollBlock');
		var $scrollBar = $fotoLenta.find('.scroll');
		var $scrollLenta = $fotoLenta.find('.lenta');    
		var $lentaBlock = $fotoLenta.find('.lentaBlock');    
		var $bigFotoImg = $fotoLenta.find('.fotoBig img');
		var $blocks = $fotoLenta.find('.block');
		var blockWidth = $blocks.outerWidth(true);
		var innerWidth = blockWidth * $blocks.length  + 20;
		
		var scrollFrameWidth = $lentaBlock.innerWidth();
		var scrollWidth = $scrollBlock.innerWidth();

		var minLeft = 0;
		var maxRight = innerWidth - scrollFrameWidth;		
		
		
		var thumbWidth = scrollFrameWidth * scrollWidth / innerWidth;
		var maxThumbRight = scrollWidth - thumbWidth;
		
		var minStep = 25;
		
		
		if($bigFotoImg.length)
		{
			$blocks.click(
			function()
			{
				$bigFotoImg.attr('src', $(this).attr('href'));
				return false;
			});
		}
		
				
		if(innerWidth < scrollFrameWidth)
		{
			$scrollBlock.hide();
			return;			
		}
		

		$scrollBar.width(thumbWidth);
		$scrollBar.css('left', minLeft + 'px');
		
		$scrollBar.draggable({ 'axis' : 'x', 'revert': false, 'containment' : 'parent',
				drag: function(event, ui) 
				{ 
					var left = $scrollBar.position().left;
					var offsetLeft = left * innerWidth / scrollFrameWidth;
					$fotoLenta.updatePosition(offsetLeft, false, true);
		 		},
				stop: function(event, ui) 
				{ 
					var left = $scrollBar.position().left;
					var offsetLeft = left * innerWidth / scrollFrameWidth;
					$fotoLenta.updatePosition(offsetLeft, true, true);
		 		}
		 				 		
		 		});

		$fotoLenta.btnScroll = function(dir)
		{
			var offset = (dir ? blockWidth : -blockWidth) - $scrollLenta.position().left;
			$fotoLenta.updatePosition(offset, true, true, true);
		}
		
		$fotoLenta.updatePosition = function(offset, updateThumb, updateView, animate)
		{
			if( offset < 0 ) offset = 0;
			if( offset > maxRight ) offset = maxRight;
			var offsetThumb = Math.floor(offset * scrollFrameWidth / innerWidth);
			if( offsetThumb < 0 ) offsetThumb = 0;
			if( offsetThumb > maxThumbRight ) offsetThumb = maxThumbRight;
			
			var scrOffset = -offset;
			var barOffset = offsetThumb;
			
			if( updateThumb ) $scrollBar.stop().animate({'left': barOffset}, animate ? 500 : 0);
			if( updateView ) $scrollLenta.stop().animate({'left': scrOffset}, animate ? 500 : 0);
		}
		
		$fotoLenta.pageScroll = function(dir)
		{
			//alert('bbb');
		}
		
		
		$scrollLeft.click(
			function()
			{
				$fotoLenta.btnScroll(false);
			}
		);

		$scrollRight.click(
			function()
			{
				$fotoLenta.btnScroll(true);
			}
		);		
	}

	var $sovetScroll = $('#sovetScroll');
	
    var $mapBlock = $('.mapBlock');
    $mapBlock.init = function()
    {
        var $this = this;
        var $map = $this.find('.map');
        var $map_items = $map.find('li');
        var $map_infos = $this.find('dt');
        var $window = $('html, body');
        $map_infos.parent().removeClass('cur');
        $map_items.click( function()
        {
            $map_infos.parent().removeClass('cur');
            var $item = $(this);
            var rel = $item.attr('rel');
            var $newSel = $map_infos.filter('*[id=' + rel + ']');
            $newSel.parent().addClass('cur');
            $window.animate({ scrollTop: $newSel.position().top}, 'slow');
        })
    }
    
    var $archive = $('.archive');
    $archive.init = function()
	{
		var $this = this;
		$this.find('dd ul li').not('.cur').find('ul').slideUp();
		$this.find('li span').click(
			function()
			{
				var $item = $(this).parent();
				
				if($item.hasClass('cur'))return;
				
				var $sel = $this.find('dd ul li.cur');
				
				$sel.find('ul').slideUp('slow', function()
				{
					$sel.removeClass('cur');
				});
				
				$item.find('ul').slideDown('slow',
					function()
					{
						$item.addClass('cur');
					});
			}
			);
			
	}    
    	
    
    
    var $video = $('.video');
    
    var $videoLayer = $('.videoBlockPopup');
    $videoLayer.init = function()
	{
		var $this = this;
		var $close = $this.find('.close');
		var $flash = $this.find('.flash');
		var $name = $this.find('.name');
		var $videoLink = $('.video');
		var $darkness = $('#darkness');
		$videoLink.click( function()
		{
			var $link = $(this);
			var $src = $link.find('.flashVideo');
			
			$name.html($src.attr('title'));
			$flash.html($src.html());
			$videoLayer.css('margin-top', -($videoLayer.height()/2));
			$darkness.fadeIn(function(){
				$videoLayer.css('margin-top', -($videoLayer.height()/2));
			});
			$this.fadeIn();
		});
		
		$close.click( function()
		{
			$this.fadeOut();
			$darkness.fadeOut();
		});
		
	}
	
	var $photoSub = $('.photoBlock');
	
	$photoSub.init = function()
	{
		var $this = this;
		var $subPhotos = $this.find('.preview .block');
		var $bigPhoto = $this.find('.fbig img');
		
		$subPhotos.click(
			function()
			{
				var $item = $(this);
				$bigPhoto.attr('src', $item.attr('href'));
			});
	}

	$photoSub.init();    
    $archive.init();	
	$videoLayer.init();		
    $mapBlock.init();		
	$projects.initLenta();
	$fotoLenta.initLenta();
	// unselectable
	$('.noselect').live('selectstart dragstart', function(evt){ evt.preventDefault();  return false; });	

	$genLenta.initLenta();
	//$genLenta.runLolaRun();
	
	$('._autohide').click(
	function()
	{
		$(this).hide();
		$(this).next('input, textarea').focus();
	});

	//
	$('._autohide').next('textarea, input').focusin(
	function()
	{
		$(this).prev('._autohide').hide();
	});

	$('._autohide').next('textarea, input').focusout(
	function()
	{
		if($(this).val().length == 0)$(this).prev('._autohide').show();
	});
	
	$('._autohide').next('textarea, input').each( function()
	{
		var $this = $(this);
        if($this.val().length > 0)
        {
        	$(this).prev('._autohide').hide();
        }
    });

}
)
