﻿/*****************************************
                Card Class
******************************************/

/*
function Card(rank, suit)
{
    this._rank = rank;
    this._suit = suit;
}
*/

function Card(i)
{
	
    this._rank = this._GetRank(i);
    this._suit = this._GetSuit(i);
    this._image = this._GetImage(this._rank,this._suit);
}


Card.prototype._rank;
Card.prototype._suit;
Card.prototype._image;

Card.prototype.Suit = function()
{
    return this._suit;
}

Card.prototype.Rank = function()
{
    return this._rank;
}

Card.prototype.Image = function()
{
    return this._image.toLowerCase();
}

Card.prototype.isJacksOrBetter = function()
{
    if (this._rank == 1 || this._rank >= 11)
    {
        return true;
    }
    return false;
}

Card.prototype.CompareTo = function(card)
{
    try
    {
        if(this._rank < card.Rank)
        {
            return -1;
        }
        else if (this._rank > card.Rank)
        {
            return 1;
        }
        return 0;
    }
    catch(e)
    {
    
    }
}

/*
    This method returns the Suit of a Card, based on the cards index (1-4).
    The index must be between 1-52
    This is useful when Cards are integers instead of card objects
    - Obsolete
*/
Card.prototype._GetSuit = function(value)
{
    if(value >= 1 && value <= 52)
    {
        return (value%4) + 1;
    }
    
    return 0;
}

/*
    This method returns the Suit of a Card, based on the cards index (1-52)
    The index must be between 1-52
    This is useful when Cards are integers instead of card objects
    - Obsolete
*/
Card.prototype._GetRank = function(value)
{
	var x;
    if(value>= 1 && value<= 52)
    {
    	x = Math.ceil(value/4);
        return x
    }
    return 0;
}

Card.prototype._GetImage = function (r,s)
{
     return this.GetShortRankString(r) + this.GetShortSuitString(s) + '.gif';
}

/*
    Returns the name of the Suit based on the value passed in.
    0 = None
    1 = Hearts
    2 = Diamonds
    3 = Clubs
    4 = Spades
*/
Card.prototype.GetSuitString = function()
{
    var s;

    switch(this._suit)
    {
        case 1:
            s = 'Hearts';
            break;
        case 2:
            s = 'Diamonds';
            break;
        case 3:
            s = 'Clubs';
            break;
        case 4:
            s = 'Spades';
            break;
        default:
            s = 'None'
    }

    return s;
}

Card.prototype.GetShortSuitString = function(s)
{
    var s1;

    switch(s)
    {
        case 1:
            s1 = 'H';
            break;
        case 2:
            s1 = 'D';
            break;
        case 3:
            s1 = 'C';
            break;
        case 4:
            s1 = 'S';
            break;
        default:
            s1 = ''
    }

    return s1;
}

/*
    Returns the name of the Rank based on the value passed in.
    0 = None
    1 = Ace
    2 = Two
    3 = Three
    11 = Jack
    12 = Queen
    13 = King
*/
Card.prototype.GetRankString = function()
{
    var r;
    switch(this._rank)
    {
        case 1:
            r = 'Ace';
            break;
        case 2:
            r = 'Two';
            break;
        case 3:
            r = 'Three';
            break;
        case 4:
            r = 'Four';
            break;
        case 5:
            r = 'Five';
            break;
        case 6:
            r = 'Six';
            break;
        case 7:
            r = 'Seven';
            break;
        case 8:
            r = 'Eight';
            break;
        case 9:
            r = 'Nine';
            break;
        case 10:
            r = 'Ten';
            break;
        case 11:
            r = 'Jack';
            break;
        case 12:
            r = 'Queen';
            break;
        case 13:
            r = 'King';
            break;
        default:
            r = 'None';
            break;
    }

    return r;
}

Card.prototype.GetShortRankString = function(r)
{
    var r1;
    
    switch(r)
    {
        case 1:
            r1 = 'A';
            break;
        case 2:
            r1 = '2';
            break;
        case 3:
            r1 = '3';
            break;
        case 4:
            r1 = '4';
            break;
        case 5:
            r1 = '5';
            break;
        case 6:
            r1 = '6';
            break;
        case 7:
            r1 = '7';
            break;
        case 8:
            r1 = '8';
            break;
        case 9:
            r1 = '9';
            break;
        case 10:
            r1 = '10';
            break;
        case 11:
            r1 = 'J';
            break;
        case 12:
            r1 = 'Q';
            break;
        case 13:
            r1 = 'K';
            break;
        default:
            r1 = '';
            break;
    }

    return r1;
}

Card.prototype.toString = function()
{
    return this.GetRankString() + ' of ' + this.GetSuitString();
}

