$(document).ready(function(){
    "use strict";
    $('#ci_update_now.prevent_default').on('click', function(e){
        if($(this).hasClass('prevent_default')){
            e.preventDefault();
            let url = $(this).attr('href');
            let btnUpgrade = $(this);
            if(btnUpgrade.html() == 'UPDATE NOW'){
                btnUpgrade.html('Downloading may take a while, please don\'t click anywhere');
            }else{
                btnUpgrade.html('installing...');
            }

            $.ajax({
                type: "GET",
                url: url,
                dataType: 'json',
                success: function(response){ 
                    $('#ci_message').html(response.message).effect('bounce');
                    btnUpgrade.attr('href', response.action)
                    btnUpgrade.html(response.caption)

                    if((response.status != 'success') || (response.caption == "Login Now")){
                        btnUpgrade.removeClass('prevent_default');
                    }
                },
                error: function(response, v){
                    $('#ci_message').addClass('error');
                    $('#ci_message').html(response.statusText).effect('bounce');
                }
            });
        }
    });
});