jQuery(document).ready( function(){
    var sortInfo = new Array();
    setTimeout(function () {
        openForm();
    }, 100);
    $('.sortable').on("click", function(){
        var sort, sortobj, data, url;
        sortobj = $(this).attr("id");
        $(this).find("p").removeClass(sortInfo[sortobj]);
        sort = sortInfo[sortobj];
        if(sort == "ASC") {
            sort = "DESC";
        } else {
            sort = "ASC";
        }
        $(this).find("p").addClass(sort);
        url = $(this).parents('table').data('url');
        data = sortobj+' '+sort;
        $.post( url,{ data:data}, function( data ) {
            $("#result").html( data );
            sortInfo[sortobj]= sort;
        });
    });


    $('.back-office-block').on('change', ".form-input", function(){

       var input = $(this);
        setTimeout(function(){
            var state = input.parent('label').attr("class");
            var parent_id = input.data('parent');
            var li = input.closest('ul').children("li");

            if(state=="label_checkbox c_on" || state=="label_radio r_on") {
                selectChosenData(parent_id,li);

            }
            setTimeout(function () {
                styleInputs();
                openForm();
                li.children("ul").each(function() {
                    if(state=="label_checkbox c_on" || state=="label_radio r_on") {
                        $(this).css("display", "block");
                    } else if(state=="label_checkbox c_off" || state=="label_radio r_off"){
                        $(this).remove();
                    }
                });
                li.children(".additional-question").each(function() {
                    var key = $(this).data('key');

                    if(state=="label_checkbox c_on" || state=="label_radio r_on" ) {
                        if(CheckAllInputs(li, key)){
                            $(this).css("display", "block");
                            $(this).addClass('status-display');
                        }
                    } else {
                        $(this).css("display", "none");
                        $(this).removeClass('status-display');
                    }
                });

            }, 450);
        }, 200);
        setTimeout(function(){

            $('#send-form').find('.form-input').each(function(){
                var state = $(this).parent('label').attr("class");
                var li = $(this).closest('ul').children("li");
                var question = $('#question'+$(this).data('parent')).closest('.additional-question').html();
                li.children(".additional-question").each(function() {
                    var key = $(this).data('key');
                    if(state=="label_checkbox c_on" || state=="label_radio r_on" ) {
                        if(CheckAllInputs(li, key)){
                            $(this).css("display", "block");
                            $(this).addClass('status-display');
                        }
                    }
                });
            });
        },1500)



    });




    function openForm(){
        $('#send-form').find('.form-input').each(function(){
            var states = $(this).parent('label').attr("class");
            var parents_id = $(this).data('parent');
            var sub_li = $(this).closest('ul').children("li");
            var children = sub_li.children("ul").length;
            if((states=="label_checkbox c_on" || states=="label_radio r_on") && children== 0 ) {
                selectChosenData(parents_id,sub_li);
            }
            setTimeout(function () {
                styleInputs();
                sub_li.children("ul").each(function() {

                    if(states=="label_checkbox c_on" || states=="label_radio r_on") {
                        $(this).css("display", "block");
                    } else {
                        $(this).css("display", "none");
                    }
                });
                sub_li.children(".additional-question").each(function() {
                    var key = $(this).data('key');
                    if(states=="label_checkbox c_on" || states=="label_radio r_on" ) {
                        if(CheckAllInputs(sub_li, key)){
                            $(this).css("display", "block");
                            $(this).addClass('status-display');
                        }
                    }
                });

            }, 450);
        })

    }


    function CheckAllInputs(li, key){
        var status = true;

        li.closest('form').find('.additional-question').each(function(){
            if($(this).data('key')==key && $(this).attr('class') == 'additional-question status-display'){
                   status =  false;
            }

        });

        return status;

    }
    $('#next-button').click(function(){
        var checkbox_stat = false;
        $('#send-form').find('.form-input').each(function(){
            if($(this).prop('checked'))
                return checkbox_stat = true;
        });
        if(checkbox_stat){
            if($('#send-form').valid()){
                $('#questionnaire').css('position', 'absolute').animate({
                    opacity: 0.0,
                    left: "9999"
                }, 1000, function() {
                    // Animation complete.
                });
                $('#address-field').animate({
                    left: "225"
                }, 1000, function() {
                    // Animation complete.
                }).find('#email').addClass('required');


            }

            var scrollTo = $("input.error").first();

            if(!scrollTo.length){
                scrollTo = $("#top");
            }
            console.log(scrollTo);
            $("html, body").animate({
                scrollTop: scrollTo.offset().top-50
            }, 800);
            $(this).closest('#send-form').find('.form-input').each(function(){
                var value = $(this).prop("checked");
                $(this).closest('ul').find("input").each(function() {
                    if(!value) {
                        $(this).prop("checked", false);
                    }
                });
            });
        }
    });

   function selectChosenData(parent_id,li){


       $.ajax({
           type: "POST",
           url: '/count_form.php/?p=form&sp=index&a=selectChosenData',
           data: "parent_id="+parent_id,
           dataType: "text",
           async: false,
           success: function (data){li.append(data);}
       });


   }


////////////////////////////////////////////////////////////////////////////////
    var d = document;
    var safari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1) ? true : false;
    var gebtn = function(parEl,child) { return parEl.getElementsByTagName(child); };
    function styleInputs(){
        var body = gebtn(d,'body')[0];

        if (!d.getElementById || !d.createTextNode) return;
        var ls = gebtn(d,'label');
        for (var i = 0; i < ls.length; i++) {
            var l = ls[i];
            if (l.className.indexOf('label_') == -1) continue;
            var inp = gebtn(l,'input')[0];
            if (l.className == 'label_checkbox') {
                l.className = (safari && inp.checked == true || inp.checked) ? 'label_checkbox c_on' : 'label_checkbox c_off';
            };
            if (l.className == 'label_radio') {
                l.className = (safari && inp.checked == true || inp.checked) ? 'label_radio r_on' : 'label_radio r_off';
            };
        };
    }
    ///////////////////////////////////////////////////////////////////////////////

});