
var PREDEF_BEHAVIORS = new Object()
PREDEF_BEHAVIORS["LINEAR"] = new Array(0,1.2494698687457007,2.4991499546724416,3.7494154601902085,4.999517776124351,6.250829046167494,7.50047317560143,8.749797409282722,10.000026517376215,11.250319186200786,12.499869169013467,13.749780615308316,14.999215222025297,16.249179035556296,17.50058151490976,18.75079133344211,20.00076240605142,21.249717317076254,22.50018981991588,23.749761316127625,24.999341485289108,26.24980114951243,27.500500923111872,28.749465318160496,30.00065968797574,31.249462415546112,32.49993962868714,33.75062630896255,35.000340388868125,36.24946642480764,37.49983540498065,38.74966044853569,39.999970038567724,41.2495861245469,42.50004541280874,43.74955347505423,45.000146215684154,46.25056069652822,47.499783077726086,48.750415391591105,50,51.249584608408895,52.500216922273914,53.74943930347178,54.999853784315846,56.25044652494577,57.49995458719126,58.7504138754531,60.000029961432276,61.25033955146431,62.50016459501935,63.75053357519236,64.99965961113188,66.24937369103745,67.50006037131286,68.7505375844539,69.99934031202426,71.2505346818395,72.49949907688813,73.75019885048758,75.00065851471089,76.25023868387237,77.49981018008413,78.75028268292374,79.99923759394858,81.24920866655789,82.49941848509023,83.7508209644437,85.0007847779747,86.25021938469169,87.50013083098654,88.74968081379922,89.99997348262379,91.25020259071728,92.49952682439857,93.7491709538325,95.00048222387565,96.2505845398098,97.50085004532755,100)
PREDEF_BEHAVIORS["EASEINOUT"] = new Array(0,0.12151605824027589,0.27426025573848056,0.46099016434521944,0.6838882976875227,0.9461765227316241,1.2503810331107146,1.6009577612947452,2.000962588993145,2.456535016203824,2.971457208170944,3.5531144900053753,4.20894527666967,4.946111014184648,5.776730382676334,6.710966733572537,7.766054039882587,8.9574143202657,10.30884255290879,11.848399238372984,13.611886732242592,15.642232710070825,18.000467263375885,20.7592819683429,24.012623792484064,27.858462303957065,32.3705693678974,37.513285037522465,43.03964438472933,48.52162131834423,53.595445170844634,58.09473199774179,62.02522552897664,65.45395258655655,68.45893182974226,71.11749064922901,73.48353742841701,75.60760017340849,77.52287062836959,79.26483279015224,80.85258686887013,82.30616315030706,83.64444407219224,84.87688444689893,86.01894658083205,87.0756842039126,88.05807784736706,88.9719337788153,89.82277412301198,90.61582250636847,91.35730497816394,92.04915521060578,92.69578959230235,93.30130114222915,93.86719310223214,94.3960831252023,94.89043537796819,95.35356236244058,95.78556919445568,96.18858180581184,96.56544490512391,96.91617578113238,97.24176143149626,97.5454525458656,97.82645901754029,98.08700729417333,98.32711449108726,98.54814519048784,98.75130874686764,98.93663830896543,99.10527302287053,99.25776569434956,99.39467257489065,99.51690836434275,99.62491937275004,99.71915497057972,99.80006712941741,99.86810996981686,99.92390533386626,100)
PREDEF_BEHAVIORS["EASEOUT"] = new Array(0,5.388930545607207,10.678028500925848,15.377930277322832,19.63115096117962,23.53366757679599,27.14581057422637,30.51760412671325,33.67732096370692,36.65501297515287,39.47025225103688,42.13851820621927,44.67318928570355,47.08860900348876,49.38986064043809,51.59055928990513,53.69624972310332,55.71333792891624,57.64657692453848,59.50121727303793,61.28286970034299,62.99290596067575,64.6396615239736,66.22266374484033,67.7460599078702,69.21189338772484,70.62656675371593,71.98584185440447,73.29828753154604,74.5619419054504,75.77910559555944,76.95206895261694,78.0830957289726,79.17440841788199,80.22440413366735,81.23723929041441,82.21322877932946,83.15274440594138,84.05970472456707,84.93094760138227,85.77034068080151,86.57822601692908,87.35498231880905,88.10258445250454,88.82136363069584,89.51167978985588,90.17392008623595,90.80990368835252,91.4185824379479,92.00306935222268,92.56231831663764,93.09670881222274,93.60784399261871,94.09485191111699,94.56041021798467,95.00363575530125,95.4248646236812,95.82444615447386,96.20369870872813,96.56195556474174,96.90047365482806,97.21952154419782,97.51937737958866,97.80032849406129,98.06337217276527,98.3073676573662,98.53398656526088,98.7434193954135,98.93533519631407,99.11006824174876,99.26884174256429,99.41094641759892,99.53708917925917,99.64749779785453,99.74240534689201,99.82205000567194,99.88667486366191,99.9365277266493,99.97194982343055,100)
PREDEF_BEHAVIORS["EASEIN"] = new Array(0,0.0052484575493672025,0.021049276180473697,0.0476556064538336,0.08516566682699178,0.13390401336179636,0.19411639499411493,0.2657788522443916,0.3490172113953527,0.444297585792075,0.5522353013941661,0.6719176252345527,0.8047156832390989,0.9504721988098375,1.1094073628139078,1.2817374485929456,1.467674634121341,1.6680739172314496,1.8825672851096265,2.1113532634275067,2.3553860426672735,2.614967205310581,2.889556538111737,3.1802045853130956,3.4881060845779466,3.811752955078429,4.15327475965066,4.512022359410306,4.889310768489974,5.284399438669762,5.698660349691043,6.132432091622864,6.586042849875806,7.061055534391958,7.556603168933496,8.07429739319021,8.613171598210467,9.176273977571991,9.76407122820655,10.375548800829204,11.012528611698169,11.675420819206028,12.364613991721729,13.083683171030724,13.831538118251618,14.608595047754047,15.4169482670663,16.258795117539204,17.1346547823247,18.045004749116966,18.993959839574526,19.98022401372035,21.006126348562315,22.075997016239857,23.18844215552618,24.34785682375873,25.558785141498586,26.819734156784403,28.135225697551913,29.507758974100984,30.94405241173943,32.446626592213605,34.02006178291623,35.66886482147646,37.40185511365608,39.223309871072146,41.13945015979495,43.165212754447865,45.30606432065723,47.580414879095215,50,52.58648255375134,55.366086228791914,58.37275091926885,61.646827223826776,65.25400994920707,69.28409540291503,73.89044075003133,79.35100986073964,100)


//uses a bezier curve, to create a 
function Behavior()
{
    //pre-defined animation-behaviors
    this.type = "Behavior"
    this.behavior = "custom"
    this.data = null
    this.frameCount = -1
    this.bezier = null
    this.array = null
    if(Behavior.arguments[0].split)
    {
        this.behavior = Behavior.arguments[0]
        switch(Behavior.arguments[0])
        {
            case "LINEAR":
                this.array = PREDEF_BEHAVIORS["LINEAR"]
            break;
            case "EASEIN":
                this.array = PREDEF_BEHAVIORS["EASEIN"]
            break;
            case "EASEOUT":
                this.array = PREDEF_BEHAVIORS["EASEOUT"]
            break;
            case "EASEINOUT":
                this.array = PREDEF_BEHAVIORS["EASEINOUT"]
            break;
        }
        this.frameCount = this.array.length
    }
    else
    {
        //user defined, if possible generate the arrays using this method
        // and program in your own pre-defined behaviors by making a label in the above switch
        
        // is assumed that arguments[0] is an object
        // for the beziercurveInterpolator class in numerics.js
        //the frameCount is determined by the last x value in the data object
        this.data = Behavior.arguments[0]
        var len =  this.data.x.length
        this.frameCount = this.data.x[len-1]
        this.bezier = new BezierCurveInterpolator(this.data);
        this.bezier.interpolate()
    }
    
    this.getIndexOfCurve = function (x)
    {
      
            //it is assumed that the method object has a "x" member
            var min = 1;
            var max = this.bezier.x.length
            do
            {
              mid = parseInt((min + max) / 2);
              if(x > this.bezier.x[mid])
                min = mid + 1
              else 
                max = mid - 1;
            }
            while (!((this.bezier.x[mid] == x) || (min > max)))
            //interval

            return Math.min(max,min)
        
    }
    
    
    this.getArray = function()
    {
        //this returns an array of the values generated by
        //the bezier curve interpolater, if it is predefined it just
        //returns the array located at the bottom
        if(this.array != null)
        {
            return this.array
        }
        else
        {
            //generate the values from the bezier curve up to framecount
            this.array = new Array()
            
            for(var i = 0; i < this.frameCount; i++)
            {
                if(i == 0){this.array[i] = 0; continue}
                if(i == this.frameCount-1){this.array[i] = 100; break;}
                var j = this.getIndexOfCurve(i)
                var curve =  this.bezier.curves[j]
                var rf = new RootFinder("BISECTION",{f:new Function("t","return "+curve.a0+"-"+i+"+"+curve.a1+"*t+"+curve.a2+"*t*t+"+curve.a3+"*t*t*t;")})
                var t = rf.method(0,1)
                this.array[i] = this.bezier.getFY(curve)(t);
            }
        }
        return this.array
    }
    
    this.print = function()
    {
        for(var i = 0; i < this.array.length; i++)
        {
            document.write(this.array[i]+",");
        }
    }
}




function getPredefBehavior(str,frameCount)
{
    switch(str)
    {
        case "LINEAR":
            data = {x:new Array(0,frameCount),y:new Array(0,100),
                    xleft:new Array(),yleft:new Array(),
                    xright:new Array(),yright:new Array()}
                    data.xleft[0] = frameCount*.59;data.yleft[0] = 5;
                    data.xright[1] = frameCount*.07;data.yright[1] = 98;
            return new Behavior(data)
        break;
        case "EASEIN":
            data = {x:new Array(0,frameCount),y:new Array(0,100),
                    xleft:new Array(),yleft:new Array(),
                    xright:new Array(),yright:new Array()}
                    data.xleft[0] = frameCount-1;data.yleft[0] = 5;
                    data.xright[1] = frameCount-1;data.yright[1] = 98;
            return new Behavior(data)
        break;
        case "EASEOUT":
            data = {x:new Array(0,frameCount),y:new Array(0,100),
                    xleft:new Array(),yleft:new Array(),
                    xright:new Array(),yright:new Array()}
                    data.xleft[0] = 0.07*frameCount;data.yleft[0] = 2;
                    data.xright[1] = 0.04*frameCount;data.yright[1] = 98;
            return new Behavior(data)
        break;
        case "EASEINOUT":
            data = {x:new Array(0,frameCount),y:new Array(0,100),
                    xleft:new Array(),yleft:new Array(),
                    xright:new Array(),yright:new Array()}
                    data.xleft[0] = frameCount-2;data.yleft[0] = 2;
                    data.xright[1] = 0.04*frameCount;data.yright[1] = 98;
            return new Behavior(data)
        break;
        default:
            return new Behavior("LINEAR");
    }
}


