// JavaScript Document

$(document).ready(function(){
  function getYOffsetY() {
    var pageY;
    if(typeof(window.pageYOffset) =='number') {
       pageY=window.pageYOffset;
    }
    else {
       pageY=document.documentElement.scrollTop;
    }
    return pageY;
  };
  
  function getYOffsetX() {
    var pageX;
    
    if(typeof(window.pageXOffset) =='number') {
       pageX=window.pageXOffset;
       
    }
    else {
       pageX=document.documentElement.scrollLeft;
    }
    
    return pageX;
  };
  
  
    $(window).resize(calculation);

    function calculation() {
    
    var boxHeight = $(".modalBox").height();
    var boxWidth = $(".modalBox").width();
    var windowWidth = $(window).width(); //retrieve current window width
    var windowHeight = $(window).height(); //retrieve current window height
    
    var offsetY = getYOffsetY();
    
    var offsetX = getYOffsetX();
    
    var top = (windowHeight / 2 - boxHeight / 2) + offsetY;
    var left = (windowWidth / 2 - boxWidth / 2) + offsetX;
    
    var maskHeight = $(window).height() + offsetY;
    var maskWidth = $(window).width() + offsetX + 200;

        $(".modalBox").css({
            'top': top + 'px',
            'left': left + 'px'
        });
    
    $(".modalMask").css({
      'width' : maskWidth + 'px',
      'height' : maskHeight + 'px'
    });
    };
  
   //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
    //show box and mask
    $(".modalBox").fadeIn();
    $(".modalMask").fadeTo("slow", 0.6).fadeTo("slow", 0.4);
    //remove scroll bars
    $("body").addClass("removeScrollBars");
    calculation();
  });
  
  //if close button is clicked
    $('.close, .modalMask').click(function (event) {
        //Cancel the link behavior
        event.preventDefault();
    //hide modal and mask
        $('.modalMask, .modalBox').fadeOut("fast");
    //return scroll bars to normal
    $("body").removeClass("removeScrollBars");
    });
});

