/*
 * vegUI is copyright (c) 2005 Stefan Pratter
 *
 * This file is part of vegUI
 *
 * vegUI is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * vegUI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with vegUI; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/******************************************************************************
 * G L O B A L S
 *****************************************************************************/
 
VEGUIOBJ[VUI_CBOX] = VegUIContentBox;

/******************************************************************************
 * V E G U I C O N T E N T B O X
 *****************************************************************************/
 
/***************************************************/
/** Constructor */

function VegUIContentBox(refName, Parent, Manager) {
  this.constructor = VegUINode;
  this.constructor(refName, Parent, Manager);
  
  this.type = VUI_CBOX;
  
  this.CHolder = this.add_child('CHolder', VUI_NODE);
  this.Content = this.CHolder.add_child('Content', VUI_NODE);
  this.CHolder.T.rmarg = 15;
  this.CHolder.T.bmarg = 15;

  /* Set up Y Axis scrollbar */  
  this.ScrollY = this.add_child('ScrollY', VUI_SCROLL);
  this.ScrollY.set('y');
  this.ScrollY.T.rmarg_nr = 0;
  this.ScrollY.T.bmarg = 15;
  
  /* Set up X Axis scrollbar */
  this.ScrollX = this.add_child('ScrollX', VUI_SCROLL);
  this.ScrollX.set('x');
  this.ScrollX.T.bmarg_nr = 0;
  this.ScrollX.T.rmarg = 15;

  this.set_cbox = vuicbox_set;
  this.build_cbox = vuicbox_build;
  this.fill = vuicbox_fill;
  this.update_bbox = vuicbox_update_bbox;
  
  this.set = this.set_cbox;
  this.build = this.build_cbox;

  this.ondock = function() {
    this.ScrollY.sync();
    this.ScrollX.sync();
  }

  this.onresize = function() {
    if(!this.Base.parentNode)
      return;
    this.update_bbox();
    this.ScrollY.sync();
    this.ScrollX.sync();
  }
}
VegUIContentBox.prototype = VegUINode;
 
/***************************************************/
/** VegUIContentBox.set_cbox(): set template
  * properties */
  
function vuicbox_set(w,h,x,y) {
  this.set_node(null,w,h,x,y);
}

/***************************************************/
/** VegUIContentBox.build_cbox(): build content
  * box element */

function vuicbox_build(toNode) {
  if(!this.build_node())
    return null;
  this.CHolder.Css.overflow = 'hidden';
  this.ScrollY.link(this.CHolder, this.Content);
  this.ScrollX.link(this.CHolder, this.Content);
  this.ScrollX.hide((this.flags & VUI_HIDE_SCROLLX));
  this.ScrollY.hide((this.flags & VUI_HIDE_SCROLLY));
  this.dock(toNode);
  return 1;
}

/***************************************************/
/** VegUIContentBox.fill(): fills content box
  * with any html node (content) */
  
function vuicbox_fill(Node) {
  this.Content.Base.appendChild(Node);

  /* set bounding box for content */
  this.update_bbox();
  this.ScrollY.sync();
  this.ScrollX.sync();
}

/***************************************************/
/** VegUIContentBox.update_bbox(): updates bounding
  * box for content node */

function vuicbox_update_bbox() {
  this.ScrollX.update_content_boundary();

  if(this.ScrollY.overflow() <= 1)
    this.Content.move(null, 0);
  if(this.ScrollX.overflow() <= 1)
    this.Content.move(0, null);
}
