Deck = Class.create({
	initialize: function() {
		this.cards = new Array();
		this.suits = this.initialize_suits();
		for(var i = 0; i < this.suits.length; i++) {
			this.add_card(
				new Card('Ace',this.suits[i])
			);
			this.create_number_cards(this.suits[i]);
			this.create_face_cards(this.suits[i]);
		}
	},
	shuffle: function() {
		// based on http://jsfromhell.com/array/shuffle
	    for(
	    	var j, x, i = this.cards.length;
	    	i;
	    	j = parseInt(Math.random() * i),
	    	x = this.cards[--i],
	    	this.cards[i] = this.cards[j],
	    	this.cards[j] = x
	    );
	},
	draw: function() {
		return this.cards.pop();
	},
	add_card: function(card) {
		var i = this.cards.length;
		this.cards.push(
			card
		)
	},
	create_number_cards: function(suit) {
		for(var i = 2; i <= 10; i++) {
			this.add_card(
				new Card(i,suit)
			)
		}
	},
	create_face_cards: function(suit) {
		var face_cards = "King,Queen,Jack"
		face_cards     = face_cards.split(',');
		for(var i = 0; i < face_cards.length; i++) {
			this.add_card(
				new Card(face_cards[i],suit)
			);
		}
	},
	initialize_suits: function() {
		var suits = new Array();
		suits.push(
			new Suit(
				'Spades',
				'black'
			)
		);
		suits.push(
			new Suit(
				'Clubs',
				'black'
			)
		);			
		suits.push(
			new Suit(
				'Diamonds',
				'red'
			)
		);
		suits.push(
			new Suit(
				'Hearts',
				'red'
			)
		);
		return suits;
	},
	toString: function() {
		var message = '';
		for(var i = 0; i < this.cards.length; i++) {
			if(i % 2 == 0) {
				message = message + '\n';
			}
			message = message + this.cards[i] + '\t\t';
		}
		return message;
	}
});

Suit = Class.create({
	initialize: function(label,color) {
		this.label = label;
		this.color = color;
	},
	toString: function() {
		return this.label;
	}
});

Card = Class.create({
	initialize: function(type,suit) {
		this.type = type;
		this.suit = suit;
	},
	same_suit: function(suit) {
		return this.suit = suit;
	},
	same_type: function(type) {
		return this.type = type;
	},
	toString: function(){
		return this.type + ' of ' + this.suit;
	}
});
