function Card(Suit,Range)
{
  this.Picture     = new Image();
  this.Picture.src = "cards/"+Range+Suit+".gif";
  this.Type        = Range;
  this.Suit        = Suit;
} 
function Deck()
{
  this.Cards = new Array();
  for(Suits=0;Suits<gSuits.length;Suits++)
    for(Range=0;Range<gRange.length;Range++)
      this.Cards[this.Cards.length] = new Card(gSuits[Suits],gRange[Range]);
}
function DrawBoard()
{
  for(Board=0;Board<gBoard.length;Board++)
  {
    if(!(Board % 4) && Board) 
      document.write('<BR>');
    document.write('<IMG STYLE="border:1px inset transparent;" HSPACE=5 VSPACE=5 SRC="cards/'+gBoard[Board]+'blank.gif" STYLE="cursor:hand;" BORDER="0" Current="" Type="'+gBoard[Board]+'" Suit="" onClick="BoardClick(this);">');
  }
}
function BoardClick(where)
{
  if(!State) return false;
  if(State == 1) //Place Card
  {
    if(where.getAttribute('Current') == "")
    {
      Type = GameDeck.Cards[ShuffleIndx[DeckIndx]].Type;
      if(Type != parseInt(Type,10) && Type != where.getAttribute('Type')) return false;
      where.setAttribute('Current',Type);
      where.setAttribute('Suit',GameDeck.Cards[ShuffleIndx[DeckIndx]].Suit);
      where.src = document.getElementById('Flipcard').src;
      document.getElementById('Flipcard').src = oSpot.src;
      BoardCount++;
      if(BoardCount == 16 || DeckIndx == 51)
      {
        State = 2;
        PrevCard = false;
        CheckIfAnyPairs();
      }
      else
        State = 0;
    }
  }
  else
  {
    Type = parseInt(where.getAttribute('Current'),10);
    if(isNaN(Type)) return false;
    if(PrevCard)
    {
      PrevCard.style.border = '1px inset transparent';
      if(parseInt(PrevCard.getAttribute('Current'),10) + Type == 10 && PrevCard != where) //because of 5's
      {
        PrevCard.src = eval(PrevCard.getAttribute('Type')+'blank.src');
        where.src    = eval(where.getAttribute('Type')+'blank.src');
        PrevCard.setAttribute('Current','');
        where.setAttribute('Current','');
        BoardCount-=2;
      } 
      PrevCard = false;
    }
    else
    {
      if(Type == 10)
      {
        where.src = eval(where.getAttribute('Type')+'blank.src');
        where.setAttribute('Current','');
        BoardCount--;
      }
      else
      {
        PrevCard = where;
        where.style.border = '1px dashed lightgreen';
      }
    }
  }
  CheckWin();
  return false;
}
function DeckClick()
{
  if(!State || (State == 2 && BoardCount < 16 && !PrevCard))
  {
    State = 1;
    DeckIndx++;
    document.getElementById('Flipcard').src = GameDeck.Cards[ShuffleIndx[DeckIndx]].Picture.src;
    if(DeckIndx == 51)
      Deckcard.src = xSpot.src;
    CheckLose(GameDeck.Cards[ShuffleIndx[DeckIndx]].Type);
    return false;
  }
}
function LoadImages()
{
  oSpot  = new Image(); oSpot.src = 'cards/o-spot.gif';
  xSpot  = new Image(); xSpot.src = 'cards/x-spot.gif';
  blank  = new Image(); blank.src = 'cards/blank.gif';
  jblank = new Image(); jblank.src = 'cards/jblank.gif';
  qblank = new Image(); qblank.src = 'cards/qblank.gif';
  kblank = new Image(); kblank.src = 'cards/kblank.gif';
  for(indx=1;indx<13;indx++)
  {
    BacksArray[indx-1] = new Image();
    BacksArray[indx-1].src = 'cards/d'+indx+'.gif';
  }
}
function Shuffle()
{
  //document.all.SOUNDTRICK.src = 'shuffle.wav';
  //Much more efficient to shuffle an array of integers versus the array of Objects
  for(indx=0;indx<5000;indx++)
  {
    Swap1 = Math.round(Math.random() * 51);
    Swap2 = Math.round(Math.random() * 51);
    Temp = ShuffleIndx[Swap1];
    ShuffleIndx[Swap1] = ShuffleIndx[Swap2];
    ShuffleIndx[Swap2] = Temp;
  }
}
function CheckLose(Type)
{
  if(Type != parseInt(Type,10))
  {
    SpaceLeft = 4;
    for(indx=0;indx<16;indx++)
      if(Type == document.images[indx].getAttribute('Type') && document.images[indx].getAttribute('Current') != '')
        SpaceLeft--;
    if(!SpaceLeft)
      alert('You lose - No Space to Place');
  }
  return;
}
function CheckIfAnyPairs()
{
  if(DeckIndx == 51)
    return;
  for(indx=0;indx<16;indx++)
    if(parseInt(document.images[indx].getAttribute('Current'),10) == 10)
      return;
  for(indx=0;indx<16;indx++)
    for(indx2=0;indx2<16;indx2++)
      if(indx != indx2 && parseInt(document.images[indx].getAttribute('Current'),10) + parseInt(document.images[indx2].getAttribute('Current'),10) == 10)
        return;
  alert("You lose - No 10's or pairs that make 10"); 
  return;
}
function CheckWin()
{
  for(indx=0;indx<16;indx++)
    if(document.images[indx].getAttribute('Current') != document.images[indx].getAttribute('Type'))
      return;
  alert('You win!!!');
  CheckTrueWin();
  WinSequence();
  return;
}
function CheckTrueWin()
{
  //check if suits of corner's match for a TRUE win...
  if(document.images[0].getAttribute('Suit')  == document.images[1].getAttribute('Suit') &&
     document.images[1].getAttribute('Suit')  == document.images[4].getAttribute('Suit') &&
     document.images[2].getAttribute('Suit')  == document.images[3].getAttribute('Suit') &&
     document.images[3].getAttribute('Suit')  == document.images[7].getAttribute('Suit') &&
     document.images[8].getAttribute('Suit')  == document.images[12].getAttribute('Suit') &&
     document.images[12].getAttribute('Suit') == document.images[13].getAttribute('Suit') &&
     document.images[11].getAttribute('Suit') == document.images[14].getAttribute('Suit') &&
     document.images[14].getAttribute('Suit') == document.images[15].getAttribute('Suit'))
    alert('True Win!!!');
  return;
}
function WinSequence()
{
  indx=0;
  for(indx=document.images.length-1;indx>0;indx--)
    if(document.images[indx].getAttribute('Type') != 'k')
      document.images[indx].parentNode.removeChild(document.images[indx]);
  Anim = setInterval('AnimLoop()',25);
  return;
}
function AnimLoop()
{
  indx++;
  width  = parseInt(document.images[0].width,10);
  height = parseInt(document.images[0].height,10);
  width*=1.05;
  height*=1.05;
  document.images[0].width  = width;
  document.images[0].height = height;
  document.images[1].width  = width;
  document.images[1].height = height;
  document.images[2].width  = width;
  document.images[2].height = height;
  document.images[3].width  = width;
  document.images[3].height = height;
  if(indx > 20)
  {
    clearInterval(Anim);
    Swap = setInterval('SwapImages()',1);
  }
  return;
}
function SwapImages()
{
  Swap1 = Math.round(Math.random() * 3);
  Swap2 = Math.round(Math.random() * 3);
  Temp  = document.images[Swap1].src;
  document.images[Swap1].src = document.images[Swap2].src;
  document.images[Swap2].src = Temp;
  return;
}
