/**
 * EmAx Object 
 */
//Modified by Ryan Gao 2007-08-15 14:14:42

var EmAx = new Object();

EmAx.dump = function(variable){
    out = $('body');
    
    out.e = function(s){
        out.append(s + '<br/>');
    }
    
    out.e('<hr>');
    out.e('VAR - (' + typeof variable + ')');
    out.e(variable);
    if(typeof variable == 'object'){
        for(key in variable){
            out.e('-- ' + key + '(' + typeof(variable[key]) + '):' + variable[key]);
        }
    }
}

/**
 * Copy style of source object to target object.
 */
EmAx.copyCss = function(target, source){
    var styles = [
        'border-top-width','border-bottom-width','border-left-width','border-right-width',
        'border-top-color','border-bottom-color','border-left-color','border-right-color',
        'border-top-style','border-bottom-style','border-left-style','border-right-style',
        'padding-top','padding-bottom','padding-left','padding-right',
        'background-color','color','font-size','height','width','line-height'
        ];
        
    for(i=0; i<styles.length; i++){
        var val = source.css(styles[i]);
        if(val != 'auto'){
            target.css(styles[i], val);
        }
    }
}

Em_box = function(opt){
    
    this.options = {
                       width:400,
                       height:'auto',
                       positionPage:true,
                       model:true,
                       modelOpacity:0.3,
                       minimizable:false,
                       scrolling:true,
                       htmlId:'JBOX_',
                       title:'NO TITLE',
                       center:false,
                       
                       _END_:0
                   };
    
    if(typeof(opt.attach) == 'object' && 'jquery' in opt.attach){
        
        var attchTo = $(opt.attach);
        
        off = attchTo.offset();
        
        this.options.width = attchTo.width();
        this.options.top = off.top + attchTo.outerHeight();
        this.options.left = off.left;
        this.options.htmlId = "JBOX_" + attchTo.attr("id");
        
    }else{
        this.options.center = true;
    }

    if(opt.center){
        this.options.positionPage = false;
    }

    for(key in opt){
        this.options[key] = opt[key];
    }

    this.create();
}

Em_box.prototype.create = function(){

    opt = this.options;
    
    this.box = jBox.open(opt.htmlId, 'inline', 'inline text', opt.title, opt);
    
    if('showTitle' in opt){
        if(opt.showTitle == false){
            $(this.box.handler).hide();
        }
    }
    
    this.ctrlDiv = $(this.box.controls);
    this.ctrlDiv.empty();

    this.btnOk = $('<input type="button" value="确定" title="Ok">').appendTo(this.ctrlDiv);
    this.btnCancel = $('<input type="button" value="取消" title="Cancel">').appendTo(this.ctrlDiv);
    
    this.btnOk.bind('click', {box : this}, this.onBtnClick);    
    this.btnCancel.bind('click', {box : this}, this.onBtnClick);    
}

Em_box.prototype.onBtnClick = function(event){
    event.data.box.hide();
}

Em_box.prototype.getContentDiv = function(){
    return this.box.content;
}

Em_box.prototype.show = function(){
    if(null == this.box){
        this.open();
    }
    this.box.show();
}

Em_box.prototype.hide = function(){
    this.box.hide();
}

Em_box.prototype.distroy = function(){
    this.box.close();
}
