function carrusel(cnt)
{
  this.articulos;
  this.numarticulos;
  this.fadeinout = fadeinout;
  this.carrswitch = carrswitch;
  this.startstop = 1;
  this.childs = cnt+'_carrusel_';
  this.interval;
  this.start = start;
  this.stop = stop;
  this.ultima;
  var _self = this;
  
  var initcnt;
  var elem;
  var newelems = new Array();
  
  
  initcnt = document.getElementById(cnt);
  
  if(initcnt)
  { 
    elem = initcnt.getElementsByTagName("td");
    if(elem)
    {
      this.numarticulos = elem.length;
      var cont = 0;
      for (var e=0;e<elem.length;e++)
      {
        var divinner = document.createElement('DIV');
        divinner.innerHTML = elem[e].innerHTML;
        newelems[cont] = divinner;
        cont++;
      }
      
      while (initcnt.childNodes.length >= 1)
      {
        initcnt.removeChild(initcnt.lastChild);
      }
      
      var div = document.createElement('DIV');
      div.style.position = 'relative';
      div.style.backgroundColor = '#ffffff';

      var control = document.createElement('IMG');
      control.src = '/pics/tools/pausa.png';
      control.style.position = 'absolute';
      control.style.top = '2px';
      control.style.right = '2px';
      control.style.cursor = 'pointer';
      // inicio rav
      control.style.display = 'none';
      // fin rav
      clickcontrol(control, _self);
      
      for (var e=0;e<newelems.length;e++)
      {
        var index = new Number(e);
        var divinfo = document.createElement('DIV');

        divinfo.style.overflow = 'auto';
        divinfo.appendChild(newelems[e]);
        divinfo.id = cnt+'_carrusel_'+e;
        clicknext(divinfo, _self, index);
        
        if(e != 0)
        {
          divinfo.style.backgroundColor = '#ffffff';
          //divinfo.style.width = '275px';
          //divinfo.style.height = '130px';
          divinfo.style.display = 'none';
          divinfo.style.opacity = '0';
          divinfo.style.filter = 'alpha(opacity= 0)';          
        }
        div.appendChild(control);
        div.appendChild(divinfo);
      }
      initcnt.appendChild(div);
    }
  }
}

function clickcontrol(img, obj)
{
  img.onclick = function (){clickc(img, obj)};
}

function clickc(img, obj)
{
  if(obj.startstop == 1)
  {
    img.src = '/pics/tools/play.png'
    obj.stop();
    obj.startstop = 2;
  }
  else if(obj.startstop == 2)
  {
    img.src = '/pics/tools/pausa.png'
    obj.fadeinout(obj.actual);
    obj.startstop = 1;
  }
}


function clicknext(div, obj, index)
{
  div.onclick = function (){clickn(obj,index)};
}

function clickn(obj, index)
{
  if(obj.startstop == 2)
    obj.carrswitch(index);
}

function start()
{
  this.fadeinout();
}

function stop()
{
  clearInterval(this.interval)
}

function fadeinout(cont)
{
  if(cont != undefined)
    var cont = cont;
  else
    var cont = 0;
  
  var obj = this;
  this.interval = setInterval(function ()
  {
    if(cont >= obj.numarticulos)
      cont = 0;
    obj.carrswitch(cont);
    cont++;

  },5525);
}

function carrswitch(index)
{
  var childs = this.childs;
  var numarticulos = this.numarticulos;
  var out = document.getElementById(childs+index);
  index++
  if(index == numarticulos)
    index = 0;
  var ind = document.getElementById(childs+index);
  this.actual = new Number(index);

  fade(out, 'out');
  setTimeout(function()
  {
    out.style.display = 'none';
    ind.style.display = 'block';
    fade(ind, 'in');
  },525);
}

function fade(obj, opc, cont)
{
  if(opc == 'in')
  {
    if(cont > 20)
      return;
    else if(cont >= 0)
    {
      cont = cont + 1;
    }
    else
      return setTimeout(function(){fade(obj,opc,0)},50);
  }
  else if(opc == 'out')
  {
    if(cont < 0)
      return;
    else if(cont <= 20)
    {
      cont = cont - 1;
    }
    else
      return setTimeout(function(){fade(obj,opc,20)},50);
  }
    
  obj.style.backgroundColor = '#ffffff';
  //obj.style.width = '275px';
  //obj.style.height = '130px';
  obj.style.opacity = cont/20;
  obj.style.filter = 'alpha(opacity= '+(cont*5)+')';
  setTimeout(function(){fade(obj,opc,cont)},25);
  
}

function carruselcreate(carr)
{
  var cont = 0;
  var carruseles = new Array();
  for (var i in carr)
  {
    carruseles[cont] = new carrusel(carr[i]);
    cont++;
  }
  
  for(var i in carruseles)
  {
    carruseles[i].start();
  }
}

function getCarrusel()
{
  //En este array se agregan los div de cada carrusel de notas
  var carruseles = new Array('medios','fotos');
  carruselcreate(carruseles)
}
window.onload = getCarrusel;
