function vecrotate(owner, options)
{
  this.defaults = {interval:1000, selected:0};
  this.options = $.extend(this.defaults, options);
  this.selectedIndex = this.options.selected;
  this.owner = $(owner);
  this.children = this.owner.children();
}

vecrotate.prototype.incSelectedIndex = function(aDelta)
{
  this.selectedIndex += aDelta;

  if (this.selectedIndex >= this.children.length)
    this.selectedIndex = 0;
  if (this.selectedIndex < 0)
    this.selectedIndex = this.children.length - 1;

  return this.selectedIndex;
}

vecrotate.prototype.current = function()
{
  var result = this.child(this.selectedIndex);
  return result;
}

vecrotate.prototype.next = function()
{
  this.incSelectedIndex(1);

  var result = this.current();

  return result;
}

vecrotate.prototype.rotate = function()
{
  var current = this.current();
  var next = this.next();

  // Show next selection 
  next.show().css({opacity: 0.0})
    .addClass('show')
    .animate({opacity: 1.0}, 1000);

  // Hide last selection
  current.hide().animate({opacity: 0.0}, 1000)
    .removeClass('show');
}

vecrotate.prototype.child = function(index)
{
  var result = this.children[index];
  result = $(result);
  return result;
}

vecrotate.prototype.log = function(msg)
{
  console.log(msg);
}

vecrotate.prototype.init = function() 
{
  this.children.css({opacity: 0.0});

  var me = this;
  var callback = function()
  {
    me.rotate();
  }

  setInterval(callback, this.options.interval);
  this.rotate();
}

var extension = 
{ 
  vecrotate: function(options) 
  {
    var vec = $(this).data('vecrotate');
    if (vec)
      return vec;

    vec = new vecrotate(this, options);
    $(this).data('vecrotate', vec);
    vec.init();

    return vec;
  }
}

$.fn.extend(extension);

