Event.observe(window,'load',function(){
	$('searchform').hide()
	categorylist=null;
	new Ajax.Request('/ajax.php?r=categorylist',
	{
		method:'get',
		onSuccess: function(transport)
		{
			error=0
			if(transport.responseJSON)
			{
				var str='<table>';
				var maxcell=3;
				var newrow=maxcell;
				categorylist=transport.responseJSON;
				$H(transport.responseJSON).each(function(c)
				{
					if(newrow==maxcell){str+='<tr>';}
					str+='<td><input type="checkbox" class="checkboxpadding" name="c['+c.key+']" id="c'+c.key+'"'+(c.value[1]=='1'?' class="defaultDisabled" disabled="disabled"':'')+'/>'+c.value[0]+'&nbsp;&nbsp;&nbsp;</td>\n';
					if(newrow==0){newrow=maxcell+1;str+'</tr>';}
					newrow--;
				});
				str+='</table>'
				$('categories').innerHTML=str;

				new Ajax.Request('/ajax.php?r=findme.racerinfo',
				{
					method:'get',
					onSuccess: function(transport)
					{
						if(transport.responseJSON)
						{
							if(transport.responseJSON.findme=='1')
							{
								transport.responseJSON.findmecategs.split(',').each(function(c2)
								{
									$('c'+c2).checked=true;
								});
							}
						}
						else
						{
							alert('Error downloading racerinfo')
						}
					},
					onFailure: function(){ alert('Error downloading racerinfo') }
				});
			}
			else{error=1}
			if(error==1){alert('Error downloading categorylist')}
		},
		onFailure: function(){ alert('Error downloading categorylist') }
	});

	/*
	$('searchform').observe('submit',function(e)
	{
		e.stop();
	*/
		$('searchform').request(
		{
			onComplete: function(transport)
			{
				if(transport.responseJSON.length==0){jQuery('#noresults').show();}else{jQuery('#noresults').hide();}
				transport.responseJSON.each(function(r){
					var tmp=new Template(unescape($('resultContact').innerHTML));
					var rep={teamid: r['team_id']};
					r['contact']=tmp.evaluate(rep);
					var tmp=new Template(unescape($('resultName').innerHTML));
					var rep={teamid: r['team_id'], name: r['name']};
					r['name']=tmp.evaluate(rep);
					var tmp2=new Template(unescape($('resultInfo').innerHTML));
					if(r.hasinfo=='1'){r['about-me']=tmp2.evaluate(rep);}
					else{r['about-me']='';}
					delete r.hasinfo;
					delete r.team_id;
				});
				new TableOrderer('searchresults',{data:transport.responseJSON,unsortedColumn : ['name','city','country','captain','contact','about-me'], columnNames : ['Team name','City','Country','Captain','Contact','Info']});
				$$('.resultInfoContact').each(function(resultInfoContact){resultInfoContact.observe('click',function(e)
				{
					e.stop();

					Lightview.show({
						href: this.href,
						rel: 'iframe',
						options: {
							width:520,
							height:480,
							topclose: false,
						}
					});
					/*
					new UI.Window({
						theme:  "mac_os_x",
						shadow: true,
						width:  600,
						height: 500}).center().setContent('<iframe src="'+this.href+'" border="0" frameborder="0" width="550" height="450">.</iframe>').show(true).activate();*/
				})});


				jQuery('.resultInfoLink').click(function(){
					var team_id=this.id.split('_')[1];
					if(!$('resultInfoE_'+team_id))
					{

						this.parentNode.parentNode.insert({after:'<tr><td colspan="8" class="resultInfoCell"><div id="resultInfoE_'+team_id+'" class="resultInfoE"><div class="helpbox-close"><a id="resultInfoEClose_'+team_id+'" class="resultInfoEClose">X</a></div><div class="categinfo"> </div><br/><span>...</span></div></td></tr>'});

						new Ajax.Request('/ajax.php?r=findme.teaminfo&team_id='+team_id,
						{
							method:'get',
							onSuccess: function(transport)
							{
								if(transport.responseJSON)
								{
									$$('#resultInfoE_'+team_id+' div.categinfo')[0].innerHTML='Categories: ';
									ca=transport.responseJSON.findmecategs.split(',');
									ca.each(function(e,i)
									{
										$$('#resultInfoE_'+team_id+' div.categinfo')[0].innerHTML+=categorylist[e][0]+' '+(ca.length-1!=i?', ':'');
									});
									$$('#resultInfoE_'+team_id+' span')[0].innerHTML=transport.responseJSON.findmetext.escapeHTML().gsub('\n','<br/>')+'<br/>';
								}
								else
								{
									alert('Error downloading teaminfo')
								}
							},
							onFailure: function(){ alert('Error downloading teaminfo') }
						});

						$('resultInfoEClose_'+team_id).observe('click',function(e){
							var team_id=this.id.split('_')[1];
							$('resultInfoE_'+team_id).hide();
						});
					}
					else
					{
						jQuery('#resultInfoE_'+team_id).toggle();
					}
				})

				var hashtid=location.hash.match(/\d+/)[0]
				if(hashtid>0)
				{
					$$('a[name=#'+hashtid+']')[0].scrollTo()
					jQuery('#resultInfo_'+hashtid).click()
				}

			},
			onFailure: function(){ alert('Error downloading search results') }
		});
	/*});*/

	$('selectall').observe('click',function(e)
	{
		$$('#categories input').each(function(e){e.checked=true})
	});

	$('selectnone').observe('click',function(e)
	{
		$$('#categories input').each(function(e){e.checked=false})
	});
});
