
/**
 * JS Popup
 *
 * @copyright Copyright (c) 2011, ecto.lt
 * @author    Benas Valančius <benas@ecto.lt>
 * @package   Framework
 * @version   2.0.0
 *
 * $Id: iPopup.js 242 2009-12-04 22:12:44Z noen $
 */

/**
 * Class
 *
 * @param string uid       - Unikalus objekto id (reikia tam jei kartais prireiktu keliu iPopup vienu kartu)
 * @param string className - Sukurtos klases pavadinimas
 */
function iPopup(uid, className)
{
    this.className = className;
    this.uid       = uid;
    this.content   = '';
    this.title     = '';
    this.visible   = true;
}

/**
 * Formuojamas iPopup html
 *
 * @param integer width - dhtml popup plotis
 * @param integer top   - dhtml popup atitraukimas nuo virsaus
 */
iPopup.prototype.addPopup = function(width, top)
{
    // DOM Objects
    this.popup      = document.createElement('div');
    this.background = document.createElement('div');
    this.block      = document.createElement('div');
    this.header     = document.createElement('div');
    this.container  = document.createElement('div');

    if(width === undefined)
        width = 500;

    if(top === undefined)
        top = 100;

    var paddingTop = top;
    var paddingLeft = (document.documentElement.offsetWidth - width) / 2;

    // Background
    $(this.background).attr({
        'id'    : 'bg_'+ this.uid,
        'class' : 'full_bg'
    }).appendTo('body');

    // Popup
    $(this.popup).attr('class','ax_bg').appendTo('body');

    // Block
    $(this.block).attr({
        'id'    : 'popup_'+ this.uid,
        'class' : 'ax_main'
    }).css({
        'top'   : paddingTop +'px',
        'left'  : paddingLeft +'px',
        'width' : width + 20 +'px'
    }).appendTo(this.popup);

    // Header
    $(this.header).attr({
        'id'    : 'ax_header',
        'class' : 'ax_title'
    }).appendTo(this.block);

    $('.ax_main').draggable({handle:'#ax_header',containment:'.ax_bg'});

    // Header actions
    var ax_action = $(document.createElement('div'));
    $(ax_action).attr('class','ax_actions').appendTo(this.header);

    eval("$('.ax_actions').click(function(){"+ this.className +".close()});");

    // Header title
    $(document.createElement('div')).html(this.title).appendTo(this.header);

    this.title = '';

    // Content
    $(this.container).attr('class','ax_content').html(this.content).appendTo(this.block);

    this.content = '';

    // Borders
    $(this.block).append( this.header ).append( this.container ).append(
        $(document.createElement('div')).attr('id','ax_loading')
    );

    // Loading
    if(!this.visible)
    {
        if(!this.visible)
            $(this.container).hide();

        $(document.createElement('div')).attr('class','ax_loading_content').html( loadingMessage ).appendTo( $('#ax_loading') );
    }
    this.visible = true;
};

/**
 * Formuojamas popup content
 *
 * @param string content - html kuris bus matomas popup
 */
iPopup.prototype.addContent = function(content)
{
    this.content += content;
};

/**
 * Popup title
 *
 * @param string title - dhtml popup title
 */
iPopup.prototype.addTitle = function(title)
{
    this.title = title;
};

/**
 * Hide popup
 */
iPopup.prototype.hide = function()
{
    this.visible = false;
};

/**
 * Show popup
 */
iPopup.prototype.show = function()
{
    $(this.container).show();
    $('#ax_loading').remove();
};

/**
 * Remove popup
 *
 * Panaikinami visi popup sukurti elementai
 */
iPopup.prototype.close = function()
{
    $(this.background).remove();
    $(this.popup).remove();
};
