﻿Popup = {}

Popup.CloseText = "Stäng";

Popup.Show = function( content )
{
    var $popup = $( "<div class='popup'><a title='" + Popup.CloseText + "' class='close-popup' href=\"javascript:;\" onclick=\"Popup.Close(this)\"><span>" + Popup.CloseText + "</span></a><div class='popup-content'>" + content + "</div></div>" );
    $( "form:first" ).prepend( $popup );
    Popup.CenterHorizontally( $popup );
    //Popup.CenterVertically( $popup );
    $popup.fadeIn( 400 );
}

Popup.Close = function( closeObj )
{
    $( closeObj ).closest( ".popup" ).fadeOut( 400 );
}

Popup.CenterHorizontally = function( $popup )
{
    var winWidth = $( window ).width();
    var popupWinWidth = $popup.width();
    
    var left;
    
    if( winWidth < popupWinWidth )
    {
        left = 0;
    }
    else
    {
        left = Math.floor( winWidth / 2 ) - Math.floor( popupWinWidth / 2 );
    }
    
    $popup.css( "left", left + "px" );
}

/**
 * Positions the popup centered in the y direction
 */
Popup.CenterVertically = function( $popup )
{
    var winHeight = $( window ).height();
    var popupWinHeight = $popup.height();
    
    var top;
    
    if( winHeight < popupWinHeight )
    {
        top = 0;
    }
    else
    {
        top = Math.floor( winHeight / 2 ) - Math.floor( popupWinHeight / 2 );
    }
  
    $popup.css( "top", top + $(document).scrollTop() + "px" );
}
