function login_head_change(dir) {
	$("#login_head_icon").hide();
	STAD_user_head += dir;
//	if (STAD_user_head < 0) STAD_user_head = STAD_images.heads.length - 1;
//	if (STAD_user_head > STAD_images.heads.length-1) STAD_user_head = 0;
	$("#login_head_icon").attr("src","http://www.luddweb.com/iploughlane/images/crowd/"+PNGfile(STAD_images.heads[STAD_user_head].file));
	$("#login_head_icon").css({
		"width":String(STAD_images.heads[STAD_user_head].width)+"px",
		"left":String(40-STAD_images.heads[STAD_user_head].width/2)+"px",
		"height":String(STAD_images.heads[STAD_user_head].height)+"px"
//		,"bottom":String(STAD_images.heads[STAD_user_head].offset)+"px"
	});
//	$("#file_head").html(STAD_images.heads[STAD_user_head].file);
	if (STAD_user_head == 0) $("#login_head_l").hide(); else $("#login_head_l").show();
	if (STAD_user_head == STAD_images.heads.length-1) $("#login_head_r").hide(); else $("#login_head_r").show();
	$("#login_head_icon").show();
	return false;
}

function login_body_change(dir) {
	$("#login_body_icon").hide();
	STAD_user_body += dir;
	if (STAD_user_body < 0) STAD_user_body = STAD_images.bodies.length - 1;
	if (STAD_user_body > STAD_images.bodies.length-1) STAD_user_body = 0;
	$("#login_body_icon").attr("src","http://www.luddweb.com/iploughlane/images/crowd/"+PNGfile(STAD_images.bodies[STAD_user_body].file));
	$("#login_body_icon").css({
		"width":String(STAD_images.bodies[STAD_user_body].width)+"px",
		"left":String(40-STAD_images.bodies[STAD_user_body].width/2)+"px",
		"height":String(STAD_images.bodies[STAD_user_body].height)+"px"
	});
//	$("#file_body").html(STAD_images.bodies[STAD_user_body].file);
	if (STAD_user_body == 0) $("#login_body_l").hide(); else $("#login_body_l").show();
	if (STAD_user_body == STAD_images.bodies.length-1) $("#login_body_r").hide(); else $("#login_body_r").show();
	$("#login_body_icon").show();
	return false;
}

function login_legs_change(dir) {
	$("#login_legs_icon").hide();
	STAD_user_legs += dir;
	if (STAD_user_legs < 0) STAD_user_legs = STAD_images.legs.length - 1;
	if (STAD_user_legs > STAD_images.legs.length-1) STAD_user_legs = 0;
	$("#login_legs_icon").attr("src","http://www.luddweb.com/iploughlane/images/crowd/"+PNGfile(STAD_images.legs[STAD_user_legs].file));
	$("#login_legs_icon").css({
		"width":String(STAD_images.legs[STAD_user_legs].width)+"px",
		"left":String(40-STAD_images.legs[STAD_user_legs].width/2)+"px",
		"height":String(STAD_images.legs[STAD_user_legs].height)+"px"
	});
//	$("#file_legs").html(STAD_images.legs[STAD_user_legs].file);
	if (STAD_user_legs == 0) $("#login_legs_l").hide(); else $("#login_legs_l").show();
	if (STAD_user_legs == STAD_images.legs.length-1) $("#login_legs_r").hide(); else $("#login_legs_r").show();
	$("#login_legs_icon").show();
	return false;
}

function login_whole_change(dir) {
	$("#login_whole_icon").hide();
	STAD_user_whole += dir;
	if (STAD_user_whole < 0) STAD_user_whole = STAD_images.whole.length - 1;
	if (STAD_user_whole > STAD_images.whole.length-1) STAD_user_whole = 0;
	$("#login_whole_icon").attr("src","http://www.luddweb.com/iploughlane/images/crowd/"+PNGfile(STAD_images.whole[STAD_user_whole].file));
	$("#login_whole_icon").css({
		"width":String(STAD_images.whole[STAD_user_whole].width)+"px",
		"left":String(40-STAD_images.whole[STAD_user_whole].width/2)+"px",
		"height":String(STAD_images.whole[STAD_user_whole].height)+"px"
	});
//	$("#file_whole").html(STAD_images.whole[STAD_user_whole].file);
	if (STAD_user_whole == 0) $("#login_whole_l").hide(); else $("#login_whole_l").show();
	if (STAD_user_whole == STAD_images.whole.length-1) $("#login_whole_r").hide(); else $("#login_whole_r").show();
	$("#login_whole_icon").show();
	return false;
}

function login_body() {
	login_stage = 2;
	if (login_ticket == 3) {
		$("#login_icon_3").hide();
		$("#login_icon_4").hide();
		$("#login_icon_6").hide();
	}
	else {
		$("#login_icon_3").show();
		$("#login_icon_4").show();
		$("#login_icon_6").show();
	}
	$("#login_code_div").hide();
	login_type_change(0); // first login
}


function login_type_change(new_type) {
	if (new_type > 0) STAD_response.user_type = new_type;
	$('#enter_button').attr("disabled", "disabled");
	$(".icon_selected").addClass("icon_unselected").removeClass("icon_selected");
	$("#login_icon_" + STAD_response.user_type).addClass("icon_selected").removeClass("icon_unselected");
//	$("#login_images_many").hide();
//	$("#login_images_whole").hide();
//	$("#login_images_buttons").hide();
	$("#login_images_wait").show();
	STAD_user_head = 0; STAD_user_body = 0; STAD_user_legs = 0; STAD_user_whole = 0;
	ajaxcall('getimages','user_id='+STAD_response.user_id+'&user_type='+STAD_response.user_type, false);
}

function response_getimages(msg) {
	if (msg > '') {
		STAD_images = eval("("+msg+")");
		if (STAD_response.user_type < 6) {
			if (STAD_has_defaults) {
				for(i=0;i<STAD_images.heads.length;i++) {
					if (STAD_images.heads[i].id == STAD_response.user_head) {
						STAD_user_head = i;
						break;
					}
				}
				for(i=0;i<STAD_images.bodies.length;i++) {
					if (STAD_images.bodies[i].id == STAD_response.user_body) {
						STAD_user_body = i;
						break;
					}
				}
				for(i=0;i<STAD_images.legs.length;i++) {
					if (STAD_images.legs[i].id == STAD_response.user_legs) {
						STAD_user_legs = i;
						break;
					}
				}
			}
			login_head_change(0); login_body_change(0); login_legs_change(0);
			$("#login_images_whole").hide();
			$("#login_images_many").show();
		}
		else {
			STAD_response.user_whole = 0;
			if (STAD_has_defaults) {
				for(i=0;i<STAD_images.whole.length;i++) {
					if (STAD_images.whole[i].id == STAD_response.user_whole) {
						STAD_user_whole = i;
						break;
					}
				}
			}
			login_whole_change(0);
			$("#login_images_many").hide();
			$("#login_images_whole").show();
		}
		$('#enter_button').attr("disabled", "");
		$("#images_wrapper").show();
		$("#wait_wrapper").hide();
	}
}

function call_checktag() {
    login_code = $.trim($("#logincode").val());
	if (login_code == '') 
		error_popup('Please enter a ticket code');
	else
		ajaxcall('checktag','login_code='+login_code, false);
	return false;
}

function response_checktag(msg) {
	if (msg > '') {
		STAD_response = eval("("+msg+")");
		if (STAD_response.exp == 1) {
			$("#checktag_response").html('Hi <strong>' + STAD_response.user_firstname + ' ' + STAD_response.user_lastname + '</strong>. Your ticket expired on ' + STAD_response.expdate + '. Until you <strong><a href="ticket.php">renew your ticket</a></strong>, you will only be able to enter as a guest.');
			login_init(4);
		}
		else
			login_init(1);
	}
	else {
		error_popup('Season ticket not found');
	}
}

function login_init(ttype) {
	$('.enter_button').attr("disabled", "disabled");
	if (ttype == 1) {
		$('#checktag_response').html('Welcome back <strong>' + STAD_response.user_firstname + ' ' + STAD_response.user_lastname + '</strong>. Your ticket is due to expire on ' + STAD_response.expdate);
		if (STAD_response.exp ==2) $('#checktag_response').append(' (<strong><a href="ticket.php">renew your ticket</a></strong>)');
		$('#checktag_response').append('.');
		if (STAD_response.user_admin == 1) $('#checktag_response').append(' You have stewarding rights.');
		$("#loginusername").val(STAD_response.user_usedname);
		$('.sp').removeAttr("disabled");
		if (STAD_response.user_tickettype == 3)
			$(".login_wall_only").show();
		else {
			$(".login_wall_only").hide();
			if (STAD_response.user_type == 7) STAD_response.user_type = 1;
		}
		STAD_has_defaults = (STAD_response.user_type > 0);
	}
	else {
		if (ttype ==4) // expired ticket
			ttype = 2;
		else
			$('#checktag_response').html('');
		$("#loginusername").val('');
		STAD_has_defaults = false;
		if (ttype == 2) {
			STAD_response = eval('({"user_id":"-2","user_type":"1","user_admin":"0"})');
			$('.hp').removeAttr("disabled");
			$(".login_no_oppo").show();
		}
		else {
			STAD_response = eval('({"user_id":"-1","user_type":"1","user_admin":"0"})');
			$('.op').removeAttr("disabled");
			$(".login_no_oppo").hide();
		}
		$(".login_wall_only").hide();
	}
	STAD_logged_in = true;
	$('#login_wrapper').hide();
	$('#welcome_wrapper').show();
	STAD_ticket = ttype;
	login_type_change();
}

function call_logout() {
	STAD_logged_in = false;
	$('#logincode').val('');
	$('#login_wrapper').show();
	$('#welcome_wrapper').hide();
}

function call_enter(stadid,tempid) {
	STAD_stadium = stadid;
	STAD_template = tempid;
	STAD_login_username = $.trim($("#loginusername").val());
	if (STAD_login_username == '') 
		error_popup('Please enter a name');
	else {
		STAD_character.ticket = STAD_ticket;
		STAD_character.stadium = stadid;
		STAD_character.template = tempid;
		STAD_character.type = STAD_response.user_type;
		STAD_character.user_id = STAD_response.user_id;
		STAD_character.user_name = STAD_login_username;
		STAD_character.admin = STAD_response.user_admin;
		if (STAD_response.user_type >= 6) {
			STAD_character.wholeid = STAD_images.whole[STAD_user_whole].id;
//			STAD_character.wholefile = STAD_images.whole[STAD_user_whole].file;
//			STAD_character.wholexpos = 40 - STAD_images.whole[STAD_user_whole].width/2;
			STAD_character.yoff = STAD_images.whole[STAD_user_whole].offset;
			STAD_character.avatar = (40-STAD_images.whole[STAD_user_whole].width/2) + "|" +STAD_images.whole[STAD_user_whole].file;
		}
		else {
			STAD_character.legsid = STAD_images.legs[STAD_user_legs].id;
//			STAD_character.legsfile = STAD_images.legs[STAD_user_legs].file;
//			STAD_character.legsxpos = 40 - STAD_images.legs[STAD_user_legs].width/2;
			STAD_character.bodyid = STAD_images.bodies[STAD_user_body].id;
//			STAD_character.bodyfile = STAD_images.bodies[STAD_user_body].file;
//			STAD_character.bodyxpos = 40 - STAD_images.bodies[STAD_user_body].width/2;
			STAD_character.headid = STAD_images.heads[STAD_user_head].id;
//			STAD_character.headfile = STAD_images.heads[STAD_user_head].file;
//			STAD_character.headxpos = 40 - STAD_images.heads[STAD_user_head].width/2;
			STAD_character.yoff = STAD_images.legs[STAD_user_legs].offset;
			STAD_character.avatar = (40-STAD_images.legs[STAD_user_legs].width/2) + "|" + STAD_images.legs[STAD_user_legs].file + "|" + STAD_images.legs[STAD_user_legs].height + "|" + (40-STAD_images.bodies[STAD_user_body].width/2) + "|" +STAD_images.bodies[STAD_user_body].file + "|" + STAD_images.bodies[STAD_user_body].height + "|" + (40-STAD_images.heads[STAD_user_head].width/2) + "|" +STAD_images.heads[STAD_user_head].file;
		}
		ajaxcall('enter',jQuery.param(STAD_character), false); 
	}
	return false;
}

function response_enter(msg) {
	if (msg > '') {
		STAD_enter_response = eval("("+msg+")");
		switch (STAD_enter_response.status) {
			case 1 : // stadium closed
				error_popup('Stadium is closed');
				break;
			case 2 : // stadium full
				if (STAD_response.user_type == 7)
					error_popup('The wall in this stand is full. Please try another stand or use a terrace character.');
				else
					error_popup('The terrace in this stand is full. Please try another stand.');
				break;
			case 3 : // asbo
				error_popup('You\'re barred');
				break;
			case 4 : // OK - you're in
				stadium_show();
				break;
			case 5 : // bad name
				error_popup('Your name can only contain letters, numbers and spaces and must be at least 3 characters long.');
				break;
		}
	}
}


Shadowbox.init({
    players:    ["html"]
});

$(document).ready(function(){
	$("#please_wait").hide();
	$("#stadium_wrapper").hide();
	$("#login_button").click(function() { return call_checktag(); });
	$("#donsguest_button").click(function() { login_init(2); });
	$("#oppoguest_button").click(function() { login_init(3); });
	$("#logout_button").click(function() { return call_logout(); });
	$(".enter_button").click(function(e) { return call_enter(e.target.id.substr(6,1),e.target.id.substr(8,1)); });
	$("#login_head_l").addClass('pointer').click(function() { login_head_change(-1); });
	$("#login_head_r").addClass('pointer').click(function() { login_head_change(1); });
	$("#login_body_l").addClass('pointer').click(function() { login_body_change(-1); });
	$("#login_body_r").addClass('pointer').click(function() { login_body_change(1); });
	$("#login_legs_l").addClass('pointer').click(function() { login_legs_change(-1); });
	$("#login_legs_r").addClass('pointer').click(function() { login_legs_change(1); });
	$("#login_whole_l").addClass('pointer').click(function() { login_whole_change(-1); });
	$("#login_whole_r").addClass('pointer').click(function() { login_whole_change(1); });
	$("#login_icon_1").addClass('pointer').click( function() { login_type_change(1); } );
	$("#login_icon_2").addClass('pointer').click( function() { login_type_change(2); } );
	$("#login_icon_3").addClass('pointer').click( function() { login_type_change(3); } );
	$("#login_icon_4").addClass('pointer').click( function() { login_type_change(4); } );
	$("#login_icon_6").addClass('pointer').click( function() { login_type_change(6); } );
	$("#login_icon_7").addClass('pointer').click( function() { login_type_change(7); } );

	groot = "http://www.luddweb.com/iploughlane/images";
	STAD_logged_in = false;
	STAD_character = new Object;
	call_logout();
});