DudeCard.prototype = Object.create(Phaser.Sprite.prototype);
DudeCard.prototype.constructor = DudeCard;

function DudeCard(game, col, row, cardCharacters, clickedHandler) {
	this.cardIndex = row * DudeCard.MAX_CARD_COLUMN_NO + col;
	this.columnNo = col;
	this.rowNo = row;
	this.cardCharacters = cardCharacters;
	this.characterNo = 0;
	this.clickedHandler = clickedHandler;

	this.isActivated = true;

	this.backupScale = 1;

	Phaser.Sprite.call(this, game, DudeCard.CARD_INITIAL_POSITION_X, DudeCard.CARD_INITIAL_POSITION_Y, 'card_front');
	this.anchor.set(0.5);
	this.scale.set(1);
	this.smoothed = false;

	this.character = game.add.sprite(0, 0, 'commoner');
	this.character.anchor.set(0.5);
	this.character.scale.set(7);
	this.character.smoothed = false;
	this.addChild(this.character);

	this.backPaper = game.add.sprite(0, 0, 'card_back');
	this.backPaper.anchor.set(0.5);
	this.backPaper.scale.set(1);
	this.backPaper.smoothed = false;
	this.backPaper.alpha = 0;
	this.addChild(this.backPaper);

	this.inputEnabled = true;
	this.events.onInputDown.add(this.onDown, this);

	this.showAndHideEvent = null;

	game.add.existing(this);
}

DudeCard.prototype.onDown = function(sprite) {
	if(this.isActivated == false)
		return;

	this.clickedHandler(this.columnNo, this.rowNo);

	/*
	if(this.backPaper.alpha == 0) {
		this.flipToFront();
	} else {
		this.flipToBack();
	}
	*/
}


DudeCard.prototype.setScale = function(size) {
	this.scale.set(size);
	this.backupScale = size;
}

DudeCard.prototype.setCharacter = function(characterNo) {
	this.characterNo = characterNo;
	this.character.loadTexture(this.cardCharacters[characterNo]);
}

DudeCard.prototype.getCharacter = function() {
	return this.characterNo;
}

DudeCard.prototype.getCardIndex = function() {
	return this.cardIndex;
}

DudeCard.prototype.getColumnNo = function() {
	return this.columnNo;
}

DudeCard.prototype.getRowNo = function() {
	return this.rowNo;
}


DudeCard.prototype.isFront = function() {
	if(this.backPaper.alpha == 0)
		return true;

	return false;
}

DudeCard.prototype.flipToFront = function() {
	this.backPaper.alpha = 0;
}

DudeCard.prototype.flipToBack = function() {
	this.backPaper.alpha = 1;
}

DudeCard.prototype.move = function(openCardColumnNo, openCardRowNo) {
	this.x = game.world.centerX - (((openCardColumnNo - 1) / 2) - this.columnNo) * 180;
	this.y = game.world.centerY - (((openCardRowNo - 1) / 2) - this.rowNo) * 160;
}

DudeCard.prototype.setActive = function(value) {
	this.isActivated = value;
}

DudeCard.prototype.show = function() {
	this.alpha = 1;
}

DudeCard.prototype.startShowAndHideAnimation = function() {
	this.flipToFront();
	this.showAndHideEvent = game.time.events.add(
		DudeCard.SHOW_AND_HIDE_TIME_SECOND * Phaser.Timer.SECOND,
		this.finishShowAndHideAnimation,
		this
	);
}

DudeCard.prototype.finishShowAndHideAnimation = function() {
	this.flipToBack();
	this.removeShowAndHideEvent();
}

DudeCard.prototype.hide = function() {
	this.alpha = 0;

	this.x = DudeCard.CARD_INITIAL_POSITION_X;
	this.y = DudeCard.CARD_INITIAL_POSITION_Y;

	this.removeShowAndHideEvent();
}

DudeCard.prototype.removeShowAndHideEvent = function() {
	if(this.showAndHideEvent != null) {
		game.time.events.remove(this.showAndHideEvent);
	}
}

DudeCard.prototype.startWaitAndHideAnimation = function() {
	this.showAndHideEvent = game.time.events.add(
		DudeCard.WAIT_AND_HIDE_TIME_SECOND * Phaser.Timer.SECOND,
		this.finishWaitAndHideAnimation,
		this
	);
}

DudeCard.prototype.finishWaitAndHideAnimation = function() {
	this.hide();
}

DudeCard.prototype.startWaitAndFlipToBackAnimation = function() {
	this.showAndHideEvent = game.time.events.add(
		DudeCard.WAIT_AND_FLIP_TO_BACK_TIME_SECOND * Phaser.Timer.SECOND,
		this.finishWaitAndFlipToBackAnimation,
		this
	);
}

DudeCard.prototype.finishWaitAndFlipToBackAnimation = function() {
	this.flipToBack();
}



DudeCard.MAX_CARD_COLUMN_NO		= 5;
DudeCard.MAX_CARD_ROW_NO		= 4;

DudeCard.CARD_INITIAL_POSITION_X	= -100;
DudeCard.CARD_INITIAL_POSITION_Y	= -100;

DudeCard.SHOW_AND_HIDE_TIME_SECOND	= 0.5;
DudeCard.WAIT_AND_HIDE_TIME_SECOND	= 0.5;
DudeCard.WAIT_AND_FLIP_TO_BACK_TIME_SECOND	= 0.5;
DudeCard.INIT_LEFT_TIME_MIN			= 2;
