$(function(){

    var token          = $('.token').val(),
        oauth_verifier = $('.oauth_verifier').val(),
        oauth_token    = $('.oauth_token').val()


    confirmArrival();

    function changeAvatar() {
        var sendData = {
            "action" : "changeAvatar",
            "data"   : {
                "oauth_token"    : oauth_token,
                "oauth_verifier" : oauth_verifier
            }
        };

        $.ajax({
            type: 'POST',
            url: 'gateway.php',
            data: sendData,
            dataType: 'json',
            success : function(ret) {
                if(!ret.error) {
                    $('.step-holder').html(ret.data);
                }
            }
        });
    }

    function confirmArrival() {
        var sendData = {
            "action" : "confirmArrival",
            "data"   : {
                "token" : token
            }
        };

        $.ajax({
            type: 'POST',
            url: 'gateway.php',
            data: sendData,
            dataType: 'json',
            success : function(ret) {
                if(!ret.error) {
                    $('.step-holder').html(ret.data);
                } else {
                    top.location.href = '/';
                }
            }
        });
    }

    $(document).on("click",".arrival-confirmation a",function(){

        var status = $(this).attr('data-status');
        var sendData = {
            "action" : "updateArrivalStatus",
            "data"   : {
                "arrivalStatus" : status
            }
        };

        $.ajax({
            type: 'POST',
            url: 'gateway.php',
            data: sendData,
            dataType: 'json',
            success : function(ret) {
                if(!ret.error) {
                    $('.step-holder').html(ret.data);
                    if(status == 1) {
                        $('.arrival-confirmation').remove();
                        chooseTransport();
                    } else {
                        arrivalDenial();
                    }
                }
            }
        });
    });

    function arrivalDenial() {
        var sendData = {
            "action" : "arrivalDenial"
        };

        $.ajax({
            type: 'POST',
            url: 'gateway.php',
            data: sendData,
            dataType: 'json',
            success : function(ret) {
                if(!ret.error) {
                    $('.step-holder').html(ret.data);
                }
            }
        });
    }

    function chooseTransport() {
        var sendData = {
            "action" : "chooseTransport"
        };

        $.ajax({
            type: 'POST',
            url: 'gateway.php',
            data: sendData,
            dataType: 'json',
            success : function(ret) {
                if(!ret.error) {
                    $('.step-holder').html(ret.data);
                }
            }
        });
    }


    $(document).on("click",".choose-transport a",function(){

        var transport = $(this).attr('data-transport');
        var sendData = {
            "action" : "updateTransport",
            "data"   : {
                "transport" : transport
            }
        };

        $.ajax({
            type: 'POST',
            url: 'gateway.php',
            data: sendData,
            dataType: 'json',
            success : function(ret) {
                if(!ret.error) {
                    $('.step-holder').html(ret.data);
                    $('.choose-transport').remove();
                    //top.location.href = 'http://localhost/dn_registration_vip/changeavatar/';
                } else {
                    console.log(ret.error);
                }
            }
        });
    });


});