YAHOO.util.Motion = function(el, attributes, duration, method) {
if (el) {
this.initMotion(el, attributes, duration, method);
}
};
YAHOO.util.Motion.prototype = new YAHOO.util.Anim();
YAHOO.util.Motion.prototype.defaultUnits.points = 'px';
YAHOO.util.Motion.prototype.doMethod = function(attribute, start, end) {
var val = null;
if (attribute == 'points') {
var translatedPoints = this.getTranslatedPoints();
var t = this.method(this.currentFrame, 0, 100, this.totalFrames) / 100;
if (translatedPoints) {
val = YAHOO.util.Bezier.getPosition(translatedPoints, t);
}
} else {
val = this.method(this.currentFrame, start, end - start, this.totalFrames);
}
return val;
};
YAHOO.util.Motion.prototype.getAttribute = function(attribute) {
var val = null;
if (attribute == 'points') {
val = [ this.getAttribute('left'), this.getAttribute('top') ];
if ( isNaN(val[0]) ) { val[0] = 0; }
if ( isNaN(val[1]) ) { val[1] = 0; }
} else {
val = parseFloat( YAHOO.util.Dom.getStyle(this.getEl(), attribute) );
}
return val;
};
YAHOO.util.Motion.prototype.setAttribute = function(attribute, val, unit) {
if (attribute == 'points') {
YAHOO.util.Dom.setStyle(this.getEl(), 'left', val[0] + unit);
YAHOO.util.Dom.setStyle(this.getEl(), 'top', val[1] + unit);
} else {
YAHOO.util.Dom.setStyle(this.getEl(), attribute, val + unit);
}
};
YAHOO.util.Motion.prototype.initMotion = function(el, attributes, duration, method) {
YAHOO.util.Anim.call(this, el, attributes, duration, method);
attributes = attributes || {};
attributes.points = attributes.points || {};
attributes.points.control = attributes.points.control || [];
this.attributes = attributes;
var start;
var end = null;
var translatedPoints = null;
this.getTranslatedPoints = function() { return translatedPoints; };
var translateValues = function(val, self) {
var pageXY = YAHOO.util.Dom.getXY(self.getEl());
val = [ val[0] - pageXY[0] + start[0], val[1] - pageXY[1] + start[1] ];
return val;
};
var onStart = function() {
start = this.getAttribute('points');
var attributes = this.attributes;
var control = attributes['points']['control'] || [];
if (control.length > 0 && control[0].constructor != Array) {
control = [control];
}
if (YAHOO.util.Dom.getStyle(this.getEl(), 'position') == 'static') {
YAHOO.util.Dom.setStyle(this.getEl(), 'position', 'relative');
}
if (typeof attributes['points']['from'] != 'undefined') {
YAHOO.util.Dom.setXY(this.getEl(), attributes['points']['from']);
start = this.getAttribute('points');
}
else if ((start[0] === 0 || start[1] === 0)) {
YAHOO.util.Dom.setXY(this.getEl(), YAHOO.util.Dom.getXY(this.getEl()));
start = this.getAttribute('points');
}
var i, len;
if (typeof attributes['points']['to'] != 'undefined') {
end = translateValues(attributes['points']['to'], this);
for (i = 0, len = control.length; i < len; ++i) {
control[i] = translateValues(control[i], this);
}
} else if (typeof attributes['points']['by'] != 'undefined') {
end = [ start[0] + attributes['points']['by'][0], start[1] + attributes['points']['by'][1]];
for (i = 0, len = control.length; i < len; ++i) {
control[i] = [ start[0] + control[i][0], start[1] + control[i][1] ];
}
}
if (end) {
translatedPoints = [start];
if (control.length > 0) { translatedPoints = translatedPoints.concat(control); }
translatedPoints[translatedPoints.length] = end;
}
};
this._onStart.subscribe(onStart);
};