var current_y = content_t;
var current_l = content_l;
var remaining_h = ch - content_t - meta_h - content_pad_b;
var minimal_h = 10;



/*
 *	****************************************************************************
 *	this function builds a box object and sets the box dimensions
 *
 *	@access   public
 *
 *	@param    bool      box has scrollbar or not
 *	@param    int       left position
 *	@param    int       absolute top position if > 0
 *	                    otherwise top position depends on previous box
 *	@param    int       width
 *	@param    mixed     height:
 *	                    - if integer > 0 sets the height in pixels
 *	                    - otherwise sets the height dynamically
 *	@param    mixed     top padding:
 *	@param    mixed     right padding:
 *	@param    mixed     bottom padding:
 *	@param    mixed     left padding:
 *	                    - if integer >= 0 sets the padding in pixels
 *	                    - otherwise sets the padding to a default value
 *	****************************************************************************
*/
function Box(id, scrollable, l, t, w, h, pad_t, pad_r, pad_b, pad_l) {
	
	this.id = id;
	this.print = true;
	this.scrollable = scrollable;
	this.scroll = false;
	this.clipable = true;
	/* background */
	
	this.l = l > 0 ? l : current_l;
	this.t = t > 0 ? t : current_y;
	this.w = w > 0 ? w : (current_l == content_l ? content_w : content_w - (current_l - content_l));
	if(this.w + this.l + content_pad_r > cw) this.w = cw - this.l - content_pad_r;
	this.pad_t = pad_t >= 0 ? pad_t : this.scrollable ? 15 : 25;
	this.pad_r = pad_r >= 0 ? pad_r : this.scrollable ? 19 : 25;
	this.pad_b = pad_b >= 0 ? pad_b : this.scrollable ? 15 : 20;
	this.pad_l = pad_l >= 0 ? pad_l : 25;
	
	layer_id = this.scrollable ? 'cnt_content_'+this.id : 'cnt_container_'+this.id;
	this.h = h > 0 ? h : getLayerHeight(layer_id) + this.pad_t + this.pad_b;

	if(remaining_h < minimal_h || this.h < minimal_h) {
		// remaining height too low for box
		//@mkr this.print = false; 
		if(this.scrollable) {
		  this.scroll = true;
		}
	} else if(this.h >= remaining_h) {
		// reminaing height lower than box
		if(this.scrollable) {
			this.h = remaining_h; 
			this.scroll = true;
		} else {
			//@mkr this.print = false;
		}
	} else if(this.scrollable && h > 0) {
		// box height of scrollable box is fixed 
		this.scroll = true;
	}
	
	// @mkr obige Abfrage ermittelt nicht zuverlässig,
	// ob Scrollbar sichtbar ist. Deshalb dieser Pfusch
	if (this.scrollable) { 
	  this.scroll = true;
	}

	if(this.print) {
		/* calculate top and left position of next box */
		// 'LLeft' = Homepage
		if(((id=='Left') && (this.w <= 390)) || (id=='BotLeft') || (id=='TopLeft')) {
			// Falls Schmale Bildbox @mkr
			current_l += this.w + box_dist;
		} else {
			// ansonsten normale Box
			current_y += this.h + box_dist;
			remaining_h -= this.h + box_dist;
		}

		/* scrolling box */
		this.container = new Object;
		this.container.l = this.l + this.pad_l;
		this.container.t = this.t + this.pad_t;
		this.container.w = this.w - this.pad_l - this.pad_r;
		if(this.scrollable) {
			if(this.scroll) {
			  this.container.w -= scrollbar_w;
			} else {
			  this.container.w -= 6;
			}
		}
		this.container.h = this.h - this.pad_t - this.pad_b;
		this.container.r = this.container.l + this.pad_l + this.container.w;
		this.container.clip_t = scrollable ? 10 : 0;
		this.container.clip_r = this.container.w;
		this.container.clip_b = this.container.h - (scrollable ? 10 : 0);
		this.container.clip_l = 0;

		/* scrollbar */
		if(scrollable) {
			this.content = new Object();
			this.content.h = getLayerHeight('cnt_content_'+this.id);
			this.content.w = this.container.w;
			this.scrollbar = new Object;
			this.scrollbar.l = this.l + this.pad_l + this.container.w + this.pad_r;
			this.scrollbar.t = this.container.t;
			this.scrollbar.w = scrollbar_w;
			this.scrollbar.h = this.container.h;
		}
	}
}

pBox = Box.prototype;

/*
 *	****************************************************************************
 *	this function positions the box and sets its visibility to hidden if 
 *	box.print == true
 *
 *	@access   public
 *	****************************************************************************
*/
pBox.set = function() {
	b = 'cnt_background_'+this.id;
	c = 'cnt_container_'+this.id;
	ct = 'cnt_content_'+this.id;
	moveLayer(b, this.l, this.t);
	moveLayer(c, this.container.l, this.container.t);
	setLayerHeight(b, this.h)
	setLayerHeight(c, this.container.h)
	//if(this.scrollable) setLayerHeight(ct, this.content.h+10);
	setLayerWidth(b, this.w)
	setLayerWidth(c, this.container.w)
	if(this.scrollable) setLayerWidth(ct, this.content.w);
	if (this.clipable) {
	  setLayerClip(c, this.container.clip_t, this.container.clip_r, this.container.clip_b, this.container.clip_l);
	}
	showLayer(b);
	showLayer(c);
}

pBox.setTopBox = function() {
	b = 'cnt_background_'+this.id;
	c = 'cnt_container_'+this.id;
	ct = 'cnt_content_'+this.id;
	//moveLayer(b, this.l, this.t);
	//moveLayer(c, this.container.l, this.container.t);
	setLayerHeight(b, this.h)
	setLayerHeight(c, this.container.h)
	//if(this.scrollable) setLayerHeight(ct, this.content.h+10);
	//setLayerWidth(b, this.w)
	//setLayerWidth(c, this.container.w)
	//if(this.scrollable) setLayerWidth(ct, this.content.w);
	setLayerClip(c, this.container.clip_t, this.container.clip_r, this.container.clip_b, this.container.clip_l);
	showLayer(b);
	showLayer(c);
}
