$(document).ready(function(){
        
    $('#finishTest').live('click',function(){
        //$.get('/frontend_dev.php/miniTest/finish.html',
            $.get('/miniTest/finish.html',
            null, function(data){
                if(data.message.type=='error'){
                    $('#askPanel').html(data.message.content);
                    $('#askPanel' ).dialog({
                        modal: true,
                        resizable: false,
                        draggable: false,
                        title: '',
                        width: 380,
                        height: 150,
                        buttons: {
                            'Nie, chcę edytować aktualny': function() {
                                $( this ).dialog( "close" );
                            },
                            "Tak, kontynuuj": function() {
                                //$.get('/frontend_dev.php/miniTest/finish.html',
                                $.get('/miniTest/finish.html',
                                {
                                    force: false
                                },
                                function(data){
                                    if(data.message.type=='error'){
                                        showErrorMessagePanel(data);
                                    }else{
                                        $('#askPanel').dialog('close');                    
                                        refreshCreatedTest();
                                    }
                                },'json')
                            }
                        }
                    });
                }else{
                    $('#panel').dialog('close');                    
                    refreshCreatedTest();
                }
            }, 'json')
    })
        
    $('#testPicker').children('span').click(function(){
        var tid=$(this).attr('id').split('_');
            
        $.get('/miniTest/load.html',
        //$.get('/frontend_dev.php/miniTest/load.html',
        {
            tid: tid[1]
        },
        function(data){
            if(data.message.type=='error'){
                $('#askPanel').html(data.message.content);
                $('#askPanel' ).dialog({
                    modal: true,
                    resizable: false,
                    draggable: false,
                    title: '',
                    width: 380,
                    height: 150,
                    buttons: {
                        'Nie, chcę edytować aktualny': function() {
                            $( this ).dialog( "close" );
                        },
                        "Tak, kontynuuj": function() {
                            $.get('/miniTest/load.html',
                            //$.get('/frontend_dev.php/miniTest/load.html',
                            {
                                tid: tid[1],
                                force: true
                            },
                            function(data){
                                if(data.message.type=='error'){
                                    showErrorMessagePanel(data);
                                }else{
                                    $('#askPanel').dialog('close');                    
                                    refreshCreatedTest();
                                }
                            },'json')
                        }
                    }
                });
            }else{
                $('#panel').dialog('close');                    
                refreshCreatedTest();
            }
        }, 'json')
            
    })
    
    $('.myTests').hover(function(){
        $(this).addClass('opacity30 relative');
        var choosenTest=$(this)
        var position=$(this).position();
        $('#testPicker').children('span').attr('id',choosenTest.attr('id'));
        $('#testPicker').removeClass('hided').addClass('absolute').attr('style','left: '+(position.left+0)+'px; top: '+(position.top-10)+'px;');
    }, function(){
        $(this).removeClass('opacity30 relative')
    })
    
    
    // Testy
    $('#newTest').live('click',function(){
        $.get('/miniTest/new.html', null,
            //$.get('/frontend_dev.php/miniTest/new.html', null,
            function(data){
                if(data.message.type=='error'){
                    $('#askPanel').html(data.message.content);
                    $('#askPanel' ).dialog({
                        modal: true,
                        resizable: false,
                        draggable: false,
                        title: '',
                        width: 380,
                        height: 150,
                        buttons: {                            
                            'Nie, chcę edytować aktualny': function() {
                                $( this ).dialog( "close" );
                            },
                            "Tak, zakończ mój test": function() {
                                $.get('/miniTest/new.html',
                                {
                                    force: true
                                },
                                function(data){
                                    if(data.message.type=='error'){
                                        showErrorMessagePanel(data);
                                    }else{
                                        $('#askPanel').dialog('close');
                                        $('#panel').html(data.message.content);
                                        showTestPanel('Nowy test')
                                    }
                                },'json')
                            }
                        }
                    });
                }else{ 
                    $('#panel').html(data.message.content);
                    showTestPanel('Nowy test'); 
                }
            },'json')
    })
    
    $('#createTest').live('click',function(){
        var form=$(this).siblings('form').serialize();        
        //$.post('/frontend_dev.php/miniTest/create.html', form,
        $.post('/miniTest/create.html', form,
            function(data){                
                if(data.message.type=='error'){
                    showErrorMessagePanel(data);
                }else if(data.message.type=='nonvalid'){
                    $('#panel').html(data.message.content);
                }else{
                    $('#panel').dialog('close');                    
                    refreshCreatedTest();
                }
            }, 'json')
    })
    
    $('#editTest').live('click',function(){ 
        $.get('/miniTest/edit.html', null,
            //$.get('/frontend_dev.php/miniTest/edit.html', null,
            function(data){
                if(data.message.type=='error'){
                    showErrorMessagePanel(data);
                }else if(data.message.type=='nonvalid'){
                    $('#panel').html(data.message.content);
                    showTestPanel('Edycja')                  
                }else{
                    $('#panel').dialog('close');                    
                    refreshCreatedTest();
                }
            }, 'json')
    })
    
    $('#updateTest').live('click',function(){
        var form=$(this).siblings('form').serialize();        
        $.post('/miniTest/update.html', form,
            //$.post('/frontend_dev.php/miniTest/update.html', form,
            function(data){
                if(data.message.type=='error'){
                    showErrorMessagePanel(data);
                }else if(data.message.type=='nonvalid'){
                    $('#panel').html(data.message.content);
                }else{
                    $('#panel').dialog('close');                    
                    refreshCreatedTest();
                }
            }, 'json')
    })
    
    
    // Pytania
    $('#newQuestion').live('click',function(){
        $.get('/miniQuestion/new.html', null,
            //$.get('/frontend_dev.php/miniQuestion/new.html', null,
            function(data){
                if(data.message.type=='error'){
                    showErrorMessagePanel(data);
                }else if(data.message.type=='nonvalid' || data.message.type=='new'){
                    $('#panel').html(data.message.content);
                    showQuestionPanel('Nowe pytanie')
                }
            }, 'json')
    })
    
    $('#createQuestion').live('click',function(){
        var form=$(this).siblings('form').serialize();
        
        $.post('/miniQuestion/create.html', form,
            //$.post('/frontend_dev.php/miniQuestion/create.html', form,
            function(data){                
                if(data.message.type=='error'){
                    showErrorMessagePanel(data);
                }else if(data.message.type=='nonvalid'){
                    $('#panel').html(data.message.content);
                }else{
                    $('#panel').dialog('close');                    
                    refreshCreatedTest();
                }
            }, 'json')
    })
    
    $('.editQuestion').live('click',function(){ 
        var questionId= null;
        if ($(this).attr('name') !== undefined) {
            questionId=$(this).attr('name');
        }
        
        $.get('/miniQuestion/edit.html', {
            //$.get('/frontend_dev.php/miniQuestion/edit.html', {
            qid: questionId
        },
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else if(data.message.type=='nonvalid'){
                $('#panel').html(data.message.content);
                showQuestionPanel('Edycja')                  
            }else{
                $('#panel').dialog('close');                    
                refreshCreatedTest();
            }
        }, 'json')
    })
    $('#updateQuestion').live('click',function(){
        var form=$(this).siblings('form').serialize();        
        $.post('/miniQuestion/update.html', form,
            //$.post('/frontend_dev.php/miniQuestion/update.html', form,
            function(data){
                if(data.message.type=='error'){
                    showErrorMessagePanel(data);
                }else if(data.message.type=='nonvalid'){
                    $('#panel').html(data.message.content);
                }else{
                    $('#panel').dialog('close');                    
                    refreshCreatedTest();
                }
            }, 'json')
    })
    
    $('.removeMyQuestion').live('click',function(){
        //$.post('/frontend_dev.php/miniQuestion/remove.html', 
        $.post('/miniQuestion/remove.html', 
        {
            qid: $(this).attr('id')
        }, 
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else if(data.message.type=='nonvalid'){
                $('#panel').html(data.message.content);
            }else{
                $('#panel').dialog('close');                    
                refreshCreatedTest();
            }
        }, 'json')
    })
    
    $('.deactivateMyQuestion').live('click',function(){
        //$.get('/frontend_dev.php/miniQuestion/deactivate.html', 
        $.get('/miniQuestion/deactivate.html', 
        {
            qid: $(this).attr('id')
        }, 
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else if(data.message.type=='nonvalid'){
                $('#panel').html(data.message.content);
            }else{
                $('#panel').dialog('close');                    
                refreshCreatedTest();
            }
        }, 'json')
    })
    
    $('.activateMyQuestion').live('click',function(){
        //$.get('/frontend_dev.php/miniQuestion/activate.html', 
        $.get('/miniQuestion/activate.html', 
        {
            qid: $(this).attr('id')
        }, 
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else if(data.message.type=='nonvalid'){
                $('#panel').html(data.message.content);
            }else{
                $('#panel').dialog('close');                    
                refreshCreatedTest();
            }
        }, 'json')
    })
    
    
    // Odpowiedzi
    $('.newAnswer ').live('click',function(){
        var qid=$(this).attr('name')
        $.get('/miniAnswer/new.html', {
            //$.get('/frontend_dev.php/miniAnswer/new.html', {
            qid: qid
        },
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else if(data.message.type=='nonvalid' || data.message.type=='new'){
                $('#panel').html(data.message.content);
                showAnswerPanel('Nowa odpowiedź')
            }
        }, 'json')
    })
    
    $('#createAnswer').live('click',function(){
        var form=$(this).siblings('form').serialize();
        
        $.post('/miniAnswer/create.html', form,
            //$.post('/frontend_dev.php/miniAnswer/create.html', form,
            function(data){                
                if(data.message.type=='error'){
                    showErrorMessagePanel(data);
                }else if(data.message.type=='nonvalid'){
                    $('#panel').html(data.message.content);
                }else{
                    $('#panel').dialog('close');                    
                    refreshCreatedTest();
                }
            }, 'json')
    })
    
    $('.editAnswer').live('click',function(){ 
        var aid= null;
        if ($(this).attr('name') !== undefined) {
            aid=$(this).attr('name');
        }
        
        $.get('/miniAnswer/edit.html', {
            //$.get('/frontend_dev.php/miniAnswer/edit.html', {
            aid: aid
        },
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else if(data.message.type=='nonvalid'){
                $('#panel').html(data.message.content);
                showAnswerPanel('Edycja')                  
            }else{
                $('#panel').dialog('close');                    
                refreshCreatedTest();
            }          
        }, 'json')
    })
    $('#updateAnswer').live('click',function(){
        var form=$(this).siblings('form').serialize();        
        $.post('/miniAnswer/update.html', form,
            //$.post('/frontend_dev.php/miniAnswer/update.html', form,
            function(data){
                if(data.message.type=='error'){
                    showErrorMessagePanel(data);
                }else if(data.message.type=='nonvalid'){
                    $('#panel').html(data.message.content);
                }else{
                    $('#panel').dialog('close');                    
                    refreshCreatedTest();
                }
            }, 'json')
    }) 
    
    $('.removeMyAnswer').live('click',function(){
        $.post('/frontend_dev.php/miniAnswer/remove.html', 
        //$.post('/miniAnswer/remove.html', 
        {
            aid: $(this).attr('id')
        }, 
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else{
                $('#panel').dialog('close');                    
                refreshCreatedTest();
            }        
        }, 'json')
    })
    
    $('.nonValidMyAnswer').live('click',function(){
        //$.get('/frontend_dev.php/miniAnswer/nonValid.html', 
        $.get('/miniAnswer/nonValid.html', 
        {
            aid: $(this).attr('id')
        }, 
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else{
                $('#panel').dialog('close');                    
                refreshCreatedTest();
            }          
        }, 'json')
    })
    
    $('.validMyAnswer').live('click',function(){
        //$.get('/frontend_dev.php/miniAnswer/valid.html', 
        $.get('/miniAnswer/valid.html', 
        {
            aid: $(this).attr('id')
        }, 
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else{
                $('#panel').dialog('close');                    
                refreshCreatedTest();
            }      
        }, 'json')
    })
    
    
    // Builder
    
    $('#testBuilder').click(function(){
        //$.get('/frontend_dev.php/miniTestBuilder/showPanel.html', 
        $.get('/miniTestBuilder/showPanel.html', 
            null, 
            function(data){
                if(data.message.type=='error'){
                    showErrorMessagePanel(data);
                }else{
                    $('#builderPanel').html(data.message.content)
                    showBuilderPanel('Generator testów')                    
                }                
            }, 'json')
    })
    
    //
    $('#test').live('keyup',function(key){
        if (this.value.length >= 2 || this.value == ''){
            var formularz=$(this).parents('form').serialize()
            $('#loaderPojecia').show();
            //$.post('/frontend_dev.php/miniTestBuilder/availableTests.html',formularz,function(data){
            $.post('/miniTestBuilder/availableTests.html',formularz,function(data){
                $('#availableTests').html(data);
                $('#loaderPojecia').hide();
            },'html')
        }
    });
    
    $('.availableTest').live('click',function(){
        var availableTestId=$(this).attr('name');
        //$.get('/frontend_dev.php/miniTestBuilder/chooseTest.html',{
        $.get('/miniTestBuilder/chooseTest.html',{
            id: availableTestId
        }, 
        function(data){
            $('#choosenTestPanel').html(data)
        }, 'html')
    })
    
    if( $('.availableQuestion').length>0){
        $('.availableQuestion').draggable()
    }
    
    $('.removeQuestion').live('click',function(){
        //$.post('/frontend_dev.php/miniTestBuilder/removeQuestion.html', 
        $.post('/miniTestBuilder/removeQuestion.html', 
        {
            qid: $(this).attr('id')
        }, 
        
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else{
                $('#builderMainPanel').html(data.message.content);
            }
        }, 'json')
    })
    
    $('.deactivateQuestion').live('click',function(){
        //$.get('/frontend_dev.php/miniTestBuilder/deactivateQuestion.html', 
        $.get('/miniTestBuilder/deactivateQuestion.html', 
        {
            qid: $(this).attr('id')
        }, 
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else{
                $('#builderMainPanel').html(data.message.content);
            }
        }, 'json')
    })
    
    $('.activateQuestion').live('click',function(){
        //$.get('/frontend_dev.php/miniTestBuilder/activateQuestion.html', 
        $.get('/miniTestBuilder/activateQuestion.html', 
        {
            qid: $(this).attr('id')
        }, 
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else{
                $('#builderMainPanel').html(data.message.content);
            }
        }, 'json')
    })
    
    $('.removeableAnswer').live('click',function(){
        //$.post('/frontend_dev.php/miniTestBuilder/removeAnswer.html', 
        $.post('/miniTestBuilder/removeAnswer.html', 
        {
            aid: $(this).attr('id')
        }, 
        function(data){
            if(data.message.type=='error'){
                showErrorMessagePanel(data);
            }else{
                $('#builderMainPanel').html(data.message.content);
            }
        }, 'json')
    })
    
})

function refreshCreatedTest(){
    $.get('/miniTest/show.html',
        //$.get('/frontend_dev.php/miniTest/show.html',
        null, function(data){
            $('#viewTestPanel').html(data);
        }, 'html')
}

function showErrorMessagePanel(data){
    $('#panel').dialog('close');
    //$('#messagePanel').html('<img src="http://wiedza24/new_layout_images/errorIcon.png" />'+data.message.content)
    $('#messagePanel').html(data.message.content)
    $('#messagePanel' ).dialog({
        modal: true,
        title: 'Błąd',
        resizable: false,
        draggable: false,
        width: 380,
        height: 150,
        buttons: {
            'Zamknij': function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

function showTestPanel(title){
    $('#panel').dialog({
        modal: true, 
        width: 540, 
        height: 420,
        title: title,
        draggable: false,
        resizable: false
    });
}

function showQuestionPanel(title){    
    $('#panel').dialog({
        modal: true, 
        width: 540, 
        height: 220,
        title: title,
        draggable: false,
        resizable: false
    });
}

function showAnswerPanel(title){
    $('#panel').dialog({
        modal: true, 
        width: 540, 
        height: 300,
        title: title,
        draggable: false,
        resizable: false
    });
}

function showBuilderPanel(title){
    $('#builderPanel').dialog({
        modal: true, 
        width: 930, 
        height: 550,
        title: title,
        draggable: false,
        resizable: false,
        close: function(event, ui) {
            refreshCreatedTest();
        }
    });
}
