$(function(){
	PushSecond();
	RunDiagrams();
	DiagramHovers();
	$('.facebookarea').mouseenter(function() {
		$(this).find('.facebook').addClass('active');
		$(this).find('.facebook').clearQueue();
		var width = 45 + $(this).find('.fb-like').width();
		$(this).find('.facebook').animate({'width': ''+width+'px'}, 600); 
	});
	$('.facebookarea').mouseleave(function() {
		$(this).find('.facebook').removeClass('active');
		$(this).find('.facebook').delay(60).animate({'width':'38px'}, 600); 
	});
	$('.done .carousel').jcarousel({
		scroll: 4,
		wrap: 'circular',
		buttonNextHTML: '<div class="next"></div>',
		buttonPrevHTML: '<div class="prev"></div>'
	});
	$('.block .holder-small .carousel').jcarousel({
		scroll: 3,
		wrap: 'circular',
		buttonNextHTML: '<div class="next"></div>',
		buttonPrevHTML: '<div class="prev"></div>'
	});
	$('.block .caption .switcher a').click( function() {
		var aIndex = $(this).attr('index');
		$('.block .caption .switcher a').each( function(){
			$(this).removeClass('active');
		});
		$(this).addClass('active');
		$(this).parents('.block').find('.switch-holder .switchable').each( function(){
			$(this).removeClass('active');
		});
		$(this).parents('.block').find('.switch-holder .switchable').eq(aIndex).addClass('active');

		if ($(this).is('.switcher.bookmarkable a')) {
			document.location.hash = $(this).attr('href');
		}
		
		// перезапускаем функции для главной при смене блока
		PushSecond();
		RunDiagrams();
		DiagramHovers();
		
		return false;
	});
	$('.block .caption .switcher.bookmarkable a').each( function() {
		var aIndex = $(this).attr('index');
		if ($(this).attr('href') == '#') $(this).attr('href', '');
		if ($(this).attr('href') == document.location.hash) {
			$(this).addClass('active');
			$(this).parents('.block').find('.switch-holder .switchable').eq(aIndex).addClass('active');
		} else {
			$(this).removeClass('active');
			$(this).parents('.block').find('.switch-holder .switchable').eq(aIndex).removeClass('active');
		}
	});
	// закрыть окно
	$('.overlay .block .caption .close').each( function(){
		$(this).click( function(){
			$(this).parents('.overlay').fadeOut();
			return false;
		});
	});

    //закрыть окно по нажатию Esc
    $(document).keydown(function(e) {
        if (e.which == '27') {
            e.preventDefault();
            $('.overlay:visible').fadeOut();
        }
    });

	// увеличчить рисунок по клику на рисунок
	$('.image.zoom a, .image.zoom div').click( function(){
		$('.increase-image .block').center();
		$(window).resize( function(){
			$('.increase-image .block').center();
		});
		OverBack($('.increase-image .background'));
		$('.increase-image').fadeIn();
		return false;
	});
	// увеличчить рисунок по клику на ссылку
	$('.block a.increase').click( function(){
		$('.image.zoom a').click();
		return false;
	});
	// задать вопрос
	$('.footer .help a').click( function(){
		$('.send-question .block').center();
		$(window).resize( function(){
			$('.send-question .block').center();
		});
		OverBack($('.send-question .background'));
		$('.send-question').fadeIn();
		return false;
	});

    $('#showHelpLink, #showHelpGraph').click( function(e) {
        e.preventDefault();
        $('.footer .help a').click();
    })

/*
    $('div.legend .help').click( function() {
        $('.footer .help a').click;
    })
*/

	// отправить форму вопроса и закрыть
	$('.send-question a.button').click( function(){
		$(this).parents('form').submit();
		$(this).parents('.overlay').fadeOut();
		return false;
	});
	// карта проезда
	$('.footer .scheme a').click( function(){
		$('.map .block').center();
		$(window).resize( function(){
			$('.map .block').center();
		});
		OverBack($('.map .background'));
		$('.map').fadeIn();
		return false;
	});
	// выпадающее меню

	var showSubMenu = function(li) {
		li.clearQueue();
		li.delay(200);
		li.queue(function() {
			li.addClass('active');
			var submenu = li.closest('.menu').find('.sub-menu[menuindex='+li.attr('menuindex')+']');
			if (!submenu.is('.current')) {
				li.closest('.menu').find('.sub-menu.current').clearQueue();
				li.closest('.menu').find('.sub-menu.current').fadeOut(300);
				submenu.fadeIn(300);
			}
			li.dequeue();
		});
	}

	var hideSubMenu = function(li) {
		li.clearQueue();
		li.delay(200);
		li.queue(function() {
			li.removeClass('active');
			var submenu = li.closest('.menu').find('.sub-menu[menuindex='+li.attr('menuindex')+']');
			if ( (!submenu.is('.current')) || submenu.length == 0 ) {
				li.closest('.menu').find('.sub-menu.current').css('opacity', 1).fadeIn(300);
				submenu.fadeOut(0);
			}
			li.dequeue();
		});
	}

	$('div.menu ul li').hover( function(){
		showSubMenu($(this));
	}, function() {
		hideSubMenu($(this));
	});

	$('div.menu .sub-menu').hover( function() {
		showSubMenu($(this).closest('.menu').find('ul li[menuindex='+$(this).attr('menuindex')+']'));
	}, function() {
		hideSubMenu($(this).closest('.menu').find('ul li[menuindex='+$(this).attr('menuindex')+']'));
	});
	
	//толкаем по высоте вторую колонку, чтобы не наезжала на широкий блок
	function PushSecond(){
		$('.col-two').css('margin-top', function(){
		return $('.wide').height();
		});
	}
	//растягиваем блок оверлея
	function OverBack(el){
		el.height($(document).height());
		el.width($(window).width());
		$(window).resize( function(){
			el.height($(document).height());
			el.width($(window).width());
		});
		return el;
	}
	// центрируем окно
	jQuery.fn.center = function () {
		this.css("position","absolute");
		this.css("top", ( $(window).height() - this.outerHeight() ) / 2 +$(window).scrollTop() + "px");
		this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
		return this;
	}
	
	/* DIAGRAMS */
	function RunDiagrams(){
		var current_image = $('.switchable.active .diagrams img').attr('src');
		$('.switchable.active .legend div').mouseover( function(){
			DiagramColors($(this),current_image);
			return false;
		});
		$('.switchable.active .legend div').mouseout( function(){
			$(this).parents('.diagrams').find('img').attr('src',current_image);
			return false;
		});
		$('.switchable.active map area').mouseover( function(){
			DiagramColors($(this),current_image);
			return false;			
		});
		$('.switchable.active map area').mouseout( function(){
			$(this).parents('.diagrams').find('img').attr('src',current_image);
			return false;			
		});
	}
	
	function DiagramColors(el,current_image){
		current_image = current_image.split(".");
		current_image = current_image[0];
		var new_image = current_image+el.attr('class')+'.png';
		el.parents('.diagrams').find('img').attr('src',new_image);
		return false;
	}
	
	function DiagramHovers() {
		$('.switchable.active map area').mouseover( function(){
			el = $(this);
			$('.switchable.active .legend .'+el.attr('class')+' a').addClass('over');
		});
		$('.switchable.active map area').mouseout( function(){
			el = $(this);
			$('.switchable.active .legend .'+el.attr('class')+' a').removeClass('over');
		});
	}
});

