function clickMedia(e) {

	if (e.data.emb > '') {
		Shadowbox.open({
			content:	e.data.emb,
			player:		"swf",
			height:     e.data.h,
			width:      e.data.w
		});
		
		return false;
	}
}

function stadium_switch(newstad) {
	if (admin_stadium == STAD_character.stadium) extra=''; else extra=admin_stadium+'s';
	$("#chatlist"+extra).hide();
	$("#predictlist"+extra).hide();
	$("#crowdlist"+extra).hide();

	if (newstad == STAD_character.stadium) extra=''; else extra=newstad+'s';
	$("#chatlist"+extra).show();
	$("#predictlist"+extra).show();
	$("#crowdlist"+extra).show();

	admin_stadium = newstad;
}

function radio_error() {
	$("#wdon-error").fadeIn('slow').fadeOut('slow');
	radio_stop(false);
}

function radio_status(new_streaming) {
	if (new_streaming) {
		$("#wdon-onair").show();
		$("#wdon-offair").hide();
	}
	else {
		$("#wdon-offair").show();
		$("#wdon-onair").hide();
		radio_stop(false);
	}
	radio_streaming = new_streaming;
}

function radio_play() {
	if (radio_streaming) {
		$("#wdon-player").jPlayer("setMedia", { mp3: stream }).jPlayer("play");
		$(".jp-play").hide();
		$(".jp-pause").show();
	}
	else
		radio_error();
}

function radio_stop(stopplayer) {
	if (stopplayer) $("#wdon-player").jPlayer("stop");
	$("#wdon-player").jPlayer("clearMedia");
	$(".jp-pause").hide();
	$(".jp-play").show();
}

function clean(data) {
//	data = data.replace(/(http:\/\/)(.*)\s/gi, '<a href=../../modules/$1$2>$1$2 target="_blank"</a>');
    data = data.replace(exp,'<a href="$1" target="_blank">$1</a>'); 
	if (swearing_permit == false) {
		newval = data.replace(bw, ReplacementText);
		if (newval.length != data.length ||  newval != data) data = newval;
	}
	return data;
}

function sendmessage(chan) {
	var stad=STAD_character.stadium;
	
	switch (chan) {
		case 'w':
		case 'm':
			message = $.trim($("#chatmess").val());
			$("#chatmess").val('');
			break;
		case 'e':
			message = $.trim($("#predictmess").val());
			$("#predictmess").val('');
			break;
	}

	if (message > '') {
		if (STAD_response.user_admin == 1) stad=admin_stadium;
		if (chan == "m") {
			$("#chatbutton").attr('disabled', true);
			setTimeout(function(){ $("#chatbutton").attr('disabled', false);},5000);
		}
		$.ajax({
			type: "POST",
			url: "methods/mess.php",
			data: "force="+dummy.getTime()+"&stad="+stad+"&m="+chan+"&t="+encodeURIComponent(message),
			success: function(msg) { message_poll(); },
			datatype: "text"
		});
	}
	return false;
}

function sendyell() {
	message = $.trim($("#chatmess").val());
	$("#chatmess").val('');
	if (message > '') {
		$("#yellbutton").attr('disabled', true);
		setTimeout(function(){ $("#yellbutton").attr('disabled', false);},5000);
		$.ajax({
			type: "POST",
			url: "methods/yell.php",
			data: "force="+dummy.getTime()+"&stad="+STAD_character.stadium+"&m="+encodeURIComponent(message)+"&id="+STAD_enter_response.seat,
			success: function(msg) { message_poll(); },
			datatype: "text"
		});
	}
	return false;
}

function wimpyPopPlayer(theFile,id,stuff) {
	if (WinId[0] && !WinId[0].closed) return false;
	WinId[0]=window.open(theFile,id,stuff);
}

function message_del(e) {
	$.ajax({
		type: "POST",
		url: "methods/delete.php", data: "force="+dummy.getTime() + "&id=" + e.data.id, success: function(msg) { message_poll(); }
	});
}

function crowd_kick(e) {
	if (confirm('Kick ' + e.data.name + '?')) {
		$.ajax({
			type: "POST",
			url: "methods/kick.php",
			data: "force="+dummy.getTime() + "&id=" + e.data.seat + "&ip=" + e.data.ip+"&stad=" + e.data.stad+"&ban=0"
		});
	}
}

function crowd_ban(e) {
	if (confirm('Permanently ban ' + e.data.name + '?')) {
		$.ajax({
			type: "POST",
			url: "methods/kick.php",
			data: "force="+dummy.getTime() + "&id=" + e.data.seat + "&ip=" + e.data.ip+"&stad=" + e.data.stad+"&ban=1"
		});
	}
}

function crowd_animate(e) {
	$.ajax({
		type: "POST",
		url: "methods/animate.php",
		data: "force="+dummy.getTime() + "&id=" + e.data.seat + "&stad=" + e.data.stad,
		success: function(msg) { message_poll(); },
		datatype: "text"
	});
}

function crowd_bounce(sid,n) {
	while (n>0) {
		$("#seat"+sid).animate({"bottom": "+=20px"}, "fast").animate({"bottom": "-=20px"}, "fast");
		n--;
	}
	return false;
}

function crowd_jump() {
	px = (11+Math.floor(Math.random()*10));
	$('#crowd').animate({'bottom': '+='+px+'px'}, 'fast').animate({'bottom': '-='+px+'px'}, 'fast').animate({'bottom': '+='+px+'px'}, 'fast').animate({'bottom': '-='+px+'px'}, 'fast');
	return false;
}

function clock_update() {
var elapsed = time_now - time_period_started + time_base;
var secs = elapsed % 60;
var mins = parseInt(elapsed / 60);
if (secs < 10) secs = '0'+secs;
$("#scoreboard_clock").html(mins + ":" + secs);
}

function tab_switch(which) {
	switch (which) {
		case 1 :
			$("#updatewin").show();
			$("#scorewin").hide();
			$("#tablewin").hide();
			$("#vidwin").hide();
			$("#updatetab").removeClass("tab_alert");
			$("#updatetab").addClass("tab_sel");
			if (!score_unseen) $("#scoretab").removeClass();
			if (!vid_unseen) $("#vidtab").removeClass();
			if (!table_unseen) $("#tabletab").removeClass();
			update_unseen = false;
			break;
		case 2 :
			$("#updatewin").hide();
			$("#scorewin").show();
			$("#tablewin").hide();
			$("#vidwin").hide();
			$("#scoretab").removeClass("tab_alert");
			$("#scoretab").addClass("tab_sel");
			if (!update_unseen) $("#updatetab").removeClass();
			if (!vid_unseen) $("#vidtab").removeClass();
			if (!table_unseen) $("#tabletab").removeClass();
			score_unseen = false;
			break;
		case 3 :
			$("#updatewin").hide();
			$("#scorewin").hide();
			$("#tablewin").show();
			$("#vidwin").hide();
			$("#tabletab").removeClass("tab_alert");
			$("#tabletab").addClass("tab_sel");
			if (!update_unseen) $("#updatetab").removeClass();
			if (!score_unseen) $("#scoretab").removeClass();
			if (!vid_unseen) $("#vidtab").removeClass();
			table_unseen = false;
			break;
		case 5 :
			$("#updatewin").hide();
			$("#scorewin").hide();
			$("#vidwin").show();
			$("#vidtab").removeClass("tab_alert");
			$("#vidtab").addClass("tab_sel");
			if (!update_unseen) $("#updatetab").removeClass();
			if (!score_unseen) $("#scoretab").removeClass();
			if (!table_unseen) $("#tabletab").removeClass();
			vid_unseen = false;
			break;
		case 4 :
			$("#chatwin").show();
			$("#predictwin").hide();
			$("#crowdwin").hide();
			$("#chattab").addClass("tab_sel");
			$("#predicttab").removeClass();
			$("#crowdtab").removeClass();
			break;
		case 7 :
			$("#chatwin").hide();
			$("#predictwin").hide();
			$("#crowdwin").show();
			$("#chattab").removeClass();
			$("#predicttab").removeClass();
			$("#crowdtab").addClass("tab_sel");
			break;			
		case 6 :
			$("#chatwin").hide();
			$("#predictwin").show();
			$("#crowdwin").hide();
			$("#chattab").removeClass();
			$("#predicttab").addClass("tab_sel");
			$("#crowdtab").removeClass();
			break;			
	}
}

function second_loop() {
	loop_count++;
	if (match_period == 1 || match_period == 3) {
		time_now += 1;
		clock_update();
	}
	if (STAD_character.ticket == 1) {
		loop_limit = rate_season;
	}
	else {
		loop_limit = crowd_count / rate_divisor;
		if (loop_limit < rate_other) loop_limit = rate_other;
	}
	if (loop_count >= loop_limit) {
		if (debug) {
			console.log('Loop ' + loop_count);
			console.log('Rate ' + rate_season + ' ' + rate_other + ' ' + rate_divisor);
		}
		loop_count=0;
		message_poll();
	}
}

function message_poll() {
	if (STAD_response.user_admin == 1)
		$.ajax({
			type: "POST",
			url: "methods/arpc.php",
			data: "req="+request+'&force='+dummy.getTime(),
			success: function(msg) { handleResponse(msg); },
			error: function(XMLHttpRequest, textStatus, errorThrown) {
			},
			datatype: "text",
			timeout: "5000"
		});
	else
		$.ajax({
			type: "POST",
			url: "methods/rpc.php",
			data: "req="+request+'&force='+dummy.getTime(),
			success: function(msg) { handleResponse(msg); },
			error: function(XMLHttpRequest, textStatus, errorThrown) {
			},
			datatype: "text",
			timeout: "5000"
		});
}

function handleResponse(response) {
	var respline = new Array();
	var update = new Array();
	var this_stad = STAD_character.stadium;
	var extra='';
	var seats_ch = new Array();

	if (instadium=false) return;
	if (debug) console.log('handleResponse');
	if(response.indexOf('#') != -1) {
		respline = response.split('#');
	    if (debug) console.log(request + '; ' + respline[0] + '; ' + respline.length);
		var newreq = Number(respline[0]);
		if (newreq == -1) {
			stadium_exit(false);
			alert('The stadium is now closed. Please have a safe journey home.');  // stadium closed
		}
		if (newreq == 0) {
			stadium_exit(false);
			alert('You have been removed from the stadium. This could be because a season ticket holder needed your place, your computer stopped communicating with the server, or you were ejected by the administrator.');  // kicked or logged out by time
		}
		crowd_count = respline[2];
		$("#scoreboard_att").html('Att: '+crowd_count);
		if (newreq > request) {
			seats_changed = false;
			for (i=3; i<respline.length; i++) {
				update = respline[i].split('|');
				if (STAD_response.user_admin == 1) {
					this_stad=update[0];
					update.splice(0,1);
					if (this_stad == STAD_character.stadium)
						extra='';
					else
						extra = this_stad+'s';
				}
			    if (debug) console.log('*' + respline[i] + '*');
				switch (update[0]) {
					case 'a' : // audio
						radio_status(update[1]==1);
						break;
					case 'b': // table
						$("#tablelist").html('<p>'+update[1]+'</p>');
						break;
					case 'c' : // character animation
						crowd_bounce(update[1],1);
						break;
					case 'e': // predict
//						if (debug) { console.log(update[3]+' '+update[4]+' '+respline[i]); }
						if (STAD_response.user_admin == 1) {
							$("#predictlist"+extra).prepend('<p id="ws-e'+update[4]+'"><img src="'+groot+'/misc/delete.png" id="ws-d'+update[4]+'" class="card pointer" /><strong>'+clean(update[1])+'</strong>: '+clean(update[2])+'</p>');
							$("#ws-d"+update[4]).bind('click',{ id:update[4] }, function (e) { message_del(e); });
						}
						else
							$("#predictlist"+extra).prepend('<p id="ws-e'+update[4]+'"><strong>'+clean(update[1])+'</strong>: '+clean(update[2])+'</p>');
						break;
					case 'm': // message
						if (STAD_response.user_admin == 1) {
							$("#chatlist"+extra).prepend('<p id="ws-e'+update[4]+'"'+((update[3]==3)?' class="chat_oppo"':'')+'><img src="'+groot+'/misc/delete.png" id="ws-d'+update[4]+'" class="card pointer" /><strong>'+clean(update[1])+'</strong>: '+clean(update[2])+'</p>');
							$("#ws-d"+update[4]).bind('click',{ id:update[4] }, function (e) { message_del(e); });
						}
						else
							$("#chatlist"+extra).prepend('<p id="ws-e'+update[4]+'"><strong>'+clean(update[1])+'</strong>: '+clean(update[2])+'</p>');
						break;
					case 't': // teams
						$("#scoreboard_clock").html('0:00');
						$("#scoreboard_ht").html(update[1]);
						$("#scoreboard_at").html(update[2]);
						$("#scoreboard_hs").html(update[3]);
						$("#scoreboard_as").html(update[4]);
						break;
					case 'w': // admin message
						$("#chatlist"+extra).prepend('<p class="admin_message"><strong>'+clean(update[1])+'</strong>: '+clean(update[2])+'</p>');
						break;
					case 'y': // yell
//						if (debug) { console.log(stx[update[1]]); console.log(Number(sty[update[1]])); }
						x = (stx[update[1]])+'px';
						y = (sty[update[1]]-12)+'px';
						if (debug) { console.log(x); console.log(y); }
						z = $('#seat'+update[1]).css('z-index');
						yellid = 'ws-b' + update[3];
						newD = '<div id="' + yellid + '" class="yell" style="position:absolute;';
						newD+= 'z-index:' + String((Number(z)+500)) + ';';
//						newD+= 'left:' + String((x)) + ';';
						newD+= 'bottom:' + String((y)) + ';">';
						$('#crowd').append(newD);
						$('#' + yellid).append(clean(update[2].substr(0,14)));
						$('#' + yellid).css('left', stx[update[1]] - $('#' + yellid).width()/2);
						$('#' + yellid).fadeIn(300).delay(3000).fadeOut(300).queue(function() { $('#'+yellid).remove(); });
						break;
					case 'h': // start/end half
					case 'u': // update
					case 'p': // scoreflash
						if (update[0] == 'p' || update[0] == 'h') {
							update[2]='<strong>'+update[2]+'</strong>';
						}
						$('#updatelist').prepend('<p><strong>'+update[1]+'</strong>: '+clean(update[2])+'</p>');
						if (update[0] == 'p' && request>0 && update[3] == 1) crowd_jump();
						if (update[0] == 'h') {
							match_period = Number(update[3])+1;
							if (match_period == 1) {
								time_period_started = update[4];
								time_base = 0;
							}
							else if (match_period == 3) {
								time_period_started = update[4];
								time_base = 45*60;
							}
						}
						break;
						if ($('#updatetab').hasClass('tab_alert') == false  && first_call == false) {
							$('#updatetab').addClass('tab_alert');
							update_unseen = true;
						}
						break;
					case 'g': // goal
						$("#scoreboard_hs").html(update[1]);
						$("#scoreboard_as").html(update[2]);
						break;
					case 's': // seat change
					case 'k': // seat kick
						seat_id = 'seat'+update[1];
						if (update[2] > '') {
							if ($("#list"+extra+update[1]).length == 0) {
								if (this_stad == STAD_character.stadium) {
									newD = '<div id="' + seat_id + '" class="avatar" style="position:absolute;';
									newD+= 'z-index:' + String((230-update[3])) + ';';
									if (update[3]<217) {
										div31 = parseInt(update[3] / 31);
										sty[update[1]] = 7 + div31*20+Number(update[6]);
										stx[update[1]] = (update[3] % 31)*30+15*(div31 % 2)-17;
									}
									else {
										sty[update[1]] = 232+Number(update[6]);
										stx[update[1]] = 320+(update[3]-217)*40;
									}
									newD += 'bottom:' + String(sty[update[1]])+'px;';
									newD += 'left:' + String(stx[update[1]])+'px;">';
									if (debug) console.log(newD);
									$('#crowd').append(newD);
									dy=0;
									for (j=7;j<update.length-2;j+=3) {
										$('#'+seat_id).append('<img src = "http://www.luddweb.com/iploughlane/images/crowd/'+PNGfile(update[j+1]) + '" style="bottom:' + String(dy)+'px;left:' + String(update[j])+'px;position:absolute" alt="'+update[2]+'" title="'+update[2]+'">');
										dy = Number(update[j+2]) + dy;
									}
									stx[update[1]] = Number(stx[update[1]]) + 40;
									sty[update[1]] = Number(sty[update[1]]) + dy - 16;
								}
								if (debug) console.log(extra+update[1]+' added');
								var list_data = '<span id="name'+extra+update[1]+'" class="pointer">'+update[2]+'</span> ['+ticket_type[update[5]-1]+']';
								if (STAD_response.user_admin == 1) {
									list_data = '<img src="'+groot+'/misc/animate.png" id="a'+extra+update[1]+'" class="card pointer" />' + list_data;
									list_data = '<img src="'+groot+'/misc/redcard.gif" id="r'+extra+update[1]+'" class="card pointer" />' + list_data;
									list_data = '<img src="'+groot+'/misc/yellowcard.gif" id="y'+extra+update[1]+'" class="card pointer" />' + list_data;
								}
								$("#crowdlist"+extra).append('<p id="list'+extra+update[1]+'">'+list_data+'</p>');
//								if (this_stad == STAD_character.stadium) {
									$("#name"+update[1]).click( function (e) { crowd_bounce(e.target.id.substr(4),1); });
									seats_changed = true;
									seats_ch[this_stad]=true;
//								}
								if (STAD_response.user_admin == 1) {
									$("#y"+extra+update[1]).bind('click',{ seat:update[1], ip: update[4], name:update[2], stad:this_stad }, function (e) { crowd_kick(e); });
									$("#r"+extra+update[1]).bind('click',{ seat:update[1], ip: update[4], name:update[2], stad:this_stad }, function (e) { crowd_ban(e); });
									$("#a"+extra+update[1]).bind('click',{ seat:update[1], stad:this_stad }, function (e) { crowd_animate(e); });
								}
							}
						}
						else {
							if ($("#list"+extra+update[1]).length > 0) {
							     if (debug) console.log(extra+update[1]+' removed');
								$("#list"+extra+update[1]).remove();
								if (this_stad == STAD_character.stadium) {
									if (update[0] == "k")
										$("#"+seat_id).attr("id","kick"+update[1]).animate({ 
											"bottom": "300px"
										}, "slow", function() { $(this).remove(); } );
									else
										$("#"+seat_id).remove();
								}
							}
						}
						break;
					case 'z': // new video feed
						if (update[2] == 'p') {
							mheader = 'Photo';
						}
						else {
							mheader = 'Video';
						}
						$("#vidlist").prepend('<div class="feed_box"><img src="'+update[4]+'" />'+mheader+': '+update[8]+'<br /><a id="v-'+ update[1] +'" href="'+update[5]+'" target="_blank"><strong>'+update[3]+'</strong></a><br />'+update[9]+'</div>');
						$("#v-" + update[1]).bind("click", { emb: update[5], w: update[6], h: update[7], media: update[2]  }, clickMedia);
						if ($('#vidtab').hasClass('tab_alert') == false  && first_call == false) {
							$('#vidtab').addClass('tab_alert');
							vid_unseen = true;
						}
						break;
					case 'x': // scores
						$("#scorelist").html('<p>'+update[1]+'</p>');
						if ($('#scoretab').hasClass('tab_alert') == false  && first_call == false) {
//							$('#scoretab').addClass('tab_alert');
//							score_unseen = true;
						}
						break;
					case 'r' : // refresh rate
						rate_season = update[1];
						rate_other = update[2];
						rate_divisor = update[3];
						break;
					case 'd' : // delete event
						$("#ws-e"+update[1]).slideUp('fast');
//						$("#ws-e"+update[1]).remove();
						break;
				}
			}
			request = newreq;
			if (seats_changed) {
				if (STAD_response.user_admin == 1) {
					for (x in seats_ch) {
						if (STAD_character.stadium == x) {
							$("#crowdlist>p").tsort("span.pointer");
//							console.log("#crowdlist>p");
						}
						else {
							$("#crowdlist"+x+"s>p").tsort("span.pointer");
//							console.log("#crowdlist"+x+"s>p");
						}
					}
				}
				else
					$("#crowdlist>p").tsort("span.pointer");
			}
		}
		time_now = Number(respline[1]);
		if (match_period == 1 || match_period == 3) clock_update();
	}
}

var request;
var instadium, adminstadium;
var match_period, time_period_started, time_now, time_base, loop_count;
var update_unseen, crowd_unseen, score_unseen, vid_unseen;
var radio_streaming;
var swearing_permit, interval_tag,first_call;
var ReplacementText = "***";
//edit the words here
//sorted alpha backwords to catch bad word groupings
var badwords=['wanking','w@nker','wanker','wank','twat','shit','sh!t','piss','pimp','niggers','nigger','motherfucking','motherfuckin','motherfuckers','motherfucker','motherfuck','fuk','fucks','fcuk','fck','fucking','fuckin','fuckers','fucker','fucked','fuck','cunts','cunt','cocksucking','cocksucker','cocksucked','cocksuck','cocks','cock','bollocks','bastard','bugger','mk dons','shroot','milton keynes','bollox','cvnt','fack'];
// var bw="\\b("+badwords.join("|")+")\\b";
var bw="("+badwords.join("|")+")";
bw=new RegExp(bw, "gi");
var ticket_type = ['Season ticket','Dons','Opponents'];
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var	stx = new Array();
var	sty = new Array();

function stadium_exit(voluntary) {
	instadium=false;
	window.clearInterval(interval_tag);
	radio_stop(true);
	if (voluntary)
		$.ajax({
			url: "methods/logout.php",
			data: "force="+dummy.getTime()
		});
	$("#stadium-wrapper").html('');
	$("#front-wrapper").show();
	$("#stadium-wrapper").hide();
}

function stadium_show() {

	$("#stadium-wrapper").hide();
	$("#wait-wrapper").show();
	$("#front-wrapper").hide();

	$("#stadium-wrapper").load('methods/stadium.php?template_id='+STAD_template, function() {

		radio_streaming = false;
		
		request = 0;
		update_unseen = false;
		crowd_unseen = false;
		table_unseen = false;
		vid_unseen = false;
		score_unseen = false;
		swearing_permit = false;
		crowd_count = 0;
		match_period = 0;
		time_period_started = 0;
		time_now = 0;
		time_base = 0;
		loop_count = 0;
		first_call = true;
		rate_season = 6;
		rate_other = 7;
		rate_divisor = 13;
		tab_switch(1);
		tab_switch(4);

		stream = "http://87.117.250.3:9216/wdon";
		$("#wdon-player").jPlayer({
			swfPath: "/assets/js",
			supplied: "mp3",
			preload: "none",
			solution: "flash,html",
			errorAlerts: false,
			warningAlerts: false,
			error: function(e) { radio_error(); },
			cssSelectorAncestor: '',
			cssSelector: {
				play: "",
				pause: "",
				stop: "",
				videoPlay: "",
				seekBar: "",
				playBar: "",
				mute: "",
				unmute: "",
				currentTime: "",
				duration: "",
				volumeBar: '.jp-volume-bar',
				volumeBarValue: '.jp-volume-bar-value'
			}
		});
		$(".jp-play").click(function() {
			radio_play();
			return false;
		});

		$(".jp-pause").click(function() {
			radio_stop(true);
			return false;
		});
		
		isIOS = navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPhone/i) != null;
		if (isIOS) {
			$(".jp-audio").css("height", "60px");
			$("[class^='jp-volume']").hide();
		}

		if (STAD_character.ticket == 1) {
			$("#yellbutton").click(function () { return sendyell() });
			$("#yellbutton").show();
		}
		else
			$("#yellbutton").hide();

		if (STAD_response.user_admin == 0)
			$("#stewbutton").hide();
		else {
			$("#stewbutton").click(function () { return sendmessage('w') });
			$("#stewbutton").show();
			$("#adminstadium").change(function() { stadium_switch($(this).val()); });
			$(".adminwin").hide();
			admin_stadium = STAD_character.stadium;
		}

//		$("#chatbutton").attr('disabled', true);
		radio_status(false);

		$("#swearbox").removeAttr("checked").click(function() { swearing_permit = !swearing_permit; });
		$("#updatetab").click(function () { tab_switch(1) });
		$("#scoretab").click(function () { tab_switch(2) });
		$("#tabletab").click(function () { tab_switch(3) });
		$("#vidtab").click(function () { tab_switch(5) });
		$("#chattab").click(function () { tab_switch(4) });
		$("#predicttab").click(function () { tab_switch(6) });
		$("#crowdtab").click(function () { tab_switch(7) });
		$("#chatform").submit(function () { return sendmessage('m') });
		$("#predictform").submit(function () { return sendmessage('e') });
		$("#jumpbutton").click(function () { crowd_jump() });
		$("#exit").click(function () { stadium_exit(true) });
		instadium=true;
		if (STAD_response.user_admin == 1) curl = 'agame_first.php';
		else curl = 'game_first.php';
		$.ajax({
			type: "POST",
			url: "methods/"+curl,
			async: false,
			data: "force="+dummy.getTime(),
			success: function(msg) {
				handleResponse(msg);
				$("#wait-wrapper").hide();
				$("#stadium-wrapper").show();
				first_call = false;
				interval_tag = window.setInterval(second_loop,1000);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert('Unable to load stadium');
				stadium_exit(false);
			},
			datatype: "text"
		});


	});
}
