$(document).on('click','form .QuestionNext',function(){
     var Group = $(this).closest('.AlternativeQuestion');
     $( Group ).slideUp();
     $( Group ).next().slideDown();
});
$(document).on('click','form .QuestionPrev',function(){
     var Group = $(this).closest('.AlternativeQuestion');
     $( Group ).slideUp();
     $( Group ).prev().slideDown();
});
$(document).on('click','form .AlternativeNext',function(){
    
     var Group = $(this).closest('.AlternativeGroup'); 
     $( Group ).slideUp();
     $( Group ).next().slideDown();
     

    
});

$(document).on('click','form .AlternativePrev',function(){
     var Group = $(this).closest('.AlternativeGroup');
     $( Group ).slideUp();
     $( Group ).prev().slideDown();
});
$(document).on('click','.ImageVariableRadio img',function(){
    var ImgParent = $(this).parent();
    var RadioHandler = $(this).closest('.ImageVariableRadioGroup');
    $('.ImageVariableRadio img',RadioHandler).removeClass('current');
    $('img',ImgParent).addClass('current');
    $('img',ImgParent).css({ opacity: 1 });
    $('.ImageVariableRadio img:not(.current)').animate({
        'opacity': 0.5 
    });
    
    $('input',ImgParent).attr('checked','checked');
});
$(document).on('click','form .ProcessSummary',function(){
    var Group = $(this).closest('.AlternativeGroup');
    $( Group ).slideUp();
    $('#ProcessSummary').slideDown();
});
$(document).on('click','.NewWeights',function(){
    $('.btn-weights').slideToggle();
    $('#NewWeightsForm').slideDown();
});

$(document).on('click','.ProcesConfirm',function(){
    
});


$(document).on('click','.ProcesMoreOptions',function(){
    //UsableWindow
    var URL = $(this).attr('data-url');
    var DataId = parseInt( $(this).attr('data-id') );
    var DataType = $(this).attr('data-type');

    
    $.getJSON( URL , function(response){
        
        if( response.status )
        {
            UsableWindow( response.html );
        } 
        else 
        {
            AlertWindow( response.error , 'danger' );
        }
         
    });
});

$(document).on('click','.DynamicEditForms .DynamicEditFormsSubmit',function(){
    
    var URL = 'ajax.php?' + $(this).closest('form').serialize() + '&btndata=' + $(this).attr('data-btn');
    
    $.getJSON( URL , function(response){
        if( response.status )
        {
            if( response.html ) $('#JsUsableWindow').html( response.html );
            if( response.refresh ) window.location.reload();
            //CloseUsableWindow();
        } 
        else 
        {
            AlertWindow( response.error );
        }
    });
});

$(function(){
    $( ".SortableUL" ).sortable();
});

$(document).on('click','#gamefun #CheckGame',function(){
    var form = $(this).closest('#gamefun');
    $.getJSON( 'ajax.php?' + $(form).serialize() , function(response){
        if( response.status )
        {
            $('#GameRequest').html( response.html );
        } 
        else 
        {
            AlertWindow( response.error , 'danger' );
        }
    });
});

$(document).on('click','.RemoveKryterium',function(){
    
    $.getJSON('ajax.php?mode=proces&function=removekryterium&id=' + $(this).attr('data-k'),function( response ){
        if( response.status )
        {
            UsableWindow( response.html );
        } 
        else 
        {
            AltertWindow( response.error , 'danger' );
        }
    });
     
});

$(document).on('click','.RemoveAlternative',function(){
    
    $.getJSON('ajax.php?mode=proces&function=removealt&id=' + $(this).attr('data-a'),function( response ){
        if( response.status )
        {
            UsableWindow( response.html );
        } 
        else 
        {
            AltertWindow( response.error , 'danger' );
        }
    });
     
});

function ElementUpdater( ElementToUpdate , UpdateType , UpdateValue )
{
    switch( UpdateType )
    {
        case 'remove':
            if( UpdateValue == 'slow' )
            {
                $(ElementToUpdate).fadeOut(800,function(){
                     $(ElementToUpdate).remove();
                });
            } 
            else 
            {
                $(ElementToUpdate).remove();   
            }
            
        break;
        
        case 'values':
            $(ElementToUpdate).val( UpdateValue );
        break;
        
        case 'html':
            $(ElementToUpdate).html( UpdateValue );
        break;
        
        case 'AddClass':
            $(ElementToUpdate).addClass( UpdateValue );
        break;
        
        case 'RemoveClass':
            $(ElementToUpdate).removeClass( UpdateValue );
        break;
        
        case 'class':
            $(ElementToUpdate).attr( 'class' , UpdateValue );
        break;

        
        case 'InputName':
            $(ElementToUpdate).attr( 'name' , UpdateValue );
        break;
        
        case 'ChangeK':
            
            $('.DecisionElement').each(function( index , input ){
                
                $(input).val( 'A: ' + $(this).attr('data-a') + ',K: ' + $(input).attr('data-k') + ',NEW:' + UpdateValue );
                 
            });
            
        break;
    }
}