var fgc = "ccc";
var bgc = "000";
var cmdList = new Array();
var cmdCount = -1;

/* Ajout du style */
this.addStyle = function(objet) {
	objet.setAttribute("style", "color:#"+fgc+";background-color:#"+bgc+";");
}

/* focus du prompt au chargement */
this.onload = function() {
	document.getElementById("cursor").addEventListener('keydown', this.callback,false);
	this.focus();
}

/* focus the prompt */
this.focus = function() {
	document.forms[0].command.focus();
}

/* Création d'un paragraphe */
this.makep = function(content) {
	var retour = document.createElement("p");
	this.addStyle(retour);
	var txt = document.createTextNode(content);
	retour.appendChild(txt);
	return retour;
}

/* Création d'un nouveau prompt */
this.newPrompt = function() {
	var retour = document.createElement("form");
	retour.setAttribute("action", "javascript:enter();");
	retour.setAttribute("method", "post");
	retour.setAttribute("enctype", "text/plain");
	var prompt = this.makep("[anonymous|~]$ ");
	var input = document.createElement("input");
	this.addStyle(input);
	input.setAttribute("type", "text");
	input.setAttribute("name", "command");
	input.setAttribute("id", "cursor");
	prompt.appendChild(input);
	retour.appendChild(prompt);
	return retour;
}

/* Affichage d'un résultat */
this.show = function(reponse) {
	var lines = reponse.split("\n");
	var cpt = 0;
	while(lines[cpt] != null) {
		if (lines[cpt] == "") {
			var br = document.createElement("br");
			document.body.appendChild(br);
		}else{
			document.body.appendChild(this.makep(lines[cpt]));
		}
		cpt++;
	}
}

/* Changement de couleur */
this.setColor = function(color1, color2) {
	bgc = color1;
	fgc = color2;
	this.addStyle(document.body);
	exist = document.getElementsByTagName("p");
	for (i=0; i<exist.length; i++) {
		addStyle(exist[i]);
	}
	this.show("Changement de couleur: fond["+color1+"], police["+color2+"]");
}

/* Nettoyage du terminal */
this.cleanTerminal = function() {
	while(document.body.childNodes.length != 0) {
		document.body.removeChild(document.body.childNodes[0]);
	}
}

/* Traitement après validation de la touche entrée */
this.enter = function() {
	// Sauvegarde de la commande demandé
	var command = document.forms[0].command.value;
	cmdCount = -1;
	if (cmdList[0] != command) { cmdList.unshift(command); }
	
	// Fixation de la commande demandé
	document.body.removeChild(document.forms[0]);
	document.body.appendChild(this.makep("[anonymous|~]$ "+command));
	
	// Traitement de la commande demandé
	if (command != "") {
		var Xhr = new XHRConnection();
		Xhr.sendAndLoad("command.php?comm="+command, "GET", this.result);
	}else{
		// Affichage d'un nouveau prompt
		document.body.appendChild(this.newPrompt());
		this.focus();
	}
}

/* Traitement du résultat (retour xhr) d'une commande */
this.result = function(obj) {
	// Récuparation du résultat de la commande
	var reponse = obj.responseText;
	// Detection de cas spéciaux
	if (reponse == "special:gohome") {
		window.home();
	}else if (reponse == "special:infres") {
		window.location.href="documents/infres2a.html";
	}else if (reponse == "special:reload") {
		window.location.reload();
	}else if (reponse == "special:clear") {
		this.cleanTerminal();
	}else if (reponse.substr(0,13) == "special:color") {
		var tmp = reponse.split("|");
		this.setColor(tmp[1], tmp[2]);
	}else{
		// action par défault
		this.show(reponse);
	}
	// Affichage d'un nouveau prompt
	document.body.appendChild(this.newPrompt());
	this.onload();
}

/* Traitement du rappel de commande */
this.callback = function(evt) {
	// appuie sur la flèche haute
	if (evt.which == 38) {
		if(cmdList[cmdCount+1] != null) {
			cmdCount += 1;
			document.getElementById("cursor").value = cmdList[cmdCount];
		}
	// appuie sur la flèche basse
	}else if (evt.which == 40) {
		if(cmdList[cmdCount-1] != null) {
			document.getElementById("cursor").value = cmdList[cmdCount-1];
			cmdCount -= 1;
		}
	}
}

