jQuery(document).ready(function(){
	
//Testa a velocidade de conexão !
	time      = new Date(); 
	starttime = time.getTime(); 
	velo=function(){
		time          = new Date(); 
		endtime       = time.getTime(); 
		if (endtime == starttime) 
			{downloadtime = 0 
			} 
		else 
		{downloadtime = (endtime - starttime)/1000; 
		} 
		kbytes_of_data = 512; 
		linespeed     = kbytes_of_data/downloadtime; 
		kbps          = (Math.round((linespeed*8)*10*1.024))/10; 
		resultado = 'Kbps: ' + kbps + ' Tempo de download: ' + downloadtime + ' KB: ' + kbytes_of_data;
		return resultado;
	}

	
	
	gravarArquivo = function(){

		//Pega Navegador
		nave = "";
		jQuery.each(jQuery.browser, function(i, val){
			if (i == "mozilla" && val == true){
				nave = "Mozilla Firefox";
			}else if(i == "safari" && val == true){
				nave = "Safari";
			}else if(i == "opera" && val == true){
				nave = "Opera";
			}else if(i == "msie" && val == true){
				nave = "Internet Explorer";
			}
		});

		navegadorCliente = nave;
		resolucaoCliente = screen.width + "X" + screen.height;
		
		jQuery.ajax({
						type: 'POST',
						url: '../includes/gravarArquivo_ajax.pt',
						data: {
								navegador: navegadorCliente,
								resolucao: resolucaoCliente
							},
						success: function (resposta){
							//Só depois chama o segundo ajax com a função de teste de velocidade, pois é preciso que ela rode sem nenhuma interferência na memória
							gravarArquivo2();
							if (resposta == "1"){
								alert("Arquivo gravado com sucesso");
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){
							msg = "Erro: " + XMLHttpRequest.status + " - " + XMLHttpRequest.statusText;
						}
				});
	}
	
	

	gravarArquivo2 = function(){
		velocidadeCliente = velo();
		jQuery.ajax({
						type: 'POST',
						url: '../includes/gravarArquivo_ajax.pt',
						data: {
								velocidade: velocidadeCliente
							},
						success: function (resposta){
							
							if (resposta == "1"){
								alert("Arquivo gravado com sucesso");
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){
							msg = "Erro: " + XMLHttpRequest.status + " - " + XMLHttpRequest.statusText;
						}
				});
	}

	//onLoad da página !
	gravarArquivo();
});
