/**
 * Quan ly hop thoai xuat hien ben trong trang web (inline)
 * @author      Nhan Nguyen <nh2nnt@yahoo.com>
 * @since       July 22, 2009
 */
iBox = new function() {
    /**
     * mau de tao ra hop thoai
     * @var string 
     */
    var _template = '<table class="box" cellspacing="0" cellpadding="0">' + 
            '<tr>' +
                '<td class="lN" />' +
                '<td class="lP lB" colspan="3" />' +
                '<td class="lE" />' +
            '</tr>' +
            '<tr>' +
                '<td class="lU" rowspan="2" />' +
                '<td class="lQ lB" />' +
                '<td class="body" />' +
                '<td class="lQ lB" />' +
                '<td class="lV" rowspan="2" />' +
            '</tr>' +
            '<tr>' +
                '<td class="lP lB" colspan="3" />' +
            '</tr>' +
            '<tr>' +
                '<td class="lW" colspan="2" />' +
                '<td class="lH" />' +
                '<td class="lS" colspan="2" />' +
            '</tr>' +
        '</table>';

    /**
     * dien giai tieu de
     * @var array
     */
    var _labels = {'OK': 'Xác nhận', 'Cancel': 'Thôi', 'Yes': 'Đồng ý', 'No': 'Không', '-': ' '};

    /**
     * tham chieu den node trinh bay hop thoai
     * @var DomNode
     */
    var _dom;

    /**
     * ket qua hien tai cua hop thoai thong bao (OK|Cancel|Yes|No)
     * @var string
     */
    this.result = '';

    /**
     * dat hop thoai giua man hinh
     * @return void
     */
    var _center = function() {
        if (_dom) {
            var doc = (dojo.doc.compatMode == 'BackCompat') ? dojo.body() : dojo.doc.documentElement;
            var scroll = dojo._docScroll(); 
            var box = dojo._getContentBox(_dom);
            var x = scroll.x + (doc.clientWidth - box.w) / 2;
            var y = scroll.y + (doc.clientHeight - box.h) / 2;

            dojo._setBox(_dom, (x < scroll.x ? scroll.x : x), (y < scroll.y ? scroll.y : y), box.w, box.h);
        }
    };

    /**
     * tao ra the cho hop thoai 
     * @return DomNode
     */
    var _fork = function() {
        // neu da tao roi thi thoi
        if (!_dom) {
            _dom = dojo.create('div', {id: '__box_container', 'class': '__box_container', innerHTML: _template}, dojo.body(), 'last');

            // giu hop thoai giua man hinh khi cuon hay thay doi kich thuoc
            dojo.connect(window, 'onscroll', function(e) {_center();});
            dojo.connect(window, 'onresize', function(e) {_center();});
        }

        return _dom;
    };

    /**
     * dat noi dung cho hop hoai
     * @param string html   
     * @return void
     */
    var _innerHTML = function(html) {
        _fork();

        if (_dom) {
            dojo.style(_dom, {width: 'auto', height: 'auto'});
            dojo.query('.body', _dom)[0].innerHTML = html;
        }
    };

    /**
     * khoi tao lai trang thai hop thoai
     * @return void
     */
    this.clearState = function() {
        _innerHTML('');

        this.hide();
        this.result = '';
        this.execute = null;
    };

    /**
     * dat noi dung cho hop hoai
     * @param string html   
     * @return void
     */
    this.innerHTML = function(html) {
        _innerHTML(html);
    };

    /**
     * dat hop thoai giua man hinh
     * @return void
     */
    this.center = function() {
        _center();
    };

    /**
     * kiem tra xem hop thoai dang hien thi
     * @return bool
     */
    this.isVisible = function() {
        return dojo.hasClass(_dom, 'on');
    };

    /**
     * hien thi hop thoai
     * @return void
     */
    this.show = function() {
        if (_dom)
            dojo.addClass(_dom, 'on');
    };

    /**
     * an hop thoai
     * @return void
     */
    this.hide = function() {
        if (_dom)
            dojo.removeClass(_dom, 'on');
    };
    
    /**
     * xu ly ket qua thuc hien
     * @return bool     tra ve true se an hop hoai; false khong an
     */
    this.execute = function(result) {
        return true;
    };

    /**
     * xu ly ket qua khi an cac nut tren hop thoai thong bao
     * @param string result     loai nut da an
     * @return void
     */
    this.handle = function(result) {
        if (this.execute) {
            this.result = result;

            if (this.execute(result)) 
                this.hide();
        }
        else {
            this.hide();
        }
    };

    /**
     * xu ly ket qua khi an cac nut tren hop thoai thong bao
     * @param string result     loai nut da an
     * @return void
     */
    this.__handle_message = function(result) {
        this.result = result;

        if (this.execute) 
            this.execute(result);

        this.hide();
    };

    /**
     * hien thi hop thoai thong bao
     * @var string text     thong diep
     * @var string title    (optional) tieu de hop thoai
     * @var string button   (optional) loai button (OK|Cancel|Yes|No)
     * @var string icon     (optional) loai icon (None|Info|Question|Warning|Error)
     * @return void
     */
    this.message = function(text, title, button, icon) {
        this.clearState();

        var html = '<table cellspacing="0" cellpadding="7">' +
            '<tr class="title-bar">' +
                '<td class="title">' + (title ? title : '&nbsp;') + '</td>' +
                '<td class="x" align="right" />' +
            '</tr>' +
            '<tr>' +
                '<td colspan="2">' +
                    '<table width="300">' +
                        '<tr>' +
                            '<td width="42" align="center"><div class="xlogo ' + (icon ? icon : 'Warning') + '">&nbsp;</div></td>' +
                            '<td>' + (text ? text : '&nbsp;') + '</td>' +
                        '</tr>' +
                    '</table>' +
                '</td>' +
            '</tr>' +
            '<tr>' +
                '<td class="xcommand" colspan="2" align="right">~button</td>' +
            '</tr>' +
        '</table>';
        var mp = (button ? button : 'OK').split('|');
        var hx = '';
        var i;

        for (i = 0; i < mp.length; i++) {
            if (mp[i] != '-')
                hx += '<input type="button" class="' + mp[i] + '" value="' + _labels[mp[i]] + '" onclick="iBox.__handle_message(\'' + mp[i] + '\')" />';
            else
                hx += _labels[mp[i]];
        }

        html = html.replace('~button', hx);

        _innerHTML(html);
        _center();

        this.show();
    };

    /**
     * hien thi hop thoai cho
     * @return void
     */
    this.wait = function() {
        this.clearState();

        var html = '<table cellspacing="0" cellpadding="7">' +
            '<tr class="title-bar">' +
                '<td class="title">Xin hãy chờ...</td>' +
            '</tr>' +
            '<tr>' +
                '<td><img src="' + jSys.baseUrl + '/images/loading.gif" /></td>' +
            '</tr>' +
        '</table>';

        _innerHTML(html);
        _center();

        this.show();
    };

    /**
     * hien thi man hinh popup hien thi thong tin
     * tu trang khac
     * @param string url    duong dan trang can xem
     * @param string title  tieu de
     * @param int width     chieu rong
     * @param int height    chieu cao
     * @return void
     */
    this.popup = function(url, title, width, height, command) {
        this.clearState();

        var html = '<table cellspacing="0" cellpadding="7">' +
            '<tr class="title-bar">' +
                '<td class="title">' + (title ? title : '&nbsp;')  + '</td>' +
                '<td class="x" align="right"><a href="javascript:void(0)" class="xclose" onclick="iBox.hide()" title="Đóng">&nbsp;</a></td>' +
            '</tr>' +
            '<tr>' +
                '<td colspan="2"><div style="border: solid 1px #eeeeee;"><iframe src="' + url + '" width="' + (width ? width : '500') + '" height="' + (height ? height : '250') + '" frameborder="0"></iframe></div></td>' +
            '</tr>';
        if (command)
            html +=
            '<tr>' +
                '<td class="xcommand" colspan="2" align="right"><input type="button" value="Đóng" onclick="iBox.hide()" /></td>' +
            '</tr>' +
        '</table>';

        _innerHTML(html);
        _center();

        this.show();
    };
};