/*
Script: mootools.extensions.js
	Contains Element extensions, <AjaxPlus>, <ActionPack>, <ImageLoader>, <Gallery>, <Sitemap>, <Slide>, <TabContainer>

Author:
	Tom Occhino

License:
	MIT-style license.
*/

Element.extend({

	visible: function(){
		return this.getStyle('display') == 'none' || this.getStyle('opacity') == 0 ? false : true;
	},
	
	hide: function(){
		return this.setStyle('display', 'none');
	},
	
	show: function(){
		return this.setOpacity(1).setStyle('display', '');
	},
	
	toggle: function(){
		return this.visible() ? this.hide() : this.show();
	},
	
	appendHTML: function(html){
		return this.setHTML(this.innerHTML + html);
	},
	
	cleanWhitespace: function(){
		$A(this.childNodes).each(function(node){
			if ($type(node) == 'whitespace') this.removeChild(node);
		}, this);
		return this;
	},
	
	setValue: function(value){
		if (this.getTag().test(/input|select|textarea/)) this.value = value;
		return this;
	},

	removeProperty: function(property){
		if (this.getProperty(property)) this.removeAttribute(property);
		return this;
	},
	
	removeProperties: function(source){
		for (var i = 0, j = source.length; i < j; i++) this.removeProperty(source[i]);
		return this;
	},
	
	disableSelection: function(){
		if (window.ie) this.onselectstart = function(){ return false };
		this.style.MozUserSelect = "none";
		return this;
	},
	
	getSiblings: function(){
		/* method 1 - not working
		return this.getParent().getChildren().remove(this); */
		
		/* method 2
		var children = [];
		this.getParent().getChildren().each(function(e){ if(e != this) children.push(e); }.bind(this));
		return children; */
		
		/* method 3 */
		var children = this.getParent().getChildren();
		children.splice(children.indexOf(this), 1);
		return children;
	},
	
	getHeight: function(){
		return this.getCoordinates().height;
	}
	
});


Array.extend({

	flatten : function(){
		var item = this.shift();
		var flattened = arguments[0] ? arguments[0] : [];
		if(!item) return flattened;
		else if($type(item) == 'array') return item.merge(this).flatten(flattened);
		else {
			flattened.push(item);
			return this.flatten(flattened);
		}
	}

});

/* TODO
String.extend({
	truncate: function(){
		
	}
});
*/