var motdIniConfig = {"colors":{"background":"#FFFF00"},"items":{"texts":["New Catalog is here! See all the new fashion jewelry"],"links":["http:\/\/www.jewelsbyparklane.com\/start\/jewelryindex"]},"home":{"texts":["New Catalog is here! See all the new fashion jewelry","Become a Park Lane Fan on Facebook and Win FREE Jewelry!!!"],"links":["\/start\/jewelryindex","http:\/\/facebook.com\/jbplinc"]},"slug":""};

var MotdWidget = Widget.extend(
{
  widgetName  : 'motd',

  itemNodes   : [],
  defaultSlug : 'items',
  delay       : 2,
  currentItem : 0,
  rotate      : true,
  direction   : 'forward',
  startItem   : 0,

  // events
  changeNode : function(currentNode, targetNode)
  {
    if(currentNode)
      currentNode.style.display = 'none';

    targetNode.style.display = '';
  },

  init : function(target, config)
  {
    this._super(target, config);

    this.currentItem = this.startItem;

    if(this.autoStart)
      this.build();

    if(this.rotate)
      this.startRotation();
  },

  build : function()
  {
    this.target.innerHTML = '';

    var listNode = document.createElement('ul');

    if(this['slug'])
      this.items = this[this['slug']];
    else
      this.items = this[this.defaultSlug];
    
    for(var i = 0; i < this.items.links.length; i++)
    {
      var link = this.items.links[i];
      var text = this.items.texts[i];

      var node = document.createElement('li');
      //node.innerHTML = '<a href="' + link + '" target="_blank">' + link + '</a><p>' + text + '</p>';
      node.innerHTML = '<a href="' + link + '" target="_blank">' + text + '</a>';

      listNode.appendChild(node);

      this.itemNodes[this.itemNodes.length] = node;

      //if(i != this.currentItem && this.rotate)
      if(this.rotate)
        node.style.display = 'none';

      if(i == this.currentItem && this.rotate)
        this.changeNode(null, node);
    }
    this.target.appendChild(listNode);
  },

  startRotation : function()
  {
    var _this = this;
    setInterval(function(){_this.rotateItems(_this);}, this.delay * 1000);
  },

  rotateItems : function(_this)
  {
    if(_this.direction == 'backward')
      _this.prev();
    else
      _this.next();
  },

  next : function()
  {
    // rotate to next item
    var oldItem = this.currentItem++;

    if(this.currentItem >= this.itemNodes.length)
      this.currentItem = 0;

    this.changeNode(this.itemNodes[oldItem], this.itemNodes[this.currentItem]);
  },

  prev : function(_this)
  {
    // rotate to prev item
    var oldItem = this.currentItem--;

    if(this.currentItem < 0)
      this.currentItem = this.itemNodes.length -1;

    this.changeNode(this.itemNodes[oldItem], this.itemNodes[this.currentItem]);
  }

});



