/*
Class: DocktabFlickr


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 DocktabFlickr = function (favicon)
{
  this.PIX_FRAME_WIDTH    = 10;
  this.PIX_MINIMUM_WIDTH  = 300;
  this.PIX_MINIMUM_HEIGHT = 160;
  this.PIX_OFFSET_TOP     = 34;
  this.PIX_OFFSET_BOTTOM  = this.PIX_OFFSET_TOP + this.PIX_FRAME_WIDTH;
  this.PIX_PADDING_BOTTOM = 300;
  this.SEC_TAB_DISAPPEAR  = 1.3;
  this.SEC_TAB_APPEAR     = 1.5

  this.elm            = {};
  this.elm.icon       = favicon;
  this.elm.tab        = $('#docktab div.tab.flickr').get(0);
  this.elm.close      = $('img.close', this.elm.tab).get(0);
  this.elm.ok         = $('button.ok', this.elm.tab).get(0);
  this.elm.tabBody    = $('#authorInfoTab').get(0);
  this.elm.authorIcon = $('div.picture', this.elm.tabBody).get(0);
  this.elm.authorName = $('p.author', this.elm.tabBody).get(0);
  this.elm.photoPage  = $('p.legend', this.elm.tabBody).get(0);

  this.isInitialized  = false;
  this.isLocal        = false;
  this.isInMotion     = false;
  this.functionsHide  = [];

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


  this.onResize();
  $(window).bind('resize', this.fnCache.onResize);
  $(this.elm.close).bind('click', this.fnCache.onClose);
  $(this.elm.ok).bind('click', this.fnCache.onClose);
}


// SCRATCH PADS
// _______________________________________________________________________


/*
Method: setContent

Clear child objects, and append 

Arguments:
  photo - (Object)
  author - (Object)

Return:
  undefined
*/
DocktabFlickr.prototype.setContent = function (photo, author)
{
  if (!this.isInMotion)
  {
    PinupController.cleanupChildren(this.elm.authorIcon);
    PinupController.cleanupChildren(this.elm.authorName);
    PinupController.cleanupChildren(this.elm.photoPage);

    this.elm.authorIcon.appendChild(DocktabFlickr.buildAuthorIcon(author.icon_url, author.username, author.profile_url));
    this.elm.authorName.appendChild(DocktabFlickr.buildAuthorName(author.profile_url, author.username));
    this.elm.photoPage.appendChild(DocktabFlickr.buildPhotoPage(photo.page_url, photo.title));
  }
  else
  {
    var self = this;
    this.functionsHide[this.functionsHide.length] = function(){
      self.setContent.apply(self, [photo, author]);
    }
  }
}


/*
Method: buildAuthorIcon

DOM Building for buddy icon link

Arguments:
  url_img - (String)
  str_title - (String)
  url_page - (String)

Return:
  (DOMElement)
*/
DocktabFlickr.buildAuthorIcon = function (url_img, str_title, url_page)
{
  var a = document.createElement('a');
  a.href = url_page;
  var img = document.createElement('img');
  img.src = url_img;
  img.title = str_title;
  a.appendChild(img);
  return a;
}


/*
Method: buildAuthorName

DOM Building for auhor link

Arguments:
  url_page - (String)
  str_name - (String)

Return:
  (DOMElement)
*/
DocktabFlickr.buildAuthorName = function (url_page, str_name)
{
  var span = document.createElement('span');
  span.appendChild(document.createTextNode('take by '));
  var a = document.createElement('a');
  a.href = url_page;
  a.innerHTML = str_name;
  span.appendChild(a);
  return span;
}


/*
Method: buildPhotoPage

DOM element building for photo page

Arguments:
  url_page - (String)
  str_name - (String)

Return:
  (DOMElement)
*/
DocktabFlickr.buildPhotoPage = function (url_page, str_name)
{
  if (!str_name)
  {
    str_name = '(untitled)';
  }
  var a = document.createElement('a');
  a.href = url_page;
  a.innerHTML = str_name;
  return a;
}


/*
Method: hideOnComplete

after finishing hiding animation, clear up the rest

Return:
  undefined
*/
DocktabFlickr.prototype.hideOnComplete = function ()
{
  //console.log('hiding complete');
  this.elm.tab.style.visibility = 'hidden';
  this.isInMotion = false;

  for (var i = 0, len = this.functionsHide.length; i < len ; ++i)
  {
    var func = this.functionsHide[i];
    if (func instanceof Function)
    {
      func();
    }
  }
  this.functionsHide = [];
}
















// ANIMATION METHODS
// _______________________________________________________________________


/*
Method: show

showing animation

Return:
  undefined
*/
DocktabFlickr.prototype.show = function ()
{
  var self = this;
  var obj = this.elm.tab;
  this.isInMotion = true;

  var init_y = (-parseInt(obj.style.height));
  obj.style.bottom = init_y + 'px';
  obj.style.visibility = 'visible';
  obj._y  = init_y;
  JSTweener.addTween(
    obj,
    {
      'delay'       : 0,
      '_y'          : -(this.PIX_PADDING_BOTTOM),
      'time'        : this.SEC_TAB_APPEAR,
      'transition'  : 'easeOutElastic',
      'onUpdate'    : function () { obj.style.bottom  = obj._y + 'px'; },
      'onComplete'  : function () {}
    }
  );
}


/*
Method: hide

hiding animation

Return:
  undefined
*/
DocktabFlickr.prototype.hide = function ()
{
  var obj = this.elm.tab;

  var goal_y = (-parseInt(obj.style.height));
  obj._y  = -(this.PIX_PADDING_BOTTOM);
  JSTweener.addTween(
    obj,
    {
      'delay'       : 0,
      '_y'          : goal_y,
      'time'        : this.SEC_TAB_DISAPPEAR,
      'transition'  : 'easeInQuart',
      'onUpdate'    : function () { obj.style.bottom  = obj._y + 'px'; },
      'onComplete'  : this.fnCache.hideOnComplete
    }
  );
}


// ACCESS METHODS
// _______________________________________________________________________



// CALLBACK METHODS
// _______________________________________________________________________




// EVENT METHODS
// _______________________________________________________________________


/*
Method: onClose

closing aciton handler

Return:
  undefined
*/
DocktabFlickr.prototype.onClose = function ()
{
  this.hide();
  PinupController.getInstance().reintroduceIcons(this.SEC_TAB_DISAPPEAR);
}


/*
Method: onResize

resizing action handler

Return:
  undefined
*/
DocktabFlickr.prototype.onResize = function ()
{
  var max_w = $(document.body).width();
  var max_h = $(document.body).height();
  var w = this.PIX_MINIMUM_WIDTH;
  this.elm.tab.style.width = w + 'px';
  this.elm.tab.style.left = Math.floor((max_w - w) / 2) + 'px';

  var h = this.PIX_MINIMUM_HEIGHT;
  this.elm.tab.style.height = (h + this.PIX_PADDING_BOTTOM) + 'px';
}


// INSTANCE UTILITY METHODS
// _______________________________________________________________________




// STATIC UTILITY METHODS
// _______________________________________________________________________



