//*** Javascript overrides
Ext.applyIf(Date, {
	getLongMonthName : function(month) {
		var months = [
			'January', 
			'February', 
			'March', 
			'April', 
			'May', 
			'June', 
			'July', 
			'August', 
			'September', 
			'October', 
			'November', 
			'December'
		];
		return months[month];
	},
	getShortMonthName : function(month) {
		var months = [
			'Jan', 
			'Feb', 
			'Mar', 
			'Apr', 
			'May', 
			'Jun', 
			'Jul', 
			'Aug', 
			'Sep', 
			'Oct', 
			'Nov', 
			'Dec'
		];
		return months[month];
	}
});

Ext.applyIf(Math, {
    /**
     * extend Math class with a randRange method
     * @return {Number} A random number greater than or equal to min and less than or equal to max.
     */
    randRange : function(min, max) {
      	return Math.max(Math.min(Math.round(Math.random() * max), max), min);
    }
});
/**
 * @class Number
 */
Ext.applyIf(Number.prototype, {
    /**
     * extend Number class with a tailored hex-string method (note toString(16) is implementation-dependant, and in IE returns signed numbers when used on full words)
     * @return {String} The number in Hexidecimal format.
     */
    toHexStr : function(){
        var s = '', v;
        for(var i = 7; i >= 0; i--) {
            v = (this >>> (i * 4)) & 0xf;
            s += v.toString(16);
        }
        return s;
    }
});



//*** Ext.js overrides
/*
Ext.ux.clone = function(obj) {
   if(obj == null || typeof(obj) != 'object') return obj;
   if (Ext.isDate(obj)) return obj.clone();      
   var cloneArray = function(arr) {
      var len = arr.length;
      var out = [];
      if (len > 0) {
         for (var i = 0; i < len; ++i)
            out[i] = Ext.ux.clone(arr[i]);
      }
      return out;      
   };      
   var c = new obj.constructor();
   for (var prop in obj) {
      var p = obj[prop];
      if (Ext.isArray(p))
         c[prop] = cloneArray(p);
      else if (typeof p == 'object')
         c[prop] = Ext.ux.clone(p);
      else
         c[prop] = p;
   }
   return c;
};
*/
/**
 * Apply Function
 */
Ext.ns('Ext.ux.util');

Ext.util.DelayedTask = function(fn, scope, args) {
    var id = null;
    var call = function() {
        id = null;
        fn.apply(scope, args || []);
    };
    this.delay = function(delay, newFn, newScope, newArgs) {
        if (id) {
            this.cancel();
        }
        fn = newFn || fn;
        scope = newScope || scope;
        args = newArgs || args;
        if (!id) {
            id = setTimeout(call, delay);
        }
    };
    this.cancel = function() {
        if (id) {
            clearTimeout(id);
            id = null;
        }
    };
};
Ext.applyIf(Ext, {
	boolean: function(value) {
		if (Ext.isEmpty(value)) return false;
		if (Ext.type(value) == 'boolean') return value;
		if (Ext.type(value) == 'string') return value.toBoolean();
		if (Ext.type(value) == 'number') return value.toString().toBoolean();
		return false;
	}
});


Ext.override(Ext.Element, {
	enable: function(enabled) {
		switch (this.getTag()) {
			case 'input': case 'select': case 'textarea': case 'button': 
				this.dom.disabled = !Ext.boolean(enabled); 
				break;
			default:
				break;
		}
		return this;
	},
	getAttribute: function(v) {
		return this.dom.getAttribute(v);
	},
	getHtml: function() {
		return this.dom.innerHTML;
	},
	getTag: function() {
		return this.dom.tagName.lower();
	},
	getValue: function() {
		var value = this.dom.value.removeLineFeeds();
		return !Ext.isEmpty(value, true) ? String.escape(value) : '';
	},
	isEnabled: function() {
		switch (this.getTag()) {
			case 'input': case 'select': case 'textarea': case 'button': 
				return !this.dom.disabled; 
			default:
				return false;
		}
	},
	removeAttribute: function(v) {
		this.dom.removeAttribute(v);
	},
	removeClasses: function() {
		if (Ext.isIE) {
			var node = this.dom.getAttributeNode('class');
			this.dom.removeAttributeNode(node);
		} else {
			this.dom.removeAttribute('class');
		}
		this.dom.className = '';
		return this;
	},
	removeStyles: function() {
		if (Ext.isIE) {
			var node = this.dom.getAttributeNode('style');
			this.dom.removeAttributeNode(node);
		} else {
			this.dom.removeAttribute('style');
		}
		return this;
	},
	setAttributeNS : function(ns, att, value) { 
        if (this.dom.setAttribute) { 
            this.dom.setAttribute(ns + ":" + att, value); 
        } 
    },
	setValue: function(v) {
		this.dom.value = v.stripEscapes();
		return this;
	},
	setVisible : function(visible, animate){
		var El = Ext.Element;
		var A = Ext.lib.Anim;
        if(!animate || !A || Ext.isIE){
            if(this.visibilityMode == El.DISPLAY){
                this.setDisplayed(visible);
            }else{
                this.fixDisplay();
                this.dom.style.visibility = visible ? "visible" : "hidden";
            }
        }else{
            // closure for composites
            var dom = this.dom;
            var visMode = this.visibilityMode;
            if(visible){
                this.setOpacity(.01);
                this.setVisible(true);
            }
            this.anim({opacity: { to: (visible?1:0) }},
                  this.preanim(arguments, 1),
                  null, .35, 'easeIn', function(){
                     if(!visible){
                         if(visMode == El.DISPLAY){
                             dom.style.display = "none";
                         }else{
                             dom.style.visibility = "hidden";
                         }
                         Ext.get(dom).setOpacity(1);
                     }
                 });
        }
        return this;
    }
});

Ext.override(Ext.CompositeElementLite, {
	enable: function(enabled) {
		this.each(function(el){
			el.enable(enabled); 
		});
		return this;
	},
	getValues: function() {
		var values = [];
		this.each(function(el){
			values.push(el.getValue()); 
		});
		return values;
	},
	removeAttribute: function(v) {
		this.each(function(el){
			el.removeAttribute(v); 
		});
		return this;
	},
	removeClasses: function() {
		this.each(function(el){
			el.removeClasses(); 
		});
		return this;
	},
	removeStyles: function() {
		this.each(function(el){
			el.removeStyles(); 
		});
		return this;
	},
	setValue : function(v) {
		this.each(function(el){
			el.setValue(v);
		});
		return this;
	}
});

//*** required

function $(e) {	
	return (Ext.get(e)) ? Ext.get(e) : Ext.get(Ext.DomQuery.select('*[name='+e+']')[0]);
}
function $$(e, f) {
	return (Ext.isEmpty(f)) ? Ext.select(e) : Ext.select(e, f);
}
function $Q(e, n) {
	return (Ext.isEmpty(n)) ? Ext.query(e) : Ext.query(e, n);
}
function $F(c, element, where) {
	var form = undefined;
	if (Ext.type(c) == 'object' || (Ext.type(c) == 'object' && (element || (element && where)))) {
		form = P.createForm(c, element, where);
	} else {
		form = Ext.get(c) ? Ext.get(c) : Ext.get(Ext.DomQuery.select('form[name='+c+']')[0]);
	}
	return form;
}

//-- P
Ext.namespace('P');

P.Delegate = {
	create : function(scope, fn, argz) {
		return fn.createDelegate(scope, argz);
	}
};

