$(document).ready(function(){

	//space fix for firefox

	//if ($.browser.mozilla) {
		var space = $('#content_before').position().top + $('#content_before').outerHeight() + 2 - $('#content').position().top;
		$('#content').css('top', space+'px');
	//}


	$('#topstripe__block_region select, .calc__city select').css('opacity', 0);

	$('#header__menu>li').mouseover(function(){
		var ul = $(this).children('ul');
		if (!ul.is(':visible')) {
			$('#header__menu>li ul').slideUp();
			ul.slideDown();
			ul.css('z-index', 1+parseInt(ul.css('z-index')));
		}
	});

	$('#header__menu>li ul').mouseleave(function(){
		$(this).slideUp();
	});

	$('.input_inlinetitle').each(function(){
		if ($(this).val()=='') {
			$(this).val($(this).attr('title'));
			$(this).css('color', '#555');
		} else {
			$(this).css('color', '#000');
		}
	});

	$('.input_inlinetitle').blur(function(){
		if ($(this).val()=='') {
			$(this).val($(this).attr('title'));
			$(this).css('color', '#555');
		} else {
			$(this).css('color', '#000');
		}
	});

	$('.input_inlinetitle').focus(function(){
		if ($(this).val()==$(this).attr('title')) {
			$(this).val('');
			$(this).css('color', '#000');
		}
	});

	var map = $('#map');

	if (map.length) {
		var html1 = '';
		var html2 = '<ul>';
		for (var i in branches) {
			var branch = branches[i];
			var left = branch.coords[0];
			var top = branch.coords[1];
			switch (branch.align) {
				case 'right':   left-=5;  top-=5;  break;
				case 'top':     left-=60; top-=20; break;
				case 'bottom':  left-=60; top-=5; break;
			}

			html1 += '<a href="'+branch.link+'" class="map__branch map__branch_'+branch.align+'" title="'+branch.title+'" id="map__branch_'+i+'" style="margin:'+top+'px 0 0 '+left+'px">'+branch.name+'</a>';

		}
		html2 += '</ul>'
		map.html(html1);

		window.select_branch = function(i) {
			var branch = branches[i];
			$('#branch-info__title').html('<a href="'+branch.link+'">'+branch.title+'</a>');
			//$('#branch-info__text').html(branch.text);
			$('.branch-info__text').hide();
			$('#branch-info__text_'+i).show();
			$('.map__branch, .branches__branch').css('color', '');
			$('#map__branch_'+i+', #branches__branch_'+i).css('color', '#801421');
		}

		$('.map__branch').mouseover(function(){
			select_branch(+$(this).attr('id').substr(12));

		});
		$('.branches__branch').mouseover(function(){
			select_branch(+$(this).attr('id').substr(17));
		});

		window.map_stop_moving = function() {
			clearInterval(map_timer);
		}

		window.map_move = function(direction) {
			//$('#map').css('left', '-100px');
			var oldl = parseInt($('#map').css('left'));
			direction = direction?-1:1;
			direction = direction*15;
			var newl = oldl+direction;
			if (newl>=0) {
				$('#mapcontainer__arrow_left').fadeOut(400);
				newl = 0;
				map_stop_moving();
			}

			if (newl<=$('#mapcontainer').width()-$('#map').width()) {
				$('#mapcontainer__arrow_right').fadeOut(400);
				newl = $('#mapcontainer').width()-$('#map').width();
				map_stop_moving();
			}
			$('#map').css('left', newl+'px');
		}

		window.map_start_moving = function(direction) {
			window.map_timer = setInterval(direction?"map_move(true)":"map_move(false)", 40);
		}




		$('#mapcontainer__arrow_right').hover(function(){
			$('#mapcontainer__arrow_left').fadeIn(400);
			map_start_moving(true)
		}, map_stop_moving);

		$('#mapcontainer__arrow_left').hover(function(){
			$('#mapcontainer__arrow_right').fadeIn(400);
			map_start_moving(false)
		}, map_stop_moving);

	}


	if ($('#calc').length) {
		$('#calc').submit(function(){
			$.get($(this).attr('action'), $(this).serialize(), function(data){
				$('#calc__result__2').text(data);
				if (!$('#calc__result:visible').length) {
					$('#calc__result').slideDown();
				}
			});
			return false;
		});
		$('.calc__city select').change(function(){
			var one = $(this);
			one.siblings('b').text($(this).children(':selected').text());
			if ($(this).parent('span').attr('id')=='calc__city_from') {
				var another = $('#calc__city_to select');
			} else {
				var another = $('#calc__city_from select');
			}

			if (one.val()==another.val()) {
				var sel = another.children(':selected');
				var sel2 = sel.prev('option');
				if (!sel2.length) {
					sel2 = sel.next('option');
				}
				sel2.attr('selected','selected');
				another.change();
			}
		});
		$('.calc__checkbox').click(function(){
			var inp = $(this).children('input');
			if (inp.val()=='1') {
				inp.val('0');
				$(this).fadeTo('fast', 0.4);
			} else {
				inp.val('1');
				$(this).fadeTo('fast', 1);
			}
		});
		$('.calc__checkbox').each(function(k){
			var inp = $(this).children('input');
			if (inp.val()=='1') {
				$(this).css('opacity', 1)
			} else {
				$(this).css('opacity', 0.4)
			}
		});
	}


});

