var current_msg_id;
var starttime = microtime(true);

function microtime(get_as_float) {
    // *     example 1: timeStamp = microtime(true);
    // *     results 1: timeStamp > 1000000000 && timeStamp < 2000000000
 
    var now = new Date().getTime() / 1000;
    var s = parseInt(now);
 
    return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}
function nieuw_bericht()
{
	$('nieuw_bericht').style.display = '';
	$('tekst').focus();
}

function pos_paper()
{
	$('paper').setStyle('left', (window.getWidth()-1000)/2);
}
function pos_club_paper()
{
	$('paper').setStyle('height', window.getHeight()-62);
}
function reageer(id)
{
	new Request({url: url, 
		data: {
			ajax_function: 'get_form',
			parent_id: id
		},
		onSuccess: function(response){
			if(response == '') document.location = '/login';
			var el = $('forum_form_'+id);
			el.innerHTML = response;
			var fx = new Fx.Slide(el);
			fx.hide();
			fx.slideIn();
			
			$('form_'+id).addEvent('submit', function(e){
				new Event(e).stop();
				new Request({
					url: url,
					update: 'forum_form_'+id,
					onComplete: function(resp){
						if(resp.length <= 1){
							location.reload(true);
						}
					}
				}).post($('form_'+id));
			});
		}
	}).send();
}
function get_competities()
{
	$('comp_competities').set('html', '').addClass('loading');
	
	new Request.HTML({
		url: url,
		data: {
			'ajax_function': 'get_competities',
			'regio': $('comp_regio').get('value'),
			'competitie_type_id': $('comp_type_id').get('value')
		},
		update: 'comp_competities',
		onComplete: function(){
			$('comp_competities').removeClass('loading');
		}
	}).send();
}

window.addEvent('domready', function(){

	if($('comp_chooser') != null)
	{
		$$('#comp_chooser, #comp_chooser_options').addEvent('mouseover', function(){
			$('comp_chooser_options').setStyles({'display': ''});
		});
		$$('#comp_chooser, #comp_chooser_options').addEvent('mouseout', function(){
			$('comp_chooser_options').setStyles({'display': 'none'});
		});
		$$('#comp_chooser_options .type').addEvent('click', function(){
			$('comp_chooser_options').setStyles({'display': 'none'});
			$('comp_chooser').set('html', this.get('html'));
			$('comp_type_id').set('value', this.id.replace('type_', ''));
			get_competities();
		});
		$$('#comp_regios .regio').addEvent('click', function(){
			$$('#comp_regios .regio').removeClass('selected');
			this.addClass('selected');
			$('comp_regio').set('value', this.get('html'));
			get_competities();
		});
	}
	
	
	if($('top_email') != null)
	{
		$('top_email').addEvent('focus', function(){
			if(this.get('value') == 'e-mail') this.set('value', '');
		});
		$('top_ww').addEvent('focus', function(){
			this.set('value', '');
		});
	}
	
	$$('.training').each(function(el){
		el.addEvent('mouseenter', function(){
			this.addClass('hover');
		});
		el.addEvent('mouseleave', function(){
			this.removeClass('hover');
		});
	});
	
	if($('h2_eigen_mailbox') != null){
		$('h2_eigen_mailbox').setStyle('opacity', .5);
		$('live_contact').addEvent('mouseenter', function(){
			this.addClass('hover');
			$('h2_live_contact').setStyle('opacity', 1);
			$('h2_eigen_mailbox').setStyle('opacity', .5);
			$('div_live_contact').setStyle('display', '');
			$('div_eigen_mailbox').setStyle('display', 'none');
		});
		$('eigen_mailbox').addEvent('mouseenter', function(){
			this.addClass('hover');
			$('h2_live_contact').setStyle('opacity', .5);
			$('h2_eigen_mailbox').setStyle('opacity', 1);
			$('div_live_contact').setStyle('display', 'none');
			$('div_eigen_mailbox').setStyle('display', '');
		});
		$('live_contact').addEvent('mouseleave', function(){
			this.removeClass('hover');
		});
		$('eigen_mailbox').addEvent('mouseleave', function(){
			this.removeClass('hover');
		});
		var live = function(){
			if($('live_contact').hasClass('hover') || $('eigen_mailbox').hasClass('hover')) return;
			if($('eigen_mailbox').hasClass("current"))
			{
				$('live_contact').fireEvent('mouseenter').removeClass('hover').addClass('current');
				$('eigen_mailbox').removeClass('current');
			} else
			{
				$('eigen_mailbox').fireEvent('mouseenter').removeClass('hover').addClass('current');
				$('live_contact').removeClass('current');
			}
		}
		live.periodical(5000);
	}
	if($('head')!=null && $('submenu')!=null)
	{
		var fx = new Fx.Slide('submenu', {wait: false});
		fx.hide();
		
		$('submenu').setStyle('display', '');
		
		$('head').addEvent('mouseenter', function(e){
			fx.slideIn();
		});
		$('head').addEvent('mouseleave', function(e){
			fx.slideOut();
		});
		$('submenu').addEvent('mouseenter', function(e){
			fx.slideIn();
		});
		$('submenu').addEvent('mouseleave', function(e){
			fx.slideOut();
		});
		
		$$('#submenu_main li').each(function(el){
			el.addEvent('mouseenter', function(){
				this.addClass("hover");
			});
			el.addEvent('mouseleave', function(){
				this.removeClass("hover");
			});
			el.addEvent('click', function(){
				$$('#submenu_main li').each(function(li){
					li.removeClass('selected');
					$(li.id.replace('main', 'sub')).setStyle("display", "none");
				});
				this.addClass('selected');
				$(this.id.replace('main', 'sub')).setStyle("display", "inline");
			});
		});
	}

	if($('regios') != null)
	{
		$$('#regios li').each(function(el){
			el.addEvent('mouseenter', function(){
				this.addClass("hover");
			});
			el.addEvent('mouseleave', function(){
				this.removeClass("hover");
			});
			el.addEvent('click', function(e){
				$$('#regios li').each(function(li){
					li.removeClass('selected');
				});
				this.addClass('selected');
				
				//fx_klasses.start(0);
				new Request.HTML({url: url,
					method: 'post',
					data: {
						regio: this.id,
						menuFunction: 1,
						act: 'get_klasses'
					},
					update: 'klasses'
				}).send();
			});
		});
	}
	

		
	try{
		$('layer_bg').addEvent('click', close_layer);
		$('close').addEvent('click', close_layer);
	}catch(e){}
		
	$$('#menu li.root').each(function(el){
		el.addEvent('mouseenter', function(e){
			this.addClass('hover');
			$$('#menu li.root a').each(function(a){
				a.removeClass('selected');
			})
			this.getFirst().addClass('selected');
			
			var html;
			if($('ul_'+this.id.replace("li","")) != null)
			{
				html = "<ul>" + $('ul_'+this.id.replace("li","")).innerHTML + "<ul>";
			} else
			{
				html = "";
			}
			$('top_sub_menu').innerHTML = html;
			if(this.hasClass('selected'))
			{
				$$('#sub_menu li').each(function(el){
					el.addClass('selected');
				});
			}
			
		});
		
		el.addEvent('mouseleave', function(){
			this.removeClass('hover');
		});
	});
		
	$$('#info_tab .tab').each(function(el){
		el.addEvent('click', function(e){
			$$('#info_tab .tab').each(function(btn){
				btn.toggleClass('selected');		
				if($(btn.id+'_content').getStyle('display')=='none')
				{
					$(btn.id+'_content').setStyle('display', '');
				} else
				{
					$(btn.id+'_content').setStyle('display', 'none');
				}
			});
		});
	});
	
	$$('.wedstrijd_types a').each(function(el){
		el.addEvent('click', function(e){
			var cls = (this.hasClass('programma') ? 'programma' : 'uitslagen');
			$$('.wedstrijd_types a.'+cls).each(function(btn){
				btn.removeClass('selected');
				if($(btn.id+'_content')!=null){
					$(btn.id+'_content').setStyle('display', 'none');
				}
			});
			this.addClass('selected');
			this.blur();
			if($(this.id+'_content')!=null){
				$(this.id+'_content').setStyle('display', '');
			}
		});
	});
	
	$$('#logos img').each(function(el){
		el.addEvent('mouseenter', function(e)
		{
			this.setAttribute('old_src', this.src);
			this.src = this.src.replace('bw/', '');
		});
		
		el.addEvent('mouseleave', function(e){
			this.src = this.getAttribute('old_src');
		});
		
	});
	
	setTimeout('iefix_logos()', 1000);
	
	$$('#toernooi_menu li').each(function(el){
		el.addEvent('mouseenter', function(){
			this.addClass('hover');
		});
		el.addEvent('mouseleave', function(){
			this.removeClass('hover');
		});
		el.addEvent('click', function(){
			$$('#toernooi_menu li').each(function(li){
				li.removeClass('selected');
			});
			$$('#toernooi_content .toernooi_content').each(function(div){
				div.setStyle('display', 'none');
			});
			$(this.id.replace('li', 'content')).setStyle('display', 'inline');
			this.addClass('selected');
		});
	});
	
	$$('.club_input').each(function(el){
		var completer = new Autocompleter.Ajax.Json(el, url, {postVar: 'q', postData: {'global_ajax_function': 'clubs'}, onSelect: function(obj){
			if(obj.id == 'input_club') update_competitie_zoek();
		}});
	});
	
	$$('.inschrijven_club').each(function(el){
		var completer = new Autocompleter.Ajax.Json(el, url, {postVar: 'q', postData: {'global_ajax_function': 'clubs'}, onSelect: function(obj){
			fill_toernooi_teams();
		}});
	});
	
	
	if($('input_home_club') != null)
	{
		$('input_home_club').addEvent('focus', function(){
			if(this.get('value') == 'zoek je club...') this.set('value', '');
		});
		var completer = new Autocompleter.Ajax.Json($('input_home_club'), url, {postVar: 'q', postData: {'global_ajax_function': 'clubs'}, onSelect: function(obj){
			new Request({
				url: url,
				data: {
					'global_ajax_function': 'redirect_club',
					'club': obj.get('value')
				},
				onComplete: function(response){
					document.location = response;
				}
			}).send();
		}});
		
	}
 
	$$('.votes').each(function(el){
		el.addEvent('mouseenter', function(){
			if(this.hasClass('disabled')) return;
			this.addClass('hover');
		});
		el.addEvent('mouseleave', function(){
			this.removeClass('hover');
		});
		
		el.addEvent('click', function(){
			if(this.hasClass('disabled')) return;
			if(el.hasClass('done'))return;
			var pos = this.getPosition();
			var scroll = $('paper').getScroll();
			$('choose').setStyles({"display": "block", "left": pos.x + 24, "top": pos.y + scroll.y - 100});
			current_msg_id = this.id.replace('votes_', '');
		});
	});
	$$('#choose div').each(function(el){
		el.addEvent('mouseenter', function(){
			var data 	= this.id.split('_');
			var nr		= data[1];
			for(i=0; i<5; i++)
			{
				if($('choose_'+i) != null)
				{
					$('choose_'+i).className = (i<=nr ? 'pick'+nr : '');				
				}
			}
		});
		el.addEvent('click', function(){
			$('votes_'+current_msg_id).addClass('done');
			$('choose').setStyles({'display': 'none'});
			new Request({url: url, 
				data: {
					ajax_function: 'vote',
					msg_id: current_msg_id,
					vote: this.id.replace('choose_', '')
				}
			}).send();
		});
	});
	$$('.speler').each(function(el){
		el.addEvent("mouseover", function(e){
			this.addClass('over');
		});
		el.addEvent("mouseout", function(){
			this.removeClass('over');
		});
		if($('speler_detail') != null)
		{
			el.addEvent("click", function(){
				$$('.speler').each(function(li){
					li.removeClass('selected');
				});
				this.addClass('selected');
				new Request.HTML({
					url: url, 
					update: 'speler_detail',
					data: {
						'ajax_function': 'speler',
						'speler': this.id.replace('speler_', '')
					}
				}).send();
			});
		}
	});
	
	if($('status') != null)
	{
		$('status').addEvent('click', function(){
			$('status_pick').setStyle('display', '');
			var pos = $('status').getCoordinates();
			$('status_pick').inject(document.body);
			$('status_pick').setStyles({"left": pos.left, "top": pos.top.toInt() + 18, "position": "absolute", "margin": 0});
		});
	}
	
	$$('li.status').each(function(li){
		li.setStyle('opacity', .5);
		li.addEvent('mouseenter', function(){
			this.setStyle('opacity', 1);
		});
		li.addEvent('mouseleave', function(){
			this.setStyle('opacity', .5);
		});
		li.addEvent('click', function(){
			$('status_pick').setStyle('display', 'none');
			$('btn_status').innerHTML = this.innerHTML;
			new Request({
				url: url,
				data: {
					'global_ajax_function': 'change_status',
					'status': this.id
				}
			}).send();
		});
	});
	
	$$('.contacter li').each(function(li){
		li.addEvent('click', function(){
			li.getParent().setStyle('display', '');
		});
	});
	
	if($('comp_cat') != null)
	{
		$('comp_cat').addEvent('change', function(){
			update_competitie_zoek();
		});
	}	
	
	$$('.comp_img').each(function(el){
		el.addEvent('click', function(){
			var img = this;
			var coor = window.getCoordinates();
			var scroll = window.getScroll();
			new Element('div')
				.setStyles({
					'width': coor.width + scroll.x,
					'height': coor.height + scroll.y,
					'background-color': 'black',
					'opacity': .7,
					'position': 'absolute',
					'z-index': 9999999,
					'top': 0,
					'left': 0
				})
				.addEvent('click', function(){
					this.dispose();
					$('comp_layer').dispose();
				})
				.set('id', 'comp_layer_bg')
				.inject(document.body);
			var _coor = this.getPosition();
			var comp_layer = new Element('div')
				.setStyles({
					'width': 10,
					'height': 10,
					'overflow': 'hidden',
					'background-color':'#202020',
					'position': 'absolute',
					'z-index': 99999999,
					'left': _coor.x,
					'top': _coor.y,
					'text-align': 'center',
					'padding-top': 20
				})
				.set('id', 'comp_layer')
				.inject(document.body);
			var fx = new Fx.Morph(comp_layer, {onComplete: function(){
				comp_layer.set('html', '<img src="'+template_url+'/images/'+img.get('id')+'_big.gif"><div style="width:820px;text-align:right;"><img src="'+template_url+'/images/close2.gif" id="close_layer"></div>');
				$('close_layer').setStyles({'margin-top': 10, 'cursor': 'pointer'}).addEvent('click', function(){
					$('comp_layer_bg').fireEvent('click');
				});
				
			}});
			fx.start({	
					'width': 840,
					'height': 620,
					'left': (((coor.width - 840)/2)+scroll.x),
					'top': 30 + scroll.y
			});
		});
	});
	
	if($('training_demo')!=null)
	{
		$('training_demo').addEvent('click', function(){
			var coor = window.getCoordinates();
			var scroll = window.getScroll();
			new Element('div')
				.setStyles({
					'width': coor.width + scroll.x,
					'height': coor.height + scroll.y,
					'background-color': 'black',
					'opacity': .7,
					'position': 'absolute',
					'z-index': 9999999,
					'top': 0,
					'left': 0
				})
				.addEvent('click', function(){
					this.dispose();
					$('comp_layer').dispose();
				})
				.set('id', 'comp_layer_bg')
				.inject(document.body);
			var _coor = this.getPosition();
			var comp_layer = new Element('div')
				.setStyles({
					'width': 10,
					'height': 10,
					'overflow': 'hidden',
					'background-color':'#202020',
					'position': 'absolute',
					'z-index': 99999999,
					'left': _coor.x,
					'top': _coor.y,
					'text-align': 'center',
					'padding-top': 20
				})
				.set('id', 'comp_layer')
				.inject(document.body);
			var fx = new Fx.Morph(comp_layer, {onComplete: function(){
				comp_layer.set('html', '<div id="training_demo_flash" style="width:700px; height:500px"></div><div style="width:700px;text-align:right;"><img src="'+template_url+'/images/close2.gif" id="close_layer"></div>');
				$('close_layer').setStyles({'margin-top': 10, 'cursor': 'pointer'}).addEvent('click', function(){
					$('comp_layer_bg').fireEvent('click');
				});
	
				var so = new SWFObject(template_url+"/swf/training_help.swf", "swfmenu", "700", "500", "9", "black");
				so.addParam("wmode", "transparent");
				so.write("training_demo_flash");
				
			}});
			fx.start({	
					'width': 720,
					'height': 550,
					'left': (((coor.width - 720)/2)+scroll.x),
					'top': 30 + scroll.y
			});
		});
		
	}
	
	if($('header_flash') != null)
	{
		var so = new SWFObject(template_url+"/swf/header.swf", "swfmenu", "320", "144", "9", "black");
		so.addParam("wmode", "transparent");
		so.write("header_flash");
	}
	
	if($('home_team_flash') != null)
	{
		var so = new SWFObject(template_url+"/swf/home_team.swf", "swfmenu", "445", "350", "9", "black");
		so.addParam("wmode", "transparent");
		so.write("home_team_flash");
	}
		
	update_login.periodical(30000);
	_uacct = "UA-1821030-2";
	urchinTracker();
	
	$$('#poll .poll_antwoord').each(function(el){
		if(el.hasClass('done'))return;
		el.addEvent('click', function(){
			$('poll_answers').set('html', 'bedankt voor je stem!');
			new Request.HTML({
				url: url,
				data: {
					'ajax_function': 'poll_answer',
					'answer':  this.get('id')
				},
				update: $('poll_answers'),
				onComplete: init_poll
			}).send();
		});
	});
	init_poll();
});

function init_poll()
{
	$$('#poll .percentage').each(function(el){
		var info = el.get('id').split('_');
		var perc = info[2].toInt();
		if(perc == 0) return;
		var fx = new Fx.Tween(el, {duration: perc*50});
		fx.start('width', 3, perc*1.6);
		
		var timer;
		var i = 0;
		var addPerc = function(perc){ 
			i++;
			var lbl = $('perc_label_'+info[1]);
			lbl.set('html', i+'%');
			if(i >= perc) $clear(timer);
		};
		timer = addPerc.periodical(50, null, perc);
	});
}

function fill_toernooi_teams()
{
	new Request({
		url: url,
		data: {
			'ajax_function': 'get_team_options',
			'club': $('club').get('value'),
			'cat_id': $('toernooi_cat_per_id').get('value')
		},
		onComplete: function(response){
			$('options').set('value', response);
			var result = JSON.decode(response);
			$('team_id').options.length = 0;
			for(i=0; result.length; i++)
			{
				$('team_id').options[$('team_id').options.length] = new Option(result[i].team, result[i].id);
			}
		}
	}).send();		
}
var update_competitie_zoek = function(){
	new Request.HTML({
		url: url,
		data: {
			'ajax_function': 'get_competities',
			'club': $('input_club').get('value'),
			'cat': $('comp_cat').get('value')
		},
		update: 'comp_result'
	}).send();
}

var update_login = function(){
	new Request.JSON({
		url: url,
		onComplete: after_update
	}).post({
			'global_ajax_function': 'update_login',
			'current_tabs': current_chat_tabs
		});
}
function after_update(result)
{
	if(result == null || result.length == null) return;
	for(var i=0; i<result.length; i++)
	{
		var item = result[i];
		if(item.type == 'newonline')
		{
			if(current_chat_tabs.indexOf(","+item.id+",") >= 0) return;
			var msg = item.msg; 
		} else // new chat
		{
			if(current_chat_tabs.indexOf(","+item.id+",") >= 0 && item.id != current_chat_tab)
			{
				$('chat_'+item.id).fireEvent('message');
				return;
			}
			var msg = item.msg;
		}
		add_global_message(msg);
	}
}


// CHAT FUNCTIONALITEIT ///////////////////////////////////////////////////////////////////////
var nr_msgs = 0;
var global_msgs;
window.addEvent('domready', function(){
	global_msgs = new Element("div").set("id", "global_msgs").inject(document.body);
});
function block_user(id)
{
	new Request({
		url: url,
		data: {
			'global_ajax_function': 'block_user',
			'user_id': id
		}
	}).send();
}
function add_global_message(txt)
{
	nr_msgs++;
	var scroll = window.getScroll();
	var msg = new Element("div")
		.addClass("global_msg")
		.setStyles({"height": 0, "overflow": "hidden"})
		.store("nr_msg", nr_msgs)
		.set('html', txt)
		.inject(global_msgs);
	
	var fx = new Fx.Tween(msg);
	fx.start('height', 100);
	
	(function(){
		nr_msgs--;
		var fx = new Fx.Tween(msg);
		fx.addEvent('onComplete', function(div){
			div.dispose();
		});
		fx.start('height', 0);
	}).delay(7000);
}
function position_global_messages()
{
	var scroll = window.getScroll();
	$$('.global_msg').each(function(el){
		el.setStyle('top', window.getHeight().toInt() + scroll.y - (100*el.retrieve('nr_msg')));		
	});
}
window.addEvent("scroll", position_global_messages);
window.addEvent("resize", position_global_messages);

var current_chat_tab;
var current_chat_tabs = ",";
function gochat(id)
{
	if($('chat') == null)
	{
		create_chat(id);
	} else if($('chat_'+id) == null) 
	{
		create_chat_tab(id);	
	}
}
function create_chat(id)
{
	var chat_div = new Element("div")
		.set("id", "chat")
		.setStyles({'top': 30, 'left': (((window.getWidth().toInt() - 1000) / 2)+637)})
		.inject(document.body);
	new Request.HTML({
		url: url,
		data: {
			'global_ajax_function': 'create_chat',
			id: id
		},
		update: 'chat',
		onComplete: add_chat_events
	}).send();
}
var timer_get_msgs;
function add_chat_events(nodes, head, html, js)
{
	var user = JSON.decode($('js').get('html'));
	chat_tab_after(user);
	$('chat_form').addEvent('submit', function(e){
		e = new Event(e).stop();
		$('user_to').set('value', current_chat_tab);
		this.set('send', {
			url: url, 
			method: 'post',
			onComplete: add_msgs			
		});
		
		this.send();
		$('chat_input').set('value', '').focus();
	});
	timer_get_msgs = get_msgs.periodical(4000);
}
function get_msgs()
{
	new Request({
		url: url,
		data: {
			'global_ajax_function': 'get_msgs',
			id: current_chat_tab
		},
		onComplete: add_msgs
	}).send();
}
function del_chat_tab(id)
{
	current_chat_tabs = current_chat_tabs.replace(id+',','');
	if(current_chat_tabs != ',' && current_chat_tab == id)
	{
		var new_current = $('chat_'+id).getPrevious();
		if(new_current == null) new_current = $('chat_'+id).getNext();
		new_current.fireEvent('click');
	}
	
	$('chat_'+id).dispose();
	$('chat_screen_'+id).dispose();
	current_chat_tabs = current_chat_tabs.replace(id+',','');
	if(current_chat_tabs == ",")
	{
		$('chat').dispose();
		timer_get_msgs = $clear(timer_get_msgs);
	}
	
}
function add_msgs(html)
{
	if(html == '#-offline-#')
	{
		alert("De gebruiker is offline gegaan.");
		del_chat_tab(current_chat_tab);
		return;
	}
	var screen = $('chat_screen_'+current_chat_tab);
	screen.set('html', screen.get('html') + html);
	screen.scrollTo(0, screen.getScrollSize().y);
}
function move_chat()
{
	if($('chat') == null) return;
	$('chat').setStyle("left", ((window.getWidth().toInt() - 1000) / 2)+637);
}
function create_chat_tab(id)
{
	var nr_tabs = -1;
	for(var i=0; i<current_chat_tabs.length; i++)
	{
		if(current_chat_tabs.charAt(i) == ',') nr_tabs++;
	}
	if(nr_tabs >= 3)
	{
		alert("Maximaal 3 chat sessies tegelijk.");
		return;
	}
	new Request.JSON({
		url: url,
		onComplete: chat_tab_after
	}).post({
			'global_ajax_function': 'create_chat_tab',
			'id': id
		});
}
function chat_tab_after(result) // {naam, id}
{
	var new_screen = new Element('div')
		.set('id', 'chat_screen_'+result.id)
		.addClass('screen')
		.inject($('chat_screens'));

	var new_tab = new Element('div')
		.set('id', 'chat_'+result.id)
		.set('html', result.naam)
		.addEvent('click', activate_tab)
		.addEvent('message', message_tab)
		.inject($('chat_tabs'))
		.fireEvent('click');
	
	current_chat_tabs += result.id + ",";
	
	new Element('span')
		.set('html', 'x')
		.setStyles({'float': 'right', 'cursor': 'pointer', 'font-weight': 'bold'})
		.addEvent('click', function(){del_chat_tab(this.retrieve('tab_id'));})
		.store('tab_id', current_chat_tab)
		.inject($('chat_'+current_chat_tab), 'top');
}
window.addEvent('resize', move_chat);

function activate_tab(e)
{
	if($(this.get('id').replace('chat_', 'chat_screen_')) == null)return; //bugfix
	this.removeClass('message');
	current_chat_tab = this.get('id').replace('chat_','');
	$$('#chat_tabs div').each(function(el){
		$(el.get('id').replace('chat_', 'chat_screen_')).addClass('inactive');
		el.addClass('inactive');
	});
	this.removeClass('inactive');
	$(this.get('id').replace('chat_', 'chat_screen_')).removeClass('inactive');
}

function message_tab(e)
{
	this.addClass('message');
}
// EINDE CHAT /////////////////////////////////////////////////////////////////////////////////




function toggle_contact(id)
{
	$$('.contacter').each(function(ul){
		ul.setStyle('display', 'none');
	});
	$('contact_'+id).setStyle('display', ($('contact_'+id).getStyle('display')=='none' ? '' : 'none'));
}
function iefix_logos()
{
	$$('#logos span').each(function(el){
		el.addEvent('mouseenter', function(e)
		{
			this.setAttribute('old_src', this.getStyle('filter'));
			this.setStyle('filter',this.getStyle('filter').replace('bw/', ''));
		});
		
		el.addEvent('mouseleave', function(e){
			this.setStyle('filter',this.getAttribute('old_src'));
		});
		
	});
	
}

function close_layer(){
	var fx = new Fx.Tween('layer_bg', 'opacity', {isSuccess: function(e){
		//this.setStyle('display', 'none');
	}});
	fx.start(0);
	$('layer').setStyle('display', 'none');
	$('layer2').setStyle('display', 'none');
	$('layer_content').innerHTML = '';
	$('layer_content2').innerHTML = '';
	$$('select').each(function(el){
		el.setStyle('display', '');
	});
};
function go_wedstrijd(id)
{
	$('layer_content').setStyle('background-image', 'url(/templates/default/images/ajax-loader.gif)');
	$('layer_bg').setStyles({'display': '', 'opacity': 0});
	var fx = new Fx.Tween('layer_bg', 'opacity');
	fx.start(.6);
	
	$('layer').setStyles({'top': (window.getHeight().toInt()-550)/2, 'left': (window.getWidth().toInt()-800)/2, 'display':''});
	
	new Request({url: url,
		method: 'get',
		data: {
			'wedstrijd_id': id,
			'getWedstrijd': 1,
			'ajaxFunction': 1
		},
		update: 'layer_content',
		isSuccess: function(response){
			$('layer_content').setStyle('background-image', 'none');
			try{
				correctPNG();
			} catch(e){}
		}
	}).send();
}
function open_layer(funct, id)
{
	$('layer_bg').setStyles({'display': '', 'opacity': 0});
	var fx = new Fx.Tween('layer_bg', 'opacity');
	fx.start(.6);
	
	$('layer').setStyles({'top': (window.getHeight().toInt()-375)/2, 'left': (window.getWidth().toInt()-500)/2, 'display':''});

	$$('select').each(function(el){
		el.setStyle('display', 'none');
	});
	
	new Request({url: url,
		method: 'post',
		data: {
			'id': id,
			'ajax_function': funct
		},
		update: 'layer_content',
		isSuccess: function(response){
			if($('verstuur')!=null){
				$("reageer_form").addEvent("submit", function(e){
					new Event(e).stop();
					$('verstuur').disabled = true; 
					new Request.HTML({
						url: url,
						update: 'bericht_div'
					}).post($('reageer_form'));
				});
			}
			$('layer_content').setStyle('background-image', 'none');
			try{
				correctPNG();
			} catch(e){}
		}
	}).send();
}
function open_layer2(funct, id)
{
	if($('layer_bg').getStyle('display') == 'none')
	{
		$('layer_bg').setStyles({'display': '', opacity: 0});
	}
	var fx = new Fx.Tween('layer_bg', 'opacity');
	fx.start(.6);
	
	$('layer2').setStyles({'top': (window.getHeight().toInt()-550)/2, 'left': (window.getWidth().toInt()-800)/2, 'display':''});
	
	$$('select').each(function(el){
		el.setStyle('display', 'none');
	});
	
	new Request({url: url,
		method: 'post',
		data: {
			'id': id,
			'db_id':id,
			'ajax_function': funct
		},
		update: 'layer_content2',
		isSuccess: function(response){
			addFormSubmit();			
			$('layer_content2').setStyle('background-image', 'none');
			try{
				correctPNG();
			} catch(e){}
		}
	}).send();
}

function addFormSubmit()
{
	$$('#toernooi form').each(function(el){
		el.removeEvents();
		el.addEvent('submit', function(e){
			new Event(e).stop();
			this.send({update: 'div_'+this.id, isSuccess: addFormSubmit});
		});
	});	
	if($('toernooi')!=null)
	{
		
	}
	if($('msg') != null)
	{
		$('msg').addEvent('submit', function(e){
			new Event(e).stop();
			this.send({update: 'layer_content2', isSuccess: addFormSubmit});
		});
	}
}

function inschrijven(id)
{
	var fx = new Fx.Slide('div_inschrijven_'+id);
	if($('div_inschrijven_'+id).getStyle('display') == 'none'){
		fx.hide();
		$('div_inschrijven_'+id).setStyle('display', '');
	}
	fx.slideIn();
}

function fillTeams()
{
	var team = $('team_id');
	var input = $('club');
	team.options.length = 0;
	new Request({url: url,
		method: 'post',
		data: {
			'ajax_function': 'get_team_options',
			'club': input.value,
			'cat_id': $('toernooi_cat_per_id').value
		},
		isSuccess: function(resp){
			$('options').value = resp;
			var obj = Json.evaluate(resp);
			for(i=0; i<obj.length; i++){
				team.options[team.options.length] = new Option(obj[i].team, obj[i].id);
			}
		}
	}).send();
}

function aanmelden(cat_id)
{
	$('toernooi_cat_per_id').value = cat_id;
	$('li4').fireEvent('click');
	$('club').focus();
}

function gostand(type)
{
	new Request({url: url,
		method: 'post',
		data: {
			'type': type,
			'ajax_function': 'gostand'
		},
		update: 'stand_inner',
		isSuccess: function(response){
			//alert(response);
		}
	}).send();
}
function goPeriode(periode, el)
{
	$$('a.periode').each(function(el){
		el.removeClass('over');
	});
	el.addClass('over');
	gostand("periode"+periode);
	el.blur();
}

function go_cat(cat)
{
	$$('.teams').each(function(el){
		el.setStyle('display', 'none');
	});
	$(cat).setStyle('display', 'block');
	
	$$('.groepen a').each(function(el){
		el.removeClass('selected');
	});
	$('go_'+cat).addClass('selected');
	
	$$('a.cat').each(function(el){
		el.removeClass('selected');
	});
	data = cat.split('_');
	$(data[1]).addClass('selected');
}

function tab_switch(tab)
{
	if(tab == 1)
	{
		$('tab_1').addClass('over');
		$('tab_2').removeClass('over');
		$('detail_1').setStyle('display', '');
		$('detail_2').setStyle('display', 'none');
	} else
	{
		$('tab_2').addClass('over');
		$('tab_1').removeClass('over');
		$('detail_2').setStyle('display', '');
		$('detail_1').setStyle('display', 'none');
	}
}
function topscore_detail(speler_id)
{
	if($('topscore_'+speler_id) != null)
	{
		$('topscore_'+speler_id).retrieve("fx").toggle();
		return;
	}
	new Request({
		url: url,
		data: {
			'ajax_function': 'topscore_detail',
			'speler_id': speler_id	
		},
		onSuccess: function(response){
			var div = new Element("div").set("html", response).set("id", "topscore_"+speler_id).setStyle("display", "none");
			
			
			var a = $('cnt_'+speler_id);
			var tr = a.getParent().getParent();
			
			var new_tr = new Element("tr");
			var td = new Element("td").set("colspan", 2).inject(new_tr);
			div.inject(td);
			new_tr.injectAfter(tr);
			
			var fx = new Fx.Slide(div).hide();
			div.setStyle("display", "").store("fx", fx);
			fx.slideIn();
		}
	}).send();
}
