//
//  (ñ) SiteLeader, 2005-2008 | http://www.siteleader.ru
//

var
  count_w = 7, // count of iterations
  count_h = 7,
  count_flash = 5,

  wait = 20, // delay

  tbl_max_w = 93, // table width (%)
  tbl_row_max_h = 170, // table row height (px)

  tbl_step_w = tbl_max_w / count_w, // steps
  tbl_row_step_h = tbl_row_max_h / count_h,

  tbl_w = tbl_step_w, // current width
  tbl_row_h = tbl_row_step_h, // current row height

  flash_max_w = 284, // flash width (px)
  flash_max_h = 151, // flash height (px)

  flash_step_w = flash_max_w / count_flash, 
  flash_step_h = flash_max_h / count_flash,

  flash_w = flash_step_w, 
  flash_h = flash_step_h;

var
  timer, table, objects, flashes, tables;

//--------------------------------------------------------
function set_rows_h(value)
{
  for(i = 0; i < table.rows.length; i++)
  {
    table.rows[i].cells[0].style.height = value + 'px';
  }
}

//--------------------------------------------------------
function set_objects_display(obj_id, value)
{
  for (i = 0; i < objects.length; i++)
  {
    if (objects[i].id == obj_id)
      objects[i].style.display = value;
  }
}

//--------------------------------------------------------
function wait_flash_loading(tbl_id, bg_id, sheet_id, flash_id)
{
  for (i = 0; i < flashes.length; i++)
  {
    if (flashes[i].id == 'flash_obj' + (i + 1))
      if (flashes[i].PercentLoaded() < 100)
      {
        timer = setTimeout ( function() { wait_flash_loading(tbl_id, bg_id, sheet_id, flash_id) }, 250 );
        return;
      }
  }
  
  clearTimeout(timer);

  for (i = 0; i < flashes.length; i++)
  {
    if (flashes[i].id == 'flash_obj' + (i + 1))
      flashes[i].StopPlay();
  }

  setTimeout( function() { do_play(tbl_id, bg_id, sheet_id, flash_id) }, 250 );
}

//--------------------------------------------------------
function do_play(tbl_id, bg_id, sheet_id, flash_id)
{
  // get table
  table = document.getElementById(tbl_id);
  if (!table) return;
  
  // get objects
  //objects = document.getElementsByTagName('div');
  //if (!objects) return;
  
  // hide sheets
  //set_objects_display(sheet_id, 'none');

  // hide flashes
  //set_objects_display(flash_id, 'none');

  // setting start values
  table.style.width = tbl_w + '%';
  set_rows_h(tbl_row_h);
  
  // GO phase1!
  timer = setInterval( function() { phase0(bg_id, sheet_id, flash_id) }, wait );
}

//--------------------------------------------------------
function phase0(bg_id, sheet_id, flash_id)
{
  if (tbl_row_h < tbl_row_max_h)
  {
    tbl_row_h += tbl_row_step_h;
    set_rows_h(tbl_row_h);
  }
  else
  {
    clearInterval(timer);

    // setting max value
    set_rows_h(tbl_row_max_h);

    // GO phase1!
    timer = setInterval( function() { phase1(bg_id, sheet_id, flash_id) }, wait );
  }
}

//--------------------------------------------------------
function phase1(bg_id, sheet_id, flash_id)
{
  if (tbl_w < tbl_max_w)
  {
    tbl_w += tbl_step_w;
    table.style.width = tbl_w + '%';
  }
  else
  {
    clearInterval(timer);

    // setting max values
    table.style.width = tbl_max_w + '%';
    
    tables = document.getElementsByTagName('table');
    if (!tables) return;

    // GO phase2!
    timer = setInterval( function() { phase2(bg_id, sheet_id, flash_id) }, wait );
  }
}

//--------------------------------------------------------
function set_bgs_size(bg_id, width, height)
{
  for (i = 0; i < tables.length; i++)
  {
    if (tables[i].className.toLowerCase() == bg_id)
    {
      tables[i].rows[0].cells[0].style.width = width + 'px';
      tables[i].rows[0].cells[0].style.height = height + 'px';
    }
  }

  /*
  for (i = 0; i < objects.length; i++)
  {
    if (objects[i].id == flash_id)
    {
      objects[i].style.width = width + 'px';
      objects[i].style.height = height + 'px';
    }
    else if (objects[i].id == sheet_id)
    {
      objects[i].style.width = width + 'px';
    }
  }
  */
}

//--------------------------------------------------------
// for function 'phase3' and 'phase4'
var obj_index = 0, flash_index = 1, flash_duration = 200; // <- 5 frames (26 per second)

//--------------------------------------------------------
function phase2(bg_id, sheet_id, flash_id)
{
  if (flash_w < flash_max_w)
  {
    flash_w += flash_step_w;
    flash_h += flash_step_h;
    set_bgs_size(bg_id, flash_w, flash_h);
  }
  else
  {
    clearInterval(timer);
    
    // setting max values
    for (i = 0; i < objects.length; i++)
    {
      if (objects[i].id == flash_id)
      {
        objects[i].style.width = flash_max_w + 'px';
        objects[i].style.height = flash_max_h + 'px';
      }
      else if (objects[i].id == sheet_id)
      {
        objects[i].style.width = flash_max_w + 'px';
      }
    }

    // GO phase3!
    obj_index = 0;
    flash_index = 1;
    set_objects_display('flash','none');
    set_objects_lefttop('flash','auto','auto');
    timer = setInterval( function() { phase3(sheet_id, flash_id) }, flash_duration );
  }
}

//--------------------------------------------------------
var sheet_margin = 0; // for 'phase4'

//--------------------------------------------------------
function phase3(sheet_id, flash_id)
{
  while (1)
  {
    if (obj_index < objects.length)
    {
      if (objects[obj_index].id == flash_id) break;
      obj_index++;
    }
    else
    {
      clearInterval(timer);
      
      // show sheets
      set_objects_display('sheet', 'block');

      // GO phase4!
      sheet_margin = 0;

      timer = setInterval( function() { phase4(sheet_id) }, wait );
      return;
    }
  }

  // show flash
  objects[obj_index++].style.display = 'block';

  if (navigator.userAgent && (navigator.userAgent.indexOf("MSIE") >= 0))
  {
    for (i = 0; i < flashes.length; i++)
    {
      if (flashes[i].id == 'flash_obj' + flash_index)
      {
        flashes[i].Rewind();
        flashes[i].Play();
        flash_index++;
        return;
      }
    }
  }
}

//--------------------------------------------------------
function set_objects_marginTop(obj_id, value)
{
  for (i = 0; i < objects.length; i++)
  {
    if (objects[i].id == obj_id)
      objects[i].style.marginTop = value;
  }
}

//--------------------------------------------------------
function set_objects_lefttop(obj_id, _left, _top)
{
  for (i = 0; i < objects.length; i++)
  {
    if (objects[i].id == obj_id)
    {
      objects[i].style.left = _left;
      objects[i].style.top = _top;
    }
  }
}

//--------------------------------------------------------
var max_margin = 18;

//--------------------------------------------------------
function phase4(sheet_id)
{
  if (sheet_margin < max_margin)
  {
    sheet_margin += 2;
    set_objects_marginTop(sheet_id, -sheet_margin + 'px')
  }
  else
  {
    clearInterval(timer);
    set_objects_marginTop(sheet_id, -max_margin + 'px')
  }
}

