/**
 * JS Popup
 *
 * @copyright Copyright (c) 2008, UAB "inaction.lt"
 * @author    Benas Valančius <benas@inaction.lt>
 * @updated   2008-08-08
 * @version   2.0.1
 * @package   Framework
 *
 * $Id$
 */

/**
 * 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.mainObj   = document.body;
    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)
{
    if(width == null) width = 500;
    if(top == null) top = 100;
    var paddingTop = top;
    var paddingLeft = (document.documentElement.offsetWidth - width) / 2;

    // background
    this.newBG = document.createElement('div');
    this.newBG.id = 'bg_'+ this.uid;
    this.newBG.className = 'full_bg';
    this.mainObj.appendChild(this.newBG);

    // centruojantis sluoksnis
    this.newBody = document.createElement('div');
    this.newBody.className = 'ax_bg';
    this.mainObj.appendChild(this.newBody);

    // blockas
    this.newBlock = document.createElement('div');
    this.newBlock.id = 'popup_'+ this.uid;
    this.newBlock.className = 'ax_main';
    if(!this.visible) this.newBlock.style.display = 'none';
    this.newBlock.style.top = paddingTop +'px';
    this.newBlock.style.left = paddingLeft +'px';
    this.newBlock.style.width = width + 20 +'px';
    this.newBody.appendChild(this.newBlock);

    var content = document.createElement('div');
    content.className = 'contentPop';

    this.newBlock.appendChild( this.roundTop() );
    this.newBlock.appendChild( content );
    this.newBlock.appendChild( this.roundBottom() );

    this.block = document.createElement('div');
    this.block.className = 'ax_block';
    this.block.style.width = width +'px';
    content.appendChild( this.block );

    // header
    var header = document.createElement('div');
    eval("$(header).mousedown(function(event){dragStart(event, 'popup_"+ this.uid +"')});");
    header.className = 'ax_header';

    var newHeader = document.createElement('div');
    newHeader.id = 'ax_header';
    newHeader.className = 'ax_title';
    header.appendChild(newHeader);

    // header actions
    var newHeaderActions = document.createElement('div');
    newHeaderActions.id = 'ax_actions';
    newHeaderActions.className = 'ax_actions';
    newHeaderActions.innerHTML = '<a href="javascript:'+ this.className +'.close()">Uždaryti</a>';
    //eval("$(newHeaderActions).click(function(event){"+ this.className +".close()});");
    newHeader.appendChild(newHeaderActions);

    // header title
    var newHeaderTitle = document.createElement('div');
    newHeaderTitle.innerHTML = this.title;
    newHeader.appendChild(newHeaderTitle);

    this.block.appendChild(header);
    
    // turinys 
    var newContent = document.createElement('div');
    newContent.id = 'ax_content';
    newContent.className = 'ax_content';
    newContent.innerHTML = this.content;

    this.content = '';
    this.block.appendChild( newContent );

    // loading...
    if(!this.visible)
    {
        this.loading = document.createElement('div');
        this.loading.id = 'ax_loading';
        this.loading.style.top = 150 +'px';
        this.loading.style.left = ((document.documentElement.offsetWidth - 150) / 2) +'px';
        this.loading.style.width = 200 +'px';

        this.loading.appendChild( this.roundTop() );

        var contentPop = document.createElement('div');
        contentPop.className = 'contentPop';

        var content = document.createElement('div');
        content.className = 'ax_loading_content';
        content.innerHTML = loadingMessage;
        contentPop.appendChild( content );
        this.loading.appendChild( contentPop );

        this.loading.appendChild( this.roundBottom() );
        
        this.newBody.appendChild( this.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.newBlock.style.display = '';
    if(this.loading && this.loading.parentNode && this.loading.parentNode.removeChild)
        this.loading.parentNode.removeChild( this.loading );
}

/**
 * Remove popup
 *
 * Panaikinami visi popup sukurti elementai
 */
iPopup.prototype.close = function()
{
    if(this.newBG && this.newBG.parentNode && this.newBG.parentNode.removeChild)
    {
        this.newBG.parentNode.removeChild(this.newBG);
    }
    if(this.newBody && this.newBody.parentNode && this.newBody.parentNode.removeChild)
    {
        this.newBody.parentNode.removeChild(this.newBody);
    }
}

iPopup.prototype.roundTop = function()
{
    var block = document.createElement('div');
    var leftTop = document.createElement('div');
    var rightTop = document.createElement('div');
    var centerTop = document.createElement('div');
    leftTop.className = 'leftTop';
    rightTop.className = 'rightTop';
    centerTop.className = 'centerTop';
    block.appendChild( leftTop );
    block.appendChild( rightTop );
    block.appendChild( centerTop );
    return block;
}

iPopup.prototype.roundBottom = function()
{
    var block = document.createElement('div');
    var leftBottom = document.createElement('div');
    var rightBottom = document.createElement('div');
    var centerBottom = document.createElement('div');
    leftBottom.className = 'leftBottom';
    rightBottom.className = 'rightBottom';
    centerBottom.className = 'centerBottom';
    block.appendChild( leftBottom );
    block.appendChild( rightBottom );
    block.appendChild( centerBottom );
    return block;
}
