/**
 * Paginated News Header
 *
 * @author     Takashi Mizohata <takashi@firstbornmultimedia.com>
 * @copyright  2008 firstborn
 * @license    firstborn internal (pending)
 */

var PaginateNewsHeader = function (elm)
{
  this.start        = 0;
  this.count        = 5;
  this.page         = 0;
  this.display      = $('.newsNav .center', elm).get(0);
  this.headlines    = null;

  var headlines = [];
  var jq_li = $('li', elm);
  jq_li.each(
    function (n, obj)
    {
      headlines[headlines.length] = obj;
    }
  );
  this.headlines = headlines;
  if (typeof(PaginateNewsHeader.right) == 'undefined')
  {
    PaginateNewsHeader.right = new RegExp('right.gif$', 'i');
    PaginateNewsHeader.left  = new RegExp('left.gif$', 'i');
  }
  if (this.headlines.length < this.count)
  {
    this.isPaginated = false;
    $(this.display.parentNode).hide();
    $('img', elm).each
    (
      function (n, obj)
      {
        $(obj).hide();
      }
    );
  }
  this.adjustView();
}

// INSTANCE MEMBER ________________________________________________________

PaginateNewsHeader.prototype.adjustView = function ()
{
  var len = this.headlines.length;
  for (var i = 0; i < len; ++i)
  {
    if ((i >= this.start) && (i < (this.start + this.count)))
    {
      $(this.headlines[i]).show();
    }
    else
    {
      $(this.headlines[i]).hide();
    }
  }
  this.setDisplay();
}


PaginateNewsHeader.prototype.hideAll = function ()
{
  var len = this.headlines.length;
  for (var i = 0; i < len; ++i)
  {
    $(this.headlines[i]).hide();
  }
}


PaginateNewsHeader.prototype.showCurrent = function ()
{
  var len = this.headlines.length;
  for (var i = 0; i < len; ++i)
  {
    if ((i >= this.start) && (i < (this.start + this.count)))
    {
      $(this.headlines[i]).show();
    }
  }
}


PaginateNewsHeader.prototype.setDisplay = function ()
{
  var last = ((this.start + this.count) < this.headlines.length)?
              (this.start + this.count):
              this.headlines.length;
  var text = 'ARTICLES ' + (this.start + 1) + ' - ' + last;
  this.display.innerHTML = text;
}


PaginateNewsHeader.prototype.clickRight = function ()
{
  if ((this.start + this.count) < this.headlines.length)
  {
    this.start += this.count;
  }
  else
  {
    this.start = 0;
  }
  this.adjustView();
}


PaginateNewsHeader.prototype.clickLeft = function ()
{
  if ((this.start - this.count) > -1)
  {
    this.start -= this.count;
  }
  else
  {
    this.start = this.headlines.length - (this.headlines.length % this.count);
  }
  this.adjustView();
}

// STATIC MEMBER __________________________________________________________

PaginateNewsHeader.findSide = function (obj)
{
  var result = null;
  if (obj.src.match(PaginateNewsHeader.right) !== null)
  {
    result = true;
  }
  else if (obj.src.match(PaginateNewsHeader.left) !== null)
  {
    result = false;
  }
  else
  {
    throw new Error('Something wrong with action object: ' + obj.src);
  }
  return result;
}

// DISPATCH EVENT _________________________________________________________

PaginateNewsHeader.dispatch = function (ev)
{
  switch (ev.data.type)
  {
    case 'CLICK':
      PaginateNewsHeader.dispatchClick(ev);
    break;

    default:
      //throw new Error('event type not defined.');
    break;
  }
}

// CLICK __________________________________________________________________

PaginateNewsHeader.dispatchClick = function (ev)
{
//  console.log('PaginateNewsHeader::dispatchClick: ' + ev.target.tagName);

  var target;
  switch ((ev.target.tagName).toLowerCase())
  {
    case 'img':
      target = ev.target;
    break;

    default:
      throw new Error('Unindentifiable trigger element: ' + ev.target.tagName);
    break;
  }

  var pnh = (Controller.getInstance()).dictionary.get(target);
  var direct = PaginateNewsHeader.findSide(target);
  if (direct)
  {
    pnh.clickRight();
  }
  else
  {
    pnh.clickLeft();
  }
}
