$=jQuery;

(function( $ ){
    
	$.fn.popUp = function (popup, width, pre_callback, post_callback, options) {

        var popup_container='body';
        
        var after_show=false;
        
        var after_click=false;

        if(options!=undefined)
        {

            if(options.hasOwnProperty("popup_container")) 
            {

                popup_container=options.popup_container;
                
            }

            if(options.hasOwnProperty("after_show")) 
            {

                after_show=options.after_show;
                
            }

            if(options.hasOwnProperty("after_click")) 
            {

                after_click=options.after_click;
                
            }
            
            
        }
        
        //If scroll put in center scroll, if not scroll, put in simple center
        
        $(popup).hide();

        $(this).on('click', function (e) {
            

            body_height=$("body").height();

            $('#layer_popup').height(body_height);
            
            $('body').prepend('<div id="layer_popup"></div>');
            
            $('body').css('overflow', 'hidden');

            content_popup=$(popup_container).prepend('<div class="content_popup"><div class="lds-facebook"><div></div><div></div><div></div></div></div>');
            
            $('.lds-facebook').show();
            
            $(popup).appendTo('.content_popup');

            if(width==undefined)
            {

                $('.content_popup').width('80%');
                
            }
            else
            {

                $('.content_popup').width(width);                
                
            }
            
            popup_height=parseInt($(popup).height())+40;
                
            window_height=$(window).height();
            
            $('.content_popup').wrap('<div id="container_popup"></div>');

            $('#container_popup').css('height', window_height);
            
            $('#container_popup').addClass('click_popup');
            $('#layer_popup').addClass('click_popup');
            
            pos_scroll=$(document).scrollTop();
                
            new_top=Math.round((window_height-popup_height)/2)+pos_scroll;

            $('#container_popup').css('top', pos_scroll);
            
            if (popup_height>window_height) 
            {

                $('.content_popup').css('margin-top', 0);

            }
            
            $('#error_insert_name').html('')            
            
            $('.close_popup').click(function () {
               
                $('#container_popup').click();
                
                if(after_click)
                {
                    
                    after_click();
                    
                }
                
                return false;
                
            });
            
            $('.click_popup').click(function (data) {
				
                if (data.target == this) {
                
                    $(popup).hide();
                    
                    $(popup).appendTo('body');
                    
                    $('#layer_popup').remove();
                    
                    $('#container_popup').remove();
                    
                    //$("body > #height-helper").contents().unwrap();
                    
                    $('#height-helper').remove();
                    
                    $('body').css('height', '100%');

                    $('body').css('overflow', 'auto');
                    
                    if(post_callback!=undefined)
                    {
                        
                        post_callback();
                        
                    }
                    
                    $('.close_popup').unbind('click');

                    if(after_click)
                    {
                        
                        after_click();
                        
                    }
                    
                    
                }
                else {
                
                    return true;
                
                }
                
                return false;
			
			});
            
            if(pre_callback!=undefined)
            {
                
                r=pre_callback(popup, this);
                
                if(r!=undefined)
                {
                        
                    if(r===false)
                    {
                        
                        return false;
                        
                    }
                    
                }
                
            }
            
            if(!after_show)
            {
            
                $(popup).fadeIn();
                
            }
            else
            {
                
                $(popup).fadeIn({'done': after_show(popup)});                
                
            }
            
            $('.lds-facebook').hide();
            
            
            return false;
            
        });
    
    }
	
})( jQuery );

$=jQuery;

(function( $ ){
    
	$.fn.simplePopUp=function (popup, width, pre_callback, post_callback, options) {
        
        var after_show=false;
        
        var after_click=false;
        
        var popup_container=false;

        if(options!=undefined)
        {

            if(options.hasOwnProperty("popup_container")) 
            {

                popup_container=options.popup_container;
                
            }

            if(options.hasOwnProperty("after_show")) 
            {

                after_show=options.after_show;
                
            }

            if(options.hasOwnProperty("after_click")) 
            {

                after_click=options.after_click;
                
            }
            
            
        }

        $(this).on('click', function (e) {
            
            if(pre_callback!=undefined)
            {
                
                r=pre_callback(popup, this);
                
                if(r!=undefined)
                {
                        
                    if(r===false)
                    {
                        
                        return false;
                        
                    }
                    
                }
                
            }
            
            //Show layer_popup
            
            body_height=$("body").height();

            $('#layer_popup').height(body_height);
            
            $('body').prepend('<div id="layer_popup"></div>');
            
            //$('body').css('overflow', 'hidden');
            
            //Show popup
        
            $(popup).css('position', 'absolute');
            
            $(popup).css('width', width);
            
            $(popup).addClass('content_popup');
            
            $(popup).appendTo('#layer_popup');
            
            /*var position=$(popup).offset();
            
            var left=position.left;
            var top=position.top;
            
            console.log($(popup).css('left'));*/
            
            scr_width=$(window).width();
            
            scr_height=$(window).height();
            
            center_left=parseInt(scr_width/2)-parseInt(width/2);
            
            center_top=parseInt(scr_height/2)-parseInt($(popup).height()/2);
            
            $(popup).css('left', center_left);
            $(popup).css('top', center_top);
        
            if(!after_show)
            {
            
                $(popup).fadeIn();
                
            }
            else
            {
                
                $(popup).fadeIn({'done': after_show(popup)});                
                
            }
            
            return false;
        
        });
        
        $(popup).find('.close_popup').click(function () {
            
            if(after_click)
            {
                
                after_click();
                
            }
            
            $(popup).hide();
            
            $(popup).appendTo('body');
            
            $('#layer_popup').remove();
            
            return false;
            
        });

    }
	
})( jQuery );

//From https://www.w3schools.com/howto/howto_js_draggable.asp

function dragElement(draggable, header) {
    
    elmnt=document.getElementById(draggable);

    var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
    if (document.getElementById(header)) {
        // if present, the header is where you move the DIV from:
        document.getElementById(header).onmousedown = dragMouseDown;
    } else {
        // otherwise, move the DIV from anywhere inside the DIV:
        elmnt.onmousedown = dragMouseDown;
    }

    function dragMouseDown(e) {
        e = e || window.event;
        e.preventDefault();
        // get the mouse cursor position at startup:
        pos3 = e.clientX;
        pos4 = e.clientY;
        document.onmouseup = closeDragElement;
        // call a function whenever the cursor moves:
        document.onmousemove = elementDrag;
    }

    function elementDrag(e) {
        e = e || window.event;
        e.preventDefault();
        // calculate the new cursor position:
        pos1 = pos3 - e.clientX;
        pos2 = pos4 - e.clientY;
        pos3 = e.clientX;
        pos4 = e.clientY;
        // set the element's new position:
        elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
        elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
    }

    function closeDragElement() {
        // stop moving when mouse button is released:
        document.onmouseup = null;
        document.onmousemove = null;
    }
}

(function( $ ){
    
	$.fn.draggable = function (header) {
        
        var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
        
        var popup=this;
        
        $(popup).find(header).on('mousedown', {draggable: this}, function (e) {
        
            e = e || window.event;
            
            e.preventDefault();
            // get the mouse cursor position at startup:
            pos3 = e.clientX;
            pos4 = e.clientY;
            
            document.onmouseup = e.data.draggable.closeDragElement;
            
            // call a function whenever the cursor moves:
            document.onmousemove = e.data.draggable.elementDrag;
        
            console.log('Moving... ');
        
        });
        
        //function elementDrag(e) {
        this.elementDrag=function (e) {
            
            console.log('Moving windows...');
            
            e = e || window.event;
            e.preventDefault();
            // calculate the new cursor position:
            pos1 = pos3 - e.clientX;
            pos2 = pos4 - e.clientY;
            pos3 = e.clientX;
            pos4 = e.clientY;
            // set the element's new position:
            /*elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
            elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";*/
            
            offset=$(popup).offset();
            
            offset_top=parseInt(offset.top);
            offset_left=parseInt(offset.left);
            
            $(popup).css('top',  (offset_top - pos2) + "px");
            $(popup).css('left',  (offset_left - pos1) + "px");
            
        }
        
        //function closeDragElement() {
        this.closeDragElement=function (e) {
        // stop moving when mouse button is released:
            document.onmouseup = null;
            document.onmousemove = null;
        }

    }
	
})( jQuery );
