// variables of the module
// root url of the server which is prepend to the other variables
var url_server = "http://www.sorties.org/dashboard/";
// name of file to call with text to add to dashboard
var url_addText2Dashboard = url_server+"addText2Dashboard.php?input=";
// name of file to call to increment the timestamp of the dashboard
var url_incTimestamp = url_server+"incTimestamp.php";
// name of file to call to get the actual timestamp
// @parameter actual timestamp that you have
var url_actualTimestamp = url_server+"actualTimestamp.php?timestamp=";
// name of file to call to get the whole dashboard
var url_getDashboard = url_server+"getDashboard.php";
// name of file to call if you want to clear the dashboard
var url_clearDashboard = url_server+"clearDashboard.php";
// path to the file to show when refreshing something
var ajax_loader_path = url_server+"images/ajax-loader-small.gif";

//Fonction de raccourci pratique
function $(el) {
  return document.getElementById(el);
}

var ajaxmgr = new ajaxmanager();
var synchrone = 0;
var data = new Object();
data.timestamp = 0;

function $(el) {
  return document.getElementById(el);
}

function updateStatus(text)
{
	if ($('status').style.display != "none")
	{
		$('status').innerHTML = text;
	}
}

function addText2Dashboard()
{
	updateStatus("Posting...");
	$('ajaxLoader').style.visibility = "visible";
	var url = url_addText2Dashboard;
	url += encodeURIComponent("<i><b>"+$('user').value+"</b></i><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+$("text2Add").value);

	$('text2Add').value = "";
	$('text2Add').focus();
	
	ajaxmgr.retrieve(url, function(request) {  /*if(request.responseText != "OK") { alert(request.responseText);}*/ refreshTextFromDashboard(); }, ajaxmgr);
	ajaxmgr.retrieve(url_incTimestamp, function (request) {}, ajaxmgr);
}

blinkerState = false;

function updateBlinker()
{
	if (blinkerState)
		$('blinker').style.background = "green";
	else
		$('blinker').style.background = "white";
		
	blinkerState = !blinkerState;

}

function refreshTextFromDashboard()
{
	var url = url_actualTimestamp+data.timestamp;
	updateStatus("Fetching..."+data.timestamp);

	ajaxmgr.retrieve(url, 
		function(request) 
			{ 
				updateBlinker();
				updateStatus(data.timestamp+ " ?= "+request.responseText);
				
				if (request.responseText != data.timestamp) 
				{
					updateStatus("Refreshing text...");
					$('ajaxLoader').style.visibility="visible";

					ajaxmgr.retrieve(url_getDashboard, function(newrequest) 
					{
						$('content').innerHTML = newrequest.responseText;
						updateStatus("status");
						$('ajaxLoader').style.visibility='hidden';
						data.timestamp = request.responseText;
						$('content').scrollTop = $('content').scrollHeight;
						
					}, ajaxmgr);
				}
			}, ajaxmgr);
}

function ClearDashboard()
{
	var ajax = new XMLHttpRequest();
    url = url_clearDashbaord;
	ajax.open('GET', url, false);
	ajax.send(null);
	
	ajaxmgr.retrieve(url_incTimestamp, function (request) {}, ajaxmgr);
	
	refreshTextFromDashboard();
}


function AutomaticRefreshDasboardHandler()
{
	refreshTextFromDashboard();
	
	window.setTimeout(AutomaticRefreshDasboardHandler, 500);
}

function startAutomaticRefreshDashboard()
{	
	window.setTimeout(AutomaticRefreshDasboardHandler, 1000);
}

function testAjaxManager()
{
	ajaxmgr.retrieve(url_getDashboard, function(request) { alert(request.responseText);}, ajaxmgr);
}

function checkEnter(e)
{
var characterCode = 0; 

if(e && e.which)
{	//if which property of event object is supported (NN4)
	e = e;
	characterCode = e.which; //character code is contained in NN4's which property
}
else
{
	e = event;
	characterCode = e.keyCode; //character code is contained in IE's keyCode property
}

if(characterCode == 13)
{ //if generated character code is equal to ascii 13 (if enter key)
	return true;
}
else
{
	return false;
}
}

function loadCookies()
{
	$('user').value = GetCookie("user");
}

function saveUser()
{
	var expires = 3650*1000*3600*24;
	var expiresDate = new Date((new Date()).getTime() + expires);
	SetCookie('user', $('user').value, expiresDate.toGMTString());
}


//Classe de définition de Dashboard
function Dashboard(id, _url_server) 
{
		if (_url_server != null)
		{
			url_server = _url_server;		
		}
		
		var dash = document.createElement("div");
		dash.setAttribute("id", "dashboard");
		var header = document.createElement("div");
		header.setAttribute("id", "header");
		
		// treat user name
		var user = document.createElement("input");
		user.setAttribute("id", "user");
		user.setAttribute("type", "text");
		user.setAttribute("value", "user");
		user.setAttribute("onkeyup", "saveUser();");
		user.onkeyup = saveUser;
		header.appendChild(user);
		
		// treat text 2 add
		var text2Add = document.createElement("input");
		text2Add.setAttribute("id", "text2Add");
		text2Add.setAttribute("type", "text");
		text2Add.setAttribute("value", "Type your text");
		text2Add.setAttribute("onkeypress", "if (checkEnter(event)) addText2Dashboard();");
		text2Add.onkeypress = function (event)  { if (checkEnter(event)) addText2Dashboard(); };
		header.appendChild(text2Add);

		// treat button to post
		var button2Add = document.createElement("input");
		button2Add.setAttribute("type", "button");
		button2Add.setAttribute("id", "addText2dashboard");
		button2Add.setAttribute("value", "+");
		button2Add.setAttribute("onclick", "addText2Dashboard();");
		button2Add.onclick = addText2Dashboard;
		header.appendChild(button2Add);
		
		// treat animated gif to display action
		var ajaxloader = document.createElement("img");
		ajaxloader.setAttribute("id", "ajaxLoader");
		ajaxloader.setAttribute("src", ajax_loader_path);
		header.appendChild(ajaxloader);

		// treat blinker
		var blinker = document.createElement("div");
		blinker.setAttribute("id", "blinker");
		blinker.setAttribute("value", "");
		header.appendChild(blinker);

		// treat content
		var content = document.createElement("div");
		content.setAttribute("id", "content");
		var tt = document.createTextNode("Loading...");
		content.appendChild(tt);

		// add status field
		var status = document.createElement("div");
		status.setAttribute("id", "status");
		status.setAttribute("display", "none");
		dash.appendChild(status);
	
		dash.appendChild(header);
		
		dash.appendChild(content);
		
		$(id).appendChild(dash);
		
		loadCookies();
		
		startAutomaticRefreshDashboard();		
}	