/** Appeller ainsi :
  <script type="text/javascript" src="/salagirlibs/countOut.js"></script>
  <script type="text/javascript">
  var veryFromUri  = "<?= $_SERVER['REQUEST_URI'] ?>";
  </script>
*/

var iwantogothere;

function prepareAllLinks() {
	var allLinks = document.getElementsByTagName('a');
	m = allLinks.length;
	if (m>1000) m=1000; // on sait jamais
	for (i=0; i<m; i++) {
		if ( 0 == allLinks[i].href.indexOf('http://') && 0 != allLinks[i].href.indexOf('http://'+location.hostname)) {
			allLinks[i].setAttribute('onclick', 'return clickedOnThat(this);');
			allLinks[i].onclick = function() { return clickedOnThatIE(this); }
		}
	}
}

// FF ne prends pas le temps de charger l'image alors que IE oui. On peut donc utiliser la méthode simple avec lui
function clickedOnThatIE(zis) {
	if (document.images)
		(new Image()).src =
			'/salagirlibs/countOut.php?to='+escape(zis.href)+'&fromServ='+escape(location.hostname)+'&fromUri='+escape(veryFromUri);
	return true;
}

//methode compliquée
function clickedOnThat(zis) {
	url = '/salagirlibs/countOut.php?to='+escape(zis.href)+'&fromServ='+escape(location.hostname)+'&fromUri='+escape(veryFromUri);
	//alert(url);
	getJSON(url);
	// on doit attendre le retour de JSON pour valider, sinon il est oubilé aussi sec et pas executé
	iwantogothere = zis.href;
	return false;
}


var createAjaxObject = function(ajaxType) {
	if(window.XMLHttpRequest){
		var http_request = new XMLHttpRequest();
		if( http_request.overrideMimeType && typeof(ajaxType) != 'undefined' && ajaxType == 'text'){
			http_request.overrideMimeType('text/xml');
		}
		return http_request;
	}
	else if(window.ActiveXObject){
		var ieVersions=['Microsoft.XMLHttp','MSXML2.XMLHttp'];
		for(var i=0, j=ieVersions.length; i<j; i++ ){
			try {
				http_request = new ActiveXObject(ieVersions[i]);
				return http_request;
			}
			catch(e) {}
		}
	}
	else{
		return false;
	}
};


var JSON;

var getJSON = function(url , somevalue) {
	var http_request=createAjaxObject('text');
	if (typeof(http_request)=='object') {
		http_request.onreadystatechange=function(){
			if (http_request.readyState==4 && http_request.status==200){
				//JSON = eval('('+http_request.responseText+')');

				document.location.href = iwantogothere;
				// là on fait des trucs sur ce qu'on reçoit, mais ce qu'on recoit, OSEF
				// ou alors on attends la confirmation de ça pour effectivement envoyer sur le lien (très méchant)
			}
			else return false;
		};
		http_request.open('GET',url,true);
		http_request.send(null);
	}
};

// placement auto dans body onload
if(window.addEventListener){
	window.addEventListener("load", function(){ prepareAllLinks(); }, false);
}
else if(window.attachEvent){
	window.attachEvent("onload", function(){ prepareAllLinks(); } );
}

