﻿
function LoadAnimation(sender, args) 
{
  /*try
  {*/
    switch(document.getElementById(InputSelectedAnimation).value)
    {
      case "0":
        {
          Animation0();
          break;
        }
      case "1":
        {
          break;
        }
    }
  /*}
  catch(err)
  {
    alert(err.description); 
  }*/
}

function Animation0()
{
  animationState = new AnimationState(75, 30);
  animationFrames = new Array();
  
  var imgCursor = document.getElementById('imgAnimationCursor');
  var imgPortfolio = document.getElementById('imgPortfolio');
  var imgPortfolio1 = document.getElementById('imgPortfolio1');
  
  animationFrames.push(new FrameInfo(imgCursor, 0, 0, 15, new StateInfo(true, 200, 200, -1), new StateInfo(true, 92, 115, 100)));
  animationFrames.push(new FrameInfo(imgCursor, 2, 15, 30, new StateInfo(true, 92), new StateInfo(true, 467)));
  animationFrames.push(new FrameInfo(imgCursor, 1, 15, 30, new StateInfo(true, -1, 115), new StateInfo(true, -1, 155)));
  animationFrames.push(new FrameInfo(imgCursor, 1, 30, 45, new StateInfo(true, 467), new StateInfo(true, 842)));
  animationFrames.push(new FrameInfo(imgCursor, 2, 30, 45, new StateInfo(true, -1, 155), new StateInfo(true, -1, 115)));
  animationFrames.push(new FrameInfo(imgCursor, 2, 45, 60, new StateInfo(true, 842), new StateInfo(true, 467)));
  animationFrames.push(new FrameInfo(imgCursor, 1, 45, 60, new StateInfo(true, -1, 115), new StateInfo(true, -1, 75)));
  animationFrames.push(new FrameInfo(imgCursor, 1, 60, 75, new StateInfo(true, 467), new StateInfo(true, 92)));
  animationFrames.push(new FrameInfo(imgCursor, 2, 60, 75, new StateInfo(true, -1, 75), new StateInfo(true, -1, 115)));
  
  animationFrames.push(new FrameInfo(imgPortfolio, 0, 0, 0, new StateInfo(true), new StateInfo(true)));
  animationFrames.push(new FrameInfo(imgPortfolio1, 0, 0, 0, new StateInfo(false), new StateInfo(false)));
  
  animationFrames.push(new FrameInfo(imgPortfolio, 0, 15, 15, new StateInfo(false), new StateInfo(false)));
  animationFrames.push(new FrameInfo(imgPortfolio1, 0, 15, 15, new StateInfo(true), new StateInfo(true)));
  
  for (var i=0; i<animationState.FrameCount; i++)
  {
    setTimeout(doAnimationFrame,(1/animationState.FrameRate*1000)*(i+1));
  }
}

Sys.Application.add_init(InitializeAnimations);

function FrameInfo(animatedobject, framefunction, startframe, endframe, startstate, endstate)
{
  this.AnimatedObject = animatedobject;
  this.FrameFunction = framefunction;
  this.StartFrame=startframe;
  this.EndFrame=endframe;
  this.StartState=startstate;
  this.EndState=endstate;
}
function StateInfo(visible, left, top, opacity)
{
  if(left !== null)
  {
    this.Left = left;
  }
  else
  {
    this.Left = -1;
  }
  
  if(top !== null)
  {
    this.Top = top;
  }
  else
  {
    this.Top = -1;
  }
  if(opacity !== null)
  {
    this.Opacity = opacity;
  }
  else
  {
    this.Opacity = -1;
  }
  if(visible !== null)
  {
    this.Visible = visible;
  }
  else
  {
    this.Visible = true;
  }
}

function AnimationState(framecount, framerate)
{
 this.FrameCount = framecount;
 this.CurrentFrame = 0;
 this.FrameRate = framerate;
}
function InitializeAnimations(sender) 
{
  Sys.Application.remove_init(InitializeAnimations);
  Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(LoadAnimation);
}


function getAnimationInfo(currentframe, startframe, endframe)
{
  var info = new Object();
  info.CurrentFrame = currentframe - startframe;
  info.FrameCount = endframe - startframe;
  return info;
}

var animationState = new AnimationState(0, 30);
var animationFrames = new Array();


function doAnimationFrame()
{
  var currentFrameInfo
  for(var i = 0; i < animationFrames.length; i++)
  {
    currentFrameInfo = animationFrames[i];
    if(animationState.CurrentFrame >= currentFrameInfo.StartFrame && animationState.CurrentFrame <= currentFrameInfo.EndFrame)
    {
      var info = getAnimationInfo(animationState.CurrentFrame, currentFrameInfo.StartFrame, currentFrameInfo.EndFrame);
      
      if(currentFrameInfo.StartState.Left >= 0)
      {
        currentFrameInfo.AnimatedObject.style.left = (CalculateFrame(currentFrameInfo.FrameFunction, info.CurrentFrame, info.FrameCount, currentFrameInfo.StartState.Left, currentFrameInfo.EndState.Left)) + 'px';
      }

      if(currentFrameInfo.StartState.Top >= 0)
      {
        currentFrameInfo.AnimatedObject.style.top = (CalculateFrame(currentFrameInfo.FrameFunction, info.CurrentFrame, info.FrameCount, currentFrameInfo.StartState.Top, currentFrameInfo.EndState.Top)) + 'px';
      }
      
      if(currentFrameInfo.StartState.Opacity >= 0)
      {
        var percent = CalculateFrame(currentFrameInfo.FrameFunction, info.CurrentFrame, info.FrameCount, currentFrameInfo.StartState.Opacity, currentFrameInfo.EndState.Opacity);
        currentFrameInfo.AnimatedObject.style.opacity = percent/100;
        currentFrameInfo.AnimatedObject.style.filter = 'alpha(opacity = '+percent+')';
      }
      if(currentFrameInfo.StartState.Visible == true)
      {
        currentFrameInfo.AnimatedObject.style.visibility = 'visible';
      }
      else
      {
        currentFrameInfo.AnimatedObject.style.visibility = 'hidden';
      }
    }
  }
  animationState.CurrentFrame++;
}

function CalculateFrame(framefunction, currentframe, framecount, start, end)
{
  switch(framefunction)
  {
    case 0:
      {
        return FrameFunction0(currentframe, framecount, start, end);
      }
    case 1:
      {
        return FrameFunction1(currentframe, framecount, start, end);
      }
    case 2:
      {
        return FrameFunction2(currentframe, framecount, start, end);
      }
      case 3:
      {
        return FrameFunction3(currentframe, framecount, start, end);
      }
      case 4:
      {
        return FrameFunction4(currentframe, framecount, start, end);
      }
  }
  return 0;
}

function FrameFunction0(currentframe, framecount, start, end)
{
  return (start + (currentframe *((end-start)/(framecount))));
}

/*function FrameFunction1(currentframe, framecount, start, end)
{
  return start + (Math.sqrt(1 - (Math.pow((currentframe/framecount)-1, 2)))*(end-start));
}
function FrameFunction2(currentframe, framecount, start, end)
{
  return start + ((1-Math.sqrt(1 - (Math.pow((currentframe/framecount), 2))))*(end-start));
}*/
function FrameFunction1(currentframe, framecount, start, end)
{
  return start + ((end-start)*Math.sin((currentframe)*(Math.PI/(2*framecount))));
}
function FrameFunction2(currentframe, framecount, start, end)
{
  return start + (-1*(end-start)*Math.cos((currentframe)*(Math.PI/(2*framecount))))+(end-start);
}
function FrameFunction3(currentframe, framecount, start, end)
{
  return start + ((end-start)*Math.cos((currentframe)*(Math.PI/(2*framecount))));
}
function FrameFunction4(currentframe, framecount, start, end)
{
  return start + ((end-start)*Math.cos((currentframe)*(Math.PI/(2*framecount))));
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
