var selectie = new Array();

function selectKlasse(klasse,stijl){
	var count = 0;
	var klasseselectie= new Array();
	for (i=0;i<deelnemers.length;i++){
		if (deelnemers[i][4+stijl]==klasse){
			klasseselectie[count]=deelnemers[i];
			count++;
		}
	}
	return klasseselectie;
}

function selectSchool(school){
	var count = 0;
	var schoolselectie= new Array();
	for (i=0;i<deelnemers.length;i++){
		if (deelnemers[i][3]==school){
			schoolselectie[count]=deelnemers[i];
			count++;
		}
	}
	return schoolselectie;
}

function sortSchool(a,b){
	return (a[0]-b[0]);
}
function sortKlasse(a,b){
	if((b[10+loc*6]==0) && (a[10+loc*6]==0)) {
		return (b[11+loc*6]-a[11+loc*6]);
	} else if (b[10+loc*6]==0) {
		return -1;
	} else if (a[10+loc*6]==0) { 
		return 1;
	} else { 
		if(b[11+loc*6]==a[11+loc*6]) {
			return (b[10+loc*6]-a[10+loc*6]);
		} else {
			return (b[11+loc*6]-a[11+loc*6]);
		}
	}
}
function writeDeelnemers(){
	var count=0;
	strhtml="";
	strhtml+='<table cellpadding="2" cellspacing="0" border="0" width="100%">';
	for (a=0;a<scholen.length;a++){
		selectie=selectSchool(a);
		selectie=selectie.sort(sortSchool);
		if (selectie.length>0){
			strhtml+="<tr><th colspan=7 class='uitslag'>" + scholen[selectie[0][3]] + "</th></tr>";
			strhtml+="<tr>";
			strhtml+="<th class='uitslag'>Rugnr</th>";
			strhtml+="<th class='uitslag'>Heer</th>";
			strhtml+="<th class='uitslag'>Dame</th>";
			strhtml+="<th class='uitslag'>Standaard</th>";
			strhtml+="<th class='uitslag'>Latin</th>";
			
						
			strhtml+="</tr>";
			count=0;
			for (i=0;i<selectie.length;i++){
				if (count==1){
					strhtml+="<tr class='lijst paarpopup' id='paar_"+selectie[i][0]+"'>";
					strhtml+="<td class='lijst'>" + selectie[i][0] + "</td>";
					strhtml+="<td class='lijst'>" + selectie[i][1] + "</td>";
					strhtml+="<td class='lijst'>" + selectie[i][2] + "</td>";
					strhtml+="<td class='lijst'>" + klassen[selectie[i][4]] + "</td>";
					strhtml+="<td class='lijst'>" + klassen[selectie[i][5]] + "</td>";
					count=0;
				}else{
					strhtml+="<tr id='paar_"+ selectie[i][0] +"' class='paarpopup'>";
					strhtml+="<td>" + selectie[i][0] + "</td>";
					strhtml+="<td>" + selectie[i][1] + "</td>";
					strhtml+="<td>" + selectie[i][2] + "</td>";
					strhtml+="<td>" + klassen[selectie[i][4]] + "</td>";
					strhtml+="<td>" + klassen[selectie[i][5]] + "</td>";
					count=1;
				}				
				strhtml+="</tr>";
			}
		}
	}
	strhtml+="</table>";
	document.write(strhtml);
	drawPopUp();
}

function drawPopUp(){
	for (a=0;a<scholen.length;a++){
		selectie=selectSchool(a);
		selectie=selectie.sort(sortSchool);
		if (selectie.length>0){
			count=0;
			for (i=0;i<selectie.length;i++){
				sContent='<div style="width: 450px; height: 100px;" class="paarpopup">';
				sContent+='<div style="width: 250px;height: 100px;float: left">';
				sContent+='<span style="font-size: 18px;">' + selectie[i][1] +'</span><br />';
				sContent+='<span style="font-size: 18px;">'+ selectie[i][2]+ '</span><br />';
				sContent+='<span style="font-size: 18px;">'+ scholen[selectie[i][3]]+ '</span>';
				
				sContent+='<div>';
				sContent+='<table class="boz_popup">';
				sContent+='<tr>';
				sContent+='<td>&nbsp;</td>';
				sContent+='<td style="padding: 2px 10px;" colspan="2">Standaard: <br />' + klassen[selectie[i][4]] + '</td>';
				sContent+='<td style="padding: 2px 10px;" colspan="2">Latin: <br />' + klassen[selectie[i][5]] + '</td>';
				sContent+='</tr>';
				sContent+='<tr>';
				sContent+='<td>&nbsp;</td>';
				sContent+='<td style="padding: 2px 10px;">Poule</td>';
				sContent+='<td style="padding: 2px 10px;">Plaats</td>';
				sContent+='<td style="padding: 2px 10px;">Poule</td>';
				sContent+='<td style="padding: 2px 10px;">Plaats</td>';
				sContent+='</tr>';
				dstart=0;
				if(seizoen=='2011_2012') {
					dstart=1;
				}
				for (d=dstart;d<data.length;d++){
					sContent+='<tr>';
					sContent+='<th style="padding: 2px;white-space: nowrap">'+ data[d] +'</th>';
					if (selectie[i][d+18] != '' ){ 
						a_standaard=selectie[i][d+18].split('-');
						sContent+='<td style="padding: 2px 10px;white-space: nowrap">'+ a_standaard[0] +'</td>';
						sContent+='<td style="padding: 2px 10px;white-space: nowrap">'+ a_standaard[1] +'</td>';
					} else {
						sContent+='<td colspan="2">&nbsp;</td>';
					}
					if (selectie[i][d+23] != '') {
						a_latin=selectie[i][d+23].split('-');
						sContent+='<td style="padding: 2px 10px;">'+ a_latin[0] +'</td>';
						sContent+='<td style="padding: 2px 10px;">'+ a_latin[1] +'</td>';
					} else {
						sContent+='<td colspan="2">&nbsp;</td>';
					}
					sContent+='</tr>';
				}
				sContent+='</table>';
				sContent+='</div>';
				sContent+='</div>';
				sContent+='<div style="width: 150px;height: 100px;float: right">';
				sContent+='	<img src="/js/uitslagen/boz' + seizoen + '/fotos/'+ selectie[i][0]+ '.' + imagetype + '" alt="'+ selectie[i][1] + ' en ' + selectie[i][2] + '" style="height:100px; width: 150px;float: right"/>';
				sContent+='</div>';
				sContent+='</div>';
			
			
				jQuery('#paar_'+selectie[i][0]).qtip({
					content: {
						text: sContent,
						title: {
							text: selectie[i][0] + ': ' + selectie[i][1] + ' en ' + selectie[i][2] + ' <br /> '+scholen[selectie[i][3]],
							button: false
						}
					},
					position: {
						target: 'mouse',
						adjust: { screen: true }
					},
					style: {
						width: 475,
						height: 200,
						title: {
							'color': '#fff',
							'font-weight': 'bold',
							'background': '#71489B'
						}
					}
				});
			}
		}
	}
}

function writeKlasse(stijl){
	var count=0;
	strhtml="";
	strhtml+='<table cellpadding="2" cellspacing="0" border="0" width="100%">';
	for (a=0;a<klassen.length;a++){
		if (a!=0){
			selectie=selectKlasse(a,stijl);
			selectie=selectie.sort(sortKlasse);
			if (selectie.length>0){
				i_colspan=9;
				if(seizoen=='2011_2012') {
					i_colspan=8;
				}
				strhtml+='<tr><th colspan="'+i_colspan+'" class="uitslag">' + klassen[selectie[0][4+stijl*1]] + ' ' + stijlen[stijl] + '</th></tr>';
				strhtml+="<tr><th class='uitslag'>Plaats</th>";
				strhtml+="<th class='uitslag'>Rugnr.</th>";
				i_start=0
				if(seizoen=='2011_2012') {
					i_start=1
				}
				for (i=i_start;i<data.length;i++){
					strhtml+="<th class='uitslag' style='font-size: 11px;'>"+ data[i] +"</th>";
				}
				strhtml+="<th class='uitslag'>Totaal</th>";
				strhtml+="<th class='uitslag'>School</th>";
				strhtml+="</tr>";
				count=0;
				for (i=0;i<selectie.length;i++){
					if (count==1){
						strhtml+="<tr class='lijst' id='paar_"+selectie[i][0]+"'>";
						strhtml+="<td class='lijst'>" + (i+1) + "</td>";
						strhtml+="<td class='lijst'>"+ selectie[i][0]+"</td>";
						if(seizoen!='2011_2012') {
							strhtml+="<td class='lijst'>" + selectie[i][6+stijl*6] + "</td>";
						}						
						strhtml+="<td class='lijst'>" + selectie[i][7+stijl*6] + "</td>";
						strhtml+="<td class='lijst'>" + selectie[i][8+stijl*6] + "</td>";
						strhtml+="<td class='lijst'>" + selectie[i][9+stijl*6] + "</td>";
						strhtml+="<td class='lijst'>" + selectie[i][10+stijl*6] + "</td>";
						strhtml+="<td class='lijst'>" + selectie[i][11+stijl*6] + "</td>";
						strhtml+='<td style="white-space: nowrap" class="lijst">' + scholen[selectie[i][3]] + "</td>";
						count=0;
					}else{
						strhtml+="<tr id='paar_"+selectie[i][0]+"'>";
						strhtml+="<td>" + (i+1) + "</td>";
						strhtml+="<td>"+ selectie[i][0]+"</td>";
						if(seizoen!='2011_2012') {
							strhtml+="<td>" + selectie[i][6+stijl*6] + "</td>";
						}
						strhtml+="<td>" + selectie[i][7+stijl*6] + "</td>";
						strhtml+="<td>" + selectie[i][8+stijl*6] + "</td>";
						strhtml+="<td>" + selectie[i][9+stijl*6] + "</td>";
						strhtml+="<td>" + selectie[i][10+stijl*6] + "</td>";
						strhtml+="<td>" + selectie[i][11+stijl*6] + "</td>";
						strhtml+='<td style="white-space: nowrap">' + scholen[selectie[i][3]] + "</td>";
						count=1;
					}	
					strhtml+="</tr>";
				}
			}
		}
	}
	strhtml+="</table>";
	document.write(strhtml);
	drawPopUp();
}

function writeChoices(keuze) {
	strhtml=""
	strhtml+="<form name=keuzeform>\n";
	strhtml+="<select name='keuze' onChange='openDoc();'>\n";
	for (i=0;i<stijlen.length;i++){
		strhtml+="<option value='" + i + "' ";
		if(keuze==i){
			strhtml+="selected";
		}
		strhtml+=">Tussenstand " + stijlen[i] + "</option>\n";
	}
	strhtml+="<option value='2' ";
	if(keuze=="2"){
		strhtml+="selected";
	}

	strhtml+=">Paren op Dansschool</option>\n";

	strhtml+="</select>\n";
	strhtml+="</form>\n";
	document.write(strhtml);
}
function openDoc(){
	waarheen=self.document.keuzeform.keuze.value;
	self.location=self.location.pathname + "?loc=" + waarheen 
}
