$(document).ready(function() {
  
    var test2 = '';
    function nthTest() {
      var 
        selector = $('#selector').val(),
        ot = $('#output-type').val(),
        sd = $('#string-delimiter').val(),
        t = $('#trim').val() == 'true' ? true : false;

      var nthNum = $('#nth').val();
      var nthText = $('#nth :selected').text();
      if (nthNum.indexOf('-') == 0) nthNum *=1;

      test2 = $(selector).textChildren(nthNum, {
        outputType: ot,
        stringDelimiter: sd, 
        trim: t          
      });
      if (typeof test2 == 'object') test2 = '[' + test2 + ']';

      var bc = ['<code>',
        '$(\'',
        selector, '\').textChildren(',
        (/^[a-z]/).test(nthText) ? '\'' + nthText + '\'' : nthText,
        (nthText !='' && (ot != 'string' || sd != '' || t != true)) ? ', ' : '',
        (ot != 'string' || sd != '' || t != true) ? '{</code>' : '</code>',
            (ot != 'string') ? '<code class="nth-options">outputType: \'' + ot + '\'' : '',
            (ot != 'string' && (sd!=''||t==false)) ? ',</code>' : (ot!='string')? '</code>' : '',
            (sd != '') ? '<code class="nth-options">stringDelimiter: \'' + sd + '\'' : '',
            (sd != '' && t == false) ? ',</code>' : (sd!='') ? '</code>' : '',
            (t == false) ? '<code class="nth-options">trim: ' + t + '</code>' : '',
        (ot != 'string' || sd != '' || t != true) ? '<code class="nth-end">}' : '<code>',
        ');</code>'].join('');
      
      //show results
      $('#nth-pre-result').html(bc);
      $('#nth-result').html(test2);
    }

    nthTest();
    
    $('#testform :input').change(function() {
      nthTest();
    });
});
