/**
 * Tweener class
 *
 *
 * @author    Glen
 * @copyright 2006 flickaway.com
 * @version   $Id: tweener.js 2043 2009-05-12 06:36:05Z glen $
 */


Tweener = function()
{
  this.pics =
  [
    ['pic1.jpg', 'c2c2c2'],
    ['pic2.jpg', 'c9ab94'],
    ['pic5.jpg', '7c7e7f'],
    ['pic6.jpg', '95916a'],
    ['pic7.jpg', '7e887b'],
    ['pic3.jpg', '60737b'],
    ['pic4.jpg', 'a5858a'],
    ['pic10.jpg', '515b71'],
    ['pic8.jpg', '8b7963'],
    ['pic9.jpg', '93929b']
  ];
}


p = Tweener.prototype;


Tweener.prototype.init = function()
{
  this.img_timer    = 0.5;
  this.bg_timer     = 3.5;
  this.delay_timer  = 6;

  this.s      = 0;                   // state ... 0 - means pic 0 is showing, 1 - means pic 1 is showing
  this.i      = 0;                   // index of pic showing
  this.i_next = 1;                   // index of pic showing next

  this.pic0 = $GE('pic0');
  this.pic1 = $GE('pic1');

  OpacityTween.set(this.pic1, 0);
  this.pic0.style.background = 'url(\'' + this.pics[0][0] + '\')';
  this.pic1.style.background = 'url(\'' + this.pics[1][0] + '\')';
  document.body.style.background = '#' + this.pics[0][1];

  var me = this;
  setTimeout(function(){me.swap();}, this.bg_timer * 1000);
}


Tweener.prototype.swap = function()
{
  var s=this.s, start=(s==0)?0:100, end=(s==0)?100:0;

  var ot = this.ot = new OpacityTween(this.pic1, Tween.EaseOut, start, end, this.img_timer);                        // fade out or fade in the top image
  var ct = this.ct = new ColorTween(document.body.style, 'background', Tween.easeOut, this.pics[this.i][1], this.pics[this.i_next][1], this.bg_timer);

  //ct.set_scope(this);   // cant do this!!! doh!!
  //ot.set_scope(this);
  var me = this;
  ot.onMotionFinished = function(){me._tween_done()};
  ot.start();
  ct.start();
}



Tweener.prototype._tween_done = function(event)
{
  //g_trace.log('tween done - ' + this.i_top);

  var elem, s=this.s, i=this.i+1, i_next=this.i_next+1, max=this.pics.length-1;
  this.i = (i > max) ? 0 : i;
  this.i_next = (i_next > max) ? 0 : i_next;

  if (s==0)                                                               // pic0 was showing ... now pic1 is
  {
    this.s = 1;
    elem = this.pic0;
  }
  else
  {
    this.s = 0;
    elem = this.pic1;
  }

  elem.style.background = 'url(' + this.pics[this.i_next][0] + ')';       // change the img of the element hidden

  var me = this;
  setTimeout(function(){me.swap();}, this.delay_timer * 1000);
}


g_tweener = new Tweener();
g_events.add('window', 'ondomload', g_tweener.init, g_tweener);

