var SearchStatus = 0;
var MessageStatus = 0;
var LoginStatus = 0;

function loadSearch(){ 
  if(SearchStatus==0){ 
    $("#backgroundPopup").css({ 
      "opacity": "0.7" 
    }); 
    $("#backgroundPopup").fadeIn("fast"); 
    $("#SearchContact").fadeIn("fast"); 
    SearchStatus = 1; 
  } 
}

function loadMessage(){ 
  if(MessageStatus==0){ 
    $("#backgroundPopup").css({ 
      "opacity": "0.7" 
    }); 
    $("#backgroundPopup").fadeIn("fast"); 
    $("#MessageContact").fadeIn("fast"); 
    MessageStatus = 1; 
  } 
}

function loadLogin(){ 
  if(LoginStatus==0){ 
    $("#backgroundPopup").css({ 
      "opacity": "0.7" 
    }); 
    $("#backgroundPopup").fadeIn("fast"); 
    $("#LoginContact").fadeIn("fast"); 
    LoginStatus = 1; 
  } 
}

function disableSearch(){ 
  if(SearchStatus==1){ 
    $("#backgroundPopup").fadeOut("fast"); 
    $("#SearchContact").fadeOut("fast"); 
    SearchStatus = 0; 
  } 
}

function disableMessage(){ 
  if(MessageStatus==1){ 
    $("#backgroundPopup").fadeOut("fast"); 
    $("#MessageContact").fadeOut("fast"); 
    MessageStatus = 0; 
  } 
}

function disableLogin(){ 
  if(LoginStatus==1){ 
    $("#backgroundPopup").fadeOut("fast"); 
    $("#LoginContact").fadeOut("fast"); 
    LoginStatus = 0; 
  } 
}

function centerSearch(){ 
  var windowWidth = document.documentElement.clientWidth; 
  var windowHeight = document.documentElement.clientHeight; 
  var popupHeight = $("#SearchContact").height(); 
  var popupWidth = $("#SearchContact").width(); 
  $("#SearchContact").css({ 
    "position": "absolute", 
    "top": windowHeight/2-popupHeight/2, 
    "left": windowWidth/2-popupWidth/2 
  }); 
  $("#backgroundPopup").css({ 
    "height": windowHeight 
  }); 
}

function centerMessage(){ 
  var windowWidth = document.documentElement.clientWidth; 
  var windowHeight = document.documentElement.clientHeight; 
  var popupHeight = $("#MessageContact").height(); 
  var popupWidth = $("#MessageContact").width(); 
  $("#MessageContact").css({ 
    "position": "absolute", 
    "top": windowHeight/2-popupHeight/2, 
    "left": windowWidth/2-popupWidth/2 
  }); 
  $("#backgroundPopup").css({ 
    "height": windowHeight 
  }); 
}

function centerLogin(){ 
  var windowWidth = document.documentElement.clientWidth; 
  var windowHeight = document.documentElement.clientHeight; 
  var popupHeight = $("#LoginContact").height(); 
  var popupWidth = $("#LoginContact").width(); 
  $("#LoginContact").css({ 
    "position": "absolute", 
    "top": windowHeight/2-popupHeight/2, 
    "left": windowWidth/2-popupWidth/2 
  }); 
  $("#backgroundPopup").css({ 
    "height": windowHeight 
  }); 
}


$(document).ready(function(){
  $("#h-search").click(function(){
    centerSearch();
    loadSearch();
  });
  $("#h-message").click(function(){
    centerMessage();
    loadMessage();
  });
  $("#login").click(function(){
    centerLogin();
    loadLogin();
  });
               
  $("#SearchContactClose").click(function(){
    disableSearch();
  });
  $("#MessageContactClose").click(function(){
    disableMessage();
  });
  $("#LoginContactClose").click(function(){
    disableLogin();
  });
  
  $("#backgroundPopup").click(function(){
      disableSearch();
      disableMessage();
      disableLogin();
  });
  $(document).keypress(function(e){
    if(e.keyCode==27 && SearchStatus==1) {
      disableSearch();
    }
    if(e.keyCode==27 && MessageStatus==1) {
      disableMessage();
    }
    if(e.keyCode==27 && LoginStatus==1) {
      disableLogin();
    }
  });
});
