﻿(function($) {
    $.extend({
        notify: function(text)
        {
            var popup = document.createElement('div');
            var windowWidth = $(window).width();
            var id = 'notify' + NotifyRandNum();
            var windowHeight = $(window).height();
            var windowScroll = $(window).scrollTop();
            
            //alert(windowScroll);
            
            
            $(popup).html(text);
            $(popup).attr('id',id);
            $(popup).addClass('notify');
            $('body').append(popup);
            
            // Once the popup is appended to the body, we can fetch the popups width.
            var popupWidth = $(popup).innerWidth();
            var popupHeight = $(popup).innerHeight();
            
            // Subtract the popup width from the total window width, and divide it by two.
            // This is used to center the popup by using left-margin
            var leftMargin = ((windowWidth-popupWidth)/2);
            $(popup).css("margin-left",leftMargin);
            $(popup).css("top", (((windowHeight-popupHeight)/2)+windowScroll)-100);
            
            // Show the notification popup
            $('#' + id).show('drop', { direction: 'up' },1000, function() {
                
                // Call animate to cause a delay before the popup disappears
                $('#' + id).animate( { "opacity": 1 }, 1000, function() {
                    
                    // Hide the popup
                    $('#' + id).hide('drop', { direction: 'down' } , 1000, function() {
                        
                        // On callback, delete the popup from the DOM
                        $(this).remove();
                    });
                });
            });
        }
    });
})(jQuery);


// Just a small function to 
var NotifyRandNum = function()
{
    var uid = Math.floor(Math.random()*1000);
    return uid;
}