/**
 * NetronautUI base class.
 * 
 * @requires MooTools.Element.Measure
 *  
 * ----
 * 
 * LICENCE
 * 
 * This work is licensed under Creative Commons Attribution-Share Alike 3.0 (Germany).
 * For details on this licence visit http://creativecommons.org/licenses/by-sa/3.0/de/
 * 
 * All rights reserved
 * 
 * @author Jakob Hohlfeld | http://www.netronaut.de
 * @copyright 2010 by Jakob Hohlfeld
 */
var NetronautUI_Widget = new Class
({
	Implements: Events,
	
	ie: Browser.Engine.trident,
	ie6: Browser.Engine.trident && Browser.Engine.version == '4',
	ie7: Browser.Engine.trident && Browser.Engine.version == '5',
	ie8: Browser.Engine.trident && Browser.Engine.version == '6',
	
	dom: {}, options: {},
	
	/**
	 * The constructor.
	 * 	
	 * @param Element el
	 * @param object options
	 */
	initialize: function ( el, options ) {
		
		// register dom
		switch($type(el)) {
			case false:
				this.dom.el = new Element('div');
			break;
			case 'object':
				this.dom.el = el.dom.container;
				this.dom.object = el;
				this.dom.object.fireEvent('addedToWidget', [this]);
			break;
			default: this.dom.el = el;
		}
		this.dom.el.addClass('netronautui-widget');
		this.dom.container = this.dom.el;
		this.dom.hitarea = [];
		
		// configure
		if(options) this.setOptions(options);
		
		// render
		this.render();
	},
	
	/**
	 * Set options.
	 * 
	 * @param object options
	 */
	setOptions: function ( options ) {
		for(var i in options) {
			this.options[i] = options[i];
		}
	},
	
	/**
	 * Render the component.
	 * 
	 */
	render: function () {
		// ..
	},
	
	open: function () {
		this.dom.container.setStyle('display', 'block');
	},
	
	close: function () {
		this.dom.container.setStyle('display', 'none');
	},
	
	/**
	 * Set the components size.
	 * 
	 * @param object size x/y for width/height
	 */
	setSize: function ( size ) {
		if($type(size.x)) this.dom.container.setStyle('width', size.x);
		if($type(size.y)) this.dom.container.setStyle('height', size.y);
		this.fireEvent('resize');
		return this;
	},
	
	/**
	 * Get the components size.
	 * 
	 * @return object x/y for width/height
	 */
	getSize: function () {
		return this.dom.container.measure(function(){return this.getSize()});
	},
	
	position: function ( options ) {
		this.dom.container.position(options);
		return this;
	},
	
	/**
	 * Override setPosition method.
	 * 
	 * @param object position (x/y)
	 */
	setPosition: function ( position ) {
		this.dom.container.setPosition(position);
		return this;
	},
	
	/**
	 * Get position.
	 * 
	 * @return object (x/y)
	 */
	getPosition: function () {
		return this.dom.container.measure(function(){return this.getPosition()});
	},
	
	closeOnClick: function ( event ) {
		node = event.target;
		while(node) {
			var found = false;
			this.dom.hitarea.each(function(item){
				if(node == item) {
					found = true;
				}
			});
			if(found) return;
			node = node.parentNode;
		}
		this.close();
	}
})