if(!Action) var Action = {};

ChevalAjax = Class.create(CommonAjax);

Action.Common = Class.create(ChevalAjax, {
initialize: function($super, form, url, options) {
	options = options || {};
	$super(options);
	if(form) {
		this._form = $(form);
		this.id = this._form.identify();
	}
	this._url = projectUrl + url;
	this.page = this._url;
	
},
send: function(options) {
	return Ajax.JSON(this, options||{});
},
execute: function(object) {
	var blocks = object.blocks;
	if(blocks) {
		$H(blocks).each(function(pair) {
			var target = $(pair.key);
			
			if(target) {
				// Empty target
				target.update('');
				// Clean invalid tooltip
				$$('.tooltip').each(
				   function(myElement) {
					   if(myElement.hasAttribute('rel') && !$(myElement.getAttribute('rel')))
							$(myElement).remove();
				   }
				 );
				// Update target
				target.update(pair.value);
			}
		});
	}
	// Hide current tooltip
	$('infoBox').hide();
	// Tutoriel
	if(typeof(object.tutoriel) != 'undefined') {
		$("tutoriel").innerHTML = object.tutoriel;
	}
}
});

Action.Horse = Class.create(Action.Common, {
initialize: function($super, form, url, options) {
	$super(form,'/elevage/chevaux' + url, options);
},
execute: function($super, object) {
	$super(object);
	changeCheval(object);
}
});

function perteEntrainement() {
	return Math.round(joueurGalop * competencesEntrainement * chevalSante) / 100;
}

function perteBalade() {
	return Math.round(joueurGalop * competencesBalade * chevalSante) / 100;
}

function gainFonction(x) {
	return - 1 / 100 * x + 3 / 2;
}

function gainCalcul(debut, fin) {

	var l = fin - debut;

	var y1 = gainFonction(debut);
	var y2 = gainFonction(fin);

	return l * (y1 + y2) / 2;
}

function besoinEnergie() {

	return Math.floor(chevalJourEnergie * energieGranules * chevalPoids / 250) * 250;

}


function changeCheval(object) {
	
	if(typeof(object.chevalMoral) != 'undefined') {
		chevalMoral = object.chevalMoral;
	}
	
	if(typeof(object.chevalEnergie) != 'undefined') {
		chevalEnergie = object.chevalEnergie;
	}
	
	if(typeof(object.chevalSante) != 'undefined') {
		chevalSante = object.chevalSante;
	}
	
	if(typeof(object.chevalJourEnergie) != 'undefined') {
		chevalJourEnergie = object.chevalJourEnergie;
	}
	
	if(typeof(object.chevalJourGranules) != 'undefined') {
		chevalJourGranules = object.chevalJourGranules;
	}
	
	if(typeof(object.chevalJourFourrage) != 'undefined') {
		chevalJourFourrage = object.chevalJourFourrage;
	}
	
	if(typeof(object.chevalJourRepas) != 'undefined') {
		chevalJourRepas = object.chevalJourRepas;
	}
	
	
	if(typeof(object.varsB1) != 'undefined'){
		b1 = object.varsB1;
	}
	if(typeof(object.varsB2) != 'undefined'){
		b2 = object.varsB2;
	}
	if(typeof(object.varsB3) != 'undefined') {
		b3 = object.varsB3;
	}
	if(typeof(object.varsB4) != 'undefined'){
		b4 = object.varsB4;
	}
	if(typeof(object.varsB5) != 'undefined'){
		b5 = object.varsB5;
	}
	if(typeof(object.varsB6) != 'undefined'){
		b6 = object.varsB6;
	}
	if(typeof(object.varsE1) != 'undefined'){
		e1 = object.varsE1;
	}
	if(typeof(object.varsE2) != 'undefined'){
		e2 = object.varsE2;
	}
	if(typeof(object.varsE3) != 'undefined'){
		e3 = object.varsE3;
	}
	if(typeof(object.varsE4) != 'undefined'){
		e4 = object.varsE4;
	}
	if(typeof(object.varsE5) != 'undefined'){
		e5 = object.varsE5;
	}
	if(typeof(object.varsE6) != 'undefined'){
		e6 = object.varsE6;	
	}
	
}


GetHarnessAjax = Class.create();
GetHarnessAjax.prototype = {
	
	id: null,
	page: null,
	
	execute: function(object) {
		$(this.id).innerHTML = object.content;
	}
	
}
Object.extend(GetHarnessAjax.prototype, CommonAjax);


RechercherJumentAjax = Class.create();
RechercherJumentAjax.prototype = {
	id: 'rechercherJument',
	page: projectUrl +'/elevage/chevaux/reserverJument',
	execute: function(object) {
		if(typeof(object.boiteListeJuments) != 'undefined') {
			$('resultatsRechercheJuments').innerHTML = object.boiteListeJuments;
		}
		if(typeof(object.boiteAlimentation) != 'undefined') {
			$('alimentationBoite').innerHTML = object.boiteAlimentation;
		}
		if(typeof(object.boiteEtat) != 'undefined') {
			$('etatBoite').innerHTML = object.boiteEtat;
		}
		if((typeof(object.alerts) != 'undefined') && (typeof(object.history) != 'undefined')) {
			$('alerts').innerHTML = object.alerts;
			$('history').innerHTML = object.history;
		}
		if(typeof(object.boiteTypeRetour) != 'undefined') {
			hideBox('reserverJumentBox');
			hideBox('proposerSaillieBox');
		}
		changeCheval(object);
	}
}
Object.extend(RechercherJumentAjax.prototype, ChevalAjax);


function getWalkGains(walkType, duration) {

	var gains = $H({
		energie : 0,
		endurance : 0,
		vitesse : 0,
		dressage : 0,
		trot : 0,
		galop : 0,
		saut : 0
	});
	
	var start;
	var end;

	var coefficient;

	if(walkType == 'trot') {
		coefficient = 18;
	} else if(walkType == 'gallop') {
		coefficient = 22;
	} else if(walkType == 'jump') {
		coefficient = 25;
	}

	//Energy
	gains.set('energie', -1 * Math.round((coefficient * duration * (duration <= 1 ? 1.5 : 0.9)) * 10) / 10);
	
	//Dressage
	start = b3;
	end = start + Math.min(duration * 2 * vieillissement, 100 - b3);
	gains.set('dressage', Math.round(gainCalcul(start, end) * traiter(c3) * perteBalade()) / 100);

	//Stamina / Speed
	if(duration <= 1) {
		start = b2;
		end = start + Math.min(duration * 2 * vieillissement, 100 - b2);

		gains.set('vitesse', Math.round(gainCalcul(start, end) * traiter(c2) * perteBalade()) / 100);
	} else {
		start = b1;
		end = start + Math.min(duration * 2 * vieillissement, 100 - b1);

		gains.set('endurance', Math.round(gainCalcul(start, end) * traiter(c1) * perteBalade()) / 100);
	}
	
	//Trot / Gallop / Jump
	if(walkType == 'trot') {
		start = b5;
		end = start + Math.min(duration * 2 * vieillissement, 100 - b5);

		gains.set('trot', Math.round(gainCalcul(start, end) * traiter(c5) * perteBalade()) / 100);
	} else if(walkType == 'gallop') {
		start = b4;
		end = start + Math.min(duration * 2 * vieillissement, 100 - b4);

		gains.set('galop', Math.round(gainCalcul(start, end) * traiter(c4) * perteBalade()) / 100);

	} else if(walkType == 'jump') {
		start = b6;
		end = start + Math.min(duration * 2 * vieillissement, 100 - b6);

		gains.set('saut', Math.round(gainCalcul(start, end) * traiter(c6) * perteBalade()) / 100);
	}
		
	return gains;
}



function getPlageGains(walkType, duration) {

	var gains = $H({
		energie : 0,
		endurance : 0,
		vitesse : 0,
		dressage : 0,
		trot : 0,
		galop : 0,
		saut : 0
	});

	// Energy
	gains.set('energie', -1 * Math.round(Math.pow(13 * duration, 0.9) * 10) / 10);
	
	var min = null;
	var max = null;
	
	var types = ['endurance', 'vitesse', 'dressage', 'galop', 'trot', 'saut'];
	
	for(i = 0; i < types.length; i++) {
	
		var type = types[i];
		
		if(competence(type) > 0) {
		
			if(
				min == null || 
				competence(type) < competence(min)
			) {
				min = type;
			}
			
			if(
				max == null || 
				competence(type) > competence(max)
			) {
				max = type;
			}
			
		}
		
	}
	
	var gainTotal = Math.round((
		potentiel("endurance") + 
		potentiel("vitesse") + 
		potentiel("dressage") + 
		potentiel("galop") + 
		potentiel("trot") + 
		potentiel("saut")
	) * plagePart) / 100;
	
	if(
		min != null && 
		max != null && 
		min != max
	) {
		
		// Le gain restant pour ce cheval
		var gainRestant = (gainTotal - plage);
		
		// Le gain de base par heure
		var gainHeure = Math.min(gainTotal / 100, 0.2);
		
		// Le gain à retrancher au gain restant
		var gainBrut = Math.min(
			gainRestant,
			duration * vieillissement * gainHeure
		);
		
		// Le gain réel avec l'impact du moral
		var gainReel = gainBrut * chevalMoral / 100;
		gainReel = Math.min(competence(min), gainReel);
		
		gains.set(min, Math.round(-1 * gainReel * 100) / 100);
		gains.set(max, Math.round(gainReel * 100) / 100);
		
	}

	return gains;
}

function potentiel(type) {
	return parseFloat($(type +"Genetique").innerHTML);
}

function competence(type) {
	return parseFloat($(type +"Valeur").innerHTML);
}

function jeuEnergie(poidsCheval, jeuFertilite, coefficientSaison) {

	var jeuTemps = document.getElementById('jeuTemps').value;

	var jeuEnergie = document.getElementById('jeuEnergie');

	if(jeuTemps == '') {

		var jeuInfo = document.getElementById('jeuInfo');
		jeuInfo.style.display = 'none';

	} else {

		jeuTemps = parseInt(jeuTemps) / 2;

		var jeuInfo = document.getElementById('jeuInfo');
		var jeuTempsText = document.getElementById('jeuTempsText');

		jeuTempsText.innerHTML = document.getElementById('jeuTemps'+ document.getElementById('jeuTemps').value).innerHTML;
		jeuEnergie.innerHTML = Math.round(0.9 * (10 + (18 - chevalAge) / 2) * jeuTemps * (2 - chevalMoral / 100), 1);
		jeuInfo.style.display = 'inline';

	}

}

function preConso(preTemps, poidsCheval, preFertilite, coefficientSaison, preProprete, moralFacteur) {

	var preConso = document.getElementById('formCenterMeadowSubmit');

	if(preTemps == '') {
		$('gainPre').hide();
	} else {
		$('gainPre').show();

		preTempsCalc = parseFloat(preTemps) / 2;
		var conso = preTempsCalc * 1.5 / 500 * poidsCheval;
		conso *= coefficientSaison;
		conso *= preFertilite / 100 * 0.8 + 0.2;

		$('formCenterMeadowFodder').innerHTML = "<b>"+kg(Math.round(conso * 10) / 10)+"</b>";
		
		var moral = moralFacteur * 1.8 * preTempsCalc * (preProprete / 100);
		
		$('formCenterMeadowMoral').innerHTML = "<b>+"+ Math.round(moral * 10) / 10 +"</b>";
		
		var energie = chevalEnergie * (Math.pow(1 + 2.5 / 100, preTempsCalc) - 1) / (2 - preProprete / 100);
		
		$('formCenterMeadowEnergie').innerHTML = "<b>+"+ Math.round(energie * 10) / 10 +"</b>";
	}

}

function boxConso(boxTemps, boxSuperficie, boxPrestige) {

	var preConso = document.getElementById('formCenterMeadowSubmit');

	if(boxTemps == '') {
		$('gainBox').hide();
	} else {
		$('gainBox').show();

		boxTempsCalc = parseFloat(boxTemps) / 2;
		
		var moral = -1 * 0.5 * boxTempsCalc * (2 - boxSuperficie / 100);
		
		$('formCenterBoxMoral').innerHTML = "<b>"+ Math.round(moral * 10) / 10 +"</b>";
		
		var energie = chevalEnergie * (Math.pow(1 + 2 / 100, boxTempsCalc) - 1) / (2 - boxPrestige / 100);
		
		$('formCenterBoxEnergie').innerHTML = "<b>+"+ Math.round(energie * 10) / 10 +"</b>";
	}

}


function entrainementGain(competence, coefficientEnergie, value) {

	var competenceUC = competence.substr(0, 1).toUpperCase() + competence.substr(1, competence.length - 1);

	var dureeDemi = value || 0;

	var entrainementInfo = $(competence +'Info');

	var gainEntrainement = parseFloat(dureeDemi);

	var coefficient;
	var entrainement;

	switch(competence) {
		case 'endurance' :
			coefficient = c1;
			entrainement = e1;
			break;
		case 'vitesse' :
			coefficient = c2;
			entrainement = e2;
			break;
		case 'dressage' :
			coefficient = c3;
			entrainement = e3;
			break;
		case 'galop' :
			coefficient = c4;
			entrainement = e4;
			break;
		case 'trot' :
			coefficient = c5;
			entrainement = e5;
			break;
		case 'saut' :
			coefficient = c6;
			entrainement = e6;
			break;
	}

	var debut = entrainement;
	var fin = entrainement + Math.min(gainEntrainement * vieillissement, 100 - entrainement);

	var gainCompetence = Math.round(gainCalcul(debut, fin) * traiter(coefficient) * perteEntrainement()) / 100;
	gainCompetence = '+'+ gainCompetence;

	var gainEnergie = Math.round(( dureeDemi / 2 ) * coefficientEnergie * (2 - chevalMoral /100) * -1 * 10) / 10;
	if(gainEnergie == 0) {
		gainEnergie = '-'+ gainEnergie;
	}
	
	$(competence +'Gain').update(gainCompetence);
	$(competence +'Energie').update(gainEnergie);


}

function traiter(valeur) {
	return Math.round(Math.pow(valeur / n - 3, 1 / 3) * 100) / 100;
}


var BonusBox = {

	show: function(params) {
		//if($('bonusContent').innerHTML == '') {
			Ajax.JSON(new BonusAjax({ 'params': params}));
		//}
	}

}

var BonusAjax = Class.create();
BonusAjax.prototype = {
	
	id: 'bonus',
	page: projectUrl +'/elevage/chevaux/bonus',
  
	execute: function(object) {
		this.content();
	}
	
}

Object.extend(BonusAjax.prototype, CommonAjax);

var BreedingAjax = Class.create();
BreedingAjax.prototype = {
	
	id: 'horseBreedingFarm',
	page: projectUrl +'/elevage/chevaux/doBreedingFarm',
  
	execute: function(object) {
		this.content();
		hideBox("breeding-farm-popup");
	}
	
}

Object.extend(BreedingAjax.prototype, CommonAjax);

var RenameAjax = Class.create();
RenameAjax.prototype = {
	
	id: 'horseName',
	page: projectUrl +'/elevage/chevaux/doRename',
  
	execute: function(object) {
		this.content();
		hideBox("rename-popup");
	}
	
}

Object.extend(RenameAjax.prototype, CommonAjax);

function searchHorse(breedingId, post) {
	
	if(post) {
		post = '&'+ post;
	} else {
		post = '';
	}
	
	post = 'id='+ breedingId + post;
	
	new Ajax.Updater(
		'horseList'+ (breedingId == 'all-horses' ? '' : breedingId),
		'/elevage/chevaux/searchHorse',
		{ 'method': 'post', 'postBody': post }
	);
	
}