/**
 * Stb javascript framework.
 * Based on mootools 1.2
 */
var Stb = {
	'version': '0.2.0'
};

/**
 *  Additions to mootools
 */
Element.implement({
	/**
	 * Same as mootools' Element.empty(), but the child
	 * elements aren't destroyed, but disposed so they can
	 * be injectected back.
	 * @see http://mootools.net/docs/Element/Element#Element:dispose
	 */
	disposeChilds: function() {
		$A(this.childNodes).each(function(node){
			Element.dispose(node);
		});
		return this;
	},
	
	insertData: function(data, prefix) {
		if(!$chk(prefix)) {
			prefix = '';
		}
		if($type(data) == 'object') {
			data = $H(data);
		}
		data.each(function(value, name) {
			className = name;
			if(prefix != '') {
				className = prefix+className.ucFirst();  
			}
			this.getElements('.'+className).each(function(node) {
				node.set('html', value);
			}, this);
		},this);
		
		return this;
	},
	
	addActions: function(actions, bind, prefix) {
		if(!$chk(prefix)) {
			prefix = '';
		}
		if($type(actions) == 'object') {
			actions = $H(actions);
		}
		actions.each(function(action, name) {
			className = 'action'+name.ucFirst();
			if(prefix != '') {
				className = prefix+className.ucFirst();  
			}
			this.getElements('.'+className).each(function(node) {
				var event = $chk(action.event) ? action.event : 'click',
					func = $chk(action.action) ? action.action : action;
				node.addEvent(event, func.bind(bind));
			}, this);
		},this);
		
		return this;
	}
});

Element.Properties.contents = (function(){
	var types = $H({
		input: 'value',
		img: 'src',
		a: 'href',
		other: 'html'
	});
	
	var contents = {
		set: function(){
			var tag = this.get('tag'),
				property = $pick(types.get(this.get('tag')), types.get('other')),
				values = arguments[0];
			if($type(values) == 'object') {
				$H(values).each(function(value, name) {
					var child = this.getElement('.'+name);
					if($chk(child)) {
						child.set('contents', value);
					}
				}, this);
			} else {
				this.set($pick(types.get(tag), types.get('other')), values);
			}
		}
	};

	return contents;
})();

String.implement({
	/**
	 * Make the string's first character uppercase
	 */
	ucFirst: function() {
	    return this.charAt(0).toUpperCase() + this.substr(1);
	}
});


Window.implement({
	/**
	 * Cross browser method te retrieve the innerSizes of the window.
	 * 
	 * @see http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	 */
	getInnerSize: function() {
		if($type(window.innerWidth) == 'number') {
			width = window.innerWidth;
			height = window.innerHeight;
		} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
			width = document.body.clientWidth;
			height = document.body.clientHeight;
		}
		return new Hash({
			x: width,
			y: height 
		});
	}
});

