id = [];
deck = [];
hand = [];

score = 0;
curmult = 1;
curdec = 0;

//probably deletable
card_known = []; 

to_draw = [];

images = [];

initialize = function() {
   var handDisp = $('hand');
    for(var i = 0; i < 5; i++) {
        var image = new Element ('img');
        image.setProperties( {
            'height': "120",
            'width':"86",
            'src': "images/-1c.png"
        });
        card_known[i] = false;
        images[i] = image; 
        image.onclick = (function (num) {
            var j = num; 
            var pick = function (target) {
                var img = new Event (target);
                if (card_known[j] == true) {
                    card_known[j] = false;
                    to_draw.push(j);
                    images[j].setProperty('src', "images/-1c.png");
                    if (to_draw.length > 5) {
                        var flip = to_draw.shift();
                        card_known[flip] = true; 
                        images[flip].setProperty('src', "images/"+cardImage(hand[flip])+".png");
                    };
                } else {
                    card_known[j] = true; 
                    to_draw.remove(j); 
                    images[j].setProperty('src', "images/"+cardImage(hand[j])+".png");
                };
            };
            return pick;
        })(i);
        image.injectInside(handDisp); 
    };
};

loadhand = function(hand) {
    var image = $('hand').firstChild; 
    for (var i = 0; i<hand.length; i++) {
        image.setProperty('src', "images/"+cardImage(hand[i])+".png");
        image = image.nextSibling;
        card_known[i] = true; 
    }
    to_draw = [];
}


ranks = [
     "Aces",
     "Kings",
    "Queens",
    "Jacks",
    "Tens",
    "Nines",
    "Eights",
    "Sevens",
    "Sixes",
    "Fives",
    "Fours",
    "Threes",
    "Wilds"
].reverse();

rank = [
     "Ace",
     "King",
    "Queen",
    "Jack",
    "Ten",
    "Nine",
    "Eight",
    "Seven",
    "Six",
    "Five",
    "Four",
    "Three",
    "Wild"
].reverse();

    

types = {
    "5" : function (r) {return "Five "+ranks[r[0]]},
    "sf": function (r) {return rank[r[0]]+" High Straight Flush"},
    "4":  function (r) {return "Four "+ranks[r[0]]+" and a "+rank[r[1]]+" kicker"},
    "fh":  function (r) {return "Full House: "+ranks[r[0]]+" over "+rank[r[1]]},
    "f": function (r) {return rank[r[0]]+" Low Flush"},
    "s": function (r) {return rank[r[0]]+" High Straight"},
    "3":  function (r) {return "Three "+ranks[r[0]]+" and  "+rank[r[1]]+", "+rank[r[2]]+" kickers"},
    "2p":  function (r) {return "Two pair: "+ranks[r[0]]+", "+ranks[r[2]]+" and a "+rank[r[2]]+" kicker"},
    "2":  function (r) {return "Pair of "+ranks[r[0]]+" and "+rank[r[1]]+", "+rank[r[2]]+", "+rank[r[3]]+" kickers"},
    "1":  function (r) {return rank[r[0]]+" high  and "+rank[r[1]]+", "+rank[r[2]]+", "+rank[r[3]]+", "+rank[r[4]]+" kickers"},
};


loadcall = function(call, id) {
    var div = $(id);
    var newdiv = new Element ('div');
    newdiv.setStyle('font-size', 'x-large'); 
    var intelcall = (types[call[0]])(call[1]); 
    newdiv.appendText(intelcall);
    div.replaceWith(newdiv);
    newdiv.setProperty('id', id); 
}

clearid = function(id) {
    var div = $(id);
    var newdiv = new Element ('div');
    div.replaceWith(newdiv);
    newdiv.setProperty('id', id); 
}

better = function(cmp) {
    var text = "Your new hand has the same value. Well, at least it is not worse...yet.";
    if (cmp == 1) {text = "You have improved your hand!";};
    if (cmp == -1) {text = "D'oh! You chose a worse hand. Please do better next time. Seriously.";};
    var div = $('cmp');
    var newdiv = new Element ('div');
    newdiv.setStyle('font-size', 'large'); 
    newdiv.appendText(text);
    div.replaceWith(newdiv);
    newdiv.setProperty('id', 'cmp'); 
};

hail = function(str) {
    var text = '';
    if (str == "mia") {text = "Hail, Mia!"};
    if (str == "mary") {text = "Hail, Mary!"};
    if (str == "mulligan") {text = "A mulligan for you?"};
    if (str == "miakan") {text = "A miakan? Really?"};
    var div = $('hail');
    var newdiv = new Element ('div');
    newdiv.setStyle('font-size', 'xx-large'); 
    newdiv.appendText(text);
    div.replaceWith(newdiv);
    newdiv.setProperty('id', 'hail'); 
};

clearhail = function () {
  setTimeout(function() {
    var div = $('hail');
    var newdiv = new Element ('div');
    newdiv.setStyle('font-size', 'large'); 
    newdiv.appendText('');
    div.replaceWith(newdiv);
    newdiv.setProperty('id', 'hail'); 
    }, 2000);
};


computescore  = function(first, second) {
    var text = "Your current score is "+score+".";
    var div = $('score');
    var newdiv = new Element ('div');
    newdiv.setStyle('font-size', 'x-large'); 
    newdiv.appendText(text);
    div.replaceWith(newdiv);
    newdiv.setProperty('id', 'score'); 
};


text='';

hd = {};

shuffle = function() {
    jSonRequest = new XHR(
        { method:'post',
          onSuccess: function(retText){
              hd = Json.evaluate(retText)
              text = retText;
              deck = hd.deck;
              hand = hd.hand;
              htype = hd.htype;
              score = 0; curmult = 1; curdec = 0;
              clearid('htype'); 
              clearid('hail'); 
              clearid('call'); 
              clearid('cmp');               
              clearid('score');               
              loadhand(hand);
              loadcall(htype, 'htype');
          }}).send("http://"+location.hostname+location.pathname.match(/(.*)\/([^/]*$)/)[1]+"/card.php", Json.toString({"choice":"newhand"}) );
};


replace = function() {
    var rep = to_draw; 
    if (rep.length < deck.length) {
        if (rep.length == 4)  {
            if ( deck.length<45 )
             {hail("mia");}
           else {hail("miakan");}
        }
        if (rep.length== 5 ) {
           if ( deck.length<45 )
             {hail("mary");}
           else {hail("mulligan");}
        };
    jSonRequest = new XHR(
        { method:'post',
          onSuccess: function(retText){ //these are global and being used. very bad!
              hd = Json.evaluate(retText);
              text = retText;
              deck = hd.deck;
              hand = hd.hand;
              htype = hd.htype;
              score = hd.score;
              curmult = hd.curmult;
              curdec = hd.curdec;
              loadhand(hd.hand);
              loadcall(hd.htype, 'htype');
              better(hd.better); 
              computescore(hd.score);
              clearhail();
              //document['pc']['select'].value = "[0, 1, 2, 3, 4]"
          }}).send("http://"+location.hostname+location.pathname.match(/(.*)\/([^/]*$)/)[1]+"/card.php", Json.toString({"choice":"draw", "deck": deck, "hand":hand, "rep":rep, "score":score, "curmult":curmult, "curdec":curdec}) );
    } else {alert ("Not enough cards left in the deck");}
};


cardImage = function(card) {
    var rank; 
    switch (card[0]) {
    case "0": rank = "T"; break;
    case "n": rank = "2"; break;
        
    default : rank = card[0]; 
    };
    suit = card[1]; 
    if (rank == 'A' && suit=='D') {
        rank='D'; suit='a';
    } else {
       suit = card[1].toLowerCase();
    };
        return rank+suit; 
//    var img = new Element('img');
//    img.setProperties({
//        height:"120"
//        width:"86"
//        src="images/"+rank+suit+".png";
//    });
//    return img;
};

