

/**
 * MoveBackground effect class.
 * Enables to move the background position of any element (x and y)
 * 
 * @author Bob Gaudaen
 * @extends Effect.Base
 */

Effect.MoveBackground = Class.create(Effect.Base, {
	
	backgroundCoords:null,
	
	initialize: function(element)
	{
		this.element = $(element);
		if (!this.element) throw(Effect._elementDoesNotExistError);
		
		this.setBackgroundCoords();
		
		var options = Object.extend({
			xfrom: this.backgroundCoords.x || 0,
			xto:   this.backgroundCoords.x || 0,
			yfrom: this.backgroundCoords.y || 0,
			yto:   this.backgroundCoords.y || 0
		}, arguments[1] || { });
		
		this.start(options);
	},
	
	setBackgroundCoords: function()
	{
		var coords = this.element.getStyle('backgroundPosition').split(' ');
		this.backgroundCoords = { x:null, y:null, xunit:null, yunit:null };
		
		// x background
		if ( Element.CSS_LENGTH.test(coords[0]) )
		{
			var components = coords[0].match(/^([\+\-]?[0-9\.]+)(.*)$/);
			this.backgroundCoords.x = parseFloat(components[1]);
			this.backgroundCoords.xunit = (components.length == 3) ? components[2] : null;
		}
		// y background
		if ( Element.CSS_LENGTH.test(coords[1]) )
		{
			var components = coords[1].match(/^([\+\-]?[0-9\.]+)(.*)$/);
			this.backgroundCoords.y = parseFloat(components[1]);
			this.backgroundCoords.yunit = (components.length == 3) ? components[2] : null;
		}
	},
	
	initPosition: function()
	{
		this.element.setStyle( {backgroundPosition: ( this.options['xfrom'] + this.backgroundCoords.xunit + ' ' + this.options['yfrom'] + this.backgroundCoords.yunit )} );
	},
	
	getNewPosition: function( factor )
	{
		var x = this.options['xfrom'] + ((this.options['xto']-this.options['xfrom'])*factor);
		var y = this.options['yfrom'] + ((this.options['yto']-this.options['yfrom'])*factor);
		return ( x + this.backgroundCoords.xunit + ' ' + y + this.backgroundCoords.yunit );
	},
	
	update: function(position)
	{
		this.element.setStyle( {backgroundPosition: this.getNewPosition(position)} );
	}
});
