var m_delay = 3000, m_speed = 500, m_t, open = false;
var left_offset;

function m_open() {
	m_cancel();
	if (!open) {
		// alert(calc());
		if (calc()) {
			// alert($(window).height() + '--' + left_offset);
			$(".tel").animate( {
				right : '+=' + left_offset
			}, m_speed, 'swing', function() {

			});
			open = true;
		}
	}
}
function m_timer() {
	m_t = window.setTimeout(m_close, m_delay);
}

function m_close() {
	if (open) {
		$(".tel").animate( {
			right : '-=' + left_offset
		}, m_speed, 'swing', function() {
			open = false;
		});
	}
}

function m_cancel() {
	if (m_t) {
		window.clearTimeout(m_t);
		m_t = null;
	}
}

function calc() {
	var max = 1600
	// var staticOff = $('.tel').css('right').replace('-', '').replace('px', '');
	var staticOff = $('.tel').css('right').replace('-', '').replace('px', '');
	var left;
	var a = $(window).width();
	if (a <= max) {
		return left_offset = staticOff;
	}
	if (a > max) {
		// Вычисляем на сколькa о блок выдвигать.
		var b = (a - max) / 2;
		if (b >= staticOff) {
			return false; // Не выдвигаем
		} else {
			return left_offset = staticOff - b;
		}
	}
	return $(window).width();
}

$(function() {
	$(".tel").bind('mouseover', m_open);
	$(".tel").bind('mouseout', m_timer);

	$('#header #nav .box .a').mouseover(function() {
		$(this).stop().animate( {
			"top" : "1"
		}, 300);
	}).mouseout(function() {
		$(this).stop().animate( {
			"top" : "10"
		}, 300);
	});

	$('#header > .logo i').css('width', 0);

	$('#header > .logo i').animate( {
		"width" : '100%'
	}, 300, 'swing');

	var arr = $('.content > .secnav > .tut').offset();
	var TutSelector = '.content > .secnav > .tut';
	var DrugSelector = '#bgsecnav';
	var drugDuration = 250;
	var topDecrement = 235;
	if (arr) {
		$(DrugSelector).css('height', $(TutSelector).height() + 10).css('top',
				arr.top - topDecrement);
		$(DrugSelector).stop().animate( {
			"width" : $(TutSelector).width() + 50
		}, drugDuration, 'swing');

		$('.content > .secnav > a').mouseover(function() {
			var a = $(this).offset();
			$(DrugSelector).stop().animate( {
				"top" : a.top - topDecrement,
				"height" : $(this).height() + 15,
				"width" : $(this).width() + 50
			}, drugDuration, 'swing')
		}).mouseout(function() {
			var a = $(TutSelector).offset();
			$(DrugSelector).stop().animate( {
				"top" : a.top - topDecrement,
				"height" : $(TutSelector).height() + 10,
				"width" : $(TutSelector).width() + 50
			}, drugDuration, 'swing')
		});
	}
});

