 /**
  *  Ext overrides:
  *  Setup base params for standard Ext classes, 
  *  overrides standard methods.
  * 
  *  @author Seabor
  *  @version 1.0
  *  @class Ext.layout.BorderLayout
  *  @class Ext.util.Format
  *  @class Ext.Toolbar
  */
  
// Border layout override for collapsed wondow's title display
// {{{
Ext.override(Ext.layout.BorderLayout, { 
        southTitleAdded  : false, 
            // private 
        onLayout : function(ct, target){ 

            var collapsed; 
            if(!this.rendered){ 
                 
                target.position(); 
                target.addClass('x-border-layout-ct'); 
                var items = ct.items.items; 
                collapsed = []; 
                for(var i = 0, len = items.length; i < len; i++) { 
                    var c = items[i]; 
                    var pos = c.region; 
                    if(c.collapsed){ 
                        collapsed.push(c); 
                    } 
                    c.collapsed = false; 
                    if(!c.rendered){ 
                        c.cls = c.cls ? c.cls +' x-border-panel' : 'x-border-panel'; 
                        c.render(target, i); 
                    } 
                    this[pos] = pos != 'center' && c.split ? 
                        new Ext.layout.BorderLayout.SplitRegion(this, c.initialConfig, pos) : 
                        new Ext.layout.BorderLayout.Region(this, c.initialConfig, pos); 
                    this[pos].render(target, c); 
                } 
                this.rendered = true; 
            } 
     
            var size = target.getViewSize(); 
            if(size.width < 20 || size.height < 20){ // display none? 
                if(collapsed){ 
                    this.restoreCollapsed = collapsed; 
                } 
                return; 
            }else if(this.restoreCollapsed){ 
                collapsed = this.restoreCollapsed; 
                delete this.restoreCollapsed; 
            } 
     
            var w = size.width, h = size.height; 
            var centerW = w, centerH = h, centerY = 0, centerX = 0; 
     
            var n = this.north, s = this.south, west = this.west, e = this.east, c = this.center; 
            if(!c){ 
                throw 'No center region defined in BorderLayout ' + ct.id; 
            } 
     
            if(n && n.isVisible()){ 
                var b = n.getSize(); 
                var m = n.getMargins(); 
                b.width = w - (m.left+m.right); 
                b.x = m.left; 
                b.y = m.top; 
                centerY = b.height + b.y + m.bottom; 
                centerH -= centerY; 
                n.applyLayout(b); 
                
                //TDGi custom title for north 
                //new config options for south region: 
                //  collapsedTitle        : 'string' 
                //  collapsedTitleCls    :  'string' 
                //  collapsedTitleStyle :  'string' 
                if (typeof n.collapsedEl != 'undefined' && n.collapsedTitle && this.southTitleAdded == false) { 
                    this.southTitleAdded = true; 
                    var cDiv = n.collapsedEl; 
                    var tpl  = new Ext.Template('<div style="float: left;padding:3px;font-family:tahoma;font-weight:bold;font-size:11px;color:#444;">{txt}</div>'); 

                    var insertedHtml = tpl.insertFirst(cDiv,{ txt : n.collapsedTitle }); 
                    if (n.collapsedTitleStyle) { 
                        insertedHtml.applyStyles(n.collapsedTitleStyle); 
                    } 
                     
                    if (n.collapsedTitleCls) { 
                        Ext.get(insertedHtml).addClass(n.collapsedTitleCls); 
                    } 

                }    
            } 
            if(s && s.isVisible()){ 
                var b = s.getSize(); 
                var m = s.getMargins(); 
                b.width = w - (m.left+m.right); 
                b.x = m.left; 
                var totalHeight = (b.height + m.top + m.bottom); 
                b.y = h - totalHeight + m.top; 
                centerH -= totalHeight; 
                s.applyLayout(b); 

                //TDGi custom title for south 
                //new config options for south region: 
                //  collapsedTitle        : 'string' 
                //  collapsedTitleCls    :  'string' 
                //  collapsedTitleStyle :  'string' 
                if (typeof s.collapsedEl != 'undefined' && s.collapsedTitle && this.southTitleAdded == false) { 
                    this.southTitleAdded = true; 
                    var cDiv = s.collapsedEl; 
                    var tpl  = new Ext.Template('<div style="float: left;">{txt}</div>'); 

                    var insertedHtml = tpl.insertFirst(cDiv,{ txt : s.collapsedTitle }); 
                    if (s.collapsedTitleStyle) { 
                        insertedHtml.applyStyles(s.collapsedTitleStyle); 
                    } 
                     
                    if (s.collapsedTitleCls) { 
                        Ext.get(insertedHtml).addClass(s.collapsedTitleCls); 
                    } 

                }                 
            } 
            if(west && west.isVisible()){ 
                var b = west.getSize(); 
                var m = west.getMargins(); 
                b.height = centerH - (m.top+m.bottom); 
                b.x = m.left; 
                b.y = centerY + m.top; 
                var totalWidth = (b.width + m.left + m.right); 
                centerX += totalWidth; 
                centerW -= totalWidth; 
                west.applyLayout(b); 
            } 
            if(e && e.isVisible()){ 
                var b = e.getSize(); 
                var m = e.getMargins(); 
                b.height = centerH - (m.top+m.bottom); 
                var totalWidth = (b.width + m.left + m.right); 
                b.x = w - totalWidth + m.left; 
                b.y = centerY + m.top; 
                centerW -= totalWidth; 
                e.applyLayout(b); 
            } 
     
            var m = c.getMargins(); 
            var centerBox = { 
                x: centerX + m.left, 
                y: centerY + m.top, 
                width: centerW - (m.left+m.right), 
                height: centerH - (m.top+m.bottom) 
            }; 
            c.applyLayout(centerBox); 
     
            if(collapsed){ 
                for(var i = 0, len = collapsed.length; i < len; i++){ 
                    collapsed[i].collapse(false); 
                } 
            } 
     
            if(Ext.isIE && Ext.isStrict){ // workaround IE strict repainting issue 
                target.repaint(); 
            } 
        } 
    }); // }}}

// ruMoney format
// {{{
Ext.apply(Ext.util.Format, {
    ruMoney: function(v){
            v = (Math.round((v-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 + "<span style=\"color:#974;\"> руб.</span>";
        }
}); // }}}

// euMoney format
// {{{
Ext.apply(Ext.util.Format, {
    euMoney: function(v){
            v = (Math.round((v-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 "<span style=\"color:#974;\">€ </span>" + v ;
        }
}); // }}}

// usMoney format
// {{{
Ext.apply(Ext.util.Format, {
    usMoney: function(v){
            v = (Math.round((v-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 "<span style=\"color:#974;\">$ </span>" + v ;
        }
}); // }}}

// noMoney format
// {{{
Ext.apply(Ext.util.Format, {
    noMoney: function(v){
            v = (Math.round((v-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;
        }
}); // }}}

// Toolbar base params
// {{{
Ext.apply(Ext.Toolbar, {
    height: 28
    ,cls: 'x-toolbar-tab'
    ,style: 'padding-top:3px;padding-left:3px'
}); // }}}

 /**
  *  Remove all listeners for specified event name
  */
Ext.apply(Ext.util.Observable.prototype, {
    purgeEventListeners: function(eventName) {
        var ce = this.events[eventName.toLowerCase()];
        if(typeof ce == "object"){
			ce.clearListeners();
    	}
    }
});

//Чтобы при изменении данных в сторе выполнялся refresh вместо onUpdate
Ext.override(Ext.DataView, 
       {setStore : function(store, initial){
        if(!initial && this.store){
            this.store.un("beforeload", this.onBeforeLoad, this);
            this.store.un("datachanged", this.refresh, this);
            this.store.un("add", this.onAdd, this);
            this.store.un("remove", this.onRemove, this);
            this.store.un("update", this.onUpdate, this);
            this.store.un("clear", this.refresh, this);
        }
        if(store){
            store = Ext.StoreMgr.lookup(store);
            store.on("beforeload", this.onBeforeLoad, this);
            store.on("datachanged", this.refresh, this);
            store.on("add", this.refresh, this);
            store.on("remove", this.refresh, this);
            store.on("update", this.refresh, this);
            store.on("clear", this.refresh, this);
        }
        this.store = store;
        if(store){
            this.refresh();
        }
    }});

// Значение по умолчанию в prompt
Ext.apply(Ext.Msg, 
    {prompt: function(title, msg, fn, v, multiline, scope){
	   this.show({
	       title : title,
	       msg : msg,
	       buttons: this.OKCANCEL,
	       fn: fn,
	       minWidth:250,
	       scope : scope,
	       prompt:true,
	       multiline: multiline,
	       value: v
	   });
	   return this;
}});
