/////////////////////////////////////////////////////
// General Variables
var wpf;
var pressureText;
var arm;
var hand;
var inkPresenterArm;
var inkPresenterHand;
var newStroke = null;
var erasePoints = null;
var currentWidth = 2;
var currentColorRect;
var machine;
var eraser;
var studio;
var currentInkPresenter;

/////////////////////////////////////////////////////
// Sounds
var ouchSoundArray = new Array();
var ouchSoundCounter = 0;
var buzzSound;

/////////////////////////////////////////////////////
// Colors
var inkBlack =  "#EE110011";
var faintRed =  "#CC770000";
var inkRed =    "#DDFF1111";
var inkGreen =  "#DD11FF11";
var inkBlue =   "#DD1111FF";
var inkYellow = "#CCFFFF11";
var CurrentColor = inkBlack;
var CurrentOutlineColor = faintRed;

/////////////////////////////////////////////////////
// Animation Storyboards
var zoomInBlack;
var zoomOutBlack;
var zoomInRed;
var zoomOutRed;
var zoomInGreen;
var zoomOutGreen;
var zoomInBlue;
var zoomOutBlue;
var zoomInYellow;
var zoomOutYellow;

function root_Loaded(sender, args) 
{
    wpf = document.getElementById("Agcontrol");
    inkPresenterArm = sender.findName("inkPresenterArm");
    inkPresenterHand = sender.findName("inkPresenterHand");

    buzzSound = sender.findName("buzz");    
    ouchSoundArray[0] = sender.findName("ouch");
    ouchSoundArray[1] = sender.findName("ouch2");
    ouchSoundArray[2] = sender.findName("ouch3");
    ouchSoundArray[3] = sender.findName("ouch4");
    ouchSoundArray[4] = sender.findName("ouch5");
    
    pressureText = sender.findName("pressureText");
    arm = sender.findName("arm");
    hand = sender.findName("handPart");
    machine = sender.findName("machine");
    eraser = sender.findName("eraser");
    studio = sender.findName("studio");
    
    zoomInBlack = wpf.content.findName("zoomInBlack");
    zoomOutBlack = wpf.content.findName("zoomOutBlack");
    zoomInRed = wpf.content.findName("zoomInRed");
    zoomOutRed = wpf.content.findName("zoomOutRed");
    zoomInGreen = wpf.content.findName("zoomInGreen");
    zoomOutGreen = wpf.content.findName("zoomOutGreen");
    zoomInBlue = wpf.content.findName("zoomInBlue");
    zoomOutBlue = wpf.content.findName("zoomOutBlue");
    zoomInYellow = wpf.content.findName("zoomInYellow");
    zoomOutYellow = wpf.content.findName("zoomOutYellow");
       
    currentInkPresenter = inkPresenterArm;
    SelectArm();
    zoomInBlack.begin();    
}

//////////////////////////////////////////////////////
// Event Handlers

function InkPresenterMouseDown(sender,args)
{
   if(sender.Name == currentInkPresenter.name)
   {
      currentInkPresenter.captureMouse();

      var stylusInfo = args.getStylusInfo();
      if (!stylusInfo.isInverted)
      {
         // start inking 
         machine.opacity = 0.5;
         newStroke = wpf.content.createFromXaml('<Stroke/>');
         newStroke.drawingAttributes.setValue("Width", currentWidth);
         newStroke.drawingAttributes.setValue("Height", currentWidth);
         newStroke.drawingAttributes.setValue("Color", CurrentColor);
         newStroke.drawingAttributes.setValue("OutlineColor", CurrentOutlineColor);
         newStroke.stylusPoints.addStylusPoints(args.getStylusPoints(currentInkPresenter));
         currentInkPresenter.strokes.add(newStroke);
         buzzSound.Play();
      }
      else
      {
         // start erasing
         erasePoints = wpf.content.createFromXaml('<StylusPointCollection/>');
         erasePoints.addStylusPoints(args.getStylusPoints(currentInkPresenter));
      }
   }
}

// Add the new points to the Stroke we’re working with
function InkPresenterMouseMove(sender,args)
{
   if (sender.name == currentInkPresenter.name)
   {
      var stylusInfo = args.getStylusInfo();
      if (!stylusInfo.isInverted && newStroke != null)
      {
         newStroke.stylusPoints.addStylusPoints(args.getStylusPoints(currentInkPresenter));
         buzzSound.position = "00:00:00";

         var points = args.getStylusPoints(currentInkPresenter);
         var latestPoint = points.getItem(0);
         var count = points.count;

         for(var i = 0; i < count; i += 2)
         {
            latestPoint = points.getItem(i);
            pressureText.text = String(points.count) + ", pressure=" + String(latestPoint.pressureFactor);
            if(latestPoint.pressureFactor == 1.0 && ouchSoundArray[ouchSoundCounter%ouchSoundArray.length].currentState != "Playing")
            {
               ouchSoundCounter++;
               ouchSoundArray[ouchSoundCounter%ouchSoundArray.length].play();
            }
         }
      }
      else if (stylusInfo.isInverted && erasePoints != null)
      {
         erasePoints.addStylusPoints(args.getStylusPoints(currentInkPresenter));
         var hitStrokes = currentInkPresenter.strokes.hitTest(erasePoints);
         for (i=0; i<hitStrokes.count; i++)
         {
            currentInkPresenter.strokes.remove(hitStrokes.getItem(i));
         }
      }
   }
}

function StudioCanvasMouseMove(sender,args)
{  
   var stylusInfo = args.getStylusInfo();
   if (stylusInfo.isInverted)
   {
       machine.opacity = 0.;
       eraser.opacity = 1.0;
       eraser["Canvas.Left"] = args.getPosition(null).x - 9;
       eraser["Canvas.Top"] = args.getPosition(null).y - 199 - wpf.content.findName("clientArea").getValue("Canvas.Top");
   }
   else
   {
       eraser.Opacity = 0.;
       machine.Opacity = 1.0;
       machine["Canvas.Left"] = args.getPosition(null).x - 9;
       machine["Canvas.Top"] = args.getPosition(null).y - 199 - wpf.content.findName("clientArea").getValue("Canvas.Top");
   } 
}

// Release the mouse
function InkPresenterMouseUp(sender,args)
{
   if (sender.Name == currentInkPresenter.name)
   {
       newStroke = null;
       currentInkPresenter.releaseMouseCapture();
       buzzSound.stop();
       machine.opacity = 1.0;
       erasePoints = null;
   }
}

function SoundEnded(sender,args)
{
    sender.stop();
    sender.position = "00:00:00";
}

//////////////////////////////////////////////
// InkBottle Selection Functions
function SelectBlack()
{
    currentWidth = 2;
    CurrentColor = inkBlack;
    CurrentOutlineColor = faintRed;
    
    zoomInBlack.begin();
    
    zoomOutRed.begin();
    zoomOutGreen.begin();
    zoomOutBlue.begin();
    zoomOutYellow.begin();
}

function SelectRed()
{
    currentWidth = 4;
    CurrentColor = inkRed;
    CurrentOutlineColor = "#00000000";
    
    zoomInRed.begin();
    
    zoomOutBlack.begin();
    zoomOutGreen.begin();
    zoomOutBlue.begin();
    zoomOutYellow.begin();
}

function SelectGreen()
{
    currentWidth = 4;
    CurrentColor = inkGreen;
    CurrentOutlineColor = "#00000000";
    
    zoomInGreen.begin();
    
    zoomOutBlack.begin();
    zoomOutRed.begin();
    zoomOutBlue.begin();
    zoomOutYellow.begin();
}

function SelectBlue()
{
    currentWidth = 4;
    CurrentColor = inkBlue;
    CurrentOutlineColor = "#00000000";
    
    zoomInBlue.begin();
    
    zoomOutBlack.begin();
    zoomOutRed.begin();
    zoomOutGreen.begin();
    zoomOutYellow.begin();
}

function SelectYellow()
{
    currentWidth = 4;
    CurrentColor = inkYellow;
    CurrentOutlineColor = "#00000000";
    
    zoomInYellow.begin();
    
    zoomOutBlack.begin();
    zoomOutRed.begin();
    zoomOutGreen.begin();
    zoomOutBlue.begin();
}

///////////////////////////////////////////////
// Scenery Selections
function StudioGarage()
{
    studio.source="assets/garage.jpg";
}

function StudioStudio()
{
    studio.source="assets/studio.jpg";
}

///////////////////////////////////////////////
// Body Part Selection Fuinctions
function SelectHand()
{
   ClearInk();
   arm.opacity = 0.0;
   
   inkPresenterArm.width = 0;
   inkPresenterArm.height = 0;
   inkPresenterHand.width = 800;
   inkPresenterHand.height = 600;
   currentInkPresenter = inkPresenterHand;
   
   var handAnimation = wpf.content.findName("translateHand");
   handAnimation.begin();
   hand.Opacity = 1.0;
}

function SelectArm()
{
   ClearInk();
   hand.opacity = 0.0;
   
   inkPresenterArm.width = 800;
   inkPresenterArm.height = 600;
   inkPresenterHand.width = 0;
   inkPresenterHand.height = 0;
   currentInkPresenter = inkPresenterArm;
   
   var armAnimation = wpf.content.findName("translateArm");
   armAnimation.begin();
   arm.Opacity = 1.0;
}

//////////////////////////////////////////////////////
// Utility Functions
function ClearInk()
{
    if(currentInkPresenter.strokes != null)
    {
        if(currentInkPresenter.strokes.count > 0)
        {
            currentInkPresenter.strokes.clear();
        }
    }
}
