/*
Class: Postcard

License:
  MIT-style license.

Author:
  Takashi Mizohata <beatak@nydd.org>

Copyright:
  2008 [nydd](http://code.nydd.org/).

Code & Documentation:
  [nydd jslib](http://code.nydd.org/).
*/

var Postcard = function (parent, desc)
{
  console.log('Postcard::constructor');
  this.PIX_OFFSET_X     = 8;
  this.PIX_OFFSET_Y     = 9;
  this.PIX_BOARD_WIDTH  = 403;
  this.PIX_BOARD_HEIGHT = 306
  this.PIX_IMAGE_WIDTH  = 428;
  this.PIX_IMAGE_HEIGHT = 331;

  this.elm        = {};
  this.elm.parent = parent;
  this.elm.desc   = desc;

  this.fnCache = {};
  this.fnCache.onResize       = jQuery.scope(this, this.onResize);
  this.fnCache.showOnComplete = jQuery.scope(this, this.showOnComplete);
  this.fnCache.hideOnComplete = jQuery.scope(this, this.hideOnComplete);

  this.isLocal        = false;
  this.functionsShow  = [];
  this.functionsHide  = [];

  this.onResize();
  $(window).bind('resize', this.fnCache.onResize);
}


// SCRATCH PADS
// _______________________________________________________________________


Postcard.prototype.setContent = function (dom)
{
  PinupController.cleanupChildren(this.elm.desc);
  this.elm.desc.appendChild(dom);
}

Postcard.prototype.appendFunctionShow = function (func)
{
  var result = this.functionsShow.length
  this.functionsShow[result] = func;
  return result;
}

Postcard.prototype.appendFunctionHide = function (func)
{
  var result = this.functionsHide.length
  this.functionsHide[result] = func;
  return result;
}


// ANIMATION METHODS
// _______________________________________________________________________


Postcard.prototype.show = function ()
{
  //console.log('Postcard::show');
  var obj     = this.elm.parent;
  var goal_x  = Math.floor(($(document.body).width() - this.PIX_BOARD_WIDTH) / 2) - this.PIX_OFFSET_X;
  obj.style.visibility = 'visible';
  obj._x      = 0 - this.PIX_IMAGE_WIDTH;
  JSTweener.addTween(
    obj,
    {
      'delay'       : 0,
      '_x'          : goal_x,
      'time'        : 1,
      'transition'  : 'easeInQuart',
      'onUpdate'    : function () { obj.style.left = obj._x + 'px'; },
      'onComplete'  : this.fnCache.showOnComplete
    }
  );
}


Postcard.prototype.hide = function ()
{
  //console.log('Postcard::hide');
  var obj     = this.elm.parent;
  var goal_x  = $(document.body).width();
  obj._x      = parseInt($(obj).css('left'));
  JSTweener.addTween(
    obj,
    {
      'delay'       : 0,
      '_x'          : goal_x,
      'time'        : 1,
      'transition'  : 'easeInQuart',
      'onUpdate'    : function () { obj.style.left = obj._x + 'px'; },
      'onComplete'  : this.fnCache.hideOnComplete
    }
  );
}


// EVENT METHODS
// _______________________________________________________________________


Postcard.prototype.onResize = function ()
{
  //console.log('Postcard::onResize');
  var max_w = $(document.body).width();
  var max_h = $(document.body).height();
  var left  =  Math.floor((max_w - this.PIX_BOARD_WIDTH) / 2) - this.PIX_OFFSET_X;
  var top   =  Math.floor((max_h - this.PIX_BOARD_HEIGHT) / 2) - this.PIX_OFFSET_Y;
  this.elm.parent.style.left = left + 'px';
  this.elm.parent.style.top = top + 'px';
}


Postcard.prototype.showOnComplete = function ()
{
  //console.log('showing complete');
  this.onResize();
  for (var i = 0, len = this.functionsShow.length; i < len ; ++i)
  {
    var func = this.functionsShow[i];
    if (func instanceof Function)
    {
      func();
    }
  }
  this.functionsShow = [];
}


Postcard.prototype.hideOnComplete = function ()
{
  //console.log('hiding complete');
  this.elm.parent.style.visibility = 'hidden';
  for (var i = 0, len = this.functionsHide.length; i < len ; ++i)
  {
    var func = this.functionsHide[i];
    if (func instanceof Function)
    {
      func();
    }
  }
  this.functionsHide = [];
}



