var Character = new Class({
	initialize: function( idclint_perso, name, idclint_famille, famille, level, level_max, idclint_perso_joueur, ph, pdd, rarity, ability, bonus, quantity, in_deck, xp, bankPrice, distrib )
	{
		// Keep backward compatibility, if the first param is an object, it contains all the properties
		if ( $type(idclint_perso) == 'object' )
		{
			this.characterProperties = idclint_perso;

			this.idclint_perso = this.characterProperties.id;
			this.name = this.characterProperties.name.toLowerCase();
			this.idclint_famille = this.characterProperties.clan_id;
			this.famille = this.characterProperties.clan_name.toLowerCase();
			this.level = this.characterProperties.level;
			this.level_min = this.characterProperties.level_min;
			this.level_max = this.characterProperties.level_max;
			this.ph = this.characterProperties.power;
			this.pdd = this.characterProperties.damage;
			this.rarity = this.characterProperties.rarity;
			this.bonus = this.characterProperties.bonus.toLowerCase();
			this.ability = this.characterProperties.ability.toLowerCase();
			this.id_ability = this.characterProperties.ability_id;
			this.ability_unlock_level = this.characterProperties.ability_unlock_level;
			this.distrib = this.characterProperties.distrib;
			this.bankPrice = this.characterProperties.bank_price;
			this.offer_at_level = this.characterProperties.offer_at_level;

			this.in_deck = this.characterProperties.in_deck;
			this.xp = this.characterProperties.xp;
			this.quantity = this.characterProperties.quantity;
			this.idclint_perso_joueur = this.characterProperties.id_player_character;
		}
		else
		{
			this.idclint_perso = idclint_perso;
			this.name = name;
			this.idclint_famille = idclint_famille;
			this.famille = famille;
			this.level = level;
			this.level_max = level_max;

			this.idclint_perso_joueur = idclint_perso_joueur;
			this.ph = ph;
			this.pdd = pdd;
			this.rarity = rarity;
			this.in_deck = in_deck;
			this.xp = xp;
			this.bankPrice = bankPrice;
			this.distrib = distrib;

			// Not always set
			if ( ability != undefined && ability !== '' ) this.ability = ability;
			else this.ability = _("No ability");

			if ( bonus != undefined && bonus !== '' ) this.bonus = bonus;
			else this.bonus = _("No ability");

			if ( quantity != undefined ) this.quantity = quantity;
		}

		this.characterURL = '/characters/?id_perso=' + this.idclint_perso;
		if ( this.idclint_perso_joueur > 0 ) this.characterURL += '&id_pj=' + escape("#" + this.idclint_perso_joueur + '-' + this.idclint_famille);

		this.name = this.name.ucfirst();

		this.levelmax = this.level_max;

		this.level2xp = new Array(500, 1500, 3000, 5000);
		this.isLevelMax = (this.level == this.level_max);
	},

	getCharacterID: function()
	{
		return this.idclint_perso;
	},

	getCharacterInCollectionID: function()
	{
		return this.idclint_perso_joueur;
	},

	getURL: function()
	{
		return this.characterURL;
	},

	getXPRatio: function()
	{
		if ( this.isLevelMax )
		{
			return 1;
		}
		else
		{
			return this.xp / this.level2xp[this.level - 1];
		}
	},

	getXPNeeded: function()
	{
		if ( this.isLevelMax )
		{
			return 0;
		}
		else
		{
			return this.level2xp[this.level - 1];
		}
	},

	getPersoImgURL: function( type, size, gray )
	{
		var regExp = new RegExp(" ","g");
		var name = this.name.replace(regExp,"");
		var famille = this.famille.replace(regExp,"");
		return staticsHost + '/urimages/perso/' + (famille + '/' + famille + '_' + name +  '_N' + this.level + '_' + type + '_' + size).toUpperCase() + (gray ? '_GRAY': '') + '.gif';
	},

	getClanImgURL: function( size, format )
	{
		var regExp = new RegExp(" ","g");
		//var name = this.name.replace(regExp,"");
		var famille = this.famille.replace(regExp,"");
		format = (format) ? format : 'png';
		return staticsHost + '/urimages/clan/' + (famille + '_' + size).toUpperCase() + '.' + format;
	},

	getFullCard: function(addlink, addDetails, gray)
	{
		var cardDiv = new Element("div", {"class": "cardNewDiv"});

		cardDiv.model = this;

		if ( this.idclint_perso_joueur > 0 ) cardDiv.setProperty('id', 'card' + this.idclint_perso_joueur);

		if ( addDetails ) cardDiv.setStyle('height', '260px');

		cardDiv.addClass(this.rarity );
		if ( gray ) cardDiv.addClass('gray');

		var cardDivHTML = '<div class="cardNewName">' + this.name.capitalize() + '</div>';
		if ( addlink )
		{
			cardDivHTML += '<a href="' + this.characterURL + '">';
		}
		cardDivHTML += '<img class="cardNewPict" src="' + this.getPersoImgURL('STD', 160, gray) + '"/>';
		if ( addlink ) cardDivHTML += '</a>';
		cardDivHTML += '<div class="cardNewClanMask iepng"></div>';
		cardDivHTML += '<img src="' + this.getClanImgURL(160) + '" class="cardNewClanPict iepng"/>';

		if ( this.xp > 0 && this.level_max != this.level ) cardDivHTML += '<img src="' + staticsHost + '/img/v2/card/xpbar.gif" class="cardNewXP" width="' + ( 112 * this.xp / this.level2xp[this.level - 1]) + '" height="8"/>';

		if ( eloForbiddenCharacters[this.idclint_perso] === true ) cardDivHTML += '<img src=' + staticsHost + '/img/v2/icons/noelo.gif class=cardNewEloValidity title="' + getString(_("@0@ is currently invalid in the ELO mode."), [this.name.capitalize()]) + '" />';

		if ( this.level_max == this.level )
		{
			cardDivHTML += '<img src=' + staticsHost + '/img/v2/card/lvl' + this.level_max + '.gif class=cardNewFirstStar />';
		}
		else
		{
			cardDivHTML += '<img src=' + staticsHost + '/img/v2/card/lvl' + this.level_max + '_' + this.level +  '.gif class=cardNewFirstStar />';
		}



		cardDivHTML += '<div class=cardNewPH>' + this.ph + '</div><div class=cardNewPDD>' + this.pdd + '</div>';

		cardDivHTML += '<div class=cardNewPower>';

		if ( this.id_ability && (this.level < this.ability_unlock_level) )
		{
			cardDivHTML += _("Unlock ability at");
			cardDivHTML += ' <span style="white-space: nowrap;">';
			for( var i = 0; i < this.ability_unlock_level; ++i )
			{
				cardDivHTML += '<img src="' + staticsHost + '/img/v2/card/small/star_on.png" class="iepng cardMiniStar"/>';
			}
			cardDivHTML += '</span>';
		}
		else cardDivHTML += this.ability.ucfirst();

		cardDivHTML += '</div>';
		cardDivHTML += '<div class=cardNewBonus>' + this.bonus.ucfirst() + '</div>';

		if ( addDetails )
		{
			if ( this.quantity > 0 ) cardDivHTML += '<img src="' + staticsHost + '/img/CarteOk.gif" class="cardOwned" /> ';
			else if ( this.quantity != -1 ) cardDivHTML += '<img src="' + staticsHost + '/img/CarteMissing.gif" align="absmiddle" class="cardOwned" /> ';

			cardDivHTML += '<div class="cardNewDetails">';

			if ( this.distrib )
			{
				if ( this.offer_at_level ) cardDivHTML += '<b>' + getString(_("Offer at Level @0@"), [this.offer_at_level]) + '</b>';
				else cardDivHTML += '<a href="/shop/#booster">' + _("Available at the Shop") + '</a>';
				cardDivHTML += '<br/>';
				if ( this.rarity == 'c' ) cardDivHTML += _("Common Card");
				else if ( this.rarity == 'u' ) cardDivHTML += _("Uncommon Card");
				else cardDivHTML += _("Rare Card");
			}
			else cardDivHTML += '<span class="boldText normalText">' + _("Collector") +"</span>";

			cardDivHTML += '</div>';
		}

		cardDiv.set('html', cardDivHTML);

		return cardDiv;
	},

	getSmallCard: function()
	{
		var cardDiv = new Element("div", {
			'styles': {
				'position': 'relative',
				'background-image': 'url(' + staticsHost + '/img/v2/card/small/bg_' + this.rarity + '.png)',
				'width': '217px',
				'height': '42px',
				'margin-bottom': '5px',
				'cursor': 'pointer'
			},
			'class': 'iepng'
		});
		if ( this.idclint_perso_joueur > 0 ) cardDiv.setProperty('id', 'card' + this.idclint_perso_joueur);

		var cardDivHTML = '';
		cardDivHTML += '<img src="' + this.getPersoImgURL('STD', 128) + '" class="" style="position: absolute; width: 37px; height: 32px; left: 5px; top: 5px;"/>';

		cardDivHTML += '<img src="' + this.getClanImgURL(128) + '" class="iepng" style="position: absolute; left: 49px; top: 6px;"/>';
		cardDivHTML += '<div style="position: absolute; top: 7px; left: 68px; text-align: center; width: 125px; color: #fff; font-weight: bold;">' + this.name.capitalize() + '</div>';

		if ( this.isLevelMax )
		{
			cardDivHTML += '<img src="' + staticsHost + '/img/v2/card/small/XPscale_02.gif" style="position: absolute; left: 47px; top: 28px; width: 67px; height: 8px;"/>';
		}
		else if ( this.xp > 0 )
		{
			var width = Math.floor(67 * this.getXPRatio());
			cardDivHTML += '<img src="' + staticsHost + '/img/v2/card/small/XPscale_01.gif" style="position: absolute; left: 47px; top: 28px; width: ' + width + 'px; height: 8px;"/>';
		}

		cardDivHTML += '<img src="' + staticsHost + '/img/v2/card/small/Mask.png" class="iepng" style="position: absolute; left: 46px; top: 27px;"/>';

		cardDivHTML += '<div style="position: absolute; top: 28px; left: 50px;">';
		for(var i = 0; i < this.level_max; ++i)
		{
			if ( i < this.level )
			{
				cardDivHTML += '<img src="' + staticsHost + '/img/v2/card/small/star_on.png" class="iepng" />';
			}
			else
			{
				cardDivHTML += '<img src="' + staticsHost + '/img/v2/card/small/star_off.png" class="iepng" />';
			}
		}
		cardDivHTML += '</div>';

		cardDivHTML += '<div style="position: absolute; top: 26px; left: 132px; text-align: center; width: 20px; color: #fff; font-weight: bold;">' + this.ph + '</div>';
		cardDivHTML += '<div style="position: absolute; top: 26px; left: 170px; text-align: center; width: 20px; color: #fff; font-weight: bold;">' + this.pdd + '</div>';

		cardDivHTML += '<img src="' + staticsHost + '/img/v2/card/small/ico_Pouvoir_' + (this.id_ability ? 'ON' : 'OFF') + '.png" title="' + this.ability + '" class="iepng" style="position: absolute; left: 196px; top: 5px;"/>';
		cardDivHTML += '<img src="' + staticsHost + '/img/v2/card/small/ico_Bonus_' + (this.bonus ? 'ON' : 'OFF') + '.png" title="' + this.bonus + '" class="iepng" style="position: absolute; left: 196px; top: 24px;"/>';

		cardDiv.set('html', cardDivHTML);

		return cardDiv;
	},

	// Static function
	updateFullCardDiv: function(cardDiv, character)
	{
			cardDiv.setProperty('id', 'card' + character.idclint_perso);
			cardDiv.getElements('.cardName').set('html',  character.name.capitalize() );
	},

	jsonReady: function()
	{
		return JSON.encode(this.characterProperties);
	}
});