function $DL(f){
	if ($DL.loaded) f();
	else window.addEvent('domready',f);
}
onDomReady(function () { $DL.loaded=true; });

$DL(function () { document.form=$("aspnetForm") || document.forms[0]; });

function addNamespace(ns) {
	if (!ns) return null;
	var levels=ns.split(".");
	var root=window;
	for (var i=0;i<levels.length;i++) {
		if (root[levels[i]]==undefined) root[levels[i]]={};
		root[levels[i]].__namespace=true; // for ms ajax
		root=root[levels[i]];
	}
	if (root[levels[0]] && window.__rootNamespaces) window.__rootNamespaces.push(root[levels[0]]); // for ms ajax
	return root;
}

function returnFalse() { return false; }

// fixes ms ajax errors
Array._forEach=Array.forEach;
function $each(iterable, fn, bind){
	if (iterable && typeof iterable.length == 'number' && $type(iterable) != 'object') Array._forEach(iterable, fn, bind);
	else for (var name in iterable) fn.call(bind || iterable, iterable[name], name);
};

Element.extend({
	show:function () {
		this.setStyle("display","");
		return this;
	},
	hide:function () {
		this.setStyle("display","none");
		return this;
	},
	setVisibilty:function (visible) {
		this.setStyle("visibility",visible ? "" : "hidden");
		return this;
	},
	visible:function () {
		var p=this;
		return p && p!=document.body && p.getStyle("display")!="none";
	},

	toggle:function () {
		this.setStyle("display",this.getStyle("display")=="none" ? "" : "none");
	},

	getTextContent:function () {
		return this.innerText || this.textContent;
	},
	setTextContent:function (value) {
		this.empty().appendText(value);
		return this;
	},

	hover:function (over,out) {
		this.addEvent("mouseenter",over);
		this.addEvent("mouseleave",out);
		return this;
	},

	setSelection:function (b) {
		if (window.ie || window.opera) this[(b?"remove":"add")+"Event"]("selectstart",returnFalse);
		if (window.gecko) this.style.MozUserSelect=b?"":"none";
		if (window.webkit) this.style.KhtmlUserSelect=b?"":"none";
		return this;
	},

	removeTextNodes:function () {
		if (this._removedTextNodes) return;
		$A(this.childNodes).each(function (o) {
			if (o.nodeType===3) {
				if (/^\s+$/.test(o.data)) o.parentNode.removeChild(o);
			}
			else $(o).removeTextNodes();
		});
		this._removedTextNodes=true;
		return this;
	},

	removeChildren:function () {
		while (this.hasChildNodes()) this.removeChild(this.lastChild);
		return this;
	},

	setPosition:function (p) {
		if (typeof(p)=="object") {
			x=p.x;
			y=p.y;
		}
		else {
			x=arguments[0];
			y=arguments[1];
		}
		this.setStyles({left:x,top:y});
		return this;
	},

	setCoordinates:function (c) {
		return this.setStyles({left:c.left,top:c.top,width:c.width,height:c.height});
	},

	findParent:function (predicate) {
		var el=this;
		while (!predicate($(el)) && el && el!=document.documentElement) el=el.parentNode;
		return el===document.documentElement ? null : el;
	}
});

String.extend({
	escapeHtml:function (isHtml) {
		var str=this;
		if (!isHtml) str=str.replace(/>/g,"&gt;").replace(/</g,"&lt;");
		else str=str.replace(/\r?\n/g,"<br/>");
		str=str.replace(/"/g,"&quot;").replace(/'/g,"&#39;");
		return str;
	},

	formatNames:function(){

	},

	format:function(){
		 var s = arguments[0];
		 var reg;
		 for (var i = 0,len=arguments.length;i<len;i++){
			reg = new RegExp("\\{"+i+"\\}","gm");
			s = s.replace(reg,arguments[i+1]);
		 }
		 return s;
	}

});

String.prototype.format = function(){
	var s = this;
	var reg;
	for (var i = 0,len=arguments.length;i<len;i++){
		reg = new RegExp("\\{"+i+"\\}","gm");
		s = s.replace(reg,arguments[i]);
	}
	return s;
}

function list(o,inside){
	var s = "";
	if (typeof(o)=="undefined")s="undefined";
	if (o==null)s="NULL";
	for(var i in o){
		s+= ( ((inside)?("\t"):("")) + i + "=" + o[i] + "\n");
		if (typeof(o[i])=="object" && o[i]!=null){
			s+=list(o[i],1);
		}
	}
	return s;
}


function alertToPopup(o){
	var s = list(o);
	var w = window.open("","","");

	w.document.open();
	w.document.write( "<xmp>"+s+"</xmp>");
	w.document.close();
}


Object.inspect=function (o,funcs,showFuncContent) {var s=[];for (var i in o) {if (typeof o[i]=="function" && funcs) s.push(i+"\t\t"+(showFuncContent ? o[i] : o[i].toString().substr(0,o[i].toString().search(/[\n\{]/))));else if (typeof o[i]!="function") s.push(i+"\t\t"+o[i]);}return "\r\n"+s.join("\r\n")+"\r\n";};
var h=Object.inspect;


Fx.Morph = Fx.Styles.extend({
	start: function(className){
		var to = {};

		$each(document.styleSheets, function(style){
			var rules = style.rules || style.cssRules;
			$each(rules, function(rule){
				if (!rule.selectorText.test('\.' + className + '$')) return;
				Fx.CSS.Styles.each(function(style){
					if (!rule.style || !rule.style[style]) return;
					var ruleStyle = rule.style[style];
					to[style] = (style.test(/color/i) && ruleStyle.test(/^rgb/)) ? ruleStyle.rgbToHex() : ruleStyle;
				});
			});
		});
		return this.parent(to);
	}

});

Fx.CSS.Styles = ["backgroundColor", "backgroundPosition", "color", "width", "height", "left", "top", "bottom", "right", "fontSize", "letterSpacing", "lineHeight", "textIndent", "opacity"];

Fx.CSS.Styles.extend(Element.Styles.padding);
Fx.CSS.Styles.extend(Element.Styles.margin);

Element.Styles.border.each(function(border){
	['Width', 'Color'].each(function(property){
		Fx.CSS.Styles.push(border + property);
	});
});
