/*
 * TODO: Copyright (c) 2007 Denis Morozov <dmorozov@exadel.com>
 *
 * ...
 */
ClientUILib.declarePackage("ClientUI.layouts.LayoutManager");

/*
/* LayoutManager.js - Base class for all layout managers
 * by Denis Morozov <dmorozov@exadel.com> distributed under the BSD license. 
 *
 * TODO: description of control 
 */
ClientUI.layouts.LayoutManager = Class.create({
	CLASSDEF: {
		name: 'ClientUI.layouts.LayoutManager',
		parent: ClientUI.common.box.Box
	},
	
	// Custom events
	/**
	 * Occured before resizing
	 */
	eventOnBeforeResize: {},
	/**
	 * Occured after resizing
	 */
	eventOnAfterResize: {}	

});

Object.extend(ClientUI.layouts.LayoutManager.prototype, {	
	initialize: function(element, parentElement) {
		ClientUI.layouts.LayoutManager.parentClass.constructor().call(this, element, parentElement);
		
		// store container element to look after
		this.container = parentElement;
		if(this.container)
			this.container = new ClientUI.common.box.Box($(this.container));

		// declare event listeners
		this.eventContainerResize = this.containerResize.bindAsEventListener(this);
		
		// Create custom event producers
		this.eventOnBeforeResize = new ClientUI.common.utils.CustomEvent('OnBeforeResize');
		this.eventOnAfterResize = new ClientUI.common.utils.CustomEvent('OnAfterResize');
		
		this.registerEvents();
	},
	registerEvents: function() {
			Event.observe(window, "resize", this.eventContainerResize);
	},
	destroy: function() {
			Event.stopObserving(window, "resize", this.eventContainerResize);
	},
   	containerResize: function(event) {
	  	//Event.stop(event);
	  	this.eventOnBeforeResize.fire();
	  	this.updateLayout();
	  	this.eventOnAfterResize.fire();
	},
	updateLayout: function() {
		if(this.container) {
			var w = this.container.getWidth();
			var h = this.container.getHeight();
			if(ClientUILib.isGecko) {
				w -= this.container.getBorderWidth("lr") + this.container.getPadding("lr");
				h -= this.container.getBorderWidth("tb") + this.container.getPadding("tb");
			}			
			this.setWidth(w);
			this.setHeight(h);
		}
		ClientUI.layouts.LayoutManager.parentClass.method("updateLayout").call(this);		
	},
	getContainer: function() {
		return this.container;
	}
});
