var opened_popap = false;
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

$(document).ready(function() {
	$(document).mousemove(function(e) {
		if(!opened_popap){
			if(e.pageY - $(document).scrollTop() <= 5){
				opened_popap = true;
				$('#overlay').fadeIn('fast');
				//$('#pop').modal({onOpen: modalOpen, onClose: simplemodal_close});
				if($(document).scrollTop()>0){
					newH = $(document).scrollTop()+(f_clientHeight()-$('#pop').height())/2;
					if(newH-f_clientHeight() > f_clientHeight()) {
						$('#pop').fadeIn(700).css('left', $(document).width()/2 - $('#pop').width()/2).css('top', newH-2*f_clientHeight()).animate({ 'top': newH }, '300');
					}else{
						$('#pop').fadeIn(1000).css('left', $(document).width()/2 - $('#pop').width()/2).css('top', f_clientHeight()-$('#pop').height()/2).animate({ 'top': newH }, '300');
					}
					$(document).scroll(function() {
//					alert($(document).scrollTop()+(f_clientHeight()-$('#pop').height())/2);
						$('#pop').animate({ 'top': $(document).scrollTop()+(f_clientHeight()-$('#pop').height())/2 }, '1');
					});
				}else{
					$('#pop').delay(200).fadeIn(500).css('left', $(document).width()/2 - $('#pop').width()/2).css('top',(f_clientHeight()-$('#pop').height())/2);
				}
//				alert($(document).scrollTop()-(f_clientHeight()-$('#pop').height())/2);
//				alert($(document).scrollTop());
			}
		}
	});
});

function closeDlg(){
	$('#pop').slideUp('fast');
	$('#overlay').fadeOut('normal');
}
