var newSelected = false;
$(function() {

	var self=this,

		$body = $('body'),
		
		rotator,
		
		$container = $('#container'),
		
		bodyPageIndex = $('body#page_index').length ? true : false,
		bodyPageWorks = $('body#page_works').length ? true : false,
		bodySingleWork = $('body#page_works2').length ? true : false,
 
		$lenta = $('#lenta'),
		$curB = $('#lenta > #block_active').length 
			? $('#lenta > #block_active') 
			: (bodyPageWorks 
				? $('#lenta > .block:last').prev()
				: $('#lenta > .block:last')
			),
		$lastB =  $('.block:last', $lenta), 
		$firstB =  $('.block:first', $lenta),
		$lentaLinks = $('#buttons_lenta');
		$linkPrev = $('.prev', $lentaLinks),
		$linkNext = $('.next', $lentaLinks),
		$blocks = $('.block', $lenta),
		$blocksNav = $('.nav_block', $blocks),
		bX = 887, 
		bW = 413,
		bH = 276,
		curP = 0,
		doNotLoad = true,
		$lentaContainer = $('#lenta'),

		$menuLi = $('.menu1 li'),
		$blockWorkListPic = $('#page_works_pic .work_list'),
		$linkBack = $('#page_works_pic .back'),
		$blockWorkListPic = $('#page_works_pic .work_list'),
		$linkBack = $('#page_works_pic .back'),
			
		autoInitHovers = true,
		
		$slidesLi = $('.slides > li'),

		$filling = $('.filling');

		
	var $navigator = $('.site_navigate'),
		$navTrigger = $('.show_nav', $navigator),
		$circle = $('.circle', $navigator),
		$c2 = $('.c2', $circle),
		$openBtn = $('.show_nav', $circle),
		$playBtn = $('.play', $circle),
		$sendBtn = $('.send', $circle),
		$navMenu = $('.menu_nav', $navigator),
		$navMenuUl = $('ul', $navMenu),
		$navMenuLi = $('li', $navMenuUl),
		$navMenuTxt = $('.txt2', $navMenu),
		$formSend = $('.form_nav', $navigator),
		$formInpName = $('#fname', $formSend),
		$formInpEmail = $('#femail', $formSend),
		$formTexarea = $('#fmessage', $formSend),
		$linkClose = $('.close', $formSend),
		$txt1 = $('.txt1', $navMenu),
		$txt2 = $('.txt2', $navMenu),
		$linkAll = $('.all', $navMenu),
		$linkPersonal = $('.personal', $navMenu),
		nav = false,
		liAct = 0;

	var $partners = $('.partners'),
		$plogo = $('div.logocont div', $partners),
		$partner = $('> a', $partners),
		$pOverLink = $('> a.other', $partners),
		logoNum = 0,
		logoAni = false;


	var preloadCount = 7;
	var qtyBlock = 4;

/*
 * Functions
 */

	function PrepareBlocks( _$blocks, _$block){
		var isLeft;

		var index = _$blocks.index( _$block );

		
		var lessIndex = Math.max( 0, index-qtyBlock );
		var moreIndex = Math.min( _$blocks.length-1 , index+qtyBlock );


		_$blocks.filter(':lt('+lessIndex+')').hide();
		_$blocks.filter(':gt('+moreIndex+')').hide();
		_$blocks.filter(':gt('+(lessIndex-1)+')').filter(':lt('+(qtyBlock*2+1)+')').show();

		isLeft = ( _$block.prevAll().filter(':visible').length+1)*bW - bX;

		return isLeft;
	}
/*
*/
    this.getEndingWord = function (n, word1, word2, word3)
    {
        var n1 = n.toString().substr(n.toString().length-1,1),
			n2 = n.toString().substr(n.toString().length,1);
        
        if (parseInt(n) > 4 && parseInt(n)<21) {
            return word3;
        }
        if(n2 == 1) {
            return word3;
        } else if(n1 == 1) {
            return word1;
        } else if((n1 > 1) && (n1 < 5)) {
            return word2;
        } else {
            return word3;
        }
    }

/*
 * Shadow Call Function
 */

	$.showShadow = function(){
		if (!$body.hasClass('shadow') && ($('._noshadow').length == 0)) {
			$body.addClass('shadow');
		}
	}

	$.hideShadow = function(){
		if ($body.hasClass('shadow')) {
			$body.removeClass('shadow');
		}
	}

/**
 * Live Functions
 */

	this.unbindAll = function(){
		$('.block .w img', $lenta).unbind('mouseover').unbind('mousemove');
		$('.block .nav_block > *', $lenta).unbind('mouseover').unbind('mouseout');
		$('.block .nav_block').hide();
	}

	this.bindCurB = function(){
		var $nb = $curB.find('.nav_block'),
			$nbimg = $curB.find('.w img'),
			$lnb = $curB.find('.nav_block > *');
		
		$nbimg.mouseover(function(){
			$nb.show();
		}).mousemove(function(){
			$nb.show();
		});
		
		$lnb.mouseover(function(){
			var $bg = $(this).find('.bg');
			$bg.css({backgroundColor:'#2191C2'});
		});
		
		$lnb.mouseout(function(e){
			var $this = $(this),
				$bg = $this.find('.bg')
				offset = $nb.offset();
	
			if(!$this.hasClass('wone') || bodySingleWork){
				$bg.css({backgroundColor:'#000000'});
			}
	
			if(!bodySingleWork && ((e.clientX < offset.left) || (e.clientX > offset.left + bW - 1) || (e.clientY < offset.top) || (e.clientY > offset.top + bH))){
				$nb.hide();
			}
		});
		
	}

	$('.video', $lenta).live('mousedown', function(){
		var $c = $(this).parent().parent(),
			$fl = $('.flashko', $c),
			$b = $('.nav_block', $c),
			$a = $('.descr a', $c),
			pageURL = bodySingleWork ? null : $a.attr('href'),
			videoURL = $('.videoURL', $c).val();
		$b.hide();
		$fl.show().flash({
			swf: '/static/swf/video.swf?'+parseInt(new Date().getTime().toString().substring(0, 10)),
			height:277,
			width:412,
			id: "videoSWF",
			name: "videoSWF",
			flashvars: {
				videoURL: videoURL,
				pageURL: pageURL
			},
			params: {
				menu: "false",
				allowscriptaccess: "always",
				quality: "high",
				wmode: "transparent",
				bgcolor: "#edf1f8"
			}
		});
	});


	$('.slide', $lenta).live('mousedown', function(){
		var that = this,
			$c = $(this).parent().parent(),
			$fl = $('.flashko', $c),
			$b = $('.nav_block', $c),
			$a = $('.descr a', $c),
			pageURL = bodySingleWork ? null : $a.attr('href'),
			json = $('.slidesDATA', $c).val(),
			slidesDATA = eval( "(" + json + ")"),
			hz = '',
			json = eval("(" + eval(hz) + ")"),
			html = '',
			imgs = '',
			dots = '',
			slide = 0,
			count = 0;
		
		$b.hide();
		
		if (slidesDATA) {
			$.each(slidesDATA, function(i, val){
				imgs += '<img src="' + val + '" alt="" />';
				dots += '<li><span>' + i + '</span></li>';
			});
		}
		
		html = 
			'<div id="slide_line"><div>'+imgs+'</div></div>'+
			'<div id="slide_play"><div class="bg"></div><ul>'+dots+'</ul>'+
				'<a href="'+pageURL+'"><span>подробнее о работе</span></a>'+
			'</div>';

		$fl.show().html(html);

		var $slideContainer = $('#slide_line'),
			$slideLine = $('#slide_line div'),
			$slidePlay = $('#slide_play'),
			$dots = $slidePlay.find('ul li'),
			$a = $slidePlay.find('a'),
			count = $dots.length;
		
		if(bodySingleWork){
			$a.hide();
		}else{
			$a.click(function(){
				window.location = $a.attr('href');
			});	
		}
		
		
		$slideLine.css({width:412*count});
		
		$dots.click(function(){
			var $this = $(this);
				indx = $dots.index(this);
			
			that.stopRotate();
			that.gotoSlide(indx);
		});

		$slideContainer.click(function(){
			that.stopRotate();
		});

		$slideContainer.mouseover(function(){
			$slidePlay.show();
		}).mouseout(function(){
			$slidePlay.hide();
		});
		
		$slidePlay.mouseover(function(){
			$slidePlay.show();
		}).mouseout(function(){
			$slidePlay.hide();
		});

		this.gotoSlide = function(i){
			$slideLine.animate({left:-i*412}, 300, function(){
				$dots.removeClass('act');
				$dots.eq(i).addClass('act');
			});
		}

		this.startRotate = function(){
			$dots.eq(0).addClass('act');
			slide = 0;
			that.stopRotate();
			$.showShadow();
			if ($c.length) {
				clearInterval(self.rotator);
				self.rotator = setInterval(that.rotate, 2000);
			}
		}
		
		this.stopRotate = function(){
			$.hideShadow();
			clearInterval(self.rotator);
		}
		
		this.rotate = function(){
			if(slide>=count){
				$.hideShadow();
				slide = 0;
			}
			that.gotoSlide(slide);
			slide++;
		}
		
		this.startRotate();	

	});

	$('.work', $lenta).live('click', function(){
		self.goToWork();
	});

	$('.descr > a', $lenta).live('click', function(){
		self.goToWork();
	});

	$('.block', $lenta).live('click', function(){
		if(!$('#header *:animated').length){
			self.inactivateButton($(this));
			self.activateBlock($(this));
			if(nav){
				self.closeNavigator();
			}
		}
		return false;
	});


/**
 * Binds
 */


	$(window).resize(function(){
		self.updateWidth();
	});

	$slidesLi.click( function() {
		var url = parseInt($(this).find('div:first').text(), 10);
		if(url){
			document.location.href =  '/guide/?current_page=' + (url - 1);
		}
	});

	$navTrigger.click(function(){
		if(nav){
			self.closeNavigator();
		}else{
			self.openNavigator();
		}
		return false;
	});

	$menuLi.hover(function(){
		$(this).find('.b').addClass('over');
	}, function(){
		$(this).find('.b').removeClass('over');
	});

	$navMenuLi.click(function(){
		var $this = $(this);
		if($this.hasClass('act')){
			$this.removeClass('act');
			liAct = liAct - 1;
		}else{
			$this.addClass('act');
			liAct = liAct + 1;
		}

		if(liAct){
			if (liAct ==1 ){
				$txt1.hide();
				$txt2.show();
				$playBtn.show();
			}
		} else {
			$txt1.show();
			$txt2.hide();
			$playBtn.hide();
		}
		
		self.changeTxt();
			
		return false;	
	});

	$linkAll.click(function(){
		for (i=1; i<10; i++){
			$('#m'+i).addClass('act');
		}		
		liAct = 9;
		$txt1.hide();
		$txt2.show();
		$playBtn.show();
		
		self.changeTxt();
	});

	$linkPersonal.click(function() {
		$navMenu.hide();
		$formSend.show();
		$sendBtn.show();
	});

	$linkClose.click(function() {
		$navMenu.show();
		$formSend.hide();
		$sendBtn.hide();
	});

	$sendBtn.click(function() {
		var form = $formSend.find('form');

		$sendBtn.hide();
		$.post("/feedback/", {name: $formInpName.val(),emal: $formInpEmail.val(),message:$formTexarea.val()});
		form.replaceWith('<span>Спасибо! Ваше сообщение отправлено</span>');
		$txt1.find('span').hide();
	});

	$formInpName.focus(function() {
		var $this = $(this);
		$this.parent().find('label').hide();
	}).blur(function() {
		var $this = $(this);
		if ($this.val() == '') {
			$this.parent().find('label').show();
		}
	});

	$formInpEmail.focus(function() {
		var $this = $(this);
		$this.parent().find('label').hide();
	}).blur(function() {
		var $this = $(this);
		if ($this.val() == '') {
			$this.parent().find('label').show();
		}
	});

	$formTexarea.focus(function() {
		var $this = $(this);
		$this.parent().find('label').hide();
	}).blur(function() {
		var $this = $(this);
		if ($this.val() == '') {
			$this.parent().find('label').show();
		}
	});

/**
 * Everything
 */

	this.destroyFlash = function () {
		try {
			// Remove the SWFUpload DOM nodes
			var movieElement = null;
			try {
				movieElement = document["videoSWF"];
			} catch (ex) {
			}
			
			if (movieElement != undefined && movieElement.parentNode != undefined && typeof(movieElement.parentNode.removeChild) === "function") {
				var container = movieElement.parentNode;
				if (container != undefined) {
					container.removeChild(movieElement);
				}
			}
			
			return true;
		} catch (ex1) {
			return false;
		}
	};

	this.changeTxt = function(){
		var $actSlides = $navMenuUl.find('li.act'),
			slidesCount = 2;
		
		$actSlides.find('span').each(function(){
			slidesCount = slidesCount + parseInt($(this).html());
		});
		
		var minCount = parseInt(slidesCount / 3);
		
		$navMenuTxt.find('span').eq(0).html(minCount + '&nbsp;' + self.getEndingWord(minCount, 'минуту', 'минуты', 'минут')).end().eq(1).html(slidesCount + '&nbsp;' + self.getEndingWord(slidesCount, 'слайд', 'слайда', 'слайдов'));
	}

	
	this.openNavigator = function(){
		self.unbindAll();
		$.hideShadow();
		$lentaLinks.hide();
		$txt1.hide();
		self.changeTxt();
		self.closeBlocks(function(){
			$circle.stop().animate({top:'162px', left:'3px'}, 500, function(){
				$navMenu.stop().fadeIn(300, function(){
					$txt1.show();
				});
				$navTrigger.addClass('act');
				$c2.show();
				$openBtn.hide();
				nav = true;
			});
		});
	}

	this.closeNavigator = function(){
		self.unbindAll();
		self.bindCurB();
		$.hideShadow();
		$txt1.hide();
		$navMenu.stop().fadeOut(300);
		$playBtn.hide();
		$sendBtn.hide();
		$c2.hide();
		$txt2.hide();
		$navMenuLi.removeClass('act');
		liAct = 0;
		$formSend.hide();
		$navTrigger.removeClass('act');

		$circle.stop().animate({top :'-7px', left :'55px'}, 500, function(){
			self.openBlock($curB);
			$lentaLinks.show();
			$openBtn.show();
			nav = false;
		});
	}

	this.openBlock = function($block, fast, callback){
		// block preloader
		var $cb = $block;
		var $cn = $block;
		var i = 0;
		for(i = 0; i < preloadCount; ++i)
		{
			var $ci = $cb.add($cn).find('img');
			$ci.each(
				function(index, element)
				{
					var $element = $(element);
					if($element.attr('rel'))
					{
						$element.attr('src', $element.attr('rel'));
						$element.removeAttr('rel');
					}
				}
			);
			
			$cb = $cb.prev('.block');
			$cn = $cn.next('.block');
		}
		
					
		
		
	
		$.hideShadow();
		self.unbindAll();
		var $w = $block.find('.w'),
			$n = $block.find('.nav_block'),
			$i = $w.find('img'),
			$d = $block.find('.descr');

		if(!fast){
			self.closeBlocks();
			if(bodyPageIndex){
				$w.stop().animate({top:'-118px', height:'342px'}, 100, function(){
					$(this).css({height:'auto'});
					self.bindCurB();
				});
			}else{
				if(bodySingleWork){		
					$d.hide();
				}
				$w.stop().animate({top:'-30px', height:'342px'}, 100, function(){
					$(this).css({height:'auto'});
					self.bindCurB();
				});	
			}
			$i.stop().animate({marginTop:0}, 100, callback ? callback : null);
		}else{
			if(bodyPageIndex){
				$w.css({top:'-118px', height:'auto'});
			}else{
				if(bodySingleWork){	
					$d.hide();
				}
				$w.css({top:'-30px', height:'auto'});	
			}
			$i.css({marginTop:0});
		}
	}

	this.closeBlocks = function(callback){
		self.unbindAll();
		$.hideShadow();
		var $w = $lenta.find('.w'),
			$i = $w.find('img'),
			$f = $('.flashko');

		$blocksNav.stop().hide();
		self.destroyFlash();
		clearInterval(self.rotator);
		$f.hide().html(''); 
		$i.css( {marginTop:'-118px'} );
		$w.css( {top:'0', height:'42px'} );

		if(callback)
			window.setTimeout(callback, 100);
	}	
	

	this.activateBlock = function($block){

		self.unbindAll();

		var iB = $blocks.index($block);
		var iC = $blocks.index($curB);
		var $w = $lenta.find('.w');
		var $i = $w.find('img');

		
		if (iB != iC) {
			$.hideShadow();

			if (iB == iC)
				return self.clickOnOpened();

			$blocks.removeClass('current');
			if (bodySingleWork)
				$filling.animate({opacity: 0}, 500);

				
			self.closeBlocks( function(){

				curP = PrepareBlocks( $blocks, $block );
				$lenta.css({left: -curP - (iC-iB)*bW});

				$lenta.animate({
					left: -curP
				}, 300, function(){
						$curB = $block;
						$curB.addClass('current');
						self.openBlock($block, false, function(){
							self.bindCurB();
							if (bodySingleWork)
								setTimeout(function(){self.goToWork();}, 100);
						});
					}
				);
			});

			if (!bodySingleWork && ($curB.offset().left > $block.offset().left) )
				self.loadStripe($block);

		}
	}

	this.inactivateButton = function($block){
		var iB = $blocks.index($block),
			iC = $blocks.index($curB),
			iF = $blocks.index($firstB),
			iL = $blocks.index($lastB);
		
		if (iB != iC) {
			$.hideShadow();
			if (iB == iL) {
				$linkNext.addClass('noclick_n').unbind().click(function(){
					return false;
				});
			}
			else {
				$linkNext.removeClass('noclick_n').unbind();
				self.bindNext();
			}
			
			if (iB == iF) {
				$linkPrev.addClass('noclick_p').unbind().click(function(){
					return false;
				});
			}
			else {
				$linkPrev.removeClass('noclick_p').unbind();
				self.bindPrev();
			}
		}
	}

	this.goToWork = function(){
		var url = $curB.find('.descr > a:first').attr('href');
		if(url){
			document.location.href = url;
		}
	}

	this.clickOnOpened = function(){
		//hz
	}

	this.hdrPrev = function() {
			//if( self.ignoreMovement ) return;
			if(!$('#lenta_cont *:animated').length && !$('#header *:animated').length){
				self.inactivateButton($curB.prev());
				self.activateBlock($curB.prev());
			}
			return false;
	}

	this.hdrNext = function() {
			if(!$('#lenta_cont *:animated').length && !$('#header *:animated').length){
				self.inactivateButton($curB.next());
				self.activateBlock($curB.next());
			}
			return false;
	}

	this.bindPrev = function(){
		$linkPrev.click( this.hdrPrev );
	}

	this.bindNext = function(){	
		$linkNext.click( this.hdrNext );
	}


	this.loadStripe = function($block) {
		if(doNotLoad){
			return;
		}

		var nodeId = $firstB.find('.descr >a:first').attr('rel');

		self.ignoreMovement = true;
		
		$.ajax(
			{
				url: "/stripe/", 
				data: {'node_id':nodeId}, 
				dataType: 'text',
				async: true,
				success: function(data){
					if(!data){
						self.ignoreMovement = false;
						doNotLoad = true;
						return;
					}
					$lenta.prepend(data);
					$firstB = $('.block:first', $lenta);
					$blocks = $('.block', $lenta);

					var width = $blocks.length * bW;
					if (width > 5000) {
						$lenta.css({width:width});
					}
					curP = ($block.prevAll().length + 1 ) * bW - bX;
					$lentaContainer.css({width:$('>*', $lentaContainer).length*420});
					self.ignoreMovement = false;
					$lenta.stop(true, true).animate({left: -curP}, 500);
				}
			//$lenta.stop().animate({left:-curP});
		});
	}

	this.updateWidth = function(){
		var numBlock,
			curMarg,
			curW;

		if ($(document.body).width() > 1254){

			numBlock = parseInt($(document.body).width() / bW);
			curW = numBlock * bW + 1;
			curMarg = parseInt((curW - 990) / 2);

			$linkBack.css({right:-curMarg+1});
			$blockWorkListPic.css({marginLeft:-curMarg, marginRight:-curMarg, width: curW});

		} else { 
			$blockWorkListPic.css({marginLeft:15, width: 827});
			$linkBack.css({right:149});
		}		
		
	}


	this.init = function(){
		var $w = $curB.find('.w'),
			$d = $curB.find('.descr'),
			iC = $blocks.index($curB),
			iF = $blocks.index($firstB),
			iL = $blocks.index($lastB);

		curP = ($curB.prevAll().length + 1 ) * bW - bX;
		$lenta.css({left:-curP});
		$curB.addClass('current');

		if(bodyPageIndex){
			$w.css({top:'-118px', height:'342px'});
		}else if(bodyPageWorks || bodySingleWork){
			$w.css({top: '-30px', height:'342px'});
			if (bodySingleWork) {
				$d.hide();
			}
		}else{
			$w.css({top: '-30px', height: '72px'});
			autoInitHovers = false;
		}

		self.bindPrev();
		self.bindNext();

		if(iC == iL){
			$linkNext.addClass('noclick_n').unbind().click(function(){return false;});
		}
		if(iC == iF){
			$linkPrev.addClass('noclick_p').unbind().click(function(){return false;});
		}

		self.openBlock($curB, true);
		self.updateWidth();
		self.unbindAll();
		self.bindCurB();
		if (bodySingleWork) {
			$curB.find('.nav_block').show();
		}
		$lentaContainer.css({width:$('>*', $lentaContainer).length*420});
	};


	$(document).keydown( function(ev){

		// If pushed ctrl
		if( ev.ctrlKey ){
			// Prev
			if( ev.keyCode == 37 )
				document.hdrPrev();
			// Next
			else if(  ev.keyCode == 39 )
				document.hdrNext();
		}
	});
	

	/* Para-a-am! */
	this.init();
			





/*
 * Projects Page
 */

	var $pageProj = $('#page_project'),
		$picsProj = $('.pics_project', $pageProj),
		$blockProj = $('.projects_block .block', $pageProj),
		$picProj1 = $('.pic_project1', $picsProj),
		$picProj2 = $('.pic_project2', $picsProj),
		$picProj3 = $('.pic_project3', $picsProj),
		$picProj4 = $('.pic_project4', $picsProj),
		$picProj5 = $('.pic_project5', $picsProj),
		$container = $('#container');
	
	$blockProj.hover(function(){
		var i = $blockProj.index(this),
			$pic = $('div:eq('+i+')', $picsProj),
			$bg = $(this).find('.bg');

		$bg.css({opacity:1});
		$pic.css({height:$container.height()}).css({visibility:'visible'});
		$pic.show();
	}, function(){
		var i = $blockProj.index(this),
			$pic = $('div:eq('+i+')', $picsProj),
			$bg = $(this).find('.bg');

		if (i == 0){
			$bg.css({opacity: 0.23});
		}
		if (i == 1){
			$bg.css({opacity: 0.5});
		}
		if (i == 2){
			$bg.css({opacity: 0.34});
		}
		if (i == 3){
			$bg.css({opacity: 0.5});
		}
		if (i == 4){
			$bg.css({opacity: 0.23});
		}
		$pic.css({height:$container.height()}).css({visibility:'hidden'});
	});

/*
 * Partners
 */

	if (bodyPageIndex) {
		$partner.hover(function(){
			var i = $partner.index(this);
			$plogo.css({top:-i*50});
			logoAni = true;
		}, function(){
			$plogo.css({top:50});
			logoAni = false;
		}).click(function(){
			return false;
		});
		
		$plogo.css({top:50});
	}


	var $prezentBlock = $('.prezent_block');

	if($prezentBlock.length){
		var prezentNum = 0;
		var countNum = $prezentBlock.find('> *').length;
	
		this.prezentSnow = function(){
			$prezentBlock.css({left: -prezentNum * 186});
			prezentNum++;
			if (prezentNum > countNum-1) prezentNum = 0;
		}
		this.prezentSnow();
		setInterval(self.prezentSnow, 3000);
	}

	$pOverLink.unbind();

/*
 * Site Navigate
 */

	var $siteNavigate = $('#site_navigate_top'),
		$navSlides = $('ul.slides > li .line', $siteNavigate),
		slideNum = 0,
		secondR = false;
		

	$(window).load(function(){
		setTimeout(function(){self.animateSlide();}, 500);
	});

	$navSlides.hover(function(){
		$(this).stop().animate({top:0}, 300);
	}, function(){
		$(this).animate({top:14}, 300);
	}).click(function(){
		var i = parseInt($('.num', $(this).parent()).html())-1;
		self.guidePost(i);
	});

	$('.next', $siteNavigate).click(function(){
		var p = $(this).attr('id');
		self.guidePost(p);
		return false;
	});

	$('.prev', $siteNavigate).click(function(){
		var p = $(this).attr('id');
		self.guidePost(p);
		return false;
	});

	$('div.play').click(function() {
		self.guidePlay();
		return false;
	});

	$('#maincont > a.close', $siteNavigate).click(function(){
		self.guideClose();
		return false;
	});


	this.animateSlide = function(){
		$navSlides.eq(slideNum).animate({top:0}, 300, function(){
			$(this).animate({top:14}, 300);
		});
		if(slideNum < ($navSlides.length-1) && !secondR){
			slideNum++;
			setTimeout(function(){self.animateSlide();}, 50);
		}else if(slideNum > 0){
			secondR = true;
			slideNum--;
			setTimeout(function(){self.animateSlide();}, 50);
		}
	};

	this.guidePost = function(id) {
		var url = '/guide/?current_page='+id;
		document.location.href = url;
	};

	this.guideClose = function() {
		var $cont = $('#mainwrap .block2');
		
		$siteNavigate.animate({top: '-185px'}, 250);
		if($cont.length && $cont.hasClass('shift')){
			$cont.animate({marginTop:-68}, 250, function(){
				$cont.removeClass('shift');
			});
		}
		
		$.ajax({
			url: '/guide/',
			data: {'stop': 'true'},
			type: 'post'
		});
	};

	this.guidePlay = function(){
		var guideItems = $('.menu_nav > ul > .act');
		var ids = Array();
		$(guideItems).each(function(){
			ids.push ( $(this).attr('id') ); 
		});
		var url = '/guide/?' + $.param({to_view: ids.join (',')}); // например - /guide/?to_view=m9
		document.location.href = url;
	};

/*
 * Site Line Resizer
 */

	var $lineSite = $('.line_site div');

	$(window).load(function(){
		$lineSite.css({height: $container.height()});
	});

	$(window).resize(function(){
		$lineSite.css({height: $container.height()});
	});

/*
 * Footer icons hover (RSS & Twitter)
 */

	$('#footer .icons .icon').hover(
		function() {
			$(this).addClass('icon_over');
		},
		function() {
			$(this).removeClass('icon_over');
		}
	);


	(function(){
		var $block = $('div.def.block.current', $lenta);
		var isLeft = PrepareBlocks($blocks, $block);

		$lenta.css({left: -isLeft});
	})();


	if( typeof SyntaxHighlighter != 'undefined' ){
		SyntaxHighlighter.autoloader( 'js /static/js/syntaxhighlig/languages/shBrushJScript.js'
											, 'php /static/js/syntaxhighlig/languages/shBrushPhp.js' 
		);

		SyntaxHighlighter.all();
	}


	(function(){
		var $d404 = $('div.d404');
		var $imgs;


		if( $d404.length != 1 )
			return;
		
		
		$imgs = $d404.find('img');

		$imgs.each(function(){
			var $img = $(this);
			var isSrc = /\/([a-zA-Z0-9]*).([a-zA-Z0-9]*)$/.exec( this.getAttribute('src') );

			var $a = $('<a href="' + this.getAttribute('src').replace( /[^\/]*$/, '/'+ isSrc[1] + '_big.png' ) + '"></a>');

			$img.after($a);
			$a.append($img);
		});

		$d404.find('a').lightBox();

	})();

		$('.vacancyBlock .form .iForm .column .field ul.posit').click(function(){
				$(this).toggleClass('show');
				if($(this).css('height') == '23px'){
						$(this).css('height','auto');
						$('.show').hover(
								function(){},
								function(){
										$('.vacancyBlock .form .iForm .column .field ul.posit').removeClass('show');
										$('.vacancyBlock .form .iForm .column .field ul.posit').css('height','23px');
										newSelected=false;
								}
						);
				}else{
						$(this).css('height','23px');
				}
		});
	
	$('.vacancyBlock .form .iForm .column .field ul.posit li').click(function(){
		$('.posit').removeClass('highlighted');
		$('.vacancyBlock .form .iForm .column .field ul.posit li').removeClass('cur');
		$(this).addClass('cur');
		$('input[name=resume_position]').attr('value', $(this).text());
		$('#toMail').attr('value',$(this).attr('title'));
		if(newSelected && !$('.forRemove').hasClass('cur')){
				$('.forRemove').remove();
				newSelected=false;
		}else{
				newSelected=true;
		}
	});
	
	$('#qrcode').hover(
		function(){
			$('.hidden').fadeIn('slow');
		},
		function(){
			$('.hidden').fadeOut('slow');
		}
	);
	

});

