/**
 * Receive data and put it in the given DOM node
 * 
 * @param string data
 * @param DOM node
 */
function receive_ajax(data, node) {
	if(node != null){
		node.innerHTML = data; 
	
		var All = node.getElementsByTagName("*");
		for (var i=0; i<All.length; i++) {
			
			// avoid setting id and name when the attribute doesn't exist
			if(All[i].getAttribute("id") != '') {
				All[i].id = All[i].getAttribute("id");
			}
			if(All[i].getAttribute("name") != '') {
				All[i].name = All[i].getAttribute("name");
			}
		}
		
		var AllScripts = node.getElementsByTagName("script");
		for (var i=0; i<AllScripts.length; i++) {
			var s = AllScripts[i];
			
			if (s.src && s.src!="") {
				// Precedement asynchrone, mis en synchrone pour eviter des problemes de dependances de scripts
				AjaxGetData(s.src, eval) ;
			} else {
				// In some cases the cdata tags are in the js data, to ensure
				// there are no js errors we ensure these tags are removed wherever
				// they are found in the code. (the reason for their presence is 
				// not known, but should be found).
				if (-1 == s.innerHTML.search('google-analytics.com')) {
					eval(s.innerHTML.replace(/(<!\[CDATA\[)|(\]\]>)/g, ''));
				}
				
			}
		}
	}
}

/**
 * Callback for the Virtual Library
 * Basically, do the same than receive_ajax, just adds a check to ensure
 * that the returned data is not the disclaimer page.
 */
function callbackLibrary(data, node){

	if (-1 == data.search("disclaimer")) {
		receive_ajax(data, node);
	} else {
		return false;
	}
}

/**
 * Send an AJAX query using GET method
 * but abort any previous connection before
 * starting the new one.
 * 
 * @param string url
 * @param {Object} callback
 * @param DOM node 
 */
function AjaxGetDataAbortOld(url, callback, node) {
	if (typeof xhr != 'undefined' && xhr.readyState != 0)
		xhr.abort();
	
	AjaxGetData(url, callback, node);
}

/**
 * Send an AJAX query using GET method
 * 
 * @param string url
 * @param {Object} callback
 * @param DOM node 
 */
function AjaxGetData(url, callback, node) {
	xhr = false;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	  
	if (xhr) {
		try {
			showTimer(url);
			xhr.open("GET", url);

			xhr.onreadystatechange = function() {
				if (xhr.readyState == 4 && xhr.status == 200) {
					var data = xhr.responseText;
					
					if (null != callback) {

						if (null != node) {
							
							callback(data, node);
						} else {
							
							callback(data);
						}
					}
					
					hideTimer(url);
				}
			}
		
			xhr.send(null);
		} catch (e) {
			//TODO remove the alert() after the JS stuff has been correctly tested.
			alert('Erreur '+e);
		}
	}
	else {
		//TODO remove the alert() after the JS stuff has been correctly tested.
		alert('AJAX not available');
		window.location = url;
	}
}

/**
 * Send an AJAX query using GET method without a callback
 * 
 * @param string url
 * @param DOM node 
 */
function AjaxSendGetData(url) {
	 AjaxGetData(url, null, null);
}

/**
 * Send an AJAX query using POST method
 * 
 * @param string url
 * @param string parameters
 * @param {Object} callback
 * @param DOM node 
 */
function AjaxPostData(url, parameters, callback, node) {
	var xhr = false;
	
	if(window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if(xhr) {
		try {
			showTimer(url);
			xhr.open("POST", url, true);
			xhr.onreadystatechange = function() {
				if(xhr.readyState == 4 && xhr.status == 200) {
					var data = xhr.responseText;
					callback(data, node);
					hideTimer(url);
				}
			}
			xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xhr.setRequestHeader("Content-length", parameters.length);
			xhr.setRequestHeader("Connection", "close");	
			xhr.send(parameters);
		} catch (e) {}
	}
	else {
		window.location = url;
	}
}

function showTimer(url) {

	var timerCase = findTimerCase(url);
	var id = 'loading-image' + timerCase;
	
	document.body.style.cursor = 'wait';
	if ($(id) && $(id).style ) {
		
		$(id).style.display = '';
	}
}

function hideTimer(url) {

	var timerCase = findTimerCase(url);
	var id = 'loading-image' + timerCase;
	
	document.body.style.cursor = '';
	if ($(id) && $(id).style ) {
		
		$(id).style.display = 'none';
	}
}

function findTimerCase(url){
	var tmp = url.split('search/videos/page');
	var timerCase = '';
	
	if(tmp.length == 2){
		timerCase = '-videos';
	}else{
		var tmp = url.split('search/stars/page');
		if(tmp.length == 2){
			timerCase = '-stars';
		}else{
			var tmp = url.split('search/videos?videos[');
			if(tmp.length == 2){
				timerCase = '-videos-suggest';
			}else{
				var tmp = url.split('search/stars?stars[');
				if(tmp.length == 2){
					timerCase = '-stars-suggest';
				}
			}
		}
	}
	return(timerCase);
}

