﻿Site.PopupWin = {}

Site.PopupWin.Show = function( id )
{
    Site.PopupWin.CenterHorizontally( id );
    Site.PopupWin.CenterVertically( id );
    
    $( "div.popup" ).draggable( { cancel: ".popup-content" });
    
    if( $.browser.msie )
    {
        $( "#" + id ).show();
        return;
    }
    
    $( "#" + id ).fadeIn( 400 );
}

Site.PopupWin.Hide = function( id )
{
    if( $.browser.msie )
    {
        $( "#" + id ).hide();
        return;
    }
    
    $( "#" + id ).fadeOut( 400 );
}

/**
 * Positions the popup centered in the x direction
 */
Site.PopupWin.CenterHorizontally = function( id )
{
    var winWidth = $( window ).width();
    var popup = $( "#" + id );
    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
 */
Site.PopupWin.CenterVertically = function( id )
{
    var winHeight = $( window ).height();
    var popup = $( "#" + id );
    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" );
}