$(document).ready(function(){

	/* 
		AKCJA: klikniecie na naglwek MECZE
		DZIALANIE: aktywuje naglwek, ukrywa zestawienie, pokazuje mecze
	*/
	$("#matches_link").bind('click',function(){
		$(this).addClass('active').next().removeClass('active');
		$("#sidetables_cms").hide();
		$("#matches_cms").show();
		$(this).blur();
	});
	
	/* 
		AKCJA: klikniecie na naglwek ZESTAWIENIA
		DZIALANIE: aktywuje naglwek, ukrywa mecze, pokazuje zestawienie
	*/
	$("#sidetables_link").bind('click',function(){
		$(this).addClass('active').prev().removeClass('active');
		$("#matches_cms").hide();
		$("#sidetables_cms").show();
		$(this).blur();
	});
	
	// jeśli nie ma zestawienia u góry, to pokaż z boku
	var upTab = $(".group_table").length;
	if(upTab < 4)
	{
		$("#sidetables_link").click();
	}
	
	// czy są galerie?
	var nogaleries = $("#no_galeries").length;
	if(nogaleries > 0)
	{
		$("#no_galeries").remove();
	}
	else
	{
		$(".photo_masters:eq(1)").remove();
	}
	
	AssignRound2Links();
	DistributeRound2();
//	$('#round2').hide();
});

function AssignRound2Links()
{
	$("#switch_tables_link").unbind().bind('click',function(){
		$('#round2').hide()
	});
	$("#switch_round2_link").unbind().bind('click',function(){
		$('#round2').show()
	});	
	$("#switch_tables_link_r").unbind().bind('click',function(){
		$('#round2').hide()
	});
	$("#switch_round2_link_r").unbind().bind('click',function(){
		$('#round2').show()
	});	
}

function DistributeRound2()
{
	if(!window.rounditems)
		return;

	var len = window.rounditems.length;
	if(len == 0)
		return;

	var abbrs = new Array();
	abbrs['Portugalia'] = 'POR';
	abbrs['Hiszpania'] = 'ESP';
	abbrs['Turcja'] = 'TUR';
	abbrs['Chorwacja'] = 'CRO';
	abbrs['Niemcy'] = 'GER';
	abbrs['Austria'] = 'AUT';
	abbrs['Polska'] = 'POL';
	abbrs['Holandia'] = 'NED';
	abbrs['Rumunia'] = 'ROU';
	abbrs['Włochy'] = 'ITA';
	abbrs['WĹ‚ochy'] = 'ITA';
	abbrs['Francja'] = 'FRA';
	abbrs['Szwecja'] = 'SWE';
	abbrs['Rosja'] = 'RUS';
	abbrs['Czechy'] = 'CZE';
	abbrs['Grecja'] = 'GRE';
	abbrs['2 miejsce B'] = '';
	abbrs['2 miejsce C'] = '';
	abbrs['2 miejsce D'] = '';
	abbrs['Zwycięzca C'] = '';
	abbrs['Zwycięzca D'] = '';	
	abbrs['Zwycięzca 26'] = '';
	abbrs['Zwycięzca 25'] = '';
	abbrs['Zwycięzca 26'] = '';
	abbrs['Zwycięzca 27'] = '';
	abbrs['Zwycięzca 28'] = '';
	abbrs['Zwycięzca 29'] = '';
	abbrs['Zwycięzca 30'] = '';
	
	var newitems = [];
	var id = 1;
	for (var i = 0; i < len; i++)
	{
		var item = window.rounditems[i];
		
		var scoreArr;
		if (item[4] == '') 
			scoreArr = new Array('', '');
		else {
			scoreTmp = item[4].split(' ');
			if (scoreTmp.length == 1) {
				scoreArr = scoreTmp[0].split(':');
			}
			else
			{
				scoreArr = scoreTmp[1].split(':');
				scoreArr[0] = scoreArr[0].substring(1);
				scoreArr[1] = scoreArr[1].substring(0,1);
			}
		}
		
		abr = '';
		if(abbrs[item[2]])
			abr = abbrs[item[2]];
		newitems.push(new Array(id, item[2], abr, scoreArr[0]));
		id += 1;
		
		abr = '';
		if(abbrs[item[3]])
			abr = abbrs[item[3]];
		newitems.push(new Array(id, item[3], abr, scoreArr[1]));
		id += 1;
	}

	var html = new Array();
	var itemslen = newitems.length;
	for(var i=0; i<itemslen; i++)
	{
		var item = newitems[i];
		var dir = 'left';
		if(item[0]==5 || item[0]==6 || item[0]==7 || item[0]==8 || item[0]==11 || item[0]==12 || item[0]==14)
			dir = 'right';
		html.push('<div id="qf_match_'+item[0]+'" class="qf_'+dir+' '+item[2]+'">');
		html.push('<span class="team">'+item[1]+'</span>');
		html.push('<span class="score">'+item[3]+'</span>');
		html.push('</div>');
	}
	
	
	$("#round2 div").html(html.join(''));
}
