/* 
 * @author: Brian roy
 * @date: 07/06/2010
 *
 * Purpose: Implements a pop up dialog box.
 *
 * Notes: Implements a pop up dialog box - which can be either
 * modal or not. The dialog box is unstyled and should be combined with
 * appropriate CSS to create the look/feel.
 *
 * CSS Notes: The script implements 2 primary divs:
 *  1) id-'modal-cover' - masks content for modal operation.
 *  2) id='save-pop' - the actual dialog container.
 *      Sub-Sections:
 *          1) div id='loader' - intended for any "loading" animation.
 *          2) div id='save-pop-msg' - contains any message text.
 *
 * Requried: JQuery 1.4.2 or greater is required.
 *
 */

/**
 * This custom Jquery function centers a Div in the middle of the screen taking care of the scroll with
 */
$.fn.centerIt = function()
{     
    $(this).offset({
                top:  parseInt($(window).scrollTop() + (($(window).height() / 2) - ( $(this).height() / 2))), 
                left: parseInt((($(window).width() - scrollbarWidth()) / 2) - ( $(this).width() / 2))
    }); 
}

function showGenericHtmlPop(html, width, height, isModal, noDupe)
{
    if (typeof noDupe != 'undefined' && noDupe && typeof dialogOpen != 'undefined' && dialogOpen == true) {
        return;
    }
    dialogOpen = true;
    
    // Already Open
    if ($('#generic-pop').length > 0) {
        $('#generic-pop').data('focus',$('*:focus')); // save current focus         
        $('#generic-pop').attr('id', 'generic-pop-backup').hide();
        if ( $('#modal-cover').length > 0)  {
            $('#modal-cover').attr('id', 'modal-cover-backup').hide();
        }                
    }

    if (typeof(isModal) == 'undefined') isModal = false;
            
    if (isModal === true) {
        $('<div class="modal-cover" id="modal-cover">').click(function(e){
            e.preventDefault();
        }).css(
        {
            'overflow': 'hidden',
            'margin-right': scrollbarWidth()    
        }
    ).fadeTo(200,.5, function(){
                $('body').append($("<div class='generic-pop' id='generic-pop'>").append(html).width(width));    
                $('#generic-pop').centerIt();
    }).appendTo('body');
        $('#modal-cover').width($(document).width()).height($(document).height());        
        $(window).resize(function(){
            $('#modal-cover').width($(document).width()).height($(document).height());
        });
    }
                                    
    $(window).resize(function(){
            $('#generic-pop').centerIt();
    });
                               
}


function scrollbarWidth()
{   
    if (($.browser.msie) && (parseInt($.browser.version,10) < 9)) {
        return 17;
    }
    var overflow = $('body').css('overflow');    
    var w = $('body').css('overflow', 'hidden').width();
    $('body').css('overflow','scroll');
    w -= $('body').width();           
    if (!w) w = $('body').width() - $('body')[0].clientWidth;
    $('body').css('overflow', overflow);    
    return w;
}


function completeGenericPop()
{
    $('#generic-pop, #modal-cover').remove();
    $('body').css({
        'overflow': 'auto',
        'margin-right': '0px'
    });
    dialogOpen = false;
    
    if ($('#generic-pop-backup').length > 0) {
        $('#generic-pop-backup').attr('id', 'generic-pop').show();        
        if ($('#modal-cover-backup').length > 0) {
            $('#modal-cover-backup').attr('id', 'modal-cover').show();
        }
        var el = $('#generic-pop').data('focus');
        el.focus();        
    }            
}

function completeSavePop(strMsg)
{
    if(typeof(strMsg) == 'undefined') strMsg = "Saved!";    
    $('div.save-pop-msg').empty().append($('<p>').append(strMsg));
    setTimeout('completeGenericPop()', 1500);
}

function showErrorSavePop(strMsg)
{
    if (typeof(strMsg) == 'undefined') strMsg = 'An error occured... please try again.';       
    $('div.save-pop-msg').empty().append($('<p>').append(strMsg));
    setTimeout('completeGenericPop()', 1500);
}

function msgBox(type, message, noDupe)
{    
    switch(type) {
    case 'info':
      btn = 3;
      break;
    case 'success':
    case 'warning':
    case 'error':        
      btn = 1;  
      break;  
    }
    
    var wrap = $('<div id="md-box">').addClass('md-' + type);    
    var msg  = $('<div id="md-message">').addClass('md-' + type + '-icon').append('<span>' + message + '</span>');    
    var button = $('<button class="btn">').addClass('btn' + btn).click(function(){
        completeGenericPop();
    }).append('<span>Ok</span>');
        
    showGenericHtmlPop(wrap.append(msg.add(button)), 540, 100, true, noDupe);
}
