﻿function $DL(func,priority) {
	if (!$DL._list) $DL._list=[];
	if ($DL._loaded) func.delay(1);
	if (priority===undefined) priority=5;
	$DL._list.push({func:func,priority:priority});
}
$DL.loadAll=function () {
	$DL._loaded=true;
	if (!$DL._list) return;

	$DL._list.sort(function (a,b) {
		return a.priority-b.priority;
	});
	
	$DL._list.each(function (o) {
		o.func.call(window);
	});
	delete $DL._list;
};

window.addEvent("domready",$DL.loadAll.bind($DL));
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;
}


$DL(function () { document.form=$("aspnetForm") || document.body; },0);

var QS={
	initialize:function () {
		QS.data=new Hash();
		location.search.substr(1).replace(/(.*?)=(.*?)(?:&|$)/g,function (match,key,value) {
			QS.data.set(key,decodeURIComponent(value));
		});
	},
	add:function (keyValueCollection) {
		if (!(keyValueCollection instanceof Hash)) keyValueCollection=new Hash(keyValueCollection);

		var newKvc=new Hash();
		newKvc.merge(keyValueCollection);
		newKvc.merge(QS.data);
		
		keyValueCollection.each(function (value,key) {
			if (value===null) newKvc.remove(key);
		});

		location.href="?"+newKvc.toQueryString();
	},
	remove:function (names) {
		for (var i=0;i<names.length;i++) QS.data.remove(names[i]);
		location.href="?"+QS.data.toQueryString();
	},
	get:function (key) {
		return QS.data.get(key);
	}
};

QS.initialize();

function h(o,funcs,showFuncContent) {
	if (o==null) return "[null]";
	var s=[];
	for (var i in o) {
		if (typeof o[i]=="function" && funcs) s.push(i+"\t\t"+(showFuncContent ? o[i] : o[i].toString().trim().substr(0,o[i].toString().search(/\r|\n|\{/))));
		else if (typeof o[i]!="function") s.push(i+"\t\t"+o[i]);
	}
	return "\r\n"+s.join("\r\n")+"\r\n";
}
function r() { alert($A(arguments).join("\n")); };

/* fix selects and labels on ie6 */
if (Browser.Engine.trident4) {
	$$("select").addEvents({
		focusin:function (e) {
			this.store("tempIndex",this.selectedIndex);
		},
		focus:function (e) {
			this.selectedIndex=this.retrieve("tempIndex");
		}
	});
}

// default failure for ajax requests
function $ajaxFailure() {
	r.apply(null,arguments);
	/*var err="";
	if (/<title>(.*?)<\/title>/.test(xh.responseText)) err=RegExp.$1;
	err=err.replace(/<.*?>/g,"");
	alert(err);*/
}
