var temps = new Date();
var tempsR = new Date();

init();
window.onload = documentHandler;

function documentHandler(evenement) {
	evenement = evenement || window.event;

	var test = new Date() - temps;

		var message = "";
		var tempsA = new Date().getTime();

		var user = LireCookie("wur");
		//id_ow dans cookie ?
		var idU = getVar("IDOW");
		if (idU == null)
		{
			idU = LireCookie("wuridu");
		}

		message = '{type:"' + evenement.type + '",' + "page:\""
				+ monescape(document.location.href) + "\"," + "nomu:\"" + user
				+ "\"," + "idU:\"" + idU + "\"," + "referrerPage:\""
				+ monescape(document.referrer) + "\"," + "Instant:\""
				+ (tempsA - tempsR) / 1000 + "\"," + "TempsAbs:\"" + tempsA
				+ "\"" + "}";

		temps = new Date();

		makeRequest("http://fitz.dapa.lip6.fr:8180/fromage/WUR", message);

}

// Initialisation, a chaque action de l'utilisateur
function init() {

	window.onunload = documentHandler;

}

//Fonction existant que sous gecko 1.8 donc reecriture pour ie
function indexOf(tableau, valeur) {
	var index = 0;
	for ( var clef in tableau) {
		if (tableau[clef] == valeur)
			{return index;}
		index++;
	}
	return -1;
}

function makeRequest(url, action) {

	if (document.getElementById('ilobtrackid')) {
		// Suppression de la balise si elle existait déjà
		var d = document.getElementById("ilobtrackid");
		document.body.removeChild(d);
	}

	// Création de la nouvelle balise :
	var script = document.createElement('script');
	script.src = url + '?action=';
	script.src += action;

	script.id = 'ilobtrackid';
	script.type = 'text/javascript';

	// Et injection dans le DOM :
	document.body.appendChild(script);

}

function monescape(text) {
	return text.replace(/:/g, "@@").replace(/&/g, "!!");
}

function EcrireCookie(nom, valeur) {
	var argv = EcrireCookie.arguments;
	var argc = EcrireCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = nom + "=" + escape(valeur)
			+ ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
			+ ((path == null) ? "" : ("; path=" + path))
			+ ((domain == null) ? "" : ("; domain=" + domain))
			+ ((secure == true) ? "; secure" : "");
}

function EffaceCookie(nom) {
	date = new Date;
	date.setFullYear(date.getFullYear() - 1);
	EcrireCookie(nom, null, date);
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1)
		{endstr = document.cookie.length;}
	return unescape(document.cookie.substring(offset, endstr));
}

function LireCookie(nom) {
	var arg = nom + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			{return getCookieVal(j);}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			{break;}

	}
	
	if (nom == "wuridu")
		{
			return "notrack";
		}

	//pas de cookie on gère un id unique UUID
	var id = new UUID();

	date = new Date;
	date.setFullYear(date.getFullYear() + 1);
	EcrireCookie("wur", id, date, "/");
	return id;
}

function getVar(variable) {

	var nom=new Array();
	var valeur=new Array();

	param = window.location.search.slice(1,window.location.search.length);

	first = param.split("&");

	for(i=0;i<first.length;i++){
		second = first[i].split("=");
		namevar = second[0];
		nom[namevar] = second[1];
	}


	//ecriture dans le cookie avant de rencoyer la valeur
	if (nom[variable]!=undefined)
	{
	date = new Date;
	date.setFullYear(date.getFullYear() + 1);
	EcrireCookie("wuridu", nom[variable], date, "/");
	return nom[variable];
	}

	else 
		return null;;
}

/*

 uuid.js - Version 0.3
 JavaScript Class to create a UUID like identifier

 Copyright (C) 2006-2008, Erik Giberti (AF-Design), All rights reserved.

 This program is free software; you can redistribute it and/or modify it under 
 the terms of the GNU General Public License as published by the Free Software 
 Foundation; either version 2 of the License, or (at your option) any later 
 version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY 
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 PARTICULAR PURPOSE. See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with 
 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
 Place, Suite 330, Boston, MA 02111-1307 USA

 The latest version of this file can be downloaded from
 http://www.af-design.com/resources/javascript_uuid.php

 HISTORY:
 6/5/06 	- Initial Release
 5/22/08 - Updated code to run faster, removed randrange(min,max) in favor of
 a simpler rand(max) function. Reduced overhead by using getTime() 
 method of date class (suggestion by James Hall).
 9/5/08	- Fixed a bug with rand(max) and additional efficiencies pointed out 
 by Robert Kieffer http://broofa.com/

 KNOWN ISSUES:
 - Still no way to get MAC address in JavaScript
 - Research into other versions of UUID show promising possibilities 
 (more research needed)
 - Documentation needs improvement

 */

// On creation of a UUID object, set it's initial value
{function UUID() {
	this.id = this.createUUID();
}}

// When asked what this Object is, lie and return it's value
{UUID.prototype.valueOf = function() {
	return this.id;
}}
{UUID.prototype.toString = function() {
	return this.id;
}}

//
// INSTANCE SPECIFIC METHODS
//

{UUID.prototype.createUUID = function() {
	//
	// Loose interpretation of the specification DCE 1.1: Remote Procedure Call
	// described at http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagtcjh_37
	// since JavaScript doesn't allow access to internal systems, the last 48 bits 
	// of the node section is made up using a series of random numbers (6 octets long).
	//  
	var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
	var dc = new Date();
	var t = dc.getTime() - dg.getTime();
	var h = '-';
	var tl = UUID.getIntegerBits(t, 0, 31);
	var tm = UUID.getIntegerBits(t, 32, 47);
	var thv = UUID.getIntegerBits(t, 48, 59) + '1'; // version 1, security version is 2
	var csar = UUID.getIntegerBits(UUID.rand(4095), 0, 7);
	var csl = UUID.getIntegerBits(UUID.rand(4095), 0, 7);

	// since detection of anything about the machine/browser is far to buggy, 
	// include some more random numbers here
	// if NIC or an IP can be obtained reliably, that should be put in
	// here instead.
	var n = UUID.getIntegerBits(UUID.rand(8191), 0, 7)
			+ UUID.getIntegerBits(UUID.rand(8191), 8, 15)
			+ UUID.getIntegerBits(UUID.rand(8191), 0, 7)
			+ UUID.getIntegerBits(UUID.rand(8191), 8, 15)
			+ UUID.getIntegerBits(UUID.rand(8191), 0, 15); // this last number is two octets long
	return tl + h + tm + h + thv + h + csar + csl + h + n;
}}

//
// GENERAL METHODS (Not instance specific)
//

// Pull out only certain bits from a very large integer, used to get the time
// code information for the first part of a UUID. Will return zero's if there 
// aren't enough bits to shift where it needs to.
{UUID.getIntegerBits = function(val, start, end) {
	var base16 = UUID.returnBase(val, 16);
	var quadArray = new Array();
	var quadString = '';
	var i = 0;
	for (i = 0; i < base16.length; i++) {
		quadArray.push(base16.substring(i, i + 1));
	}
	for (i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {
		if (!quadArray[i] || quadArray[i] == '')
			quadString += '0';
		else
			quadString += quadArray[i];
	}
	return quadString;
}}

// Replaced from the original function to leverage the built in methods in
// JavaScript. Thanks to Robert Kieffer for pointing this one out
{UUID.returnBase = function(number, base) {
	return (number).toString(base).toUpperCase();
}}

// pick a random number within a range of numbers
// int b rand(int a); where 0 <= b <= a
{UUID.rand = function(max) {
	return Math.floor(Math.random() * (max + 1));
}}

// end of UUID class file

