/*
 * Yuichi Tateno. <hotchpotch@N0!spam@gmail.com>
 * http://rails2u.com/
 * 
 * The MIT License
 * --------
 * Copyright (c) 2007 Yuichi Tateno.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

var JSTweener={looping:false,frameRate:60,objects:[],defaultOptions:{time:1,transition:'easeoutexpo',delay:0,prefix:{},suffix:{},onStart:undefined,onStartParams:undefined,onUpdate:undefined,onUpdateParams:undefined,onComplete:undefined,onCompleteParams:undefined},inited:false,easingFunctionsLowerCase:{},init:function(){this.inited=true;for(var key in JSTweener.easingFunctions){this.easingFunctionsLowerCase[key.toLowerCase()]=JSTweener.easingFunctions[key];}},toNumber:function(value,prefix,suffix){if(!suffix){suffix='px';}return value.toString().match(/[0-9]/)?Number(value.toString().replace(new RegExp(suffix+'$'),'').replace(new RegExp('^'+(prefix?prefix:'')),'')):0;},addTween:function(obj,options){var self=this;if(!this.inited){this.init();}var o={};o.target=obj;o.targetPropeties={};for(var key in this.defaultOptions){if(typeof options[key]!='undefined'){o[key]=options[key];delete options[key];}else{o[key]=this.defaultOptions[key];}}if(typeof o.transition=='function'){o.easing=o.transition;}else{o.easing=this.easingFunctionsLowerCase[o.transition.toLowerCase()];}for(var key in options){if(!o.prefix[key]) o.prefix[key]='';if(!o.suffix[key]) o.suffix[key]='';var sB=this.toNumber(obj[key],o.prefix[key],o.suffix[key]);o.targetPropeties[key]={b:sB,c:options[key]-sB};}setTimeout(function(){o.startTime=(new Date()-0);o.endTime=o.time*1000+o.startTime;if(typeof o.onStart=='function'){if(o.onStartParams){o.onStart.apply(o,o.onStartParams);}else{o.onStart();}}self.objects.push(o);if(!self.looping){self.looping=true;self.eventLoop.call(self);}},o.delay*1000);},eventLoop:function(){var now=(new Date()-0);for(var i=0; i<this.objects.length; i++){var o=this.objects[i];var t=now-o.startTime;var d=o.endTime-o.startTime;if(t >= d){for(var property in o.targetPropeties){var tP=o.targetPropeties[property];try{o.target[property]=o.prefix[property]+(tP.b+tP.c)+o.suffix[property];} catch(e){}}this.objects.splice(i,1);if(typeof o.onUpdate=='function'){if(o.onUpdateParams){o.onUpdate.apply(o,o.onUpdateParams);}else{o.onUpdate();}}if(typeof o.onComplete=='function'){if(o.onCompleteParams){o.onComplete.apply(o,o.onCompleteParams);}else{o.onComplete();}}}else{for(var property in o.targetPropeties){var tP=o.targetPropeties[property];var val=o.easing(t,tP.b,tP.c,d);try{o.target[property]=o.prefix[property]+val+o.suffix[property];}catch(e){}}if(typeof o.onUpdate=='function'){if(o.onUpdateParams){o.onUpdate.apply(o,o.onUpdateParams);}else{o.onUpdate();}}}}if(this.objects.length > 0){var self=this;setTimeout(function(){self.eventLoop()},1000/self.frameRate);}else{this.looping=false;}}};JSTweener.Utils={bezier2:function(t,p0,p1,p2){return (1-t)*(1-t)*p0+2*t*(1-t)*p1+t*t*p2;},bezier3:function(t,p0,p1,p2,p3){return Math.pow(1-t,3)*p0+3*t*Math.pow(1-t,2)*p1+3*t*t*(1-t)*p2+t*t*t*p3;},allSetStyleProperties:function(element){var css;if(document.defaultView&&document.defaultView.getComputedStyle){css=document.defaultView.getComputedStyle(element,null);}else{css=element.currentStyle } for(var key in css){if(!key.match(/^\d+$/)){try{element.style[key]=css[key];} catch(e){};}}}};JSTweener.easingFunctions={easeNone:function(t,b,c,d){return c*t/d+b;}};JSTweener.easingFunctions.linear=JSTweener.easingFunctions.easeNone;
