/* MooTools Extras by Marc LP / CoolWind */

/*
	String
		(from Prototype)
		stripTags
		escapeHTML
		unescapeHTML

*/

String.extend({
				stripTags: function() {    return this.replace(/<\/?[^>]+>/gi, '');  },
			  
				escapeHTML: function() {
					var div = document.createElement('div');
					var text = document.createTextNode(this);
					div.appendChild(text);
					return div.innerHTML;
				  },
				
				unescapeHTML: function() {
					var div = document.createElement('div');
					div.innerHTML = this.stripTags();
					return div.childNodes[0] ? (div.childNodes.length > 1 ?
					  $A(div.childNodes).inject('',function(memo,node){ return memo+node.nodeValue }) :
					  div.childNodes[0].nodeValue) : '';
				  }	
			 }
);

Element.extend({
	
	getOffsets : function() {
		offset = {	left: this.offsetLeft, 
					top: this.offsetTop, 
					height: this.offsetHeight, 
					width: this.offsetWidth};

		var parent = this.offsetParent;
		while (parent) {

			offset.left += parent.offsetLeft;
			offset.top += parent.offsetTop;
			
			if(parent.scrollTop) 	{ offset.top -= parent.scrollTop;	}
			if(parent.scrollLeft)	{ offset.left -= parent.scrollLeft;	}

			parent = parent.offsetParent;
		}
		
		return offset
	},			   
			   
			   
	fx_pulsate: function() {
		if(!this.fxm_pulsate) this.fxm_pulsate = new Fx.Style(this, 'opacity', {duration: 300});
		this.fxm_pulsate.start(0).chain(function() {
			this.fxm_pulsate.start(1);
		}.bind(this)).chain(function() {
			this.fxm_pulsate.start(0);
		}.bind(this)).chain(function() {
			this.fxm_pulsate.start(1);
		}.bind(this)).chain(function() {
			this.fxm_pulsate.start(0);
		}.bind(this)).chain(function() {			
			this.fxm_pulsate.start(1);
		}.bind(this));	// necessitem bind(this) perque el this al que fa referčncia la instrucció dins el chain seria el propi chain!!! i volem que sigui l'element this de fora
		return this;
	},
	
	fx_fadein: function() {
		this.setOpacity(0);
		this.setStyle("display", "block");
		if(!this.fxm_fadein) this.fxm_fadein = new Fx.Style(this, 'opacity', {duration: 1000, wait: false});
		this.fxm_fadein.start(0,1);
		return this;
	},
	
	fx_fadeout: function() {
		this.setOpacity(1);
		if(!this.fxm_fadeout) this.fxm_fadeout = new Fx.Style(this, 'opacity', {duration: 1000, wait: false});
		this.fxm_fadeout.start(1,0);
		return this;
	},
	
	fx_abscenter: function() {
		// centra un element absolut a la pantalla
		var coords = this.getCoordinates();
		var body_c = browser_window_coordinates();
		var el_left = (body_c.width - coords.width) / 2;
		var el_top = (body_c.height - coords.height) / 2;		
		this.setStyles({ top: el_top, left: el_left });
	},
	
	fx_puff: function() {
		
		var coords = this.getCoordinates();
		
		var tmp = new Fx.Styles(this, {duration: 800});
				
		tmp.start({	'opacity': [1,0],
					'width': [coords.width, coords.width * 3],
					'height': [coords.height, coords.height * 5],
					'font-size': [10, 15]
					}).chain(function(){ this.style.display = 'none'; }.bind(this));

		return this;		
	}
});
