jQuery(function($){
$('html').append('<div class="scrollbar" style="overflow:scroll;"></div>');
var scrollsize=window.innerWidth - $('.scrollbar').prop('clientWidth');
$('.scrollbar').hide();
$('.modal-open').click(function(){
$('html, body').addClass('lock');
$('body').append('<div class="modal-overlay"></div>');
$('.modal-overlay').fadeIn('slow');
var modal='#' + $(this).attr('data-target');
$(modal).wrap("<div class='modal-wrap'></div>");
$('.modal-wrap').show();
modalResize();
$(modal).fadeIn('slow');
$(modal).click(function(e){
e.stopPropagation();
});
$('.modal-wrap, .modal-close').off().click(function(){
$(modal).fadeOut('slow');
$('.modal-overlay').fadeOut('slow',function(){
$('html, body').removeClass('lock');
$('.modal-overlay').remove();
$(modal).unwrap();
});
});
$(window).on('resize', function(){
modalResize();
});
function modalResize(){
var w=$(window).width();
var h=$(window).height();
var mw=$(modal).outerWidth(true);
var mh=$(modal).outerHeight(true);
if((mh > h)&&(mw > w)){
$(modal).css({'left': 0 + 'px','top': 0 + 'px'});
}else if((mh > h)&&(mw < w)){
var x=(w - scrollsize - mw) / 2;
$(modal).css({'left': x + 'px','top': 0 + 'px'});
}else if((mh < h)&&(mw > w)){
var y=(h - scrollsize - mh) / 2;
$(modal).css({'left': 0 + 'px','top': y + 'px'});
}else{
var x=(w - mw) / 2;
var y=(h - mh) / 2;
$(modal).css({'left': x + 'px','top': y + 'px'});
}}
});
});