$(document).ready(function(){

    var timer = setInterval( updateDisplay, 5001);
    var timer_master_progress = setInterval( updateMasterprogress, 5002);

    $("button").click(function(){

        if ( $(this).attr("class") == "navbar-toggle collapsed") {

        return;
        };

        clicked = $(this).attr("name");
        console.log(clicked)
        class_val = $(this).attr("class");
        id = $(this).attr("id");

        req = $.ajax({
                type : 'POST',
                url : "/",
                data : {'data':clicked}
        });
        $('#'+id).fadeOut(100).fadeIn(300);

        req.done(function(data) {
             // console.log(data);
              console.log(data.result);
              console.log(data.button);

             if (class_val == 'btn-primary') {
                $('#'+id).addClass('btn btn-danger');
                $('#'+id).removeClass('btn-primary');
            };

            if (class_val == 'btn btn-primary') {
                        $('#'+id).addClass('btn btn-danger');
                        $('#'+id).removeClass('btn btn-primary');
            };

            if (class_val == 'btn-danger') {
                        $('#'+id).addClass('btn btn-primary');
                        $('#'+id).removeClass('btn-danger');
            };

            if (class_val == 'btn btn-danger') {
                        $('#'+id).addClass('btn btn-primary');
                        $('#'+id).removeClass('btn btn-danger');
            };

            if (data.former_button_to_switch) {

                        var num = data.former_button_to_switch
                        console.log('auto_click '+num)
                        $("#"+num).click(); // Click on the button
            };


                if (data.result == 'deactivate_audio') {
                    console.log('deactivate_audio')

                        audio_control = 'audio_with_controls';
                        myAudio = document.getElementById(audio_control);
                        myAudio.pause();
                        myAudio.src = "";
                        myAudio.load()
                };

                if (data.result == 'activate_audio') {
                    console.log('activate_audio')

                        url = data.query_url;
                        console.log(url);
                        audio_control = 'audio_with_controls';
                	    myAudio = document.getElementById(audio_control);
                	    myAudio.pause();
                	    myAudio.src = "";
                	    // myAudio.load()

                	    myAudio.src = url;
                        myAudio.volume = 0.25;
                	    myAudio.load()
                	    myAudio.play = true;

                	    // myAudio.muted  = true;
                };

        });


	});

	function updateDisplay() {
        var req;
        var clicked;
        var id;
        var class_val;
        req = $.ajax({
                type: 'GET',
                url: "/display_info",
                cache: false,
        });

        req.done(function(data) {
            var displays = '';
            var display = '';
            var table_id = '';
            var title = '';

            displays = data.result.split(",");
                $.each(displays, function(idx, val) {

                    display = val;

                    if (display.length != 0) {

                        display = val.split("=");
                        table_id = display[0]
                        title = display[1]
                        if (title != 'Null') {
                        $('#Display_'+table_id).attr("value", title) ;
                        }
                        if (title == 'Null') {
                        $('#Display_'+table_id).attr("value", '') ;
                        }
                        // console.log(title)

                    }

                });

        });


    };

	function updateMasterprogress() {
        var req;

        req = $.ajax({
                type: 'POST',
                url: "/index_posts_percent",
                cache: false,
                data : {'percent': 'percent'}
        });

        req.done(function(data) {
            var percent = '';

            percent = data.result;
            // console.log('########### '+percent+' #########');
            if (percent == 0) {
                    $('.progress-bar').css('width', 25+'%').attr('aria-valuenow', 25).html('Timer Off');
                }
            if (percent != 0) {
                $('.progress-bar').css('width', percent+'%').attr('aria-valuenow', percent).html('Run, Forrest! RUN!');
                if (percent >= 100) {
                    window.location.href = "/page_flash";
                }
            }

        });


    };

});