/*
 * Firstborn Multimedia Modal
 * 
 * (c) takashi mizohata
 * Jan 31, 2008
 */


var FBModal = function ()
{
  try
  {
    this.ids = {};
    this.ids.overray     = "FBModalOverray";
    this.ids.element     = "FBModal";
    this.ids.elementWrap = "FBModalWrap";

    this.elementW = 336;
    this.elementH = 280;

    this.element = null;
    this.overray = null;

    this.isListening = false;
  }
  catch (ex)
  {
    //console.log(ex);
  }
}


FBModal.prototype.openWindow = function (func) {
  //console.log('openWindow');
  var size = FBModal.getWindowSize();
  switch (true)
  {
    case (this.overray == null):
      this.overray = this.buildOverlay( size[0], size[1] );
      this.element = this.buildElement();
      this.init();
    break;

    case (this.element == null):
      this.resizeOverlay(size[0], size[1]);
      this.element = this.buildElement();
    break;

    default:
      this.resizeOverlay(size[0], size[1]);
    break;
  }
  this.setAbsoluteCenter();
  this.show(func);
}


FBModal.prototype.init = function ()
{
  //console.log('init');
  Event.observe(window, 'resize', this.onResize.bind(this), true);
  Event.observe(this.overray, 'click', this.hide.bind(this));
}


FBModal.prototype.show = function (func)
{
  //console.log('show');
  this.isListening = true;
  //Element.show(this.overray);
  if (func === undefined)
  {
    func = function (effect) {}
  }

  new Effect.Parallel(
    [
      new Effect.Appear(this.overray, { duration: 0.9, from: 0.0, to: 0.7 }),
      new Effect.Appear(this.element, { duration: 0.8 })
    ], 
    {
      'afterFinish': func
    }
  );
/*
  var me = this;
  new Effect.Parallel(
    [ new Effect.Appear(this.overray, { duration: 0.8 }) ],
    {
      'afterFinish': function(effect) { Element.show(me.element); }
    }
  );
*/
}


FBModal.prototype.hide = function ()
{
  //console.log('hide');
  this.isListening = false;
  try
  {
    document.body.removeChild(this.element);
  }
  catch (ex)
  {
    //console.log(ex);
  }
  this.element = null;
  //Element.hide(this.overray);
  Effect.Fade(this.overray, { duration: 0.5 });
}


FBModal.prototype.buildElement = function ()
{
  //console.log('buildElement');
  var elm = Builder.node(
    'div', 
    {'id':this.ids.elementWrap, 'style':'display:none'},
    [Builder.node(
      'div',
      {'id': this.ids.element}
    )]
  );
  return document.body.appendChild(elm);
}


FBModal.prototype.buildOverlay = function (_w, _h)
{
  //console.log('buildOverlay::' + _w + ', ' + _h);
  var elm = Builder.node('div');
  elm.id  = this.ids.overray;
  //Element.setWidth(elm, _w);
  //Element.setHeight(elm, _h);
  elm.style.width  = _w + 'px';
  elm.style.height = _h + 'px';
  elm.style.display = 'none';
  return document.body.appendChild(elm);
}


FBModal.prototype.onResize = function () {
  //console.log('onResize');
  if (this.isListening) {
    this.setAbsoluteCenter();
    //Element.setWidth(this.overray, 0);
    //Element.setHeight(this.overray, 0);
    this.overray.style.width  = '0px';
    this.overray.style.height = '0px';
    if ( this.resizeSlot != null ) {
      clearTimeout( this.resizeSlot );
    }
    this.resizeSlot = setTimeout(this.resizeOverlay.bind(this), 200);
  }
}


FBModal.prototype.resizeOverlay = function (_w, _h) {
  //console.log('resiezeOverlay');
  console.log(_w);
  console.log(_h);
  if (_h === undefined)
  {
    var size = FBModal.getWindowSize();
    _w = size[0];
    _h = size[1];
  }
  //Element.setWidth(this.overray, _w);
  //Element.setHeight(this.overray, _h);
  this.overray.style.width  = _w + 'px';
  this.overray.style.height = _h + 'px';
  this.resizeSlot = null;
}




FBModal.prototype.setAbsoluteCenter = function (_w, _h)
{
  if (_w === undefined)
  {
    var size = FBModal.getWindowSize();
    _w = size[2];
    _h = size[3];
  }
  this.element.style.left = Math.abs((_w - this.elementW) / 2) + 'px';
  this.element.style.top  = Math.abs((_h - this.elementH) / 2) + 'px';
}



FBModal.getPageScroll = function()
{
  var xScroll, yScroll;
  if (window.pageYOffset) 
  {
    yScroll = self.pageYOffset;
    xScroll = self.pageXOffset;
  }
    else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
  {
    yScroll = document.documentElement.scrollTop;
    xScroll = document.documentElement.scrollLeft;
  } 
  else if (document.body) // all other Explorers
  {
    yScroll = document.body.scrollTop;
    xScroll = document.body.scrollLeft;	
  }
  return [xScroll,yScroll];
}


FBModal.getWindowSize = function ()
{
  var xScroll, yScroll;

  if (document.documentElement.scrollHeight)
  {
    xScroll = document.documentElement.scrollWidth;
    yScroll = document.documentElement.scrollHeight;
  }
  else if (window.innerHeight && window.scrollMaxY) 
  {
    xScroll = window.innerWidth + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  } 
  else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
  { 
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } 
  else // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  {
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }


  var windowWidth, windowHeight;
//  console.log(self.innerWidth);
//  console.log(document.documentElement.clientWidth);

  if (window.innerHeight) // all except Explorer
  {
    if (document.documentElement.clientWidth)
    {
      windowWidth = document.documentElement.clientWidth; 
    } 
    else 
    {
      windowWidth = window.innerWidth;
    }
    windowHeight = window.innerHeight;
  } 
  else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
  {
    windowWidth  = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } 
  else if (document.body) // other Explorers
  { 
    windowWidth  = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }

  // for small pages with total height less then height of the viewport
  if (yScroll < windowHeight)
  {
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

//  console.log("xScroll " + xScroll)
//  console.log("windowWidth " + windowWidth)

  // for small pages with total width less then width of the viewport
  if (xScroll < windowWidth)
  {
    pageWidth = xScroll;
  } 
  else 
  {
    pageWidth = windowWidth;
  }
//  console.log("pageWidth " + pageWidth)

  return [pageWidth, pageHeight, windowWidth, windowHeight];
}


