$(document).ready(function()
{
    var baseLink = 'addonmodules.php?module=productlinker&modpage=';
    
    //$('.prettySelect').select2();
    
    $('.changeType').on('change', function() 
    {
        var that    =   $(this);
        var box     =   that.parent().parent().parent();

        $('#lmgajaxloader').show();
        
        if(that.val() == '1')
        {
            $.get(baseLink + 'getaddons&ajax=1', function(html)
            {
                box.find('.addonorconfig').html(html);
                $('#lmgajaxloader').hide();
            });
            
            box.find('.configoptionsvaluebox').hide();
            box.find('.swappricebox').show();
            
            box.find('.addonorconfig').attr('name', box.find('.addonorconfig').attr('name').replace('configoption_id', 'addon_id'));
            box.find('.configoptionsvalue').html('');
        }
        else if(that.val() == '2')
        {
            $.get(baseLink + 'getconfigoptions&ajax=1', function(html)
            {
                box.find('.addonorconfig').html(html);
                box.find('.addonorconfig').trigger('change');
                
                $('#lmgajaxloader').hide();
            });
            
            /*$.get(baseLink + 'getconfigoptions&ajax=1', function(html)
            {
                box.find('.addonorconfig').html(html);
                box.find('.addonorconfig').trigger('change');
                $('#lmgajaxloader').hide();
            });*/
            box.find('.swappricebox').hide();
            box.find('.configoptionsvaluebox').show();
            box.find('.addonorconfig').attr('name', box.find('.addonorconfig').attr('name').replace('addon_id', 'configoption_id'));
        }
    });
    
    $('.addonorconfig').on('change', function()
    {
        var that = $(this);
        $('#lmgajaxloader').show();
        
        if(that.attr('name').indexOf('configoption_id') > 0)
        {
            $.get(baseLink + 'getconfigoptionsvalues&id='+ that.val() +'&ajax=1', function(html)
            {
                that.parent().parent().parent().find('.configoptionsvalue').html(html);
                $('#lmgajaxloader').hide();
            });
        }
        $('#lmgajaxloader').hide();
    });
    
    $('.deleterelation').on('click', function() {
       if(! confirm('Are you sure you want to delete this relation?')) return false;; 
    });

    $('#clientAutocomplete').autocomplete({
      source: 'addonmodules.php?module=productlinker&modpage=getclients&ajax=1',
      select: function(event, ui)
      {
          event.preventDefault();
          
          $('#clientAutocomplete').val(ui.item.label);
          $('#clientId').val(ui.item.value);
      },
      focus: function(event, ui)
      {
          event.preventDefault();
          
          $('#clientAutocomplete').val(ui.item.label);
          $('#clientId').val(ui.item.value);
      },
      response: function(event, ui)
      {
          if(ui.content.length === 0)
          {
              $('#clientId').val('');
          }
      }
    });
    
    $('.deletelicense').on('click', function(e) 
    {
        if(! confirm('Are you sure you want to delete this relation link? This will also delete user product linked by this relation!')) return false;
    });
    
    $('#clientAutocomplete').on('blur', function() {
       var that = $(this);
       
       if(that.val() === '')
       {
           $('#clientId').val('');
       }
    });
    
    $('#domain').autocomplete({
        source: 'addonmodules.php?module=productlinker&modpage=getdomains&ajax=1',
    });
    
    $('#clearFilters').on('click', function() { document.location.href = 'addonmodules.php?module=productlinker&modpage=licenses';});
    $('#clearLogFilters').on('click', function() { document.location.href = 'addonmodules.php?module=productlinker&modpage=logs';});

    $('table tr th.sortable').on('click', function() {
        var that = $(this);
        
        document.location.href = that.find('a').attr('href');
    });
    
    $('#toggleFiltersBox').on('click', function(e) {
           $('#filtersBox').slideToggle('fast');
           e.preventDefault();
       }); 
});