

//CAPTURE FOR DIALOG
//TODO: to je samo prvi test... preveriti, FF, ....

Ext.BasicDialog.prototype.startMove = function()
{
    this.el.dom.setCapture();
    if(this.proxyDrag){
        this.proxy.show();
    }       
        
    if(this.constraintoviewport !== false){
        this.dd.constrainTo(document.body, {right: this.shadowOffset, bottom: this.shadowOffset});
    }
}
    
Ext.BasicDialog.prototype.endMove = function()
{
    this.el.dom.releaseCapture();
    if(!this.proxyDrag){
        Ext.dd.DD.prototype.endDrag.apply(this.dd, arguments);
    }else{
        Ext.dd.DDProxy.prototype.endDrag.apply(this.dd, arguments);
        this.proxy.hide();
    }
    this.refreshSize();
    this.adjustAssets();
    this.focus();
    this.fireEvent("move", this, this.xy[0], this.xy[1]);
}


Ext.lib.Ajax.getPath = function(url)
{
    paramIndex = url.indexOf("?");

    if (paramIndex != -1)
        url = url.substring(0, paramIndex);
    pathIndex = url.lastIndexOf("/");
    return url.substring(0, pathIndex + 1);
}

Ext.lib.Ajax.proxyUri = applicationSettings.proxyUri; //"/rwproxy/RWProxy.ashx";

Ext.lib.Ajax.asyncRequest  = function(method, uri, callback, postData)
{
    var o = this.getConnectionObject();
    
    if (!o) {
        return null;
    }
    else 
    { 
        //TODO absoluten uri se lahko zacne tudi drugace (glej document.location.protocol)
        //TODO absoluten naslov je lahko na isti host - ni treba klicat proxy 
        if(uri.toLowerCase().startsWith('http'))  //absolutno
        { 
            uri = Ext.lib.Ajax.proxyUri + "?method=" + method + "&remoteUrl=" + escape(uri);
        }
        
        
        o.conn.open(method, uri, true);

        if (this.useDefaultXhrHeader) {
            if (!this.defaultHeaders['X-Requested-With']) {
                this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
            }
        }

        if(postData && this.useDefaultHeader){
            this.initHeader('Content-Type', this.defaultPostHeader);
        }

         if (this.hasDefaultHeaders || this.hasHeaders) {
            this.setHeader(o);
        }

        this.handleReadyState(o, callback);
        o.conn.send(postData || null);

        return o;
    }
}

Ext.tree.AsyncTreeNode = function(config){
    this.loaded = false;
    this.loading = false;
    this.xloader = config.xloader || null;
    
    Ext.tree.AsyncTreeNode.superclass.constructor.apply(this, arguments);
    /**
    * @event beforeload
    * Fires before this node is loaded, return false to cancel
    * @param {Node} this This node
    */
    this.addEvents({'beforeload':true, 'load': true});
    /**
    * @event load
    * Fires when this node is loaded
    * @param {Node} this This node
    */
    /**
     * The loader used by this node (defaults to using the tree's defined loader)
     * @type TreeLoader
     * @property loader
     */
};


Ext.extend(Ext.tree.AsyncTreeNode, Ext.tree.TreeNode, {
    expand : function(deep, anim, callback){
                        
        if(this.loading){ // if an async load is already running, waiting til it's done
            var timer;
            var f = function(){
                if(!this.loading){ // done loading
                    clearInterval(timer);
                    this.expand(deep, anim, callback);
                }
            }.createDelegate(this);
            timer = setInterval(f, 200);
            return;
        }
        if(!this.loaded){
            if(this.fireEvent("beforeload", this) === false){
                return;
            }
            this.loading = true;
            this.ui.beforeLoad(this);
            var loader = this.xloader || this.loader || this.attributes.loader || this.getOwnerTree().getLoader();
           
            if(loader){
                loader.load(this, this.loadComplete.createDelegate(this, [deep, anim, callback]));
                return;
            }
        }
        Ext.tree.AsyncTreeNode.superclass.expand.call(this, deep, anim, callback);
    },
    
    /**
     * Returns true if this node is currently loading
     * @return {Boolean}
     */
    isLoading : function(){
        return this.loading;  
    },
    
    loadComplete : function(deep, anim, callback){
        this.loading = false;
        this.loaded = true;
        this.ui.afterLoad(this);
        this.fireEvent("load", this);
        this.expand(deep, anim, callback);
    },
    
    /**
     * Returns true if this node has been loaded
     * @return {Boolean}
     */
    isLoaded : function(){
        return this.loaded;
    },
    
    hasChildNodes : function(){
        if(!this.isLeaf() && !this.loaded){
            return true;
        }else{
            return Ext.tree.AsyncTreeNode.superclass.hasChildNodes.call(this);
        }
    },

    /**
     * Trigger a reload for this node
     * @param {Function} callback
     */
    reload : function(callback){
        this.collapse(false, false);
        while(this.firstChild){
            this.removeChild(this.firstChild);
        }
        this.childrenRendered = false;
        this.loaded = false;
        if(this.isHiddenRoot()){
            this.expanded = false;
        }
        this.expand(false, false, callback);
    }
});
    


Ext.util.JSON = new (function(){
    var useHasOwn = {}.hasOwnProperty ? true : false;
    
    
    
    
    var pad = function(n) {
        return n < 10 ? "0" + n : n;
    };
    
    var m = {
        "\b": '\\b',
        "\t": '\\t',
        "\n": '\\n',
        "\f": '\\f',
        "\r": '\\r',
        '"' : '\\"',
        "\\": '\\\\'
    };

    var encodeString = function(s){
        if (/["\\\x00-\x1f]/.test(s)) {
            return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                var c = m[b];
                if(c){
                    return c;
                }
                c = b.charCodeAt();
                return "\\u00" +
                    Math.floor(c / 16).toString(16) +
                    (c % 16).toString(16);
            }) + '"';
        }
        return '"' + s + '"';
    };
    
    var encodeArray = function(o){
        var a = ["["], b, i, l = o.length, v;
            for (i = 0; i < l; i += 1) {
                v = o[i];
                switch (typeof v) {
                    case "undefined":
                    case "function":
                    case "unknown":
                        break;
                    default:
                        if (b) {
                            a.push(',');
                        }
                        a.push(v === null ? "null" : Ext.util.JSON.encode(v));
                        b = true;
                }
            }
            a.push("]");
            return a.join("");
    };
       
    var encodeDate = function(o){  
        var encodedDateZone="";
        var h=o.getTimezoneOffset();
        if(h<0)
            encodedDateZone="+" + pad(Math.abs(parseInt(h/60)));
        else
            encodedDateZone="-" + pad(parseInt(h/60));
        encodedDateZone+=":"+pad(Math.abs(h%60));   
              
        return '"' + o.getFullYear() + "-" +
                pad(o.getMonth() + 1) + "-" +
                pad(o.getDate()) + "T" +
                pad(o.getHours()) + ":" +
                pad(o.getMinutes()) + ":" +
                pad(o.getSeconds()) + "." +
                pad(o.getMilliseconds()) +
                encodedDateZone + '"';
    };
    
    
    this.encode = function(o){
        if(typeof o == "undefined" || o === null){
            return "null";
        }else if(o instanceof Array){
            return encodeArray(o);
        }else if(o instanceof Date){
            return encodeDate(o);
        }else if(typeof o == "string"){
            return encodeString(o);
        }else if(typeof o == "number"){
            return isFinite(o) ? String(o) : "null";
        }else if(typeof o == "boolean"){
            return String(o);
        }else {
            var a = ["{"], b, i, v;
            for (i in o) {
                if(!useHasOwn || o.hasOwnProperty(i)) {
                    v = o[i];
                    switch (typeof v) {
                    case "undefined":
                    case "function":
                    case "unknown":
                        break;
                    default:
                        if(b){
                            a.push(',');
                        }
                        a.push(this.encode(i), ":",
                                v === null ? "null" : this.encode(v));
                        b = true;
                    }
                }
            }
            a.push("}");
            return a.join("");
        }
    };
    
    
    this.decode = function(json){
        return eval("(" + json + ')');
    };
    
})();

Ext.encode = Ext.util.JSON.encode;
Ext.decode = Ext.util.JSON.decode;



Ext.form.HtmlEditor.prototype.fixKeys = function() // Htmleditor v IE vstavlja <br/> namesto odstavkov <p>, za kar ne vidimo vzroka, nas pa moti.
{ 
    if(Ext.isIE){
        return function(e){
            var k = e.getKey(), r;
            if(k == e.TAB){
                e.stopEvent();
                r = this.doc.selection.createRange();
                if(r){
                    r.collapse(true);
                    r.pasteHTML('&nbsp;&nbsp;&nbsp;&nbsp;');
                    this.deferFocus();
                }
            }
        };
    }else if(Ext.isOpera){
        return function(e){
            var k = e.getKey();
            if(k == e.TAB){
                e.stopEvent();
                this.win.focus();
                this.execCmd('InsertHTML','&nbsp;&nbsp;&nbsp;&nbsp;');
                this.deferFocus();
            }
        };
    }else if(Ext.isSafari){
        return function(e){
            var k = e.getKey();
            if(k == e.TAB){
                e.stopEvent();
                this.execCmd('InsertText','\t');
                this.deferFocus();
            }
         };
    }
}();

Ext.Button.prototype.onClick=function(e)
{
	if (e)
	{
		e.preventDefault();
	}
	if ((!Ext.isOpera && e.button != 0 ) || (Ext.isOpera && e.button!=1))
	{
		return;
	}
	if (!this.disabled)
	{
		if (this.enableToggle)
		{
			this.toggle();
		}
		if (this.menu && !this.menu.isVisible())
		{
			this.menu.show(this.el, this.menuAlign);
		}
		this.fireEvent("click", this, e);
		if (this.handler)
		{
			this.el.removeClass("x-btn-over");
			this.handler.call(this.scope || this, this, e);
		}
	}
}

