var Card = new Class({
	
	//implements
	Implements: [Options],

	//options
	options: {
		value: null,
		cut: false,
		orgHand: null,
		played: false,
		status: 'down',
		idPrefix: 'card'
	},
	
	suit: 		'CDHS',
	suitNames:	new Array('Clubs','Diamonds','Hearts','Spades'),
	suitHtml:	new Array('&clubs;','&diams;','&hearts;','&spades;'),
	suitColors:	new Array('black','red','red','black'),

	rank:		'A23456789TJQK',
	rankNames:	new Array('Ace','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King'),

	values:		new Hash({
		id:			null,
		deck:		null,
		card:		null,
		value:		null,
		rank:		null,
		suit:		null,
		suitCode:	null,
		suitName:	null,
		suitColor:	null,
		suitHtml:	null,
		rank:		null,
		rankCode:	null,
		rankName:	null,
		cardCode:	null,
		cardName:	null,
		html:		null
	}),
	
	//initialization
	initialize: function(options) {
		this.setOptions(options);
		
		if (this.options.value) {
			this.setCard(this.options.value);
			
			/*			
			try {
				this.values.set('id', 			this.options.value);
				this.values.set('deck', 		parseInt(this.options.value / 52));
				this.values.set('card', 		this.options.value - (this.values.get('deck') * 52));
				this.values.set('rank', 		parseInt(this.values.get('card') % 13));
				this.values.set('value', 		this.values.get('rank')+1 < 10 ? this.values.get('rank')+1 : 10);
				this.values.set('suit', 		parseInt(((this.values.get('card')) / 13)));
				this.values.set('suitCode', 	this.suit.charAt(this.values.get('suit')));
				this.values.set('suitName', 	this.suitNames[this.values.get('suit')]);
				this.values.set('suitColor', 	this.suitColors[this.values.get('suit')]);
				this.values.set('suitHtml', 	this.suitHtml[this.values.get('suit')]);
				this.values.set('rankCode', 	this.rank.charAt(this.values.get('rank')));
				this.values.set('rankName', 	this.rankNames[this.values.get('rank')]);
				this.values.set('cardCode', 	this.values.get('rankCode') + this.values.get('suitCode'));
				this.values.set('cardName', 	this.values.get('rankName') + " of " + this.values.get('suitName'));
				this.values.set('html', 		this.createHtml());
			}
			catch(e) {
				console.log("Error Initializing Card");
				console.log(e);
			}
			*/
		}
		
		return this;
	},
	
	
	setValues: function() {
		if (this.options.value) {
			try {
				this.values.set('id', 			this.options.value);
				this.values.set('deck', 		parseInt(this.options.value / 52));
				this.values.set('card', 		this.options.value - (this.values.get('deck') * 52));
				this.values.set('rank', 		parseInt(this.values.get('card') % 13));
				this.values.set('value', 		this.values.get('rank')+1 < 10 ? this.values.get('rank')+1 : 10);
				this.values.set('suit', 		parseInt(((this.values.get('card')) / 13)));
				this.values.set('suitCode', 	this.suit.charAt(this.values.get('suit')));
				this.values.set('suitName', 	this.suitNames[this.values.get('suit')]);
				this.values.set('suitColor', 	this.suitColors[this.values.get('suit')]);
				this.values.set('suitHtml', 	this.suitHtml[this.values.get('suit')]);
				this.values.set('rankCode', 	this.rank.charAt(this.values.get('rank')));
				this.values.set('rankName', 	this.rankNames[this.values.get('rank')]);
				this.values.set('cardCode', 	this.values.get('rankCode') + this.values.get('suitCode'));
				this.values.set('cardName', 	this.values.get('rankName') + " of " + this.values.get('suitName'));
				this.values.set('html', 		this.createHtml());
			}

			catch(e) {
				console.log("Error setValues Card");
				console.log(e);
			}
		}
	},
	

    // Return the Card Value    (1 to 52 for Cards or -1 to -4 for Jokers)
	getId: function() {
		return this.options.value;
		return this.values.get('id');
	},
	

    // Return the Card Value    (1 to 52 for Cards or -1 to -4 for Jokers)
	setId: function(value) {
		this.options.value = value;
	},
	

    // Return the Card Value    (1 to 52)
	getCard: function() {
		return this.options.value - (this.getDeck() * 52);
		//return this.values.get('card');
	},
	

    // Set the Value of the Card
    //  0 - 51      Deck #1
    // 52 - 103     Deck #2
	setCard: function(value) {
		this.options.value = value;
	},
	

    // Get the Deck # the Card came from
    getDeck: function() {
        return parseInt(this.options.value / 52);
		//return this.values.get('deck');
    },
    
    
    // Return Card Suit         (0, 1, 2, 3)
    getSuit: function() {
        return parseInt(((this.getCard()) / 13));
		//return this.values.get('suit');
    },


    // Return Card Suit         (C, D, H, S)
    getSuitCode: function() {
        return this.suit.charAt(this.getSuit());
		//return this.values.get('suitCode');
    },


    // Return Card Suit         (Club, Diamond, Heart, Suit)
    getSuitName: function() {
        return this.suitNames[this.getSuit()];
		//return this.values.get('suitName');
    },
    
    
    // Return Card Suit         (Black, Red, Red, Black)
    getSuitColor: function() {
    	return this.suitColors[this.getSuit()];
		//return this.values.get('suitColor');
    },


    // Return Card Suit         (&club;, &diam;, &heart;, &spade;)
    getSuitHtml: function() {
        return this.suitHtml[this.getSuit()];
		//return this.values.get('suitHtml');
    },


    // Return Card Rank         (1 to 13)
    getRank: function() {
        return parseInt(this.getCard() % 13);
		//return this.values.get('rank');
    },


    // Return Card Rank         (A23456789TJQK)
    getRankCode: function() {
        return this.rank.charAt(this.getRank());
		//return this.values.get('rankCode');
    },
    

    // Return Card Suit         (Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King)
    getRankName: function() {
        return this.rankNames[this.getRank()];
		//return this.values.get('rankName');
    },


    // Return Card Value        (1 to 10)
    getValue: function() {
    	return this.getRank()+1 < 10 ? this.getRank()+1 : 10;
		//return this.values.get('value');
    },


    // Return Card Name String  (AC, TS, QD)
    getCardCode: function() {
        //if (this.getValue() <= 0)
        //    return null;
        
        return this.getRankCode() + this.getSuitCode();
		//return this.values.get('cardCode');
    },


    // Return Card Name String  (Ace of Clubs, Ten of Hearts, Queen Of Diamonds)
    getCardString: function() {
        //if (this.getValue() <= 0)
        //    return null;
        
        return this.getRankName() + ' of ' + this.getSuitName();
		//return this.values.get('cardName');
    },
    
    
    setCut: function(cut) {
    	this.options.cut = cut;
    },
    
    
    getCut: function() {
    	return this.options.cut;
    },
    
	
	setStatus: function(status) {
		this.options.status = status;
	},
    
	
	getStatus: function() {
		return this.options.status;
	},
	
	
	getSpots: function() {
		switch (this.getRank()+1) {
			case 1: 
				return new Array('ace');
			break;
			
			case 2: 
				return new Array('spotB1', 'spotB5');
			break;
			
			case 3: 
				return new Array('spotB1', 'spotB3', 'spotB5');
			break;
			
			case 4: 
				return new Array('spotA1', 'spotA5', 'spotC1', 'spotC5');
			break;
			
			case 5: 
				return new Array('spotA1', 'spotA5', 'spotB3', 'spotC1', 'spotC5');
			break;
			
			case 6: 
				return new Array('spotA1', 'spotA3', 'spotA5', 'spotC1', 'spotC3', 'spotC5');
			break;
			
			case 7: 
				return new Array('spotA1', 'spotA3', 'spotA5', 'spotB3', 'spotC1', 'spotC3', 'spotC5');
			break;
			
			case 8: 
				return new Array('spotA1', 'spotA3', 'spotA5', 'spotB2', 'spotB4', 'spotC1', 'spotC3', 'spotC5');
			break;
			
			case 9: 
				return new Array('spotA1', 'spotA2', 'spotA4', 'spotA5', 'spotB3', 'spotC1', 'spotC2', 'spotC4', 'spotC5');
			break;
			
			case 10: 
				return new Array('spotA1', 'spotA2', 'spotA4', 'spotA5', 'spotB2', 'spotB4', 'spotC1', 'spotC2', 'spotC4', 'spotC5');
			break;
			
			case 11: 
				return new Array('spotA1', 'spotC5');
			break;
			
			case 12: 
				return new Array('spotA1', 'spotC5');
			break;
			
			case 13: 
				return new Array('spotA1', 'spotC5');
			break;
		}
		
		return new Array();
	},
	
	
	createHtml: function() {
		if (this.getCut()) {
			var cut = 'Cut';
		} else {
			var cut = ' ';
		}
		
		var suitHtml 	= this.getSuitHtml();
		var card		= new Element('div', {
			'class': 'card draggable PlayingCard'+cut, 
			'id': this.options.idPrefix+'_'+this.getId()
		});
		
		if (this.getStatus() == 'up') {
			var front	= new Element('div', {'class': 'front '+this.getSuitColor()}).inject(card);
			var iTop	= new Element('div', {'class': 'index_top', 'html': this.getRankCode()+'<br />'+suitHtml}).inject(front);

			if (this.getRank()+1 > 10) {
				var rankName = this.getRankName();
				var img	= new Element('img', {'class': 'face ', 'src': '../resources/img/cards/'+rankName.toLowerCase()+'.gif'}).inject(front);
			}
	
			var spots = this.getSpots();

			for (var cs=0; cs<spots.length; cs++) {
				var spot = new Element('div', {'class': spots[cs], 'html': suitHtml}).inject(front);
			}

			var iBottom	= new Element('div', {'class': 'index_bottom', 'html': this.getRankCode()+'<br />'+suitHtml}).inject(front);

			var cardTooltip = card.clone();
			cardTooltip.addClass('cardTooltip');
			cardTooltip.setAttribute('id', 'cardTooltip_'+this.getId());
			
			var tipCon = new Element('div');
			cardTooltip.inject(tipCon);			
			//card.setAttribute('title', tipCon.get('html'));
			front.setAttribute('title', tipCon.get('html'));
			
			if (cardToolTips) {
				//toolTips.attach(card);			
				cardToolTips.attach(front);			
			}
		}

		return card;
	},
    
	
	
	
	buildHTML: function() {
		return this.createHtml();
		
		//return this.values.get('html');
		
		if (this.getCut()) {
			var cut = 'Cut';
		} else {
			var cut = ' ';
		}
		
		var suitHtml 	= this.getSuitHtml();
		var card		= new Element('div', {
			'class': 'card draggable PlayingCard'+cut, 
			'id': this.options.idPrefix+'_'+this.getId()
		});
		
		if (this.getStatus() == 'up') {
			var front	= new Element('div', {'class': 'front '+this.getSuitColor()}).inject(card);
			var iTop	= new Element('div', {'class': 'index_top', 'html': this.getRankCode()+'<br />'+suitHtml}).inject(front);

			if (this.getRank()+1 > 10) {
				var rankName = this.getRankName();
				var img	= new Element('img', {'class': 'face ', 'src': '../resources/img/cards/'+rankName.toLowerCase()+'.gif'}).inject(front);
			}
	
			var spots = this.getSpots();

			for (var cs=0; cs<spots.length; cs++) {
				var spot = new Element('div', {'class': spots[cs], 'html': suitHtml}).inject(front);
			}

			var iBottom	= new Element('div', {'class': 'index_bottom', 'html': this.getRankCode()+'<br />'+suitHtml}).inject(front);

			var cardTooltip = card.clone();
			cardTooltip.addClass('cardTooltip');
			cardTooltip.setAttribute('id', 'cardTooltip_'+this.getId());
			
			var tipCon = new Element('div');
			cardTooltip.inject(tipCon);			
			//card.setAttribute('title', tipCon.get('html'));
			front.setAttribute('title', tipCon.get('html'));
			
			if (cardToolTips) {
				//toolTips.attach(card);			
				cardToolTips.attach(front);			
			}
		}

		return card;
	},
    
	
	setPlayed: function(played) {
		this.options.played = played;
	},
    
	
	getPlayed: function() {
		return this.options.played;
	}
	
	
});

