jQuery.fn.centrar = function()
{
    ancho = this.width();
    ancho_contenedor = this.parent().width();
//    alert('ancho:'+ancho+', ancho contenedor:'+ancho_contenedor)
    this.attr('style','margin-left:' + (ancho_contenedor-ancho)/2 + 'px');
}
function destacar_noticia(not,num)
{
	for (i=0; i<num; i++)
  {
    document.getElementById('noticia'+i).className="noticia_thumbnail";
  }
  document.getElementById('noticia'+not).className="noticia_thumbnail_destacado";
}
function sobre(objeto,raton,color)
{
   objeto.style.cursor=raton;
   objeto.bgColor=color;
}
function fuera(objeto,color)
{
   objeto.bgColor=color;
}
function cambiamos(objeto,propiedad,color)
{
   eval(objeto+".style."+propiedad + " = '"+ color + "'");
}

// Funciones de la p�gina de estad�sticas de un jugador
function cambiar_foto()
{
  xajax_cambiar_foto_jug(126,"jugadores");
  setTimeout(cambiar_foto,20000);
} 
function cambiaANTIGUO(obj,unid,color,id_tem)
{
  if (!id_tem) id_tem="0";
  if (obj.id=='' & (obj.seleccionado=='' | obj.seleccionado==null))
  {
    obj.style.background=color;
    obj.id=unid;
    if (id_tem!="0")
      document.getElementById(id_tem).style.background=color;
  }
  else if (obj.seleccionado=='' | obj.seleccionado==null)
  {
    obj.style.background='#FFFFFF';
    obj.id='';
    if (id_tem!="0")
      document.getElementById(id_tem).style.background='#FFFFFF';
  }
}
function cambia(obj,unid,color,id_tem)
{
  if (!id_tem) id_tem="0";
  if (obj.lang=='')
  {
    obj.style.backgroundColor=color;
    obj.lang='es';
    if (id_tem!="0")
      document.getElementById(id_tem).style.backgroundColor=color;
  }
  else
  {
    obj.style.backgroundColor='#FFFFFF';
    obj.lang='';
    if (id_tem!="0")
      document.getElementById(id_tem).style.backgroundColor='#FFFFFF';
  }
}

function cambia_click(obj,unid,color,id_tem)
{
  if (obj.seleccionado=='' | obj.seleccionado==null)
  {
    obj.style.background=color;
    obj.seleccionado=unid;
    if (id_tem!="0")
      document.getElementById(id_tem).style.background=color;
  }
  else
  {
    obj.style.background='#FFFFFF';
    obj.seleccionado='';
    if (id_tem!="0")
      document.getElementById(id_tem).style.background='#FFFFFF';
  }
}


function cambia2(obj,id_tem)
{
  if (!id_tem) id_tem="0";
  if (obj.lang=='')
  {
    obj.style.backgroundColor='#FFFFFF';
    if (id_tem!="0")
      document.getElementById(id_tem).style.backgroundColor='#FFFFFF';
  }
}
function cambia2ANTIGUO(obj,id_tem)
{
  if (!id_tem) id_tem="0";
  if (obj.id=='' & (obj.seleccionado=='' | obj.seleccionado==null))
  {
    obj.style.background='#FFFFFF';
    if (id_tem!="0")
      document.getElementById(id_tem).style.background='#FFFFFF';
  }
}
function cambia_simple(obj,unid,color)
{
  if (obj.id=='')
  {
    obj.style.background=color;
    obj.id=unid;
  }
  else
  {
    obj.style.background='#FFFFFF';
    obj.id='';
  }
}
function cambia2_simple(obj)
{
  if (obj.id=='')
  {
    obj.style.background='#FFFFFF';
  }
}

function abre(fichero,titulo,opciones)
{
  window.open(fichero,titulo,opciones);
}
function compruebaEmail(form)
{
	var letras = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"
	correo = form.email.value
	posarroba1 = correo.indexOf("@",0)
	posarroba2 = correo.indexOf("@",posarroba1+1)
	if (posarroba2 != -1 || posarroba1 == -1)
		return false
	partes = correo.split("@")
	for (i=0; i!=2; i++)
		{
		if (partes[i] == "")
				return false
		longitud = partes[i].length
		primeravez = 1
		for (j=0; j<=longitud; j++)
				{
				if (letras.indexOf(partes[i].charAt(j)) == -1)
					return false
				}
		pospunto1 = -1
		pospunto2 = -1
		do {
				pospunto1 = partes[i].indexOf(".",pospunto2)
				pospunto2 = partes[i].indexOf(".",pospunto1+1)
				if (pospunto1 == 0 || (i == 1 && pospunto1 == longitud-1) || (i == 1 && pospunto2 == longitud-1) || pospunto2 == pospunto1+1 || (i == 1 && pospunto1 == -1 && primeravez == 1))
					return false
				if ((pospunto1 == longitud-1 && i == 0) || (pospunto2 == longitud-1 && i == 0))
					return true
				primeravez = 0
				if (pospunto2 == -1)
					pospunto2 = longitud-1
				} while (pospunto1 != -1)
		}
	return true
}
function cargando(mensaje)
{
	xajax.$('cargando_mensaje').innerHTML = mensaje;
	xajax.callback.global.onRequest = function() {xajax.$('cargando').style.display = 'block';}
	xajax.callback.global.beforeResponseProcessing = function() {xajax.$('cargando').style.display = 'none';};
}

function cantera(){
		var stretchers = $$('div.accordion');
		stretchers.each(function(item){item.setStyles({'height': '0', 'overflow': 'hidden','display':'block'});});
		window.onload = function(){ //safari cannot get style if window isnt fully loaded
			var togglers = $$('h3.toggler');
			var bgFx = [];
			togglers.each(function(toggler, i){
				toggler.defaultColor = toggler.getStyle('background-color');			
				bgFx[i] = new Fx.Color(toggler, 'background-color', {wait: false});
			});
	
		var myAccordion = new Fx.Accordion(togglers, stretchers, {opacity: false, start: false, transition: Fx.Transitions.quadOut,onActive: function(toggler, i){
				bgFx[i].toColor('#ffffff'); //Este es el color al seleccionar
				toggler.getFirst().setStyle('color', '#3D127A');
			},
			onBackground: function(toggler, i){
				bgFx[i].clearTimer();
				toggler.setStyle('background-color', toggler.defaultColor);
				toggler.getFirst().setStyle('color', '#ffffff');
			}
		});
		
		//anchors
		function checkHash(){
			var found = false;
			$$('h3.toggler a').each(function(link, i){if (window.location.hash.test(link.hash)){myAccordion.showThisHideOpen(i);found = true;}});
			return found;
		}

		if (!checkHash()) myAccordion.showThisHideOpen(0);
		
		var ball = $E('#header h1');
		var ballStyles = new Fx.Styles(ball, {duration: 800, transition: Fx.Transitions.elasticOut});
		new Drag.Move(ball, { 
			onComplete: function(){
				ballStyles.custom({'top': [this.element.getStyle('top').toInt(), 13], 'left': [this.element.getStyle('left').toInt(), 358]});
			}
		});
	};
	try {Window.disableImageCache();}catch(e){}
}
function ordenacampo(property)
{
  return function (a,b)
  {
    if (a.minutos==0 && property!="dorsal" && property!="nombre")
      return 1;
    else if (b.minutos==0 && property!="dorsal" && property!="nombre")
      return -1;
    else if (property=="minutos" | property=="t1p" | property=="t2p" | property=="t3p")
    {
      if (property=="minutos")
        return (a[property] < b[property]) ? 1 : (a[property] > b[property]) ? -1 : 0;
      if (property=="t1p")
        return (a[property] < b[property]) ? 1 : (a[property] > b[property]) ? -1 : (a.t1c < b.t1c) ? 1 : (a.t1c > b.t1c) ? -1 : a.minutos-b.minutos;
      if (property=="t2p")
        return (a[property] < b[property]) ? 1 : (a[property] > b[property]) ? -1 : (a.t2c < b.t2c) ? 1 : (a.t2c > b.t2c) ? -1 : a.minutos-b.minutos;
      if (property=="t3p")
        return (a[property] < b[property]) ? 1 : (a[property] > b[property]) ? -1 : (a.t3c < b.t3c) ? 1 : (a.t3c > b.t3c) ? -1 : a.minutos-b.minutos;
    }
    else
    {
      return (a[property] < b[property]) ? 1 : (a[property] > b[property]) ? -1 : a.minutos-b.minutos;
    }
  }
}
function reordena(equipo,modo)
{
  if (equipo=="jug_cbm")
  {
    jugadores = jug_cbm;
  }
  else
  {
    jugadores = jug_riv;
  }
  indice = [];
  ind = 0;
  for (i=(jugadores.length-1);i>=0;i--)
  {
    indice[ind] = i;
    ind++;
  }
  for(i=0;i<jugadores.length;i++)
  {
    if (modo=="asc")
    {
      j = indice[i];
    }
    else
    {
      j=i;
    }
    $("#"+equipo+i+" td").eq(0).html(jugadores[j].dorsal);
    $("#"+equipo+i+" td").eq(1).html(jugadores[j].nombreenlace);
    $("#"+equipo+i+" td").eq(2).html(jugadores[j].titular);
    $("#"+equipo+i+" td").eq(3).html(jugadores[j].tiempo);
    $("#"+equipo+i+" td").eq(4).html(jugadores[j].puntos);
    if (jugadores[j].t1c!="-")
    {
      $("#"+equipo+i+" td").eq(5).html(jugadores[j].t1c+"/"+jugadores[j].t1i);
    }
    else
    {
      $("#"+equipo+i+" td").eq(5).html(jugadores[j].t1c);
    }
    $("#"+equipo+i+" td").eq(6).html(jugadores[j].t1p);
    if (jugadores[j].t2c!="-")
    {
      $("#"+equipo+i+" td").eq(7).html(jugadores[j].t2c+"/"+jugadores[j].t2i);
    }
    else
    {
      $("#"+equipo+i+" td").eq(7).html(jugadores[j].t2c);
    }
    $("#"+equipo+i+" td").eq(8).html(jugadores[j].t2p);
    if (jugadores[j].t3c!="-")
    {
      $("#"+equipo+i+" td").eq(9).html(jugadores[j].t3c+"/"+jugadores[j].t3i);
    }
    else
    {
      $("#"+equipo+i+" td").eq(9).html(jugadores[j].t3c);
    }
    $("#"+equipo+i+" td").eq(10).html(jugadores[j].t3p);
    $("#"+equipo+i+" td").eq(11).html(jugadores[j].rd);
    $("#"+equipo+i+" td").eq(12).html(jugadores[j].ro);
    $("#"+equipo+i+" td").eq(13).html(jugadores[j].rt);
    $("#"+equipo+i+" td").eq(14).html(jugadores[j].asis);
    $("#"+equipo+i+" td").eq(15).html(jugadores[j].contra);
    $("#"+equipo+i+" td").eq(16).html(jugadores[j].tf);
    $("#"+equipo+i+" td").eq(17).html(jugadores[j].tc);
    $("#"+equipo+i+" td").eq(18).html(jugadores[j].mates);
    $("#"+equipo+i+" td").eq(19).html(jugadores[j].br);
    $("#"+equipo+i+" td").eq(20).html(jugadores[j].bp);
    $("#"+equipo+i+" td").eq(21).html(jugadores[j].fp);
    $("#"+equipo+i+" td").eq(22).html(jugadores[j].fr);
    $("#"+equipo+i+" td").eq(23).html(jugadores[j].masmenos);
    $("#"+equipo+i+" td").eq(24).html(jugadores[j].val);
  }
}

function ordena(equipo,campo,i)
{
  if (equipo=="jug_cbm")
  {
    jugadores = jug_cbm;
  }
  else
  {
    jugadores = jug_riv;
  }
  modo = "";
  for (j=0;j<25;j++)
  {
    if (j!=i && j!=2)
    {
      $("#orden"+equipo+"_"+j).attr('src',"/images/flechas_orden.gif");
      for(h=0;h<jugadores.length;h++)
      {
        $("#"+equipo+h+" td").eq(j).css('fontWeight',"normal");
      }
    }
  }
  if ($("#orden"+equipo+"_"+i).attr('src')=="/images/flechas_orden.gif")
  {
    if (equipo=="jug_cbm")
    {
      jug_cbm.sort(ordenacampo(campo));
    }
    else
    {
      jug_riv.sort(ordenacampo(campo));
    }
    if (campo=="dorsal" || campo=="nombre")
    {
      modo = "asc";
      $("#orden"+equipo+"_"+i).attr('src',"/images/flecha_asc.gif");
    }
  }
  if ($("#orden"+equipo+"_"+i).attr('src')=="/images/flecha_desc.gif" && modo=="")
  {
    $("#orden"+equipo+"_"+i).attr('src',"/images/flecha_asc.gif");
    modo = "asc";
  }
  else if (modo=="")
  {
    $("#orden"+equipo+"_"+i).attr('src',"/images/flecha_desc.gif");
    modo = "desc";
  }
  for(h=0;h<jugadores.length;h++)
  {
    $("#"+equipo+h+" td").eq(i).css('fontWeight',"bold");
  }
  reordena(equipo,modo);
}


