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


var Promo = function (eSrcElm, iDestElm)
{
  //console.log('Promo::Constructor');

  // compile regex once, and cache it.
  if (typeof(Promo.suffix) == 'undefined')
  {
    Promo.suffix = {};
    Promo.suffix.open = '-open';
    Promo.suffix.close = '-close';
    Promo.suffix.anime = '-anime';
    Promo.suffix.animeR = '-animeR';

    Promo.regexp = Promo.compileRegexp(Promo.suffix);
    Promo.intialSecond = 3000;
    Promo.pauseSecond = 333;
  }

  this.source = eSrcElm;
  this.target = iDestElm;
  this.triangle = $('img.triangle', $(eSrcElm)).get(0);
  this.path = Promo.parsePath(this.triangle.src);
  this.isVisible = false;
  this.lastAction = null;

  // scrape link
  var a = $('a', this.target);
  if (a.size() != 0)
  {
    this.linkTo = (a.get(0)).getAttribute('href');
  }
  else
  {
    this.linkTo = null;
  }

  this.timerHandler = null;
}

// MEMBER METHODS _________________________________________________________

Promo.prototype.autoShow = function ()
{
  window.clearTimeout(this.timerHandler);
  this.show();
  this.timerHandler = window.setTimeout($.scope(this, this.hide), Promo.intialSecond);
}

Promo.prototype.show = function ()
{
  $(this.target).animate({'top': Promo.visibleTopPosition}, 'slow');
  this.setImageAnime();
  this.isVisible = true;
}

Promo.prototype.hide = function ()
{
  $(this.target).animate({'top': Promo.invisibleTopPosition}, 'slow');
  this.setImageAnimeR();
  this.isVisible = false;
}

Promo.prototype.setImageOpen = function ()
{
  this.triangle.src = this.path.dirname + '/' + this.path.basename + Promo.suffix.open + '.' + this.path.ext;
}

Promo.prototype.setImageClose = function ()
{
  this.triangle.src = this.path.dirname + '/' + this.path.basename + Promo.suffix.close + '.' + this.path.ext;
}

Promo.prototype.setImageAnime = function ()
{
  this.triangle.src = this.path.dirname + '/' + this.path.basename + Promo.suffix.anime + '.' + this.path.ext;
}

Promo.prototype.setImageAnimeR = function ()
{
  this.triangle.src = this.path.dirname + '/' + this.path.basename + Promo.suffix.animeR + '.' + this.path.ext;
}


// STATIC MEMBERS _________________________________________________________

Promo.visibleTopPosition = 4;
Promo.invisibleTopPosition = -120;

Promo.checkFocus = function (arr)
{
  var result = true;
  jQuery.each(arr, function (n, obj)
  {
    if (typeof(obj) == 'undefined')
    {
      result = false;
    }
  });
  return result
}

Promo.parsePath = function (str)
{
  var result = {};
  var arr_dirs = str.split('/');
  result.filename = arr_dirs.pop();
  if (result.filename.match(Promo.regexp) != null)
  {
    var arr_file = (result.filename.replace(Promo.regexp, '')).split('.');
  }
  else
  {
    var arr_file = result.filename.split('.');
  }
  result.dirname = arr_dirs.join('/');
  result.ext = arr_file.pop();
  result.basename = arr_file.join('.');
  return result;
}

Promo.compileRegexp = function (obj)
{
  var str = '';
  for (var i in obj)
  {
    str += obj[i] + '|';
  }
  str = str.substring(0, (str.length - 1));
  return new RegExp(str);
}


// DISPATCH EVENT _________________________________________________________

Promo.dispatch = function (ev)
{
  switch (ev.data.type)
  {
    case 'HEADER_CLICK':
      Promo.dispatchHeaderClick(ev);
    break;

    case 'HEADER_ENTER':
      Promo.dispatchHeaderEnter(ev);
    break;

    case 'PROMO_LEAVE':
      Promo.dispatchPromoLeave(ev);
    break;

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

// HEADER_ENTER ___________________________________________________________

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

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

    case 'img':
      target = ev.target.parentNode;
    break;

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

  var promo = (Controller.getInstance()).dictionary.get(target);
  if (promo.timerHandler != null)
  {
    window.clearTimeout(promo.timerHandler);
  }
  if (!promo.isVisible)
  {
    promo.show();
  }

  promo.lastAction = 'HEADER_ENTER';
}

// HEADER_CLICK _____________________________________________________________

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

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

    case 'img':
      target = ev.target.parentNode;
    break;

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

  var promo = (Controller.getInstance()).dictionary.get(target);
  //console.log(promo.linkTo);
  if (promo.linkTo != null)
  {
    window.location.href = promo.linkTo;
  }
}

// PROMO_LEAVE _____________________________________________________________

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

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

    case 'a':
    case 'p':
    case 'img':
      target = $(ev.target).parents().filter('div').get(0);
    break;

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

  var c = target.getAttribute('className') || target.getAttribute('class');

  var finish = function (elm)
  {
    var promo = (Controller.getInstance()).dictionary.get(elm);
    if (promo.isVisible)
    {
      window.setTimeout(jQuery.scope(promo, promo.hide), Promo.pauseSecond);
    }
    promo.lastAction = 'PROMO_LEAVE';
  }

  switch (true)
  {
    case (c == 'each'):
      finish(target);
    break;

    case (c == 'header'):
      target = $('.each', target.parentNode).get(0);
      finish(target);
    break;

    case (c.indexOf('promo') != -1):
      target = $('.each', target).get(0);
      finish(target);
    break;

    default:
      throw new Error('Unidentifed event element: ' + target);
    break;
  }
}

