/*
* Miscellaneous functions
* @author: Joseph 'Pcjoe' Florencio
* @date: 12-03-05
*/

// Get directional constant from arbitrary vector
function getDirectionFromVector(vector, useDiag)
{
  // No length, default to back dir
  if(vector.getLength() == 0)
  { return DIR_BACK;
  }
  var arrayDirections = new Array(8);
  var i;
  for(i=0;i<8;i++)
  { arrayDirections[i] = false;
  }
  arrayDirections[DIR_FRONT] = new VectorSet(0,-1);
  arrayDirections[DIR_BACK] = new VectorSet(0,1);
  arrayDirections[DIR_LEFT] = new VectorSet(-1,0);
  arrayDirections[DIR_RIGHT] = new VectorSet(1,0);
  if(useDiag)
  {
    arrayDirections[DIR_FRONTLEFT] = new VectorSet(-1,-1);
    arrayDirections[DIR_FRONTRIGHT] = new VectorSet(1,-1);
    arrayDirections[DIR_BACKLEFT] = new VectorSet(-1,1);
    arrayDirections[DIR_BACKRIGHT] = new VectorSet(1,1);
  }
  var largestDot = -2;
  var largestDotIdx = DIR_BACK;
  var temp = 0;
  for(i=0;i<8;i++)
  {
    if(arrayDirections[i])
    {
      temp = arrayDirections[i].getDotProduct(vector);
      if(temp > largestDot)
      {
        largestDot = temp;
        largestDotIdx = i;
      }
    }
  }
  return largestDotIdx;
}

// Take in a directional constant and return a string
function getStringFromDir(dir)
{
  switch(dir)
  {
    default:
    case DIR_RIGHT:
        return "right";
        break;
    case DIR_LEFT:
        return "left";
        break;
    case DIR_FRONT:
        return "front";
        break;
    case DIR_BACK:
        return "down";
        break;
    case DIR_FRONTRIGHT:
        return "frontright";
        break;
    case DIR_FRONTLEFT:
        return "frontleft";
        break;
    case DIR_BACKRIGHT:
        return "downright";
        break;
    case DIR_BACKLEFT:
        return "downleft";
        break;
  }
  return "down";
}

/*
* Functions to fade from the game window to/from the form window
*/
function setTrans(style, amt)
{
  style.filter = "alpha(opacity="+amt*100+")";
  style.opacity = amt;
}

fadeAmt = 0;
inFocus = "form";
fadeSpeed = 25;
fadeRate = 0.05;
function fadeGame()
{
    fadeAmt = fadeAmt - fadeRate;
    if(fadeAmt < 0)
    {   fadeAmt = 0;
    }
    var game = document.getElementById('playbox').style;
    var dogform = document.getElementById('mainwindow').style;
    setTrans(game,fadeAmt);
    setTrans(dogform,1-fadeAmt);

    if(fadeAmt > 0)
    {	setTimeout("fadeGame()",fadeSpeed);
    }
    else
    {
        game.display = "none";
        dogform.display = "block";
    }
}

function fadeForm()
{
    fadeAmt = fadeAmt - fadeRate;
    if(fadeAmt < 0)
    {   fadeAmt = 0;
    }
    var game = document.getElementById('playbox').style;
    var dogform = document.getElementById('mainwindow').style;
    setTrans(dogform,fadeAmt);
    setTrans(game,1-fadeAmt);
    
    if(fadeAmt > 0)
    {	setTimeout("fadeForm()",fadeSpeed);
    }
    else
    {
        game.display = "block";
        dogform.display = "none";
    }
}

// Focus on game (Fade out form, fade in game)
function focusGame()
{
    if(inFocus == "game" || fadeAmt != 0)
    {   return;
    }
    inFocus = "game";
    var game = document.getElementById('playbox').style;
    var dogform = document.getElementById('mainwindow').style;
    game.display = "block";
    dogform.display = "block";
    fadeAmt = 1;
    setTrans(game,0);
    setTrans(dogform,1);
    fadeForm();
}

// Focus on form (Fade out game, fade in form)
function focusForm()
{
    if(inFocus == "form" || fadeAmt != 0)
    {   return;
    }
    inFocus = "form";
    var game = document.getElementById('playbox').style;
    var dogform = document.getElementById('mainwindow').style;
    game.display = "block";
    dogform.display = "block";
    fadeAmt = 1;
    setTrans(game,1);
    setTrans(dogform,0);
    fadeGame();
}

// Precache Image
function precacheImage( img )
{
  // Preload image into array of document object
  if(document.images)
  {
    if(!document.p)
    {
      document.p = new Array();
    }
    var length = document.p.length;
    document.p[length] = new Image;
    document.p[length].src = img;
  }
}

