/* Minification failed. Returning unminified contents.
(1,1): run-time error CSS1019: Unexpected token, found '$'
(1,2): run-time error CSS1019: Unexpected token, found '('
(1,12): run-time error CSS1031: Expected selector, found '('
(1,12): run-time error CSS1025: Expected comma or open brace, found '('
(18,2): run-time error CSS1019: Unexpected token, found ')'
(21,10): run-time error CSS1031: Expected selector, found 'initMap('
(21,10): run-time error CSS1025: Expected comma or open brace, found 'initMap('
(56,10): run-time error CSS1031: Expected selector, found 'updateEventParticipants('
(56,10): run-time error CSS1025: Expected comma or open brace, found 'updateEventParticipants('
(86,10): run-time error CSS1031: Expected selector, found 'renderEventParticipants('
(86,10): run-time error CSS1025: Expected comma or open brace, found 'renderEventParticipants('
(126,10): run-time error CSS1031: Expected selector, found 'showEventParticipants('
(126,10): run-time error CSS1025: Expected comma or open brace, found 'showEventParticipants('
(132,10): run-time error CSS1031: Expected selector, found 'updateEventPrice('
(132,10): run-time error CSS1025: Expected comma or open brace, found 'updateEventPrice('
(152,10): run-time error CSS1031: Expected selector, found 'backupParticipants('
(152,10): run-time error CSS1025: Expected comma or open brace, found 'backupParticipants('
(179,10): run-time error CSS1031: Expected selector, found 'reloadParticipants('
(179,10): run-time error CSS1025: Expected comma or open brace, found 'reloadParticipants('
(203,10): run-time error CSS1031: Expected selector, found 'prefillNameForRegisteringUser('
(203,10): run-time error CSS1025: Expected comma or open brace, found 'prefillNameForRegisteringUser('
 */
$(function () {
    var getLat = $('#map').data('lat');
    var getlng = $('#map').data('lng');

    initMap(getLat, getlng);

    $(".will-participate").change(function () {
        updateEventParticipants(true);
    });

    $("#NumberOfParticipants").change(function () {
        updateEventParticipants(false);
    });

    if ($("#NumberOfParticipants").val()) {
        showEventParticipants();
    }
});


function initMap(lat, lng) {
	var map = new google.maps.Map(document.getElementById('map'), {
		scrollwheel: false,
		zoomControl: true,
		zoomControlOptions: {
			position: google.maps.ControlPosition.LEFT_TOP
		},
		zoom: 8,
		center: { lat: lat, lng: lng },
		mapTypeControl: true,
		mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
			position: google.maps.ControlPosition.BOTTOM_CENTER,
			mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
		}
	});

	map.mapTypes.set('map_style', new google.maps.StyledMapType(_mapStyle, { name: "Streetkings" }));
	map.setMapTypeId('map_style');

	var image = new google.maps.MarkerImage(
		"/Content/Images/marker_maps.png",
		null,
		null,
		null,
		new google.maps.Size(21, 34)
	);

	var marker = new google.maps.Marker({
		position: { lat: lat, lng: lng },
		map: map,
		icon: image
	});
}

function updateEventParticipants(isWillParticipateChange) {

    // Check if registering user will be playing
    var willParticipate = ($(".will-participate:checked").val() == "True");

    // Check current value of NumberOfParticipants
    var numberOfParticipants = $("#NumberOfParticipants").val() ? parseInt($("#NumberOfParticipants").val()) : 0;

    // Add or substract registering user from the participants
    if (isWillParticipateChange) {
        if (willParticipate) {
            numberOfParticipants = numberOfParticipants + 1;
        } else {
            numberOfParticipants = numberOfParticipants - 1;
        }
    }

    // Update dropdown with new value
    $("#NumberOfParticipants").val((numberOfParticipants == 0) ? "" : numberOfParticipants);

    // Get eventId from hidden element
    var eventId = $("#EventId").val();

    // Render participants blocks
    renderEventParticipants(eventId, willParticipate, numberOfParticipants);

    // Update the price based on participants
    updateEventPrice(eventId, numberOfParticipants);
}

function renderEventParticipants(eventId, willParticipate, numberOfParticipants) {

    // Show loader
    $('#tg-loader').show();

    // Temporary store particiapants
    var participants = backupParticipants();

    // Call action
    var options = {};
    options.url = "/Events/GetParticipantListPartialHtml";
    options.type = "POST";
    options.contentType = "application/json";
    options.data = JSON.stringify({ eventId: eventId, registeringUserWillParticipate: willParticipate, numberOfParticipants: numberOfParticipants });
    options.success = function (data) {

        // Hide loader
        //$('#tg-loader').hide();

        // Add html to div
        $("#event-participants").html(data.html);

        // Fix validation after dynamicly adding blocks
        $.validator.unobtrusive.parseDynamicContent($('#EventRegistrationForm'));

        // Reload back the participants
        reloadParticipants(participants);

        $('#is-registering-user-firstname').val($('#Firstname').val());
        $('#is-registering-user-lastname').val($('#Lastname').val());

        // Prefill the registering user's name in the first box if needed
        prefillNameForRegisteringUser();
    };
    $.ajax(options);

    // Show
    showEventParticipants();
}

function showEventParticipants() {

    // Show div with participants
    $("#event-participants").show();
}

function updateEventPrice(eventId, numberOfParticipants) {

    // Price total element only exists when the event pricetype is 'Per deelnemer'
    if ($("#event-price-total").length && numberOfParticipants > 0) {

        // Call action
        var options = {};
        options.url = "/Events/GetEventPrice";
        options.type = "POST";
        options.contentType = "application/json";
        options.data = JSON.stringify({ eventId: eventId, numberOfParticipants: numberOfParticipants });
        options.success = function (data) {

            // Update price total
            $("#event-price-total").html(data.priceTotal);
        };
        $.ajax(options);
    }
}

function backupParticipants() {

    var participants = {};
    var count = 0;

    // Loop over all block and create objects
    $('.tg-participant-block').each(function () {

        var idPrefix = "Participants_" + count + "__";

        // Get values
        var key = $(this).find(".tg-participant-title").html();
        var firstname = $(this).find("#" + idPrefix + "FirstName").val();
        var lastname = $(this).find("#" + idPrefix + "LastName").val();
        var club = $(this).find("#" + idPrefix + "Club").val();
        var team = $(this).find("#" + idPrefix + "Team").val();
        var knvb = $(this).find("#" + idPrefix + "KnvbNumber").val();

        // Add to array
        participants[key] = { firstname: firstname, lastname: lastname, club: club, team: team, knvb: knvb };

        count++;
    });

    return participants;
}

function reloadParticipants(participants) {

    var count = 0;

    // Loop over all blocks and fill form from array
    $('.tg-participant-block').each(function () {

        var idPrefix = "Participants_" + count + "__";
        var key = $(this).find(".tg-participant-title").html();

        if (participants[key] != undefined) {

            // Set back values
            $(this).find("#" + idPrefix + "FirstName").val(participants[key].firstname);
            $(this).find("#" + idPrefix + "LastName").val(participants[key].lastname);
            $(this).find("#" + idPrefix + "Club").val(participants[key].club);
            $(this).find("#" + idPrefix + "Team").val(participants[key].team);
            $(this).find("#" + idPrefix + "KnvbNumber").val(participants[key].knvb);
        }

        count++;
    });
}

function prefillNameForRegisteringUser() {

    // Firstname
    if ($("#Firstname").length &&
        $("#Firstname").val() &&
        $('.is-registering-user').find("#Participants_0__FirstName").length &&
        !$('.is-registering-user').find("#Participants_0__FirstName").val()) {
        $('.is-registering-user').find("#Participants_0__FirstName").val($("#Firstname").val());
    }

    // Lastname
    if ($("#Lastname").length &&
        $("#Lastname").val() &&
        $('.is-registering-user').find("#Participants_0__LastName").length &&
        !$('.is-registering-user').find("#Participants_0__LastName").val()) {
        $('.is-registering-user').find("#Participants_0__LastName").val($("#Lastname").val());
    }
}
