function closeall(i) {
  $$('.twit').each(function(bubble,n) {
    if ( bubble.get('opacity') > 0 && i != n ) bubble.tween('opacity',0);
  });
}

function showbub(handle) {
  handle = $(handle);
  doshow = handle.retrieve('doshow');
  dotweet = handle.retrieve('req');
  dotweet.send('count=1');
  doshow.start('opacity',1);
  var index = handle.retrieve('index');
  closeall(index);
}

window.addEvent('domready',function() {

  $$('.twit').each(function(bubble,n){
    var user = bubble.getProperty('rel');
    var tweet = bubble.getElement('.tweet span');
    var logo = bubble.getElement('.twitterlogo');
    var req = new Request({
      url: 'twgetuser.php?user='+user,
      onRequest: function() {
        logo.tween('opacity',.25);
        //tweet.innerHTML = 'Loading...';
      },
      onSuccess: function(responseText,responseXml) {
        tweet.innerHTML = responseText;
        logo.tween('opacity',1);
      }
    });


    var bubbamov = new Fx.Tween(bubble);
    bubble.store('doshow',bubbamov);
    bubble.store('index',n);
    bubbamov.set('opacity',0);
    bubble.store('req',req);
    bubble.addEvents({
      'mouseleave': function(){
        bubbamov.start('opacity',0);
      },
      'mouseenter': function(){
        bubbamov.set('opacity',1);
      }
    }
    );
  });
  var cqi = 1;
  var cqh = new Fx.Tween($('cqhandle'), {link: 'chain'});
  function cqa() {
    cqh.start('opacity',.05).start('top',cqi * -155).start('opacity',1);
    if ( cqi++ == 3 ) cqi = 0;
    setTimeout(cqa,10000);
  }
  setTimeout(cqa,6000);
/*
  $('cqhandle').addEvent('click',function(){
    cqa();
  });
*/
  $$('.slidebox').each(function(box,n) {
    var boxinner = box.getElement('.slideboxinner');
    box.store('cursornum',0);
    box.store('imagenum',boxinner.getElements('img').length);
    var mover = new Fx.Tween(box.getElement('.slideboxinner'));
    box.advance = function(n) {
      var images = box.retrieve('imagenum');
      if ( typeof(n) == "undefined" ) {
        n = box.retrieve('cursornum');
        if ( n == images-1 ) n = 0;
        else n = n + 1;
      }
      var alinks = box.getElements('.slideboxcontrol a');
      alinks.each(function(e,n){
        e.setStyles( { 'border-color': '#afc450','background-color': '#eff2cf' });
      });
      alinks[n].setStyles( { 'border-color':'#532f25', 'background-color': '#cfbeb4'});

      mover.start('left',n*-215);
      box.store('cursornum',n);
    }
    box.timeadvance = function() {
      return setTimeout(function(){
        box.advance();
        box.store('interval',box.timeadvance());
      },9000);
      box.timeadvance();
      //},Math.floor(Math.random()*3+7)*1000);
    }
    boxinner.setStyle('width',boxinner.getElements('img').length*215+'px');
    for(n=0;n<box.retrieve('imagenum');n++) {
      elem = new Element('a', {
        'styles': {
          display: 'inline-block',
          border: 'solid 1px #532f25',
          width: '14px',
          height: '9px',
          margin: '4px 4px 0 0',
          background: '#eff2cf',
          lineHeight: '1px',
          float: 'left'
        },
        'href': '#'
      });
      elem.inject(box.getElement('.slideboxcontrol'));
    }
    elem = new Element('div', {
      'styles': {
        clear: 'both'
      }
    });
    elem.inject(box.getElement('.slideboxcontrol'));
    var alinks = box.getElements('.slideboxcontrol a').each(function(e,n){
      e.addEvent('click',function(){
        //box.store('interval',box.timeadvance());
        //mover.start('left',n*-215);
        //box.store('cursornum',n);
        box.advance(n);
        return false;
      });
    });
    box.advance(0);
    box.timeadvance();
  });
});
