function initFaro() {
    newDeck();
    clearSelected();
    displayFaroDeck();
}

function faroShuffleIn() {
   faroShuffleInNTimes(document.getElementById('opsCountTextField').value);
}
function faroShuffleInNTimes(shuffles) {
    if (shuffles == null || shuffles == "") {
        shuffles = 1;
    } else if (isNaN(shuffles)) {
        shuffles = 0;
        document.getElementById('opsCountTextField').value="";
    }
    for (x = 0; x < shuffles; x++) {
        var shuffledDeck = new Array(52);
        for (y = 0; y < 52; y++) {
            if (y < 26) {
                shuffledDeck[(2 * y) + 1] = deck[y];
            } else {
                shuffledDeck[(2 * y) - 52] = deck[y];
            }
        }
        deck = shuffledDeck;
    }
    displayFaroDeck();
}

function faroShuffleOut() {
   faroShuffleOutNTimes(document.getElementById('opsCountTextField').value);
}
function faroShuffleOutNTimes(shuffles) {
    if (shuffles == null || shuffles == "") {
        shuffles = 1;
    } else if (isNaN(shuffles)) {
        shuffles = 0;
        document.getElementById('opsCountTextField').value="";
    }
    for (x = 0; x < shuffles; x++) {
        var shuffledDeck = new Array(52);
        for (y = 0; y < 52; y++) {
            if (y < 26) {
                shuffledDeck[2 * y] = deck[y];
            } else {
                shuffledDeck[(2 * y) - 51] = deck[y];
            }
        }
        deck = shuffledDeck;
    }
    displayFaroDeck();
}

function displayFaroDeck() {
   for (i=0;i<deck.length;i++) {
      var myDigit = i;
      var myCardNo = "cardimage" + myDigit;
      var myCard = getImage(myCardNo);
      myCard.style.backgroundImage="url(" + getCardImage(deck[i]) + ")";
   }
}

function selectCard(cardNum) {
   //removeHighlight();
   document.getElementById('selectedCardNum').value=cardNum;
   //addHighlight();
}
function moveCardDownBy(positions) {
   if (positions == null || positions == "") {
       positions = 1;
   } else if (isNaN(positions)) {
       positions = 0;
       document.getElementById('moveCardUpCount').value="";
   }
   if (isCardSelected()) {
       //removeHighlight();
      var myCardPos = document.getElementById('selectedCardNum').value;
      var hold = 0;
      if ( positions > (51 - (myCardPos * 1)) ) {
         positions = (51 - (myCardPos *1 ));
      }
      for (i=0;i<positions;i++) {
         if (myCardPos==51) {
            alert("You cannot move this card any further up.");
            break;
         } else {
            hold = deck[myCardPos];
            deck[myCardPos] = deck[(myCardPos * 1) + 1];
            deck[(myCardPos * 1) + 1] = hold;
            myCardPos++;
         }
      }
      document.getElementById('selectedCardNum').value = myCardPos;
      displayFaroDeck();
      //addHighlight();
   } else {
      alert("You must click on a card to move first.");
   }
}
function moveCardUpBy(positions) {
   if (positions == null || positions == "") {
       positions = 1;
   } else if (isNaN(positions)) {
       positions = 0;
       document.getElementById('moveCardDownCount').value="";
   }
   if (isCardSelected()) {
       //removeHighlight();
      var myCardPos = document.getElementById('selectedCardNum').value;
      var hold = 0;
      if ( positions > (myCardPos * 1) ) {
         positions = (myCardPos * 1);
      }
      for (i=0;i<positions;i++) {
         if (myCardPos==0) {
            alert("You cannot move this card any further down.");
            break;
         } else {
            hold = deck[myCardPos];
            deck[myCardPos] = deck[(myCardPos * 1) - 1];
            deck[(myCardPos * 1) - 1] = hold;
            myCardPos--;
         }
      }
      document.getElementById('selectedCardNum').value = myCardPos;
      displayFaroDeck();
      //addHighlight();
   } else {
      alert("You must click on a card to move first.");
   }
}
function isCardSelected() {
   if (document.getElementById('selectedCardNum').value==null || document.getElementById('selectedCardNum').value=="") {
      return false;
   } else {
      return true;
   }
}
function moveCardDown() {
   moveCardDownBy(document.getElementById('opsCountTextField').value);
}
function moveCardUp() {
   moveCardUpBy(document.getElementById('opsCountTextField').value);
}
function moveCardToTop() {
   var temp = document.getElementById('selectedCardNum').value;
   moveCardUpBy( (temp * 1) );
}
function moveCardToBottom() {
   var temp = document.getElementById('selectedCardNum').value;
   moveCardDownBy( 51 - (temp * 1) );
}
function moveCardOrdinalLeft() {
    if (isCardSelected()) {
        opsCount = document.getElementById('opsCountTextField').value;
        if (opsCount == null || opsCount == "") {
            opsCount = 1;
        } else if (isNaN(opsCount)) {
            opsCount = 0;
            document.getElementById('opsCountTextField').value="";
        }
        if ( (opsCount * 1) > 0 ){
            var myCardPos = document.getElementById('selectedCardNum').value;
            var myCol = (myCardPos*1)%13;
            var moves = (opsCount*1)%13;
            if (moves > myCol*1) {
                moveCardDownBy(13-(moves*1));
            } else {
                moveCardUpBy(moves*1);
            }
        }
    }
}
function moveCardOrdinalRight() {
    if (isCardSelected()) {
        opsCount = document.getElementById('opsCountTextField').value;
        if (opsCount == null || opsCount == "") {
            opsCount = 1;
        } else if (isNaN(opsCount)) {
            opsCount = 0;
            document.getElementById('opsCountTextField').value="";
        }
        if ( (opsCount * 1) > 0 ){
            var myCardPos = document.getElementById('selectedCardNum').value;
            var myCol = (myCardPos*1)%13;
            var moves = (opsCount*1)%13;
            if (moves > (12-(myCol*1))) {
                moveCardUpBy(13-(moves*1));
            } else {
                moveCardDownBy(moves*1);
            }
        }
    }
}
function moveCardOrdinalDown() {
    if (isCardSelected()) {
        opsCount = document.getElementById('opsCountTextField').value;
        if (opsCount == null || opsCount == "") {
            opsCount = 1;
        } else if (isNaN(opsCount)) {
            opsCount = 0;
            document.getElementById('opsCountTextField').value="";
        }
        if ( (opsCount * 1) > 0 ){
            var myCardPos = document.getElementById('selectedCardNum').value;
            var myRow = Math.floor((myCardPos*1)/13);
            var moves = (opsCount*1)%4;
            if (moves > (3-(myRow*1))) {
                moveCardUpBy(13*(4-(moves*1)));
            } else {
                moveCardDownBy(13*(moves*1));
            }
        }
    }
}
function moveCardOrdinalUp() {
    if (isCardSelected()) {
        opsCount = document.getElementById('opsCountTextField').value;
        if (opsCount == null || opsCount == "") {
            opsCount = 1;
        } else if (isNaN(opsCount)) {
            opsCount = 0;
            document.getElementById('opsCountTextField').value="";
        }
        if ( (opsCount * 1) > 0 ){
            var myCardPos = document.getElementById('selectedCardNum').value;
            var myRow = Math.floor((myCardPos*1)/13);
            var moves = (opsCount*1)%4;
            if (moves > myRow) {
                moveCardDownBy(13*(4-(moves*1)));
            } else {
                moveCardUpBy(13*(moves*1));
            }
        }
    }
}
function clearSelected() {
   document.getElementById('selectedCardNum').value="";
}
function addHighlight() {
    var myCardPos = document.getElementById('selectedCardNum').value;
    var selectedCard = "cardimage" + myCardPos;
    thiscard = getImage(selectedCard);
    thiscard.border=2;
}
function removeHighlight() {
    var myCardPos = document.getElementById('selectedCardNum').value;
    var selectedCard = "cardimage" + myCardPos;
    thiscard = getImage(selectedCard);
    thiscard.border=0;
}
function reverseDeck() {
    deck = deck.reverse();
    clearSelected();
    displayFaroDeck();
}
function cutDeck() {
   if (isCardSelected()) {
       //removeHighlight();
      var myCardPos = document.getElementById('selectedCardNum').value;
      var temp = new Array();
      for (i=0;i<(52-(myCardPos*1));i++) {
         temp[i]=deck[i+(myCardPos*1)];
      }
      for (j=0;j<(myCardPos*1);j++) {
         temp[j+(52-(myCardPos*1))]=deck[j];
      }
      deck=temp;
      clearSelected();
      displayFaroDeck();
      //addHighlight();
   } else {
      alert("You must click on a card first.");
   }
}


