/**
 * status
 * 0 - disabled
 * 1 - enabled
 */ 
var popupStatus = 0;

function loadPopup(){
  if(popupStatus==0){
    $("#backgroundPopup").css({
      "opacity": "0.7"
   });
   $("#backgroundPopup").fadeIn("slow");
   $("#wineList").fadeIn("slow");
    popupStatus = 1;
  }
}

function disablePopup(){  
  if(popupStatus==1){  
    $("#backgroundPopup").fadeOut("slow");  
    $("#wineList").fadeOut("slow");  
    popupStatus = 0;  
  }  
}  

function centerPopup(){  
  //data for centering  
  var windowWidth = document.documentElement.clientWidth;  
  var windowHeight = document.documentElement.clientHeight;  
  var popupHeight = $("#wineList").height();  
  var popupWidth = $("#wineList").width();  
  //centering  
  $("#wineList").css({  
    "position": "absolute",  
    "top": "20px",  
    "left": "300px"  
  });   
    
  $("#backgroundPopup").css({  
    "height": document.documentElement.height,
    "width": document.documentElement.width
  });  
}  


$(document).ready(function(){  
  //LOADING POPUP  
  $("#winelist").click(function(){  
    centerPopup();  
    loadPopup();  
  });  
  
  //CLOSING POPUP  
  //Click the X  
  $("#wineListClose").click(function(){  
    disablePopup();  
  });  
  //Click out  
  $("#backgroundPopup").click(function(){  
    disablePopup();  
  });  
  //Press Escape   
  $(document).keypress(function(e){  
    if(e.keyCode==27 && popupStatus==1){  
    disablePopup();  
    }  
  });   
});  
