/*

Create Race.

Functions required to create the race instance.

ie horses to compete, their forms,odds and bonuses etc.

To Do : include jockeys etc.
		draw in cookie histories.
*/

if (debug) { alert('create_race loaded'); }

function horse(name,number,h_form,bonus) {
	this.name = name;
	this.horse_number = number;
	this.h_form = h_form;
	this.bonus = bonus;
	this.grass = "";
	this.odds_win = 0;
	this.odds_win_display = 0;
	this.odds_place = 0;
	this.odds_place_display = 0;
	this.place = 0;
	this.horse_fell = 0;
}

// version 1 : bonus is 0-30 work out ratio from location in this.

// version 2 : work out comparison to average bonus.

// version 1
function calc_odds(i) {

	var tmp = horses[i].bonus;

	if (tmp > 24) {

		horses[i].odds_win = 0.125;
		horses[i].odds_win_display = "1:8";
		horses[i].odds_place = 0.1;
		horses[i].odds_place_display = "1:10";

	} else if (tmp > 20) {

		horses[i].odds_win = 0.25;
		horses[i].odds_win_display = "1:4";
		horses[i].odds_place = 0.2;
		horses[i].odds_place_display = "1:5";

	} else if (tmp > 18) {

		horses[i].odds_win = 0.4;
		horses[i].odds_win_display = "2:5";
		horses[i].odds_place = 0.3;
		horses[i].odds_place_display = "1:3";


	} else if (tmp > 16) {

		horses[i].odds_win = 0.8;
		horses[i].odds_win_display = "4:5";
		horses[i].odds_place = (2/3);
		horses[i].odds_place_display = "2:3"


	} else if (tmp > 14) {

		horses[i].odds_win = 1;
		horses[i].odds_win_display = "1:1";
		horses[i].odds_place = (2/3)
		horses[i].odds_place_display = "2:3";


	} else if (tmp > 12) {

		horses[i].odds_win = 2;
		horses[i].odds_win_display = "2:1";
		horses[i].odds_place = 1;
		horses[i].odds_place_display = "1:1";


	} else if (tmp > 10) {

		horses[i].odds_win = 4;
		horses[i].odds_win_display = "4:1";
		horses[i].odds_place = 2;
		horses[i].odds_place_display = "2:1";


	} else if (tmp > 8) {

		horses[i].odds_win = 6;
		horses[i].odds_win_display = "6:1";
		horses[i].odds_place = 4;
		horses[i].odds_place_display = "4:1"


	} else if (tmp > 6) {

		horses[i].odds_win = 8;
		horses[i].odds_win_display = "8:1";
		horses[i].odds_place = 6;
		horses[i].odds_place_display = "6:1";


	} else if (tmp < 7) {

		horses[i].odds_win = 10;
		horses[i].odds_win_display = "10:1";
		horses[i].odds_place = 8;
		horses[i].odds_place_display = "8:1:";


	}
}

function calc_form(h_form) {

	var bonus = 60;
	for (var j = 1; j < h_form.length; j++) {
		if (h_form[j] > 15) {
			bonus = bonus - 10; // scratch
		} else {
			bonus = bonus - h_form[j];
		} // the form
	}
	//return Math.round(bonus/2);
	return Math.round(bonus/4);
}

function show_form(h_form) {

	var form_str = "";
	for (var i = 1; i < h_form.length; i++) {
		if (h_form[i] > 15) {
			form_str = form_str + "- "; // scratch
		} else if (h_form[i] > 9) {
			form_str = form_str + "0 "; // 10th or more
		} else {
			form_str = form_str + eval(h_form[i]) + " "; // top 9
		} // the form
	}
	return form_str;
}

function check_it(choice,i) {
	for (var j = 1; j<i; j++) {
		if (choice == my_array[j]) {
			return 0;
		}
	}
	return 1;
}

function choose_horsenumbers(num) {
	var i = 1;
	while (i <= num) {
		var choice = randNum(horse_names.length)+1;
		if (check_it(choice,i)) {
			my_array[i] = choice;
			i++;
		}
	}
}

function create_horses() {

	choose_horsenumbers(horses_racing);

	for (var i = 1; i <= this.horses_racing; i++) {
		var tmp_form = new makeArray(randNum(20),randNum(20),randNum(20),randNum(20),randNum(20));
		horse_number = my_array[i];
		//alert(horse_number);
		horses[i] = new horse(horse_names[horse_number],horse_number,tmp_form,calc_form(tmp_form),0,0);
		calc_odds(i);
	}

}

function create_horse_options() {

	var the_html = "";

	for (var i = 1; i <= this.horses_racing; i++) {

		//requires selected for bets

		the_html = the_html + '<OPTION VALUE="'+i+'">'+horses[i].name+'</OPTION>';

	}

	return the_html;

}

var horses_racing = randNum(10)+6; // Number of Horses 6..15

//alert("Horses : "+horses_racing);

var my_array = new Array(horses_racing);

var horses = new Array(horses_racing);

var winner = 0; // init

create_horses(); // init

var horse_html = create_horse_options(); // option html
