/**
 * JS Application
 *
 * @author    agung.hp
 * @copyright (c) 2008
 * @date      14/06/2008 13:58
 * @version   $Id$
 */
Ext.BLANK_IMAGE_URL = 'includes/ext/resources/images/default/s.gif';

Ext.namespace('Webi', 'Webi.fx', 'Webi.format');
Ext.onReady(function(){
    Webi.fx = function(){
        return {
            fade: function(val, el){
                var ob = Ext.get(el);
                if (val){
                    if (!ob.isVisible()){
                        ob.fadeIn({useDisplay:true});
                    }
                }else{
                    if (ob.isVisible()){
                        ob.fadeOut({useDisplay:true});
                    }
                }
            },
            slide: function(val, el){
                var ob = Ext.get(el);
                if (val){
                    if (!ob.isVisible()){
                        ob.slideIn('t', {useDisplay:true});
                    }
                }else{
                    if (ob.isVisible()){
                        ob.slideOut('t', {useDisplay:true});
                    }
                }
            },
            
            setRowBg: function(togle, rowid){
                var row = Ext.get(rowid);
                if (togle){
                    row.setStyle('background-color', '#E1FFE3');
                }else{
                    row.setStyle('background-color', '');
                }
            }            
        };
    }();

    Webi.format = function(){
        return {
            number: function(value){
                v = (Math.round((value-0)*100))/100;
                v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);

                v = String(v);
                var ps = v.split('.');
                var whole = ps[0];
                var sub = ps[1] ? ','+ ps[1] : ',00';
                var r = /(\d+)(\d{3})/;
                while (r.test(whole)) {
                    whole = whole.replace(r, '$1' + '.' + '$2');
                }
                v = whole + sub;
                if(v.charAt(0) == '-'){
                    return '-' + v.substr(1);
                }
                return "" +  v;
            }
        };
    }();
});

Webi.Server = function(config){
    config = config || {};

    Ext.apply(this, config);
    this.addEvents(
        'beforerequest',
        'success',
        'failure'
    );
    Webi.Server.superclass.constructor.call(this);
};

Ext.extend(Webi.Server, Ext.util.Observable, {
    showStatus: true,
    statusTitle: "Info Status",
    successMsg: "Data berhasil di proses",
    failureMsg: "Terjadi kesalahan pada operasi yang dilakukan",
    params: {},
    url: '',
    setParams: function(params){
        this.params = params;
        return this.params;
    },
    setParam: function(name, value){
        this.params[name] = value;
        return this.params;
    },
    getParams: function(){
        return this.params;
    },
    request: function(){
        if(this.fireEvent("beforerequest", this) !== false){
            Ext.Ajax.request({
               url: this.url,
               success: this.onSuccess,
               failure: this.onFailure,
               params: this.getParams(),
               scope: this
            });
        }
    },
    onSuccess: function(response, options){
        try{
            var data = Ext.decode(response.responseText);

            if (data.success && data.success == true){                
                this.fireEvent("success", this, data);
            }else{
                if (this.showStatus == true){
                    Ext.MessageBox.alert(this.statusTitle, data.msg || this.failureMsg);
                }
                this.fireEvent("failure", this, data);
            }
        }catch (e){
            if (e.name == 'SyntaxError'){
                this.onFailure(response, options);
            }else{
                throw e;
            }
        }
    },
    onFailure: function(response, options){
        this.fireEvent("failure", this, []);
        
        Ext.Msg.show({
            title:'Server Error',
            msg: ('Error Status : ' + response.statusText + '<br />Terjadi kesalahan pada operasi yang dilakukan, mohon hubungi administrator mengenai kesalahan ini'),
            buttons: Ext.Msg.OK,
            icon: Ext.MessageBox.ERROR,
            minWidth: 200
        });
    },
    setURL: function(url){
        this.url= url;  
    }
});
