
var popupStatus = 0;
function centerPopup(){  
  // ширина и высота окна браузера  
  var windowWidth = document.documentElement.clientWidth;  
  var windowHeight = document.documentElement.clientHeight;  
  var popupHeight = $("#login-popup-wrap").height();  
  var popupWidth = $("#login-popup-wrap").width();  
  // размещаем окно в центре страницы  
  $("#login-popup-wrap").css({  
  "position": "absolute",  
  "top": jQuery("#login-popup-wrap").offset().top+150, //windowHeight/2-popupHeight/2,  
  "left": windowWidth/2-popupWidth/2  
  });  
  // только для MS IE 6  
  $("#backgroundPopup").css({  
  "height": windowHeight  
  });  
}

function disablePopup(){  
  // Закрываем окно только если оно открыто 
  if(popupStatus==1){  
  $("#backgroundPopup").fadeOut("slow");  
  $("#login-popup-wrap").fadeOut("slow");  
  popupStatus = 0;  
  }  
}

function loadPopup(){  
  // Открываем окно только если оно закрыто 
  if(popupStatus==0){  
  $("#backgroundPopup").css({  
  "opacity": "0.7"  
  });  
  $("#backgroundPopup").fadeIn("slow");  
  $("#login-popup-wrap").fadeIn("slow");  
  popupStatus = 1;  
  }  
}

$(document).ready(function(){ 
  // ОТКРЫТИЕ ОКНА 
  // Событие - щелчек по кнопке 
  $(".registerLink").click(function(){ //#buttonLogin
  // размещаем окно в центре страницы 
  centerPopup(); 
  // открываем окно 
  loadPopup(); 

	return false;
  }); 
   
  // ЗАКРЫТИЕ ОКНА 
  // Событие - щелчок по "x" 
  $("#popupClose").click(function(){ 
  // закрываем окно 
  disablePopup(); 
  }); 
  // Событие - щелчок за пределами окна 
  $("#backgroundPopup").click(function(){
  // закрываем окно 
  disablePopup(); 
  }); 
  // Событие - нажата клавиша Escape 
  $(document).keypress(function(e){ 
  if(e.keyCode==27 && popupStatus==1) { 
  // закрываем окно 
  disablePopup(); 
  } 
  }); 
});
