//use util.js, list.js

/**
 * usage:
 *                 var dialog = new Dialog ();
 *                 dialog.setTitle( 'title...' );
 *                 dialog.setError( 'errormessage' );
 *                 dialog.setTarget( 'target' )
 *                 dialog.setDraggable( true );
 *                 dialog.addCloseButton( 'ddddd' );
 *                 dialog.show( type, 350 );
 */

var SEQ = 0;
var z_index = 10;
var dialogList = new List();

/**
 * Dialogot megvalosito objektum
 */
function Dialog ( id ) {

    this.seqValue = SEQ++;

    this.type = 'Info';
    this.draggable = true;

    this.DIV_ID = id ? id : 'box_'+this.seqValue;
    this.TITLE_ID = 'title'+this.seqValue;
    this.CLOSE_ID = 'close'+this.seqValue;
    this.ERROR_ROW_ID = 'errorRow'+this.seqValue;
    this.TARGET_ID = 'target'+this.seqValue;
    this.BTN_ROW_ID = 'btnRow'+this.seqValue;

    this.div = document.createElement( 'div' );
    this.div.isDialog = true;
    //    this.div.style.dispose = 'none';
    this.div.id = this.DIV_ID;
    this.div.style.dispose = 'none';
    document.body.appendChild ( this.div );

    this.div.innerHTML = '<table cellpadding="0" cellspacing="0" width="100%" onmousedown="moveTop( \''+this.DIV_ID+'\' );">'+
    '<tr>'+
    '<td align="left" id="'+this.TITLE_ID+'">&nbsp;</td>'+
    '<td id="'+this.CLOSE_ID+'" class="close" onclick="dispose( \''+this.DIV_ID+'\' );">&nbsp;</td>'+
    '</tr>'+
    '<tr>'+
    '<td colspan="2" id="'+this.ERROR_ROW_ID+'" class="errorRow"></td>'+
    '</tr>'+
    '<tr>'+
    '<td colspan="2" id="'+this.TARGET_ID+'"></td>'+
    '</tr>'+
    '<tr>'+
    '<td colspan="2" id="'+this.BTN_ROW_ID+'" align="center"></td>'+
    '</tr>'+
    '</table>';

    this.show = function ( type, _width ){
        if ( type ){
            this.setStyle(type);
        } else {
            this.setDraggable( this.draggable );
        }
        if ( _width ){
            this.setWidth(_width);
        }
        this.div.style.display = 'block';
        dialogList.add( this );
    }

    this.dispose = function (){
        dispose ( this.DIV_ID );
    }

    this.getId = function (){
        return this.DIV_ID;
    }

    this.hidden = function (){
        this.div.style.display = 'none';
    }

    this.setTitle = function ( _title ){
        byId( this.TITLE_ID ).innerHTML = _title;
    }

    this.setError = function ( _error ){
        byId( this.ERROR_ROW_ID ).innerHTML = _error;
    }

    this.setTarget = function ( _target ){
        byId( this.TARGET_ID ).innerHTML = _target;
    }

    this.setStyle = function ( type ){
        // elements
        var close = byId( this.CLOSE_ID );
        var title = byId( this.TITLE_ID );
        var target = byId( this.TARGET_ID );
        var btnRowObj = byId( this.BTN_ROW_ID );
        //style of elements
        this.div.className = 'dialog' + type;
        close.className = 'close' + type;
        title.className = 'title' + type;
        //if ( this.dr )
            target.className = 'target' + type;
        btnRowObj.className = 'btnRow' + type;
        this.setDraggable( this.draggable );
    }

    this.setWidth = function ( _width ){
        var close = byId( this.CLOSE_ID );
        var title = byId( this.TITLE_ID );
        //style of elements
        this.div.style.width = _width+'px';
        this.div.style.left = ((800 - _width)/2)+'px';
        this.div.style.top = (getScrollTop() + 250 )+'px';
        title.style.width = (_width-16)+'px';
        close.style.width = 16 + 'px';
    }

    this.removeAllButton = function (){
        var btnRowObj = byId( this.BTN_ROW_ID );
        removeAllChild ( btnRowObj );
    }

    this.addButton = function ( text, onClick, id, className ){
        var newBtn = document.createElement( 'input' );
        newBtn.type = 'button';
        newBtn.value = text;
        newBtn.setAttribute( 'onclick',  onClick ? onClick : 'dispose( \''+this.DIV_ID+'\');' );
        newBtn.id = id ? id : 'btn' + (SEQ++);
        newBtn.className = className ? className : 'dialogBtn';
        var btnRowObj = byId( this.BTN_ROW_ID );
        btnRowObj.appendChild( newBtn );

    }
    
    this.getButton = function ( btnIndex ){
        var btnRowObj = document.getElementById( this.BTN_ROW_ID );
        return btnRowObj.childNodes[btnIndex];
    }

    this.addCloseButton = function ( text, id, className ){
        this.addButton(text ? text : 'Bezár', 'dispose( \''+this.DIV_ID+'\' );', id, className)
    }

    this.setDraggable = function ( draggable ){
        this.draggable = draggable;
        var title = byId( this.TITLE_ID );
        if ( draggable ){
            title.setAttribute( 'onmousedown',  'startDrag( event, \''+this.DIV_ID+'\' );' );
            title.style.cursor = 'move';
        } else {
            title.removeAttribute( 'onmousedown' );
            title.style.cursor = 'default';
        }
    }
}

/**
 * Bezarja a dialogot
 */
function dispose ( dialogId ){
    var dialog = byId( dialogId );
    if ( dialog ){
        dialogList.removeByElement( findDialog ( dialogId ) );
        dialog.parentNode.removeChild ( dialog );
    } else {
        alert ( 'Hiba a dialóg bezárása közben: nem létezik [id:'+dialogId+']!' );
    }
}

/**
 * Felulre helyezi a dialogot
 */
function moveTop ( dialogId ){
    //alert ( 'moveTop' );
    var dialog = byId( dialogId );
    if ( dialog ){
        dialog.style.zIndex = z_index++;
    } else {
        alert ( 'Hiba a dialóg el\u0151remozgatása közben: nem létezik [id:'+dialogId+']!' );
    }
}

/**
 * Visszaadja a dialog objektumot
 */
function findDialog ( dialogId ){
    for ( var i = 0; i < dialogList.size(); i++ ){
        if ( dialogList.get( i ).getId() == dialogId ){
            return dialogList.get( i );
        }
    }
    return null;
}

/**
 * Visszaadja, hogy a dialog latszik-e
 */
function isVisibleDialog ( dialogId ){
    var dialog = findDialog ( dialogId );
    return dialog && isVisibleAndDisplay ( dialog.getId() );
}

/**
 * Bezar minden megnyitott dialogot
 */
function closeAllDialog (){
    var dList = dialogList;
    for ( var i = 0; i < dList.size(); i++ ){
        dispose ( dList.get( i ).DIV_ID );
    }
}
