$(document).ready(function() {

    $('#corporatesite, #countryselect').hover(function() {

        $(this).animate({
            backgroundColor: '#F4F9FC'
        }, 200, function() {
            // Animation complete.
        });

    }, function() {

        $(this).animate({
            backgroundColor: '#FFFFFF'
        }, 200, function() {
            // Animation complete.
        });

    });

    $('#change-country').click(function(e) {
        $('.row-country').removeClass('row-country');
        PopulateCountryServices($('#countries').val());
        $(this).closest('fieldset').css('height', '245px')
        e.preventDefault();
    });

    $('#countries').change(function(e) {
            PopulateCountryServices($(this).val());
     });

    $('#corporatesite-button').click(function(e) {
        $.ajax({
            async: false,
            type: 'GET',
            url: "/callback/site-chooser/remember/?Remember=" + true,
            dataType: "json",
            success: function(response) {
            },
            error: ajaxFailure
        });
    });


    $('#services').change(function(e) {
        $('#form-countryselect').attr('action', $('#services').val());
    });

    $('#form-countryselect').attr('action', $('#services').val());

});


function ajaxFailure(xhr, error) {
    alert('Error: ' + error + '\n' + xhr.responseText);
}

function PopulateCountryServices(countryCode) {

    $.ajax({
        async: false,
        type: 'GET',
        url: "/callback/site-chooser/choose/?CountryCode=" + countryCode,
        dataType: "json",
        success: function(response) {
            $("#services-list").html(response.Services);

            $('#services').change(function(e) {
                $('#form-countryselect').attr('action', $('#services').val());
            });

            $('#form-countryselect').attr('action', $('#services').val());
        },
        error: ajaxFailure
    });
    
}
