// (c) Benoît PIN 2006 
// http://plinn.org
// Licence GPL

function FragmentImporter(url, onAfterPopulate, baseElement) {
	var thisImporter = this;
	this.url = url;
	this.onAfterPopulate = (!onAfterPopulate) ? function(){return;} : onAfterPopulate;
	this.baseElement = baseElement;
	//if (baseElement && window.console)
	//	console.warn('Deprecation warning : usage of baseElement will be removed. Use an xml based response.');
}

FragmentImporter.prototype._load = function(url) {
	var req = new XMLHttpRequest();
	var thisImporter = this;
	req.onreadystatechange = function() {
		switch (req.readyState) {
			case 1 :
				// éventuellement fonction qui affiche une barre de progression
				showProgressImage();
				break;
			case 4 :
				// idem pour masquer
				hideProgressImage();
				if (req.status == '200')
					thisImporter.populateBaseElement(req);
				else
					alert('Error: ' + req.status);
				
		};
	};

	//req.open("GET", url, true);
		req.open("POST", url, true);
		req.send(null);
};

FragmentImporter.prototype.load = function() {
	this._load(this.url);
};

FragmentImporter.prototype.useMacro = function(template, macro, fragmentId) {
	this._load(this.url + "/use_macro?template=" + template + "&macro=" + macro + "&fragmentId=" + fragmentId);
};


FragmentImporter.prototype.populateBaseElement = function(req) {
	// :( IE : innerHTML is read-only for these tags:
	// COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR

	if (req.getResponseHeader('Content-Type').indexOf('text/html;charset=utf-8') != -1) {
		var fragments = req.responseXML.documentElement.childNodes;
		var fragment, dest, scripts;
		for (var i=0 ; i < fragments.length ; i++) {
			fragment = fragments[i];
			if (fragment.nodeName == 'fragment') {
				dest = document.getElementById(fragment.getAttribute('id'));
				dest.innerHTML = fragment.firstChild.nodeValue;
				
				scripts = dest.getElementsByTagName('script');
				for (var j=0 ; j < scripts.length ; j++)
					globalScriptRegistry.loadScript(scripts[j]);
			}
		}
	}
	else {
		this.baseElement.innerHTML = req.responseText;
		
//		var scripts = this.baseElement.getElementsByTagName('script');
//		for (var i=0 ; i < scripts.length ; i++)
//			globalScriptRegistry.loadScript(scripts[i]);
	}

	var onAfterPopulate = this.onAfterPopulate;
	if (typeof(onAfterPopulate) == "string") {
//		if (console)
//			console.warn('Deprecation warning : onAfterPopulate may not be a string (' + onAfterPopulate + ')');
		onAfterPopulate = eval(onAfterPopulate);
	}
	onAfterPopulate();
}

function showProgressImage() {
	try {
		var progressImageDiv = document.getElementById("progressImgDiv");
		progressImageDiv.style.visibility = 'visible';

		//var progressImage = document.getElementById("progressImg");
		//progressImage.src = 'templates/common/icons/indicator.gif';

	} catch (e) {}
}

function hideProgressImage() {
	try {
		//var progressImage = document.getElementById("progressImg");
		//progressImage.src = 'templates/common/icons/indicator_ok.gif';

		var progressImageDiv = document.getElementById("progressImgDiv");
		progressImageDiv.style.visibility = 'hidden';
	} catch (e) {}
}