﻿//CONSTRUCTOR

function Tweener(oControl, oRootElement) {

    //PROPERTIES
    this.__oControl = oControl;
    this.__oRootElement = oRootElement;
    
    this.__oTargetElement;
    
    //INITIATE 
    this.tTween = new TweenManager(this.__oControl, this.__oRootElement);

   //ANIMATION TYPES
   
   
   
   //FADE N SLIDE
   this.fadeNSlide = function(oTarget,
                        nFrom,
                        nTo,
                        sDelay,
                        sDuration
   ) {
   
   
     //PROPERTIES
     var _oTarget = oTarget;
     var _nFrom = nFrom;
     var _nTo = nTo;
     var _sDelay = sDelay; 
     var _sDuration = sDuration;
     var _sStoryboard = _oTarget.name + "_Story";
     
     
     var _nFrom2 = _oTarget["Canvas.Top"] + 10;
     var _nTo2 = _nFrom2 - 10;
     
       this.removeStoryboard(_sStoryboard);
     
      this.tTween.add(_sStoryboard, "KeyFrame"); //INITIATE TWEEN TYPE
      this.tFade = this.tTween.Get(_sStoryboard); //SET REFERENCE
      
      //SET PROPERTIES FOR ANIMATION;
      this.tFade.target = _oTarget;
      this.tFade.property = "Opacity"; 
      this.tFade.storyboard = _sStoryboard;   
     
      this.tFade.addSpline("0.6,0.0 0.9,0.00",_nFrom ,_sDelay);
      this.tFade.addSpline("0.0,0.0 0.0,0.00",_nTo ,_sDuration);
      this.tFade.addKeyFrame();
      
      
      this.tFade.property = "(Canvas.Top)"; 
      this.tFade.addSpline("0.6,0.0 0.9,0.00",_nFrom2 ,_sDelay);
      this.tFade.addSpline("0.2,0.0 0.2,0.00",_nTo2 ,_sDuration);
      this.tFade.addKeyFrame();
      
      
      //BUILD
      this.tFadeXaml = this.build(this.tFade);
      this.tFadeXaml.Begin();
      
      return this.tFadeXaml;
      }
      
      
      
   //FADE
   this.fade = function(oTarget,
                        nFrom,
                        nTo,
                        sDelay,
                        sDuration,
                        sStatus
   ) {
    //PROPERTIES
     var _oTarget = oTarget;
     var _nFrom = nFrom;
     var _nTo = nTo;
     var _sDelay = sDelay; 
     var _sDuration = sDuration;
     var _sStoryboard = _oTarget.name + "_Story";
     
    
     
     this.removeStoryboard(_sStoryboard);
     
      this.tTween.add(_sStoryboard, "KeyFrame"); //INITIATE TWEEN TYPE
      this.tFade = this.tTween.Get(_sStoryboard); //SET REFERENCE
      
      //SET PROPERTIES FOR ANIMATION;
      this.tFade.target = _oTarget;
      this.tFade.property = "Opacity"; 
      this.tFade.storyboard = _sStoryboard;   
     
      this.tFade.addSpline("0.6,0.0 0.9,0.00",_nFrom ,_sDelay);
      this.tFade.addSpline("0.0,0.0 0.0,0.00",_nTo ,_sDuration);
      this.tFade.addKeyFrame();
      
    
      
      //BUILD
      this.tFadeXaml = this.build(this.tFade);
      this.tFadeXaml.Begin();
      
      if(sStatus ==  "callComplete") {
        this.__oTargetElement = _oTarget;
        this.onAnimationCompleted(this.tFadeXaml);
      }
      return this.tFadeXaml;
      }
    
    
    
    
  this.fadeNSlide2 = function(oTarget,
                        nFrom,
                        nTo,
                        sDelay,
                        sDuration
   ) {
   
   
     //PROPERTIES
     var _oTarget = oTarget;
     var _nFrom = nFrom;
     var _nTo = nTo;
     var _sDelay = sDelay; 
     var _sDuration = sDuration;
     var _sStoryboard = _oTarget.name + "_Story";
     
     this.removeStoryboard(_sStoryboard);
     
      this.tTween.add(_sStoryboard, "KeyFrame"); //INITIATE TWEEN TYPE
      this.tFade = this.tTween.Get(_sStoryboard); //SET REFERENCE
      
      //SET PROPERTIES FOR ANIMATION;
      this.tFade.target = _oTarget;
      this.tFade.property = "Opacity"; 
      this.tFade.storyboard = _sStoryboard;   
     
      this.tFade.addSpline("0.6,0.0 0.9,0.00",0,_sDelay);
      this.tFade.addSpline("0.0,0.0 0.0,0.00",1,_sDuration);
      this.tFade.addKeyFrame();
      
      
      this.tFade.property = "(Canvas.Left)"; 
      this.tFade.addSpline("0.6,0.0 0.9,0.00",_nFrom ,_sDelay);
      this.tFade.addSpline("0.2,0.0 0.2,0.00",_nTo ,_sDuration);
      this.tFade.addKeyFrame();
      
      
      //BUILD
      this.tFadeXaml = this.build(this.tFade);
      this.tFadeXaml.Begin();
      
      return this.tFadeXaml;
      }
  
 
  //BUILD ANIMATION 
  this.build = function(oAnimation) {
    this._ani = oAnimation.buildTween();
    this.__oRootElement.Resources.add(this._ani);
    return  this._ani;
  }
  
  //ANIMATION COMPLETE
  this.onAnimationCompleted = function(xamlFragment) {
   xamlFragment.addEventListener("Completed", Silverlight.createDelegate(this, this.completed));
  }
  
  this.completed = function(sender, eventArgs) {
   if( this.__oRootElement.findName(sender.children.getItem(0)["Storyboard.TargetName"]) !=null) {
         this.__oRootElement.findName(sender.children.getItem(0)["Storyboard.TargetName"]).visibility = "Collapsed";
   }
 }
  
  
  //GARBAGE COLLECTION  
  
  
  this.removeStoryboard = function(oStoryboard) {
    if(this.__oRootElement.findName(oStoryboard) != null) {
            this.__oRootElement.Resources.Remove(this.__oRootElement.findName(oStoryboard));
         }
   
  }
}

