var fantasy_team_price = 100.0;
var player_per_team = 3;

function transfers_list(page) {
	var sel_item = $('fantasy-position-only').value;
	var temp_list = new Array();
	var final_list = new Array();
	
	var patt=new RegExp("^(pos|team)_(\.+)$");
	var matches = patt.exec(sel_item); 
	var ident = matches ? matches[2] : null;
		
	if (matches && (matches[1] == 'pos')) {
		fplayers.each(function(item) {
			if (item && (item[_fposition] == ident)) {
				temp_list[temp_list.length] = item;
			}
		});
		fxfer_per_page = 20;
	} else if (matches && (matches[1] == 'team')) { 
		fplayers.each(function(item) {
			if (item && (item[_fteam] == ident)) {
				temp_list[temp_list.length] = item;
			}
		});
		fxfer_per_page = 120;
	} else {
		temp_list = fplayers.clone();
		fxfer_per_page = 20;
	}

	var sortc = _fprice;
	temp_list.sort(function(a, b) { return b[sortc] - a[sortc]; });
	l = fxfer_per_page * page;
	var start = fxfer_per_page * (page - 1);
	for (var i = 0; i < fxfer_per_page; i++) {
		if (temp_list[start + i]) final_list[i] = temp_list[start + i];
	}
	
	var html = '<table cellpadding="0px" cellspacing="0px" border="0px" class="fantasy-opcije-tabela-in"><tr class="header"><td>Ime</td><td>Tim</td><td>Cena</td><td>Poeni</td></tr>';
	
	var positions = new Array('GK', 'D', 'M', 'F');
	var position_captions = new Array('Golman', 'Odbrana', 'Sredina', 'Napad');
	
	var l = final_list.length;
	for (var ipos = 0; ipos < positions.length; ipos++) {
		var found = false;
		var html_frag = '';
		for (var i = 0; i < l; i++) {
			if (final_list[i][_fposition] == positions[ipos]) {
				found = true;
				html_frag += '<tr>' + 
'<td><a href="javascript:fantasy_add_to_squad(' + final_list[i][_fid] + ')">' + final_list[i][_fshort_name] + '</a></td>' +
'<td>' + final_list[i][_fteam_tla].toUpperCase() + '</td>' +
'<td>' + final_list[i][_fprice] + '</td>' +
'<td>' + final_list[i][_fpoints] + '</td>' +
'</tr>';
			}
		}
		if (found) {
			html += '<tr class="pozicija"><td><a href="javascript:$(\'fantasy-position-only\').value=\'' + positions[ipos] + '\';transfers_list(1);">' + position_captions[ipos] + '</a></td>' +
				'<td></td><td></td><td></td></tr>' + html_frag;
		}
	}

	var html_frag = '<tr class="dole">';
	if (page > 1) {
		html_frag += '<td><a href="javascript:transfers_list(' + (page - 1) + ')">prethodna</a></td>';
	} else {
		html_frag += '<td>&nbsp;</td>';
	}
	html_frag += '<td></td><td></td>';
	if (final_list.length == fxfer_per_page) {
		html_frag += '<td><a href="javascript:transfers_list(' + (page + 1) + ')">sledeća</a></td>';
	} else {
		html_frag += '<td>&nbsp;</td>';
	}
	html_frag += '</tr>';
	html += html_frag + '</table>';
	$('fantasy-players-list').innerHTML = html;
}

function fantasy_add_to_squad(id) {
	var found;
	if (fplayers[id]) {
		for (var i = 0; i < fantasy_current_squad.length; i++) {
        	if (fantasy_current_squad[i] && (fantasy_current_squad[i][_fid] == id)) {
				alert(fantasy_current_squad[i][_fshort_name] + " je već izabran");
                return;
			}
		}
		
		// 2 - 5 - 5 - 3
		var istart, iend;
		switch (fplayers[id][_fposition]) {
			case 'GK':
				istart = 0; iend = 2;
				break;
			case 'D':
				istart = 2; iend = 7;
				break;
			case 'M':
				istart = 7; iend = 12;
				break;
			case 'F':
			default:
				istart = 12; iend = 15;
		}
		
		for (var i = istart; i < iend; i++) {
			found = false;
			if (!fantasy_current_squad[i]) {
				fantasy_current_squad[i] = fplayers[id];
				found = true;
				found_pos = i;
				break;
			}
		}
		if (!found) {
			alert('Nema mesta');
			return;
		}		
	}
	
	if (found) {
		fantasy_refresh_squad(found_pos);
		fantasy_validate_transfers_and_set_ui();
	}
}

function fantasy_refresh_squad(table_pos) {
	if (fantasy_current_squad[table_pos]) {
		if (fantasy_orig_squad[table_pos] && (fantasy_current_squad[table_pos][_fid] == fantasy_orig_squad[table_pos][_fid])) {
			$('fantasy-team-p' + table_pos + '-onoff').innerHTML = '<a href="javascript:fantasy_remove_from_squad(' + fantasy_current_squad[table_pos][_fid] + ')"><img src="http://static.b92.net/sport/euro2008/images/izmene-off.gif" /></a>';
			$('fantasy-team-p' + table_pos + '-team').innerHTML = '<a href="?section=team&id=' + fantasy_orig_squad[table_pos][_fteam] + '" target="_blank">' + fantasy_orig_squad[table_pos][_fteam_short_name] + '</a>';
		} else {
			$('fantasy-team-p' + table_pos + '-onoff').innerHTML = '<a href="javascript:fantasy_revert_squad(' + table_pos + ')"><img src="http://static.b92.net/sport/euro2008/images/izmene-in.gif" /></a>';
			$('fantasy-team-p' + table_pos + '-team').innerHTML = '<a href="?section=team&id=' + fantasy_current_squad[table_pos][_fteam] + '" target="_blank">' + fantasy_current_squad[table_pos][_fteam_short_name] + '</a>';
		}
		$('fantasy-team-p' + table_pos + '-player').innerHTML = '<a href="?section=player&player=' + fantasy_current_squad[table_pos][_fid] + '" target="_blank">' + fantasy_current_squad[table_pos][_fshort_name] + '</a>';
		$('fantasy-team-p' + table_pos + '-bought-price').innerHTML = fantasy_current_squad[table_pos][_fprice];
		$('fantasy-team-p' + table_pos + '-points').innerHTML = fantasy_current_squad[table_pos][_fpoints];
		$('fantasy-team-p' + table_pos + '-price').innerHTML = fantasy_current_squad[table_pos][_fsellprice];
	} else {
		$('fantasy-team-p' + table_pos + '-onoff').innerHTML = '<a href="javascript:fantasy_revert_squad(' + table_pos + ')"><img src="http://static.b92.net/sport/euro2008/images/izmene-out.gif" /></a>';
	}
	
	var total_worth = 0;
	fantasy_current_squad.each(function(item) {
		if (item) total_worth += item[_fsellprice];
	});
	var total_worth_orig = 0;
	fantasy_orig_squad.each(function(item) {
		if (item) total_worth_orig += item[_fsellprice];
	});

	var in_the_bank = fantasy_bank_balance + total_worth_orig - total_worth + 0.0001;
	if (in_the_bank > 0) {
		$('in-the-bank').innerHTML = '<div class="mojtim-preostalo">preostalo iz budžeta <span>' + (Math.floor(in_the_bank * 10) / 10) + '</span></div>';
	} else {
		$('in-the-bank').innerHTML = '<div class="transfer-minus">u minusu ste  <span>' + (Math.ceil(Math.abs(in_the_bank) * 10) / 10) + '</span></div>';
	}
}

function fantasy_revert_squad(table_pos) {
	fantasy_current_squad[table_pos] = fantasy_orig_squad[table_pos];
	fantasy_refresh_squad(table_pos);
	fantasy_validate_transfers_and_set_ui();
}

function fantasy_remove_from_squad(id) {
	var found = false;
	if (fplayers[id]) {
		for (var i = 0; i < fantasy_current_squad.length; i++) {
        		if (fantasy_current_squad[i] && (fantasy_current_squad[i][_fid] == id)) {
				fantasy_current_squad[i] = null;
				found = true;
				found_pos = i;
				break;
			}
		}
	}
	if (found) {
		fantasy_refresh_squad(found_pos);
		fantasy_validate_transfers_and_set_ui();
	}
	
}

function fantasy_validate_transfers_and_set_ui() {
	var msg = fantasy_validate_transfers();
	if (msg.length > 0) {
		$('tranfer-submit').hide();
		$('fantasy-xfer-err').innerHTML = '<ul><li>' + msg.join('</li><li>') + '</li></ul>';
	} else {
		$('tranfer-submit').show();
		$('fantasy-xfer-err').innerHTML = '';
	}
}

function fantasy_validate_transfers() {
	valid = true;
	var players_per_team = new Hash();
	var msg = new Array();
	fantasy_current_squad.each(function(item) {
		if (item) {
			if (!players_per_team[item[_fteam_name]]) 
				players_per_team[item[_fteam_name]] = 1;
			else players_per_team[item[_fteam_name]]++;
		} else {
			if (valid) {
				valid = false;
				msg.push('Morate izabrati svih 15 igrača');
			}
		}
	});
	players_per_team.each(function(pair) {
		if (pair.value > player_per_team) {
			msg.push('Više od ' + player_per_team + ' igrača ' + pair.key);
		}
	});

	var total_worth = 0;
	fantasy_current_squad.each(function(item) {
		if (item) total_worth += item[_fsellprice];
	});
	var total_worth_orig = 0;
	fantasy_orig_squad.each(function(item) {
		if (item) total_worth_orig += item[_fsellprice];
	});
	if (fantasy_bank_balance + total_worth_orig - total_worth < 0) {
		msg.push('Prekoračili ste limit');
	}
	
	return msg;
}

function fantasy_submit_team() {
	var msg = fantasy_validate_transfers();
	if (msg.length == 0) {
		html = '';
		fantasy_current_squad.each(function(item) {
			if (item) {
				html += '<input type="hidden" name="players[]" value="' + item[_fid] + '" />';
			}
		});
		$('transfer-make').innerHTML = html;
		document.forms['transfer-make'].submit();
	}
}

function fantasy_my_inout(table_row, id) {
	if (table_row <= 11) { // napolje iz prvih 11
		$('fantasy-my-' + table_row + '-icon').innerHTML = '<a href="javascript:fantasy_my_inout(' + table_row + ', ' + id + ');"><img src="http://static.b92.net/sport/euro2008/images/izmene-out.gif" /></a>';
	}
}
