var vyp_height
var shown = false;
var showing = false;

function show_mod(){
	if(shown)return;
	shown = true;
	if (showing) return;
	showing = true;
	$("#bl").show();
	$("#bl").animate({right: 0}, 400, 'swing', function () { 
		$('#bl').show();
		showing = false;
		if (!shown) {
			shown = true;
			hide_mod();
		}
	})
	
}

function hide_mod(){
	//alert("hide")
	if (!shown) return;
	shown = false;
	if (showing) return;
	showing = true;
	$("#bl").animate({right: -140}, 400, 'swing', function () {
		$('#bl').hide();
		showing = false;
		if (shown) {
			shown = false;
			show_mod();
		}
	})
}


$(document).ready( function(){
	vyp_height = $('#bl').height();
	$('#bl').hide();
})



$(window).scroll(function () { 
	var te_top = $('#ttop').offset().top;
	var ve_top = $('#tbot').offset();
	ve_top = ve_top ? ve_top.top : ve_top;

	var doc_height = document.body.scrollHeight;
	var win_height = $(window).height();
	var div_height = win_height/3; 
	
//	alert(ve_top-te_top)
	
	if(ve_top-te_top>600){

	if (div_height < vyp_height) div_height = vyp_height;
	
	//alert(te_top);
	
	if (doc_height - te_top < div_height) {
		var scr = $(document).scrollTop() + win_height;
	} else {
		var scr = $(document).scrollTop() + win_height - div_height;
	}
//	alert(te_top+"<"+scr)
//	alert(ve_top)
	if (te_top+200 < scr) {
		if (ve_top && $(document).scrollTop() + win_height > ve_top) {
			hide_mod();
		} else {// alert("!")
			show_mod();
		}
	} else {
		hide_mod();
	}
	}
});

	

