
var Browser = {
  isIE:function ()
  {
    if( Browser._vIE!=-1 )
      return Browser._vIE>0;

    var re = /MSIE ([0-9]+)/;
    r = navigator.userAgent.match(re);

    if( r ) Browser._vIE = r[1];
    else Browser._vIE = 0;

    if( Browser._vIE>0 ) {
      Browser._tableDisplay='block';
      Browser._trDisplay='block';
    }

    return Browser._vIE>0;
  },

  ieVer:function()
  {
    if( Browser._vIE==-1 )
      Browser.isIE();
    return Browser._vIE;
  },

  _tableDisplay: 'table',
  _trDisplay: 'table-row',
  _vIE:-1
};


var Opacity = {
  //  set the opacity for different browsers
  set: function( id, val )
  {
    var v = val/100;
    var o = document.getElementById(id);
    var s = o.style;
    s.opacity = (v);
    s.MozOpacity = (v);
    s.KhtmlOpacity = (v);
    s.filter = "alpha(opacity=" + val + ")";
  },

  setStyle: function( s, val )
  {
    var v = val/100;
    s.opacity = (v);
    s.MozOpacity = (v);
    s.KhtmlOpacity = (v);
    s.filter = "alpha(opacity=" + val + ")";
  },

  get: function( id )
  {
    var val = 0;

    //if the element has an opacity set, get it
    if(document.getElementById(id).style.opacity < 100) {
      val = document.getElementById(id).style.opacity * 100;
    }
    else {
      val = 100;
    }

    return parseInt(val,10);
  }
};

function trim(val)
{
  str = new String(val);
  while (str.substr(0,1) == ' ')
    str = str.substr(1, str.length);
  while (str.substr(str.length-1, str.length) == ' ')
    str = str.substr(0,str.length-1);
  return str;
};


var DarthFader = {

  _data:new Array(),
  _pauses:new Array(),
  _div:new Array(),
  _iDiv:0,
  _bFirst:true,
  _mode:'',
  _href:'',


  _pauseMS:2000,

  // fade out current
  // hide current
  // show next
  // fade in
  // pause

  init: function() {
    if( !document.getElementById ) return false;
    var d = document.getElementById('darthfader'); if( !d ) return;

    var ie6 = Browser.ieVer()==6;
    if( ie6 ) {
      var p = getPosition(d);
    }

    var o = d.getElementsByTagName('div');
    var div;
    if( o.length==0 ) { // two divs for swapping content
      div = document.createElement('div'); div.id="ddf0";d.appendChild( div ); div.style.position="absolute";DarthFader._div.push(div);
      if( ie6 ) {
        div.style.left = p.x+'px';
        div.style.top = p.y+16+'px';
      }
      div = document.createElement('div'); div.id="ddf1";d.appendChild( div ); div.style.position="absolute";DarthFader._div.push(div);
      if( ie6 ) {
        div.style.left = p.x+'px';
        div.style.top = p.y+16+'px';
      }
    }

    DarthFader._mode = d.getAttribute('mode');
    DarthFader._href = d.getAttribute('href');

    var a = d.getAttribute('imgs');
    a = a.split(',');
    var s;

    for( var i=0; i<a.length; i++ ) {
      s = trim(a[i]);
      if( s && s.length ) {
        DarthFader._data.push( s );
      }
    }


    a = d.getAttribute('pauses');
    if( a ) {
      a = a.split(',');

      for( i=0; i<a.length; i++ ) {
        s = trim(a[i]);
        if( s && s.length ) {
          DarthFader._pauses.push( parseInt(s,10) );
        }
      }
    }

    DarthFader.setNextContent();
    DarthFader.waitForNextImage();
  },

  setNextContent: function () {
    if( DarthFader._bFirst ) {
      var d = document.getElementById('darthfader'); if( !d ) return;
      var idx = d.getAttribute('start');
      if( idx=='random' ) {
        DarthFader._idx = parseInt( Math.random()*DarthFader._data.length, 10 );
      }
      else if( idx>0 ) {
        DarthFader._idx = idx;
      }
      else {
        DarthFader._idx = 0;
      }
    }
    else if( DarthFader._mode=='random' ) {
      DarthFader._idx = parseInt( Math.random()*DarthFader._data.length, 10 );
    }
    else if( DarthFader._mode=='random2nd' ) {
      DarthFader._idx = parseInt( Math.random()*DarthFader._data.length, 10 );
      DarthFader._mode = '';
    }
    else if( ++DarthFader._idx>=DarthFader._data.length ) {
      DarthFader._idx = 0;
    }

    var imsrc = DarthFader._data[DarthFader._idx];

    var div = DarthFader.getNextDiv();

    var h = '<img src="'+imsrc+'" />';
    if( DarthFader._href ) {
      h = '<a href="'+DarthFader._href+'">'+h+'</a>';
    }
    div.innerHTML = h;
    if( DarthFader._pauses[DarthFader._idx] ) {
      DarthFader._pauseMS = DarthFader._pauses[DarthFader._idx];
    }
    else {
      DarthFader._pauseMS = 0;
    }

//    DarthFader.removeImageIfNotExists(imsrc);
  },

//  removeImageIfNotExists: function(imsrc) {
//    Query.request(imsrc, DarthFader.removeImageIfNotExists_Callback)
//  },

//  removeImageIfNotExists_Callback: function( status ) {
//    if( !status ) {
//      // img did not exist
//      var div = DarthFader.getNextDiv();
//      div.innerHTML = '<img src="../feed/t.php?h=150&w=515&f=32&t='+escape(DarthFader._data[DarthFader._idx])+'" />';
//    }
//  },

  getNextDiv: function() {
    return DarthFader._div[1-DarthFader._iDiv];
  },

  getCurrentDiv: function() {
    return DarthFader._div[DarthFader._iDiv];
  },

  waitForNextImage: function() {
    if( !DarthFader.isNextLoaded() ) {
      setTimeout('DarthFader.waitForNextImage()',250);
      return;
    }

    DarthFader.showNext();
  },

  isNextLoaded: function() {
    var div = DarthFader.getNextDiv();

    var im = div.getElementsByTagName('img');
    for( var i=0; i<im.length; i++ ) {
      if( !DarthFader.isImageLoaded(im[i]) ) {
        return false;
      }
    }
    return true;
  },

  isImageLoaded: function(im) {
    if( im.naturalWidth!=undefined && im.naturalWidth==0 )
      return false;
    return im.complete;
  },

  moveNext: function() {
    DarthFader.setNextContent();
    DarthFader.hideCurrent();
  },

  hideCurrent: function() {
    if( !DarthFader.isNextLoaded() ) {
      setTimeout('DarthFader.hideCurrent()',250);
      return;
    }
    var o = DarthFader.getCurrentDiv();
    o.style.zIndex=1;
    DarthFader.showNext();
  },

  showNext: function() {
    var o = DarthFader.getCurrentDiv();
//    o.style.display='none';

    DarthFader._iDiv = 1 - DarthFader._iDiv;

    o = DarthFader.getCurrentDiv();
    Opacity.set(o.id,0);
    o.style.display='block';
    o.style.zIndex=2;
    document.getElementById('debug').innerHTML=o.id;

    if( DarthFader._bFirst ) {
      DarthFader._bFirst = false;
      Opacity.set(o.id,100);
      setTimeout('DarthFader.moveNext()',DarthFader._pauseMS);
    }
    else {
      DarthFader.fadeTo( o, 100, "setTimeout('DarthFader.moveNext()',"+DarthFader._pauseMS+")" );
    }
  },

  fadeTo: function( o, to, onComplete ) {
    clearInterval( DarthFader.timerId );
    DarthFader.bFadeIn = parseInt(to,10)>0 ? true : false;
    DarthFader.to = DarthFader.bFadeIn ? 100 : 0;
    DarthFader.cur = parseInt(Opacity.get(o.id),10);
    DarthFader.onComplete = onComplete;
    DarthFader.target = o;
    DarthFader.style = o.style;

    DarthFader.timerId = setInterval( 'DarthFader.step()', 25 );
  },

  step: function() {
    Opacity.setStyle( DarthFader.style, DarthFader.cur );

    if( DarthFader.cur==DarthFader.to ) {
      clearInterval( DarthFader.timerId );
      if( DarthFader.onComplete ) eval(DarthFader.onComplete);
    }
    else if( DarthFader.bFadeIn ) {
      if( DarthFader.cur<5 ) DarthFader.cur++;
      else if( DarthFader.cur>95 ) DarthFader.cur++;
      else DarthFader.cur += 1;
    }
    else {
      if( DarthFader.cur<5 ) DarthFader.cur--;
      else if( DarthFader.cur>95 ) DarthFader.cur--;
      else DarthFader.cur -= 1;
    }
  },

  timerId:0,
  ms:0,
  onComplete:null,
  cur:0,
  to:0,
  dir:0,
  delta:0,
  target:null,
  style:null
};





function blur(o) { if(o.blur)o.blur() };


