


var Cb0;
var Cb1;
var Cb2;
var SPb;

$(function(){
	Cb0 = new CCbox('v0', 'qcheckbox', 21);
	Cb1 = new CCbox('v1', 'qcheckbox', 21);
	Cb2 = new CCbox('v2', 'qcheckbox', 21);
	SPb = customcontrols['ansmode'];
	if (SPb){
		SPb.setHandler('Spiel.modeChanged()');
	}
})

function PrStat(){
	this.falschpnkt = 0;
	this.falschcnt = 0;
	this.skippedpnkt = 0;
	this.skippedcnt = 0;
	this.rightcnt = 0;
	this.totalcnt = 0;
	this.totalpnkt = 0;
	this.toHtml = function(){
		$('#falschrow .col2').html(RB_Lang.FRAGEN('<b>'+this.falschcnt+'</b>')+'');
		$('#falschrow .col3').html(RB_Lang.FEHLERCNT('<b class="red">'+this.falschpnkt+'</b>')+'');
		$('#skippedrow .col2').html(RB_Lang.FRAGEN('<b>'+this.skippedcnt+'</b>')+'');
		$('#skippedrow .col3').html(RB_Lang.FEHLERCNT('<b class="red">'+this.skippedpnkt+'</b>')+'');
		
		$('#rightcnt').html(RB_Lang.FRAGEN('<b class="green">'+this.rightcnt+'</b>')+'');
		$('#totalrow .col2').html(RB_Lang.FRAGEN('<b class="blue">'+this.totalcnt+'</b><font class="blue">')+'</font>');
		$('#totalrow .col3').html(RB_Lang.FEHLERCNT('<b class="red">'+this.totalpnkt+'</b><font class="blue">')+'</font>');
		if (this.totalpnkt>10){
			$('.punkte-yes').attr('class', 'punkte-no');
			$('#preovercont .question-h h2').html('<b class="red">'+RB_Lang.TSTNOTOK+'</b>');
			$('#preovercont .question-h h1').html('<b class="red">'+RB_Lang.TSTNOTOK2+'</b>');
		}
	}
}

function SpielFunc(gameId, uid, pass, totalQ, gcl, uansmode){
	this.gameId = gameId;
	this.uid = uid;
	this.pass = pass;
	this.current = 0;
	this.total = totalQ;
	this.over = 0;
	this.progress = null;
	this.tickets = null;
	this.url = null;
	this.qstart = 0;
	this.marks = '';
	this.ansmode = uansmode;
	this.browsing = false;
	this.cl = gcl;
	this.prstat = new PrStat();
	this.endscreen = false;
	
	
	this.getNextQ = function(){
		if (!this.progress) this.progress='';
		this.progress = this.progress.strrpad(this.total, '0');
		i = this.current;
		next = -1;
		while(i<this.progress.length){
			num = this.progress.charCodeAt(i)-48;num = num%2;
			if (num==0){next = i; break;}i++;
		}
		if (next==-1){
			i = 0;
			while(i<this.progress.length){
				num = this.progress.charCodeAt(i)-48;num = num%2;
				if (num==0){next = i; break;}i++;
			}
			
		}
		return next;
	}
	
	this.setStat = function(stat){
		
		var ub = new JSGameUsr(stat);
		this.score = ub.getScore();
		this.progress = ub.getStat();
		this.curans = ub.getCurans();
		this.current = ub.getCurq();
		
		this.qstart = ub.getCurstart()*1000;
		if (this.qstart==0) this.qstart = (new Date().getTime());
		//alert(new Date().getTime());
		
		//now getNextQ should return current position
		if (this.getNextQ()<0){
			this.over = true;
			this.browsing = true;
		}else{
			this.over = false;
			
		}
		this.marks = ub.getMarks();
		
		this.updateProgress();
		this.showTime();
		if (this.current==2000){
			$('#fsiq').show();
			Spiel.realEnd(false);
		}
	}
	
	this.setBrowsing = function(mode){
		this.browsing = mode;
	}
	
	this.isBrowsing = function(mode){
		return this.browsing;
	}
	this.dump = function(){
		this.progress = this.progress.strrpad(this.total, '0');
		var j = 0;
		log (this.progress);
		while(j<this.progress.length){
			var num = this.progress.charCodeAt(j)-48;
			var answered = (num%2==0)?false:true;
			var marked = this.isMarked(j)?'-m':'';
			var correct = (answered)?(((num%4-num%2)>0)?'-green':'-red'):'';
			var pnkt = parseInt((''+Spiel.tickets[j][6]).substring(4,5));
			var txt = (this.over)?''+pnkt:'';
			log(j+' '+num+' '+answered+' '+correct);
			j++;
		}
		return false;
	}
	this.updateProgress = function(){
		this.progress = this.progress.strrpad(this.total, '0');
		var j = 0;
		prline = '';
		
		var falschpnkt = 0;
		var falschcnt = 0;
		var skippedpnkt = 0;
		var skippedcnt = 0;
		var rightcnt = 0;
		var totalcnt = 0;
		var totalpnkt = 0;
		
		while(j<this.progress.length){
			var num = this.progress.charCodeAt(j)-48;
			var answered = (num%2==0)?false:true;
			var marked = this.isMarked(j)?'-m':'';
			var correct = (answered)?(((num%4-num%2)>0)?'-green':'-red'):'';
			var pnkt = parseInt((''+Spiel.tickets[j][6]).substring(4,5));
			var txt = (this.over)?''+pnkt:'';
			
			if (answered){
				if ((num%4-num%2)>0){
					rightcnt++;
				}else{
					falschcnt++;falschpnkt+=pnkt;totalpnkt+=pnkt;
				}
			}else{
				skippedcnt++;skippedpnkt+=pnkt;totalpnkt+=pnkt;
			}
			if (this.ansmode){
				if (this.current == j){
					prline += '<span class="q-nr'+correct+marked+'"></span>';
				}else{
					prline += '<a href="#" onclick="javascript: return Spiel.show('+j+')" class="q-nr'+correct+marked+'">'+txt+'</a>';
				}
			}else{
				correct = answered?'-blue':'';
				if (this.current == j){
					prline += '<span href="#" class="q-nr'+correct+marked+'"></span>';
				}else{
					prline += '<a href="#" onclick="javascript: return Spiel.show('+j+')" class="q-nr'+correct+marked+'">'+txt+'</a>';
				}
			}
			
			j = j+1;
			
		}
		Spiel.prstat.totalcnt = Spiel.total;
		Spiel.prstat.falschpnkt = falschpnkt;
		Spiel.prstat.falschcnt = falschcnt;
		Spiel.prstat.skippedpnkt = skippedpnkt;
		Spiel.prstat.skippedcnt = skippedcnt;
		Spiel.prstat.rightcnt = rightcnt;
		Spiel.prstat.totalpnkt = totalpnkt;
		
		$('#cuprogress').html(prline);
		return prline;
	}
	
	this.isAnswered = function(index){
		num = this.progress.charCodeAt(index)-48;
		return (num%2==0)?false:true;
	}
	
	this.show = function(index){
		if (Spiel.endscreen){
			$('#preovercont').hide();
			$('#ticketcont').show();
		}
		num = this.progress.charCodeAt(index)-48;
		answered = (num%2==0)?false:true;
		url = '/fa/de/ghandler-ansskip/'+this.uid+'/'+this.gameId+'/'+this.pass+'/'+(this.current-1)+'/'+index;
		$.ajax({ url: url, async: true});
		this.showQuestion(index);		
		return false;
	}
	
	this.end = function(){
		$('#ticketcont').hide('fast', function(){
			$('#ticketcont').hide();//chrome bug
			Spiel.prstat.toHtml();
			$('#preovercont').show();
		});
		
		Spiel.endscreen = true;
//		url = '/fa/de/ghandler-leave/'+this.uid+'/'+this.gameId+'/'+this.pass;
//		$.ajax({ url: url, async: true});
//		document.location = '/fuehrerschein-lernsystem/';
		return false;
	}
	
	this.continueGame = function(){
		$('#preovercont').hide('fast', function(){
			$('#ticketcont').show();
		});
		return false;
	}
	
	this.realEnd = function(ui){
		Spiel.over = true;
		Spiel.endscreen = true;
		Spiel.ansmode = 1;
		$('#ticketcont').hide('fast', function(){
			$('#ticketcont').hide();//chrome bug
			$('#gamendbtn').html('Zur Übersicht');
			$('#notfinished').hide();
			Spiel.current = 1000;
			progress = Spiel.updateProgress();
			$('#gamendprogress').html(progress);
			if (Spiel.prstat.falschcnt==0){
				$('#firsterror').hide();
				$('#checkblock').html('');
				
			}else{
				$('#checkblock').html('<a href="#" onclick="javascript:return Spiel.nextError()" class="bigbtn bigbtn-red">'+RB_Lang.NEXTERROR+'</a>');
			}
			Spiel.prstat.toHtml();
			$('#checkblock').attr('class', 'exitbtn');
			//add iq display
			$('#preovercont').show();
			
			$('#gamendtoolbar').show();
			
		});
		url = '/fa/de/ghandler-gamedone/'+Spiel.uid+'/'+Spiel.gameId+'/'+Spiel.pass;
		$.ajax({ url: url, async: true, success:Spiel.gameRes});
	
		return false;
	}
	
	this.gameRes = function(data){
		$('#fsiq').html('<h2>'+RB_Lang.TSTFSIQ+': <b class="green">+'+data+'</b></h2>');
		$('#fsiq').show();
	}
	
	
	this.setTickets = function (tickets){this.tickets = tickets;}
	
	this.answer = function(){
		astr = '';
		astr+= (Cb0.isChecked())?'1':'0';
		astr+= (Cb1.isChecked())?'1':'0';
		astr+= (Cb2.isChecked())?'1':'0';
		line = this.tickets[this.current][6];
		corLine = line.substring(0, 3);
		add = (astr==corLine)? '1':'0';
		
		this.setAnswer(astr, (astr==corLine));
		this.updateProgress();
		next = this.getNextQ();

		url = '/fa/de/ghandler-ansdone/'+this.uid+'/'+this.gameId+'/'+this.pass+'/'+add+'/'+astr+'/'+this.current+'/'+next;
		$.ajax({ url: url, async: true, success:function(data){
			
		}}); 
		if (!this.ansmode){
			if (astr!=corLine){
				//log('неправильно '+this.current);
			}else{
				//log('правильно '+this.current);
			}
		}else{
			this.showAnswer();
			return false;
		}
		
		if (next<0){
			Spiel.realEnd(true);
		}else{
			this.showQuestion(next);
		}
		return false;
	}
	
	this.nextQ = function(){
		next = this.getNextQ();
		if (next<0){
			Spiel.realEnd(true);
		}else{
			this.showQuestion(next);
		}
		return false;
	}
	
	this.nextB = function(){
		next = Spiel.current+1;
		if (next==Spiel.total){
			next = 0;
		}
		this.showQuestion(next);
		return false;
	}
	
	this.prevB = function(){
		next = Spiel.current-1;
		if (next<0){
			next = Spiel.total;
		}
		this.showQuestion(next);
		return false;
	}
	
	this.firstError = function(){
		var j = 0;
		while(j<this.progress.length){
			num = this.progress.charCodeAt(j)-48;
			answered = (num%2==0)?false:true;
			error = (answered)?(((num%4-num%2)>0)?false:true):false;
			if (error){
				Spiel.show(j);
				break;
			}
			j = j+1;
		}
		return false;
	}
	
	this.nextError = function(){
		var j = Spiel.current+1;
		
		while(j<this.progress.length){
			num = this.progress.charCodeAt(j)-48;
			answered = (num%2==0)?false:true;
			error = (answered)?(((num%4-num%2)>0)?false:true):false;
			if (error){
				Spiel.show(j);
				break;
			}
			j = j+1;
		}
		if (j==Spiel.total) Spiel.firstError();
		return false;
	}
	this.asyncShowAnswer = function(){
		$('#questionblock').slideUp('fast', function(){
			Spiel.asyncShowAnser();
			$('#questionblock').slideDown('fast');
		});
	}
	
	this.showAnswer = function(){
		var line = Spiel.tickets[Spiel.current][6];
		var corLine = line.substring(0, 3);
		var ver0 = parseInt(corLine.substring(0,1))+1;
		var ver1 = parseInt(corLine.substring(1,2))+1;	
		var ver2 = parseInt(corLine.substring(2,3))+1;
		$('#v0r').css('background-position', '0 -'+(ver0*21)+'px');
		$('#v1r').css('background-position', '0 -'+(ver1*21)+'px');
		$('#v2r').css('background-position', '0 -'+(ver2*21)+'px');
		var astr = '';
		astr+= (Cb0.isChecked())?'1':'0';
		astr+= (Cb1.isChecked())?'1':'0';
		astr+= (Cb2.isChecked())?'1':'0';
		
		Cb0.hide();Cb1.hide();Cb2.hide();
		$('#v0u').css('background-position', '0 -'+Spiel.uansBGPos(corLine, astr, 0)+'px');
		$('#v1u').css('background-position', '0 -'+Spiel.uansBGPos(corLine, astr, 1)+'px');
		$('#v2u').css('background-position', '0 -'+Spiel.uansBGPos(corLine, astr, 2)+'px');
		$('#v0u').show();$('#v1u').show();$('#v2u').show();
		$('#qtoolwork').hide();
		if (Spiel.isBrowsing()){
			$('#qtoolshowans').hide();  $('#qtoolbrowse').show();
		}else{
			$('#qtoolshowans').show();  $('#qtoolbrowse').hide();
		}		
	}
	
	this.uansBGPos = function(corLine, uline, pos){
		cor = parseInt(corLine.substring(pos,pos+1));
		uans =  parseInt(uline.substring(pos,pos+1));
		pos = (cor==uans)?((cor==0)?63:84):((cor==0)?21:42);
		return pos;
	}	
	
	this.setAnswer = function(flags, correct){
		correct = (correct)?'1':'0';
		binform = flags+correct+'1';
		
		code = parseInt(binform, 2);
		code +=48;
		
		if (this.progress.length>this.current){
			this.progress = this.progress.substring(0, this.current)+String.fromCharCode(code)+this.progress.substring(this.current+1);
		}else{
			this.progress = this.progress.substring(0, this.current)+String.fromCharCode(code);
		}
	}
	
	this.skip = function(){
		this.current++;
		next = this.getNextQ();
		url = '/fa/de/ghandler-ansskip/'+this.uid+'/'+this.gameId+'/'+this.pass+'/'+(this.current-1)+'/'+next;
		$.ajax({ url: url, async: true});
		if (next<0){Spiel.realEnd(true);}else{this.showQuestion(next);}
		return false;
	}
	this.cleanCorrect=function(){
		$('#v0r').css('background-position', '0 0');
		$('#v1r').css('background-position', '0 0');
		$('#v2r').css('background-position', '0 0');
		$('#v0u').css('background-position', '0 0');
		$('#v1u').css('background-position', '0 0');
		$('#v2u').css('background-position', '0 0');
		Cb0.uncheck();Cb0.show();
		Cb1.uncheck();Cb1.show();
		Cb2.uncheck();Cb2.show();
	}
	
	this.showQuestion = function(next){
		$('#questionblock').hide('slide', {direction: 'left'}, function(){
			$('#qimg').attr('src', '/img/spinner.gif');
			Spiel.cleanCorrect();
			$('#qtoolshowans').hide(); $('#qtoolwork').show(); $('#qtoolbrowse').hide();
			var nt = Spiel.tickets[next];
			if (!nt) return;
			
			var txt = nt[1].nl2br();
			var cmntext = nt[2];
			var var0 = nt[3].nl2br();;
			var var1 = nt[4].nl2br();
			var var2 = nt[5].nl2br();
			
			$('#question').html(txt);
			$('#qnum').html('Frage '+(next+1)+' von '+(Spiel.total));
			$('#questionv0').html(var0);
			$('#questionv1').html(var1);
			$('#questionv2').html(var2);
			var grund = 'Fehlerpunkte: ';
			if ((''+nt[6]).substring(5,6)=='0'){
				$('#tclass').attr('class', 'klasse-13');
			}else{
				$('#tclass').attr('class', 'klasse-'+Spiel.cl);
			}
			var topic = grund+(''+nt[6]).substring(4,5);
			$('#qtopic').html(topic);
			if (var2==''){
				$('#ans2div').hide();
				Cb2.hide();
			}else{
				$('#ans2div').show();
				Cb2.show();
			}
			var haspic = nt[6];
			haspic = (''+haspic).substring(3, 4);
			
			if (cmntext==''){
				$('#cmntxt').hide();
			}else{
				$('#cmntxt').html(cmntext);
				$('#cmntxt').show();
			}
			
			if (haspic=='1'){
				$('#qimg').attr('src', '/img/tpc/'+nt[7]+'.jpg');
				$('#qimgdiv').show();
				
			}else{
				$('#qimgdiv').hide();
			}
			Spiel.current = next;
			if (Spiel.curMarked()){
				$('#markButton').html(RB_Lang.UNMARK+'');
			}else{
				$('#markButton').html(RB_Lang.MARKIEREN+'');
			}
			url = '/fa/de/ghandler-ansstart/'+Spiel.uid+'/'+Spiel.gameId+'/'+Spiel.pass;
			$.ajax({ url: url, async: true}); 
			Spiel.qstart = new Date().getTime();
			UsrList.setProgress(Spiel.uid, Spiel.progress);
			Spiel.updateProgress();
			if (Spiel.isAnswered(next)){
				Spiel.showAnswer();
			}
			if (Spiel.isAnswered(next) || Spiel.over){
				$('#qtoolbrowse').show();$('#qtoolwork').hide();$('#qtoolshowans').hide();
			}else{
				$('#qtoolbrowse').hide();$('#qtoolwork').show();$('#qtoolshowans').hide();
			}
			
			$('#questionblock').show('slide', {direction: 'right'});
		});
	}

	this.boxChecked = function(){
		str = '';
		str+= (Cb0.isChecked())?'1':'0';
		str+= (Cb1.isChecked())?'1':'0';
		str+= (Cb2.isChecked())?'1':'0';
		url = '/fa/de/ghandler-ans/'+uid+'/'+gameId+'/'+pass+'/'+str;
		$.ajax({ url: url, async: true });
	}
	
	this.mark = function(){
		str = String.fromCharCode(48+this.current);
		if (Spiel.curMarked()){
			flag = 0;
			this.marks = this.marks.replace(str, '');
			$('#markButton').html(RB_Lang.MARKIEREN+'');
			$('#markButton2').html(RB_Lang.MARKIEREN+'');
		}else{
			flag = 1;
			this.marks +=str;
			$('#markButton').html(RB_Lang.UNMARK+'');
			$('#markButton2').html(RB_Lang.UNMARK+'');
		}
		url = '/fa/de/ghandler-mark/'+uid+'/'+gameId+'/'+pass+'/'+this.current+'/'+flag;
		$.ajax({ url: url, async: true });
		this.updateProgress();
		block = UsrList.getBlock(this.uid);
		block.setMarks(this.marks);
		UsrList.fillUsrBlock(this.uid);
		return false;
		
	}
	this.isMarked = function(pos){
		for (i=0; i<this.marks.length; i++){
			if ((this.marks.charCodeAt(i)-48)==pos) return true;
		}
		return false;
	}
	this.curMarked = function(){
		return this.isMarked(this.current);
	}
	
	this.modeChanged = function(){
		this.ansmode = ($('#ansmode').attr("checked"));
		$('#wtbtn').html(this.ansmode?RB_Lang.PRUFEN+'':RB_Lang.WEITER+'');
		$('#wtbtn').attr("class", this.ansmode?'bigbtn bigbtn-yellow':'bigbtn bigbtn-green');
		this.updateProgress();
	}
	
	this.showTime = function(){
		
		var secs = ((new Date().getTime())-this.qstart)/1000;
		secs = Math.round(secs);
		if (secs<0) secs = 0;
		var min = (secs - secs%60)/60;
		secs = secs%60;
		if (min==0){
			
			time = (''+secs).strlpad(2,'0');
			if (secs>40){
				$('#qtime').attr('class', 'timered');
			}else{
				$('#qtime').attr('class', 'time');
			}
		}else{
			if (min>59) min='..';
			time = min+':'+ (''+secs).strlpad(2,'0');
			$('#qtime').attr('class', 'timered');
		}
		$('#qtime').html('');
		$('#qtime').html(''+time);
		return 1;
	}
}





function GameUsrList(){
	var users = new Array();
	var tmpUsers = new Array();
	var usrBlock = new Array();
	var selectedUsr = 0;
	var listMode = 0;
	
	this.getBlock = function(uid){
		return users[uid];
	}
	this.setListMode = function(mode){
		listMode = mode;
	}
	this.updateScrolls = function(){
		if (listMode==0){
			$('#usrlist').jScrollPane({showArrows:true, scrollbarWidth: 13, arrowSize: 13});
			$('#chatlist').jScrollPane({showArrows:true, scrollbarWidth: 13, arrowSize: 13});
		}else{
			var api = $("#participants").scrollable({api:true}); 
			log(api);
			api.reload();
		}
		
	}
	
	this.setProgress = function(uid, progress){
		u = users[uid];
		if (u){
			u.setStat(progress);
			UsrList.fillUsrBlock(uid);
		}
	}
	
	this.startUpdate = function(){
		tmpUsers = new Array();
	}
	
	this.endUpdate = function(){
		toAdd = new Array();
		toUpdate = new Array();
		for(uid in tmpUsers){
			if (!(uid in users)){
				toAdd.push(uid);
			}else{
				toUpdate.push(uid);
			}
		}	
		
		for(uid in users){
			if (!(uid in tmpUsers)){
				this.dropUser(uid);
			}
		}
		UsrUiCache.prepCache(toAdd.slice());
		for(i in toUpdate){this.updateUser(tmpUsers[toUpdate[i]]);}
		for(i in toAdd){this.addUser(tmpUsers[toAdd[i]]);}
		this.sort();
	}
	
	this.sort = function(){
		
		if ($('#finish span:first-child').attr('name')!='1'){
			var elem = $('#usrlist span[name="1"]');
			$('#finish').append(elem);
		}
		var index=2;
		while(true){
			var sbnxt = $('#usrlist span[name="'+(index)+'"]:first');
			if (sbnxt.size()==0){
				return;	
			}
			var prev = $('#usrlist span[name="'+(index-1)+'"]');
			var prevnext = prev.next(':first');
			var posNext = parseInt(prevnext.attr('name'));
			if (posNext!=index){
				prev.after(sbnxt);
			}
			index++;
		}	
	}
	
	this.addUser = function(ub){
		
		users[ub.getId()] = ub;
		uid = ub.getId();
		html = UiHelp.getGameUsrHtml(uid, true);
		$('#usrlist').append(html);
		this.updateScrolls();
		this.fillUsrBlock(ub.getId());		
	}
	
	this.updateUser = function(ub){
		users[ub.getId()] = ub;

		
		this.fillUsrBlock(ub.getId());
	}
	
	this.selectUser = function(uid){
		if (selectedUsr>0){
			oldSel = selectedUsr;
			selectedUsr = 0;
			$('#'+oldSel+'ulcont').html(UiHelp.getGameUsrHtml(oldSel, false));
			UsrList.fillUsrBlock(oldSel);
		}
		$('#'+uid+'ulcont').html(UiHelp.getExtGameUsrHtml(uid, false));
		UsrList.fillExtUsrBlock(uid);
		selectedUsr = uid;
		this.updateScrolls();
		
		return false;
	}
	
	this.fillExtUsrBlock = function(uid){
		ustat = users[uid];
		ui = UsrUiCache.getCache(uid);
		
		if (ui){}else{ return false;}
		var sel = '#'+uid+'ulcont';
		var nickname = (ui.getNickname()=='')?'Gast':ui.getNickname();
		
		$(sel+' h1').html('<img src="/img/key.png" class="'+ui.getSexCl()+'"/>'+nickname);
		if (ui.getSex()==2){
			$(sel+' .useravatar img').attr('src', '/img/usr/gast/avatar.jpg');
			$(sel+' a').attr('href', '#');
			$(sel+' a').removeAttr('onclick');
			$(sel+' a').removeAttr('target');
		}else{
			$(sel+' .useravatar img').attr('src', '/img/usr/'+ui.getId()+'/avatar.jpg?rev='+ui.getArev());
		}
		$(sel).attr('name', ustat.getPos());
		$(sel+' .platz').html(ustat.getPos());
		if (ui.getSex()==2){
			$(sel+' .toolbox').hide();
			$(sel+' span').hide();
		}
		//alert($(sel+' span').html());
		if (listMode==0){
			if ($(sel).attr('name')==0){
				$(sel+' span').html('<h3>route:</h3><h4>ca. '+ui.getDist()+'km von dir</h4>');
			}else{
				var seit = ustat.getSeit();
				if (seit>0){
					seit = ' (seit '+seit+'min)';
				}else{
					seit = '';
				}
				
				$(sel+' span').html('<h3>FS-IQ:</h3><h4>'+ustat.getScore()+seit+'</h4>');
				$(sel+' span#result').show();
			}
			
			$(sel+' .progress').show();
			//now calc the stat
			total = Spiel.total;
			line = ustat.getStat();
			line = line.strrpad(Spiel.total, '0');
			marks = ustat.getMarks();
			
			j=0;
			cnt = 0;
			ln = '';
			prline = '';
			
			while(j<line.length){
				num = line.charCodeAt(j)-48;j++;
				answered = (num%2==0)?false:true;
				marked = '';
				
				for (i=0; i<marks.length; i++){
					if ((marks.charCodeAt(i)-48)==j){marked='-m'; break;}
				}
				
				correct = (answered)?(((num%4-num%2)>0)?'ok':'no'):'skip';
				prline += '<a class="'+correct+marked+'"></a>';
			}
			
			$(sel+' .progress').html(prline);
			
			
			
		}else{
			
			$(sel+' #result').html('<h3>FS-IQ:<br/>alter:</h3><h4>'+ui.getIq()+'<br/>'+ui.getAge()+'</h4>');
			$(sel+' #info').html('<h3>route:</h3><h4>ca. '+ui.getDist()+'km von dir</h4>');
			$(sel+' #result').attr('id', 'result2');
			$(sel+' #info').attr('id', 'result2');
			$(sel+' .progress').remove();
		}
	
	}
	
	this.fillUsrBlock = function(uid){
		if (selectedUsr==uid){
			UsrList.fillExtUsrBlock(uid);
			return;
		}
		ustat = users[uid];
		ui = UsrUiCache.getCache(uid);
		if (ui){}else{ return false;}
		var sel = '#'+uid+'ulcont';
		var nickname = (ui.getNickname()=='')?'Gast':ui.getNickname();
		$(sel+' h1').html('<img src="/img/key.png" class="'+ui.getSexCl()+'"/>'+nickname);
		$(sel).attr('name', ustat.getPos());
		$(sel+' .platz').html(ustat.getPos());
		if (ui.getSex()==2){
			$(sel+' .useravatar img').attr('src', '/img/usr/gast/avatar.jpg');
		}else{
			$(sel+' .useravatar img').attr('src', '/img/usr/'+ui.getId()+'/avatar.jpg?rev='+ui.getArev());
		}
			
			//now calc the stat
			if (listMode==0){
				
				if ($(sel).attr('name')==0){
				}else{
					var seit = ustat.getSeit();
					if (seit>0){
						seit = ' (seit '+seit+'min)';
					}else{
						seit = '';
					}
					$(sel+' span').html('<h3>'+RB_Lang.TURFSIQ+':</h3><h4>'+ustat.getScore()+seit+'</h4>');
				}
				$(sel+' .progress').show();
				total = Spiel.total;
			
				line = ustat.getStat();
				i=0;
				cnt = 0;
				while(i<line.length){
					num = line.charCodeAt(i)-48;num = num%2;i++;
					if (num==1){cnt++}
				}
				pr = Math.abs(161*cnt/total);
				
				$(sel+' .progress').attr('style', 'width:'+pr+'px');
				
				
			}else{
				//alert('here');
				$(sel+' .progress-bg').remove();
				
				$(sel+' #result').html('<h3>route:</h3><h4>ca. '+ui.getDist()+'km von dir</h4>');
				$(sel+' #result').attr('id', 'info2');
//				$(sel+' #result').attr('id', 'info2');
			}
		
	}
	this.dropUser = function(uid){
		delete users[uid];
		$('#'+uid+'ulcont').remove();
		this.updateScrolls();
	}
	
	this.fetch = function(){
		url = '/fa/de/ghandler-usrstatlist/'+Spiel.uid+'/'+Spiel.gameId+'/'+Spiel.pass;
		$.ajax({ url: url, async: false, success:function(data){
		    UsrList.parseLine(data, false);
			
		} });
		return 1;
	}
	
	this.dirfetch = function(uid, gameId, pass){
		url = '/fa/de/ghandler-usrstatlist/'+uid+'/'+gameId+'/'+pass;
		$.ajax({ url: url, async: false, success:function(data){
		    UsrList.parseLine(data, false);
			
		} });
		return 1;
	}
	
	this.parseLine = function(data, silent){
		UsrList.startUpdate();
		var blocks = data.split('^^^\n');
		var chat = '';
		var data = blocks[0];
		if (blocks.length>1){
			chat = blocks[1];
		}
		lines = data.split('\n');
		var len = lines.length;
	    for (index in lines){
	    	if (lines[index]!=''){
	    		var ub = new JSGameUsr(lines[index]);
	    		tmpUsers[ub.getUid()]=ub;
	    	}
	     	
	    }
	    if (silent){
	     	users = tmpUsers;
	    }else{
	    	UsrList.endUpdate();
	    }
	    
	    if (chat!=''){
	    	Chat.addMessages(chat);
	    }
	    this.sort();
	    return 1;
	}
	
	this.updateAll = function(){
		//suppose that all users are in the cache
		for (i in users){
			UsrList.fillUsrBlock(i);
		}
	}

}

UsrList = new GameUsrList();

function startGame(tp){
	if (tp<3){
		$(function(){
			Ticker.addHandler('UsrList.fetch()', 5000, 1);
			Ticker.addHandler('Spiel.showTime()', 1000);
		});
	}else{
		$(function(){
			Ticker.addHandler('Spiel.showTime()', 1000);
		});
	}
	if (!Spiel.endscreen) Spiel.showQuestion(Spiel.current);
}










