speed_init = 1;
speed = 0;
t0 = 0;
carousel_x0_2 = 0;
carousel_y0_2 = 0;
item_width = 0;

PI_180 = Math.PI / 180;
steps = new Array();

carousel_interval = null;

function init_carousel () {
  if (!document.getElementById
      || !document.getElementById('carousel'))
    return;

  var carousel = document.getElementById('carousel');
  var items = carousel.getElementsByTagName('li');
  
  var max_w = 0;
  var max_h = 0;

  for (i = 0 ; i < items.length ; i++) {
    var img = items[i].getElementsByTagName('img')[0];
    if (!img)
      continue;

    img.style.height = 'auto';
    img = img.cloneNode(false);

    if (img.height > max_h)
      max_h = img.height;
    if (img.width > max_w)
      max_w = img.width;

    items[i].style.position = 'absolute';
    items[i].style.width = img.width+ 'px';
    

    items[i].onmouseover = function () {
      speed = 0;
    };
    items[i].onmouseout = function() {
      if (carousel_interval != null) {
        speed = speed_init;
      }
    }

    steps[i] = 360 * i/items.length;
  }

  with (carousel) {
    c_width = className.replace(/.*width_([1-9][0-9]*).*/, '$1');
    c_height = className.replace(/.*height_([1-9][0-9]*).*/, '$1');
    style.width = c_width + 'px';
    style.height = (c_height < max_h ? max_h : c_height) + 'px';
    style.position = 'relative';
  }

  carousel_x0_2 = (parseInt(carousel.style.width) - max_w) / 2;
  carousel_y0_2 = (parseInt(carousel.style.height) - max_h) / 2;
  item_width = parseInt(items[0].style.width);
      
  if (document.getElementById('carousel_commands')) {
    document.getElementById('carousel_play').onclick = function() {
      if (speed != 0) {
        stop_carousel();
      } else {
        start_carousel();
      }
    } // carousel_play.onclick()

    document.getElementById('carousel_rwd').onmousedown =
    document.getElementById('carousel_rwd').onkeydown =
      function() {
        if (speed != 0)
          speed = speed_init * -10;
        else {
          t0 -= steps[1];
          draw_carousel();
        }
      };

    document.getElementById('carousel_ffw').onmousedown =
    document.getElementById('carousel_ffw').onkeydown =
      function() {
        if (speed != 0)
          speed = speed_init * 10;
        else {
          t0 += steps[1];
          draw_carousel();
        }
      };

    document.getElementById('carousel_rwd').onmouseout =
    document.getElementById('carousel_rwd').onmouseup =
    document.getElementById('carousel_rwd').onkeyup =
    document.getElementById('carousel_rwd').onblur =
    document.getElementById('carousel_ffw').onmouseout =
    document.getElementById('carousel_ffw').onmouseup =
    document.getElementById('carousel_ffw').onkeyup =
    document.getElementById('carousel_ffw').onblur =
      function() { if (speed != 0) speed = speed_init; }

    document.getElementById('carousel_commands').style.display = 'block';
  }

  t0 = 90;
  draw_carousel();
}

function start_carousel () {
  speed = speed_init;
  carousel_interval = setInterval(play_carousel, 20);
}

function stop_carousel () {
  speed = 0;
  clearInterval(carousel_interval);
  carousel_interval = null;
}

function play_carousel() {
  if (speed == 0)
    return;
    
  t0 += speed;
  t0 %= 360;
  
  draw_carousel();
}

function draw_carousel() {
  var items = document.getElementById('carousel').getElementsByTagName('li');

  for (i = 0 ; i < items.length ; i++) {
    w = (t0 + steps[i]) * PI_180;
    cos_w = Math.cos(w);
    sin_w = Math.sin(w);
//    r = Math.pow((2 * sin_w + 1) / 3, 2) + Math.pow(cos_w / 3, 2);
    r = Math.pow((sin_w + 2) / 3, 2);

    with (items[i].style) {
      zIndex = parseInt(r * 1000);
      if (document.all && !window.opera) {
        filter  = 'alpha(opacity='+ (r*100)+ ')';
      } else {
        opacity = r;
      }
      fontSize = r+ 'em';
      top = (carousel_y0_2 * (1 + sin_w))+ 'px';
      left = (carousel_x0_2 * (1 + cos_w))+ 'px';
    }
    
    img = items[i].getElementsByTagName('img')[0];
    if (img)
      img.style.width = (item_width * r) + 'px';
  }
}

init_carousel();
