// vi:tabstop=2:expandtab
function fadeChange(remixInfo,target,newinput,type){
  var t = remixInfo.find(target);

  t.stop().animate({ opacity: 0 }, {complete:function(){
    if(type=='text'){
		  t.html(newinput);
    }else{
      t.attr('src', newinput);
    };
    if ($.browser.msie)
      t.fadeIn(function() {
        this.style.removeAttribute('filter');
      });
    else
      t.animate({ opacity: 1 });
	}});
}

function changeInfo(aRemix){
  // get to the parent div and change everything for 'featured'
  var aRemix = aRemix;
  var remixInfo = aRemix.parent().parent().parent();

  // move slider
  remixInfo.find('span.author-slider').stop().animate({
    backgroundPosition: (-12+(parseInt(aRemix.find('span.count').text())*60))+"px 0px"
  });

  // changing image
  fadeChange(remixInfo,'img.featured-avatar',aRemix.find('img').attr('src'),'img');

  // changing title, author, viewcount
  fadeChange(remixInfo,'span.featured-title',aRemix.find('span.title').text(),'text');
  fadeChange(remixInfo,'span.featured-author',aRemix.find('span.author').text(),'text');
  fadeChange(remixInfo,'span.featured-views',aRemix.find('span.views').text(),'text');
  fadeChange(remixInfo,'span.featured-rating',aRemix.find('span.rating').html(),'text');

  // change the anchor link
  remixInfo.parent().find('a.story-anchor').attr('href', aRemix.attr('href'));
}

function randomChangeInfo(){
  changeInfo($('li.author-remix a.remix:random'))
}

// MAIN program
$(document).ready(function(){
  jQuery.jQueryRandom = 0;
  jQuery.extend(jQuery.expr[":"],
  {
      random: function(a, i, m, r) {
          if (i == 0) {
              jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
          };
          return i == jQuery.jQueryRandom;
      }
  });

  // TODO: randomly change stuff on a timer
  setInterval('randomChangeInfo()', 4000);

  // on hover over li.author-remix, slide span.author-slider's background position
  $('li.author-remix a.remix')
    // TODO: refactor the whole thing to a function that takes a a.remix as param, so that in turn can put this on a timer
    .hover(function(){changeInfo($(this));}
  );
});

