var jsLoadCount = 0;
var jsOnLoadScript = '';

var jq = jQuery.noConflict();

function ajaxStdClb( msg) {
	if (!msg) {
		return( false);
	}
	if (jq( 'htmlCleanup', msg).length > 0) {
		htmlCleanup();
	}
	jsOnLoadScript = jq( 'onload', msg).text();
	var head = document.getElementsByTagName( 'head')[0];
	var inlineJs = jq( 'inlineJs', msg).text();
	if (inlineJs != '') {
		var script = document.createElement( 'script');
		script.type = 'text/javascript';
		script.appendChild( document.createTextNode( inlineJs));
		head.appendChild( script);
	}
	
	var el = jq( 'js', msg);
	var scripts = document.getElementsByTagName( 'script');
	var get = [];
	for (var i=0; i<el.length; i++) {
		var add = true;
		for (var j=0; j<scripts.length; j++) {
			if (scripts[j].src.replace( window.location.protocol+'//'+window.location.host, '').indexOf( el[i].firstChild.nodeValue) >= 0) {
				add = false;
				break;
			}
		}
		if (add) {
			get.push( el[i].firstChild.nodeValue);
		}
	}
	if (get.length > 0) {
		extMerge( get, 'js');
	}
		
	var el = jq( 'css', msg);
	var css = document.getElementsByTagName( 'link');
	var get = [];
	for (var i=0; i<el.length; i++) {
		var add = true;
		for (var j=0; j<css.length; j++) {
			if (css[j].href.replace( window.location.protocol+'//'+window.location.host, '').indexOf( el[i].firstChild.nodeValue) >= 0) {
				add = false;
				break;
			}
		}
		if (add) {
//			jq(head).append( '<link type="text/css" rel="stylesheet" href="'+el[i].firstChild.nodeValue+'"></link>');
			get.push( el[i].firstChild.nodeValue)
		}
	}
	if (get.length > 0) {
		extMerge( get, 'css');
	}
	var el = jq( 'inlineCss', msg);
	if (el != '') {
		var style = document.createElement( 'style');
		style.type = 'text/css';
		style.rel = 'stylesheet';
		style.appendChild( document.createTextNode( el));
		head.appendChild( style);
	}
	var el = jq( 'replace', msg);
	for (var i=0; i<el.length; i++) {
		if (jq(el[i]).attr( 'el') == 'title') {
			document.title = jq(el[i]).text();
			continue;
		}
		var o = jq( jq(el[i]).attr( 'el'));
		if (o.length > 0) {
			o.replaceWith( jq(el[i]).text());
		}
	}
	if (jsLoadCount == 0 && jsOnLoadScript != '') {
		jsLoaded();
	}
	jq('.loading').remove();
	if (typeof documentReady == 'function') {
		documentReady();
	}
}
function extMerge( list, type) {
	var limit = 1000;
	var get = '';
	var paths = [];
	for (var i=0; i<list.length; i++) {
		get += list[i]+',';
		if (get.length > limit) {
//			extMergePath( get, type);
			paths.push( get);
			get = '';
		}
	}
	if (get != '') {
//		extMergePath( get, type);
		paths.push( get);
	}
	if (type == 'js') {
		jsLoadCount += paths.length;
	}
	for (var i=0; i<paths.length; i++) {
		extMergePath( paths[i], type);
	}
}
function extMergePath( path, type) {
	var head = document.getElementsByTagName( 'head')[0];
	if (appVer) {
		path = appVer+','+path;
	}
//	alert( path);
	if (type == 'css') {
		jq(head).append( '<link type="text/css" rel="stylesheet" href="/ext/,'+path+'"></link>');
	} else if (type == 'js') {
		var scr = document.createElement( 'script');
		head.appendChild( scr);
		scr.type = 'text/javascript';
		scr.src = '/ext/,'+path;
		scr.onload = scr.onreadystatechange = function(){
		    if ((!this.readyState ||
		    this.readyState == "loaded" || this.readyState == "complete") ) {
		        jsLoaded();
		
		        // Handle memory leak in IE
		        scr.onload = scr.onreadystatechange = null;
		    }
		};		// most browsers
	}
}
function jsLoaded() {
	if (jsLoadCount > 0) {
		--jsLoadCount;
	}
	if (jsLoadCount > 0) {
		return( true);
	}
	if (jsOnLoadScript != '') {
		jq(document).append( '<script type="text/javascript">'+jsOnLoadScript+'</script>');
		jsOnLoadScript = '';
	}
}
function setLoading( container) {
	if (!container.height()) {
		return( true);
	}
	jq('<div class="loading"><a href="#" onclick="jq(this.parentNode).remove();return(false);">X</a></div>').prependTo( container).width( container.width()).height( container.height());
}
function formData( container) {
	var data = {};
	jq(':input', container).each( function() {
		if (this.type && (this.type == 'radio' || this.type == 'checkbox') && ! this.checked) {
			return( true);
		}
		data[this.name] = this.value;
	});
	return( data);
}

