var ServerMonitor = new function()
{
	this.currentDatetime 	  = new Date();
	this.diffDateTime    	  = 0;
	this.oneSecond       	  = 1000;
	this.valuesOfDelay  	  = [2000, 1750, 1200, 1000, 750, 500, 400, 250, 120];
	this.valuesOfDelayPercent = [0   , 30  , 50  , 65  , 80 , 85 , 90 , 95 , 99];
    this.currentDelay    	  = this.valuesOfDelay.length;
	this.showOnConsole		  = false;
	this.showOnTitle		  = false;
	this.currentDelayPercent  = 0;
	this.colorLevel3          = '#006600';
	this.colorLevel2          = '#FFA200';
	this.colorLevel1          = '#FF000A';
	this.data                 = [99, 99, 99, 99, 99];
	this.currentPointer       = 0;
	this.maxPointer           = 4;

	/**
	 *Configura as corres de acordo com o level
	 **/
	this.setColor = function(colorLevel1, colorLevel2, colorLevel3)
	{
		this.colorLevel3 = colorLevel3;
		this.colorLevel2 = colorLevel2;
		this.colorLevel1 = colorLevel1;
	}

	/**
	 * Reinicia o contador
	 */
	this.reset = function()
	{
		this.currentDatetime = new Date();
	}

	/**
	 * Atualiza contador e exibe na página
	 */
	this.update = function(error)
	{
		var afterRequestTime = new Date();
		var x = 0;
		
		if (error == true)
		{
			this.diffDateTime = 0;
			this.currentDelay = this.valuesOfDelay[0];
		}
		else
		{
			this.diffDateTime = ((afterRequestTime.getTime()-this.currentDatetime.getTime()));

			for (x = 0; x < this.valuesOfDelay.length; x++)
			{
				if (this.diffDateTime >= this.valuesOfDelay[x])
				{
					this.currentDelay = this.valuesOfDelay[x];
					break;
				}
			}
		}


		

		

		this.currentDelayPercent = this.valuesOfDelayPercent[x];
		this.checkForNaN();
		this.storeCurrentPercent();
		this.currentDelayPercent = this.getPercentAverage();
		this.checkForNaN();
		this.showPercentIndicator();
		this.showIndicatorGraph();

		var message = "Conectividade com o servidor (delay em ms: " + this.diffDateTime + " e delay atual: " + this.currentDelay + "): " + (this.currentDelayPercent) + "% - Nível: " + this.getLevel();

		if (this.showOnConsole == true)
		{
			console.log(message);
			console.log('Dados medidos: ' + this.data);
			console.log('Tempo médio: ' + this.getPercentAverage());
		}
		
		//alert(message);
		
		if (this.showOnTitle == true)
		{
			document.title = message;
		}
	}

	/**
	 * Obtém o indicador de nível (1,2,3) da resposta
	 */
	this.getLevel = function()
	{
		if (this.currentDelayPercent <= 5)
		{
			return 1;
		}
		else if (this.currentDelayPercent <= 20)
		{
			return 2;
		}
		else if (this.currentDelayPercent <= 40)
		{
			return 3;
		}
		else if (this.currentDelayPercent <= 60)
		{
			return 4;
		}
		else if (this.currentDelayPercent <= 80)
		{
			return 5;
		}
		else
		{
			return 6;
		}
	}

	/**
	 * Exibe a porcentagem de resposta
	 */
	this.showPercentIndicator = function()
	{
		$('#server_connectivity').html(this.currentDelayPercent + '%');

		var level = this.getLevel();

		if (level == 1 || level == 2)
		{
			$('#server_connectivity').css('color', this.colorLevel1);
		}
		else if (level == 3 || level == 4)
		{
			$('#server_connectivity').css('color', this.colorLevel2);
		}
		else if (level == 5 || level == 6)
		{
			$('#server_connectivity').css('color', this.colorLevel3);
		}
	}

	/**
	 * Exibe a imagem do gráfico
	 */
	this.showIndicatorGraph = function()
	{
		var level = this.getLevel();		
		$('#server_connectivity_container').attr('class', 'conn_status_' + level);
	}

	/**
	 * Guarda a porcentagem da diferença atual
	 */
	this.storeCurrentPercent = function()
	{
		this.data[this.currentPointer] = this.currentDelayPercent;
		
		if (this.currentPointer + 1 > this.maxPointer)
		{
			this.currentPointer = 0;
		}
		else
		{
			this.currentPointer++;
		}
	}

	/**
	 * Obtém a média de todos as porcentagens medidas
	 */
	this.getPercentAverage = function()
	{
		var total = 0;
		var x = 0;
		
		for (x = 0; x < this.data.length; x++)
		{
			total += this.data[x];
		}

		return (total / this.data.length);
	}

	/**
	 * Verifica por dados que não são números
	 */
	this.checkForNaN = function()
	{
		if (isNaN(this.currentDelayPercent))
		{
			this.currentDelayPercent = 99;
		}
	}
}

