var sectionArray = [];

function update_total_remaining_tickets_and_check_sold_out (check)
{
	//      Count and populate total remaining tickets
	$.ajax({
		type: "GET",
		url: "/requests/get_total_remaining_tickets/",
		success: function (response)
		{
			$("span#totalRemainingTickets").html(response);
			if (response == 0)
			{
				$("div#signupForm").hide();
				$("div#signupEnd").show();
				$("span#ticketSectionH1").html("We're at Full Capacity");
				$("h2#ticketSectionH2").html("ALL 250 BRILLIANT TICKETS HAVE GONE");
			}
			Cufon.refresh();
		}
	});
}

function remove_form_error_classes ()
{
	$("#stu_forename").prev().removeClass('invalid_field');
	$("#stu_surname").prev().removeClass('invalid_field');
	$("#stu_number").prev().removeClass('invalid_field');
	$("#stu_email").prev().removeClass('invalid_field');
}

function clean_form ()
{
	$("#attendeeFormErrors").html("");
	$("#attendeeFormErrors").hide();
	remove_form_error_classes();
	$("#stu_forename").attr('value', '');
	$("#stu_surname").attr('value', '');
	$("#stu_number").attr('value', '');
	$("#stu_email").attr('value', '');
	$("div#signupFormButton").show();
	$("div#signupFormWait").hide();
}

function ticket_signup ()
{
	var params = {};
	params.stu_forename = $("#stu_forename").attr('value');
	params.stu_surname = $("#stu_surname").attr('value');
	params.stu_number = $("#stu_number").attr('value');
	params.stu_email = $("#stu_email").attr('value');
	var postVars = jQuery.param(params);
	//      Wait
	$("div#signupFormButton").hide();
	$("div#signupFormWait").show();
	//
	$.ajax({
		type: "POST",
		url: "/requests/ticket_signup/",
		data: postVars,
		success: function (response)
		{
			remove_form_error_classes();
			var parsedResponse = JSON.parse(response);
			//      If errors
			if (!parsedResponse.success)
			{
				var error_msg;
				//      Error type
				if (parsedResponse.error_type == "presense")
				{
					error_msg = "Please complete the form below to continue.";
					//      Field indicators
					for (field_id in parsedResponse.fields)
					{
						$('#' + parsedResponse.fields[field_id]).prev().addClass('invalid_field');
					}
				}
				else if (parsedResponse.error_type == "email")
				{
	console.log("mail");
					error_msg = "Please enter a valid email address to continue.";
					$('#stu_email').prev().addClass('invalid_field');
				}
				else if (parsedResponse.error_type == "number")
				{
					error_msg = "Please enter a valid student ID to continue."
					$('#stu_number').prev().addClass('invalid_field');
				}
				else if (parsedResponse.error_type == "exists")
				{
					error_msg = "It looks like you are already signed up for the event.";
				}
				else
				{
					error_msg = "Something went wrong, maybe give it another go.";
				}
				$("#signupErrorCopy").html("<p>" + error_msg + "</p>");
				$("#signupErrorCopy").addClass('error');
				$("div#signupFormButton").show();
				$("div#signupFormWait").hide();
			}
			else
			{
				//      Swap content
				$("div#signupForm").fadeOut(500, function()
				{
					clean_form();
					$("div#signupComplete").fadeIn(500);
				});
				//      Update remaining tickets
				update_total_remaining_tickets_and_check_sold_out(false);
			}
		},
		error: function ()
		{
			$("#signupErrorCopy").html("<p>Woops, something went wrong, maybe give it another go.</p>");
			$("#signupErrorCopy").addClass('error');
			$("div#signupFormButton").show();
			$("div#signupFormWait").hide();
		}
	});
}

function navigateToSection (sectionName)
{
	var elStr = (sectionName == sectionArray[0]) ? 0 : $('div#section_' + sectionName).offset().top;
	$('html, body').animate({ scrollTop: elStr }, 500);
	return false;
}

function navigateToAnchor (anchorID)
{
	$('html, body').animate({ scrollTop: $('div#' + anchorID).offset().top });
	return false;
}

$(document).ready(function()
{
	//      Setup nav actions
	$('div#navigation ul li a').each(function (i)
	{
		//      Populate section array with current link
		sectionArray.push($(this).attr('id'));
		$(this).click(function ()
		{
			navigateToSection(sectionArray[i]);
			pageTracker._trackEvent('Navigation', 'click', $(this).attr('id'));
		});
	});

	//      Config section id's
	$('div.section').each(function (i)
	{
		$(this).attr('id', 'section_' + sectionArray[i+1]);
	});

	//      Grab url section and navigate to section
	if ($("#onLoadAutoScrollToSection").html() != 'None')
	{
		navigateToSection($("#onLoadAutoScrollToSection").html());
		pageTracker._trackEvent('Navigation', 'fromURL', $("#onLoadAutoScrollToSection").html());
	}

	update_total_remaining_tickets_and_check_sold_out(true);

	//      Setup header image hotspots
	//      Generic hotspot
	$("div.hotspot").hover(function ()
	{
		$(this).css('cursor', 'pointer');
		$(this).find(".tooltip").show();
	}, function ()
	{
		$(this).css('cursor', 'none');
		$(this).find(".tooltip").hide();
	});
	//      Hotspot events
	$("div#headerImageUniSpot").click(function ()
	{
		window.open("http://www.hud.ac.uk");
		pageTracker._trackEvent('Hotspots', 'click', 'university logo');
	});
	$("div#headerImageLeftSpot").click(function ()
	{
		navigateToAnchor('anchorWhere');
		pageTracker._trackEvent('Hotspots', 'click', 'where');
	});
	$("div#headerImageMiddleSpot").click(function ()
	{
		navigateToSection('about');
		pageTracker._trackEvent('Hotspots', 'click', 'about');
	});
	$("div#headerImageRightSpot").click(function ()
	{
		navigateToAnchor('anchorWOF');
		pageTracker._trackEvent('Hotspots', 'click', 'graduate wheel of fortune');
	});

	//	Surprise over
	$("div#surprise").hover(function ()
	{
		$(this).find("a").find("img").attr('src', '/media/assets/images/surprise_out_over.png');
	}, function ()
	{
		$(this).find("a").find("img").attr('src', '/media/assets/images/surprise_out.png');
	});

	//	Buttons over
	$("#wofButton").hover(function ()
	{
		$(this).attr('src', '/media/assets/images/button_contribute_now_over.png');
	}, function ()
	{
		$(this).attr('src', '/media/assets/images/button_contribute_now_up.png');
	});
	$("#signupButton").hover(function ()
	{
		$(this).attr('src', '/media/assets/images/button_form_submit_over.png');
	}, function ()
	{
		$(this).attr('src', '/media/assets/images/button_form_submit_up.png');
	});
	$(".becomeFanButton").hover(function ()
	{
		$(this).attr('src', '/media/assets/images/button_become_fan_over.png');
	}, function ()
	{
		$(this).attr('src', '/media/assets/images/button_become_fan_up.png');
	});

});


