function goONOweb() {
	window.location='http://www.ono.es';
}
function StatButtonMode(m) {
	switch(m) {
		case true:
			$('stat_button').removeClassName('stat_button_disabled');
			$('stat_button').addClassName('stat_button');
			$('stat_button').observe('click', function(event) {goStatistics()});
			break;
		case false:
			$('stat_button').removeClassName('stat_button');
			$('stat_button').addClassName('stat_button_disabled');
			$('stat_button').stopObserving('click');
			break;
		case 'ONO':
			$('stat_button').stopObserving('click');
			$('stat_button').removeClassName('stat_button');
			$('stat_button').addClassName('stat_button_goONO');
			$('stat_button').observe('click', function(event) {goONOweb()});
			break;
		case 'volver':
			$('test_now').addClassName('stat_button_goStats');
			$('test_now').observe('click', function(event) {
				window.location='?q=statspage';
				$('test_now').stopObserving('click');
				$('test_now').removeClassName('stat_button_goStats');
			});
			break;
		default:
			alert('esternocleidomastoideo');		
	}
}
function DrawStatGraph(barh, value, text, n) {
	cap = 'sg' + n + '_rcap';
	bar = 'sg' + n + '_rbar';
	up = 'sg' + n + '_rup';
	ref_bar = 'sg' + n + '_pbar';
	if(value > 100) {
		value = 99;
	}
	if(n=='x')
		$(cap).innerHTML = text;
	else
		$(up).innerHTML = value + text;
	$(ref_bar).setStyle({height: barh + 'px'});
	$(bar).setStyle({height: '0px'});
	var alto = Math.round(value * barh/100);
	if(Prototype.Browser.Gecko)
	{
		new Effect.Morph(bar, {
		  style: 'height: ' + alto + 'px;',
		  fps: 18,
		  duration: 2 
		});
	}
	else
	{
		if(Prototype.Browser.IE) { //baixo IE
			if(oito)
				axustealt = barh - alto;
			if(sete)
				axustealt = barh - alto + 16;
			if(seis)
				axustealt = barh - alto + 16;
			if(n=='x')
				axustealt = barh - alto - 0;
			$(bar).setStyle({
				marginTop: ( axustealt + 'px'),	
				height: (alto + 'px')
			});	
		}
		else {	//Baixo Chrome
			$(bar).style.marginTop += ($(bar).style.marginTop + (barh - alto)) + 'px';	
			$(bar).style.height = alto + 'px';
		}
	}
}
function WriteName(n, text) {
	name = 'sg' + n + '_name';
	$(name).innerHTML = left(text.toLowerCase(), 7);
}
function WriteData(n, text) {
	data = 'sg' + n + '_data';
	$(data).innerHTML = text;
}

function left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}


function AdjustWidth(n) {
	var name = 'sg' + n + '_name';
	var data = 'sg' + n + '_data';
	var tmp_name = 'sg' + (n-1) + '_name';
	var width = $(name).getWidth();
	var tmp_width = $(tmp_name).getWidth();
	if(n != 1) {
		if(width <= tmp_width) {
			$(name).setStyle({width: (tmp_width-25) + 'px'});
		}
		else {
			for(y=8;y<n;y++){
				tmp_name = 'sg' + y + '_name';
				$(tmp_name).setStyle({width: (width-25) + 'px'});
			}
		}
	}
	$(data).setStyle({width: '266px'});
}
function ScaleGraph(per, id) {
	new Effect.Scale(id, per, {transition: Effect.Transitions.full});
}
function ShowStatData(m) {
	switch(m) {
		case 1:		//Media General ADSL
			result = statsJSON[0];
			DrawStatGraph(105, result, '%', 1);
			/*if(firstclick[0]!=1) {
					firstclick[0] = 1;
					//ScaleGraph(120, 'stat_mediaxeral');
			}*/
			break;
		case 2:		//Media por Operador
			result = statsJSON[1];
			if(firstclick[1] == 0) {
				ScaleGraph(120, 'statsbyoperator');
				firstclick[1] = 1;
			}
			for(x=1;x<=6;x++) {
				DrawStatGraph(105, result[x], '%', x+1);
			}
			break;
		case 3:		//10 Mellores resultados
			result = statsJSON[2];
			for(x=1;x < 11;x++) {
				WriteName(x+7, result[x].name);
				WriteData(x+7, '<b>' + left(result[x].city.toLowerCase(), 9) + '</b>' + result[x].supplier 
						+ '/&nbsp;C:&nbsp;' + Math.round(result[x].productspeed/1024)
						+ 'Mb-&nbsp;R:&nbsp;' + Math.round(result[x].download/1024*10)/10 + 'Mb');
				AdjustWidth(x+7);
			}
			break;
		case 4:		//10 peores resultados
			result = statsJSON[3];
			for(x=1;x<11;x++) {
				WriteName(x+17, result[x].name);
				WriteData(x+17, '<b>' + left(result[x].city.toLowerCase(), 9) + '</b>' + result[x].supplier 
						+ '/&nbsp;C:&nbsp;' + Math.round(result[x].productspeed/1024) 
						+ 'Mb-&nbsp;R:&nbsp;' + Math.round(result[x].download/1024*100)/100 + 'Mb');
				AdjustWidth(x+17);
					
			}
			break;
		case 'x':	//Resultados test -> ResultPage.tpl
			result = $('sgx_rbar').getHeight();
			$('sgx_rbar').setStyle({visibility: 'visible'});
			$('sgx_rbar').setStyle({height: '0'});
			DrawStatGraph(105, result, 'REAL', 'x');
			//ScaleGraph(80, 'testresultstats');			
			//$('sgx_pbigcap').setStyle({});
			//$('sgx_rbigcap').setStyle({});
			break;
		default:
			alert('ornitorrinco');
			break;
	}

}
function ShowOperatorStats() {
	if($("stat_content2"))
	{
		$('stat_content2').setStyle({visibility: 'visible'});
		$('stat_title2').addClassName('selectQ');
	}
	else
	{
		ShowOperatorStats.delay(0.5);
		/*console.debug('waiting for "stat_content2"');*/
	}
}
function GetStatData() {
	new Ajax.Request('stats/statdata.php', {
		  method:'post',
		  onSuccess: function(transport){
			 ShowStatData.delay(0.8, 2);
			 ShowOperatorStats.delay(1, 2);
		     statsJSON = transport.responseText.evalJSON(); 
		   }
		});
}
