/* ---- MOSTRAR OCULTAR OBJETOS ---- */
function popupParciales() {
	Effect.BlindDown('parciales');
	$('onParciales').hide();
	$('offParciales').show();
}


function closeParciales() {
	$('onParciales').show();
	$('offParciales').hide();
	Effect.BlindUp('parciales');
	if (blnCloseOnCancel) window.close();
}

<!-- END MOSTRAR OCULTAR OBJETOS -->

var waitingResponse = 0;
var timer=null;
var timeout = 20;
function setUpRefresh() {
	waitingResponse = 0;
	var objState = $V('hdn_ppcev_data').evalJSON();
	if (objState.blnRefresh) {
		counter = new PeriodicalExecuter(function (pe) {
						if ($('div_counter') == null) {
							return;
						}
						$('div_counter').innerHTML = 'Actualización en ' + timeout + ' segundos';
						if (timeout-- <1) {
							$('div_counter').innerHTML = 'Actualizando...';
							pe.stop();
						}
						

						},1);
		timer = new PeriodicalExecuter(function(pe) { 
			pe.stop();			
			refreshContent();
			},20);
	}
}

function showDrivers(strAccion) {
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[objState.intDriversCat];
	if (strAccion!='todos') {
		strUrl = '/upload/htmls/ppcev/drivers_enabled_'+idRace+'.htm?ale='+aleatorio;
		AppObj.Ajax.updaterAjax(strUrl,'div_ppcev_content');
	} else {
		showCars();
	}
}
function postMessage() {
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[objState.intDefaultCat];
	var idRound = objState.idRound[objState.intDefaultCat];
	var message = Url.encode($V('txt_message'));
	AppObj.Ajax.updaterAjax('/modules/ppcev/postmessage.ajax.php?idrace=' + idRace + '&idround=' + idRound + '&message=' + message,'');
	Effect.BlindDown('div_messageposted');
	Effect.BlindUp('div_postmessage');
	$('txt_message').value = '';
	AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/mensajesusuarios_'+idRace+'_'+idRound+'.htm?ale='+aleatorio,'div_postedmessages');
	new PeriodicalExecuter(function(pe) {
	    pe.stop();
		Effect.BlindDown('div_postmessage');
		Effect.BlindUp('div_messageposted');

	}, 60);
	
}

function changeActivitiesCat(intCat) {
	var aleatorio = Math.round(Math.random()*10000);
	$('hdn_Categoria').value = intCat;
	var objState = $V('hdn_ppcev_data').evalJSON();
	objState.intDefaultCat = intCat;
	$('hdn_ppcev_data').value = Object.toJSON(objState);
	var idRace = objState.idRace[objState.intDefaultCat];
	$('idTandaActual').value = objState.idRound[objState.intDefaultCat]
	//if (objState.strContent == 'results' && objState.blnRefresh) {
	if (objState.strContent == 'results') {
		showResults($('hdn_LastRound_'+intCat).value);
	}
	if (objState.strRightBar == 'comments') {
		showComments();
	}
	if (objState.strContent == 'groups') {
		showGroups();
	}
	AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/actividades_'+idRace+'.htm?ale='+aleatorio,'div_activities');
}

function onRefresh() {
	waitingResponse--;
	if (waitingResponse==0) {
		if (timeout<1) {
		timeout=20;
		counter = new PeriodicalExecuter(function (pe) {
			if ($('div_counter') == null) {
				return;
			}
			$('div_counter').innerHTML = 'Actualización en ' + timeout + ' segundos';
			if (timeout-- <1) {
				$('div_counter').innerHTML = 'Actualizando...';
				pe.stop();
			}});
		}
		setUpRefresh();
	}
}

function refreshContent() { // Actualizar las actividades
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	waitingResponse++;
	var idRaceActividades = document.getElementById('hdn_Carrera').value;
	var idRace = objState.intRunningRace;
	var idRound = objState.intRunningRound;
	AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/actividades_'+idRaceActividades+'.htm?ale='+aleatorio,'div_activities',{loading:false,onComplete:onRefresh});
	if (idRound != objState.intRunningRound) { // Se esta viendo una tanda que no es la que se esta corriendo. No actualizar nada!
		return;
	}
	if (objState.strContent == 'results') {
		if ($('hdn_Modo').value == 'graphic') {
			waitingResponse++;
			AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/mensajesadmin_'+idRace+'_'+idRound+'.htm?ale='+aleatorio,'div_admin_messages',{loading:false,onComplete:onRefresh});
		} else {
			waitingResponse++;
			if ($('idTandaActual').value==idRound && document.getElementById('hdnIsResults')!=null) { //Actualizo los resultados, solo si estoy en la tanda correspondiente
				AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/tanda_'+idRace+'_'+idRound+'.htm?ale='+aleatorio,'div_ppcev_content',{loading:false,onComplete:onRefresh});
			} else {
				onRefresh();
			}
			waitingResponse++;
			if ($('idTandaActual').value==idRound) { //Actualizo los comentarios del Admin solo si estoy posicionado en la tanda que tengo que actualizar
				AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/mensajesadmin_'+idRace+'_'+idRound+'.htm?ale='+aleatorio,'div_admin_messages',{loading:false,onComplete:onRefresh});
			} else {
				onRefresh();
			}
		}
	}
	if (objState.strRightBar == 'comments') {
		waitingResponse++;
		if ($('idTandaActual').value==idRound) { //Actualizo los comentarios de usuarios solo si estoy posicionado en la tanda que tengo que actualizar
			AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/mensajesusuarios_'+idRace+'_'+idRound+'.htm?ale='+aleatorio,'div_postedmessages',{loading:false,onComplete:onRefresh});
		} else {
			onRefresh();
		}
	}
	
	ViewChangeMode(idRound);
}

function showChampionship(){
	var aleatorio = Math.round(Math.random()*10000);
	strUrlTopTen = '/upload/htmls/campeonato_top_ten_1.htm?ale='+aleatorio;
	strUrlPlayOff  = '/upload/htmls/play_off_1.htm?ale='+aleatorio;
	AppObj.Ajax.updaterAjax(strUrlPlayOff,'div_ppcev_playoff');
	AppObj.Ajax.updaterAjax(strUrlTopTen,'div_ppcev_right');
}
function showNews() {
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	var arrRace = Object.toJSON(objState.idRace);
	var idRound = objState.idRound[objState.intDefaultCat];
	var strMode =  objState.strMode;
	objState.strRightBar = 'news';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	document.getElementById('div_ppcev_playoff').innerHTML = '';
	strUrl = '/upload/htmls/ppcev/noticias_'+objState.idRace[1]+'_'+objState.idRace[2]+'.htm?ale='+aleatorio;
	AppObj.Ajax.updaterAjax(strUrl,'div_ppcev_right');
}
function showNewsPPCEV (intIdNews) {
	AppObj.Ajax.updaterAjax('/modules/ppcev/shownews.ajax.php?idnews=' + intIdNews,'div_ppcev_content',{onComplete  : function() {	myLightbox.updateImageList(); }});
}

function showComments() {
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[$('hdn_Categoria').value];
	var idRound = $('idTandaActual').value;
	var strMode =  objState.strMode;
	objState.strRightBar = 'comments';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	document.getElementById('div_ppcev_playoff').innerHTML = '';
	if (idRound!='' && idRound!='null') {
		AppObj.Ajax.updaterAjax('/modules/ppcev/getcomments.ajax.php?idrace='+idRace+'&idround='+idRound+'&mode='+strMode,'div_ppcev_right');
	} else {
		//En caso que no haya comentarios cargados
		AppObj.Ajax.updaterAjax('/modules/ppcev/views/nocomentarios.htm','div_ppcev_right');
	}
}

function showDriversByCat(idCat) {
	AppObj.Ajax.updaterAjax('/modules/ppcev/drivers.ajax.php?idrace=' + idRace[idCat] ,'div_drivers');
	$('showOnlyEnabled').checked=false;
}

function showSerieGrid() {
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[$('hdn_Categoria').value];
	objState.strContent = 'grid';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	AppObj.Ajax.updaterAjax('/modules/ppcev/grid.ajax.php?idrace=' + idRace + '&round=serie','div_ppcev_content');
}
function showFinalGrid () {
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[$('hdn_Categoria').value];
	objState.strContent = 'grid';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	AppObj.Ajax.updaterAjax('/modules/ppcev/grid.ajax.php?idrace=' + idRace + '&round=final','div_ppcev_content');
}

function changeGridSerie (intSerie) {
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[$('hdn_Categoria').value];
	objState.strContent = 'grid';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	AppObj.Ajax.updaterAjax('/modules/ppcev/grid_serie.ajax.php?idrace=' + idRace + '&serie=' + intSerie,'grid_content');
}
//
function showStats() {
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[objState.intDefaultCat];
	objState.strContent = 'stats';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	var aleatorio = Math.round(Math.random()*10000);
	strUrl = '/upload/htmls/ppcev/estadisticas_'+idRace+'.htm?ale='+aleatorio;
	AppObj.Ajax.updaterAjax(strUrl,'div_ppcev_content');
}
function changeStatsCat(intCat) {
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[intCat];
	objState.strContent = 'stats';
	$('hdn_ppcev_data').value = Object.toJSON(objState);
	var aleatorio = Math.round(Math.random()*10000);
	strUrl = '/upload/htmls/ppcev/estadisticas_'+idRace+'.htm?ale='+aleatorio;
	AppObj.Ajax.updaterAjax(strUrl,'div_ppcev_content');
}
function showGroups() {
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[objState.intDefaultCat];
	objState.strContent = 'groups';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	var aleatorio = Math.round(Math.random()*10000);
	strUrl = '/upload/htmls/ppcev/grupos_'+idRace+'.htm?ale='+aleatorio;
	AppObj.Ajax.updaterAjax(strUrl,'div_ppcev_content');
}
function showCars() {
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[objState.intDriversCat];
	objState.strContent = 'cars';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	strUrl = '/upload/htmls/ppcev/fulldrivers_'+idRace+'.htm?ale='+aleatorio;
	AppObj.Ajax.updaterAjax(strUrl,'div_ppcev_content');
}
function changeCarsTab(intCat) {
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	objState.intDriversCat = intCat;
	var idRace = objState.idRace[objState.intDriversCat];
	objState.strContent = 'cars';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	strUrl = '/upload/htmls/ppcev/fulldrivers_'+idRace+'.htm?ale='+aleatorio;
	AppObj.Ajax.updaterAjax(strUrl,'div_ppcev_content');
}

function goGraphicMode(object) {
	$('hdn_Modo').value = 'graphic';
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[objState.intDefaultCat];
	var idRound = objState.idRound[objState.intDefaultCat];
	objState.strMode = 'graphic';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/tanda_'+idRace+'_'+idRound+'_grafico.htm?ale='+aleatorio,'div_ppcev_content',{onComplete: function() {	Effect.BlindDown('div_graphic_mode');	Effect.BlindUp('div_table_mode');}});
	if (objState.strRightBar == 'comments') {
		showComments();	
	}
}

function goTableMode(object) {
	$('hdn_Modo').value = 'table';
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	var idRace = objState.idRace[objState.intDefaultCat];
	var idRound = objState.idRound[objState.intDefaultCat];
	objState.strMode = 'table';
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/tanda_'+idRace+'_'+idRound+'.htm?ale='+aleatorio,'div_ppcev_content',{onComplete: function() {	Effect.BlindUp('div_graphic_mode'); Effect.BlindDown('div_table_mode');}});
	if (objState.strRightBar == 'comments') {
		showComments();	
	}
}

function changeRound(idRound) {
	$('idTandaActual').value = idRound;
	var objState = $V('hdn_ppcev_data').evalJSON();
	objState.idRound[objState.intDefaultCat] = idRound;
	$('hdn_ppcev_data').value = Object.toJSON(objState);	
	showResults(0);
	if (objState.strRightBar == 'comments') {
		showComments();	
	}
}

function changeScheduleCat (intCat) {
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	strUrl = '/upload/htmls/ppcev/cron_'+objState.idRace[intCat]+'.htm?ale='+aleatorio;
	AppObj.Ajax.updaterAjax(strUrl,'DivSchedule');
}

function showSchedule() {
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	strUrl = '/upload/htmls/ppcev/croncompleto_'+objState.idRace[1]+'_'+objState.idRace[2]+'.htm?ale='+aleatorio;
	AppObj.Ajax.updaterAjax(strUrl,'div_ppcev_content');
}

function showResults(intLastRound) {
	var aleatorio = Math.round(Math.random()*10000);
	var objState = $V('hdn_ppcev_data').evalJSON();
	var strRaces = Object.toJSON(objState.idRace);		
	var idRace = objState.idRace[$('hdn_Categoria').value];
	if (intLastRound==0) {
		var idRound = objState.idRound[objState.intDefaultCat];
	} else {
		var idRound = intLastRound;
	}
	if (idRound==null) { //En caso que se abra la venatana y aun no haya resultados cargados
		AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/cron_'+idRace+'.htm?ale='+aleatorio,'div_ppcev_content');
	} else {
		ViewChangeMode(idRound);
		objState.strContent = 'results';
		$('hdn_ppcev_data').value = Object.toJSON(objState);
		AppObj.Ajax.updaterAjax('/upload/htmls/ppcev/tanda_'+idRace+'_'+idRound+'.htm?ale='+aleatorio,'div_ppcev_content');
	}
}

function ViewChangeMode(idRound) {
	var objState = $V('hdn_ppcev_data').evalJSON();
	if (idRound==4 || idRound==5 || idRound==6 || idRound==7 || idRound==12 || idRound==13 || idRound==14 || idRound==15 || idRound==8) {
		if (idRound == objState.intRunningRound) {
			$('DivChangeModeVisualization').style.display='';
		} else {
			$('DivChangeModeVisualization').style.display='none';
		}
	} else {
		$('DivChangeModeVisualization').style.display='none';
	}
}