function dynamicSearch(params) {
    var locked = true;
    var container = $('#dynamicSearch');
    container.html('');
    var ul = $('<ul>');
    
    // Check select menus exist. If not, create them!
    var menus = new Array();
    var labels = new Array();
    $.each(params,function(i,item) {
        var menu = $('#ds-'+item[1]);
        var li = $('<li>');
        li.addClass('clearfix');
        if(menu.length == 0) {
            li.html('<label for="ds-'+item[1]+'">'+item[0]+'</label>');
            li.html(li.html()+'<select id="ds-'+item[1]+'" name="keyword[eq:'+item[1]+':yes]"><option value="">All</option></select>');
        }
        li.appendTo(ul);
    });
    
    container.append(ul);
    
    // Set initial results and assign events
    setDynamicResults(params);
    $('#dynamicSearch > ul > li:not(:last-child) > select').focus(function() { locked = false; });
    $('#dynamicSearch > ul > li:not(:last-child) > select').change(function() {
        if(locked == false) {
            $(this).blur();
            setDynamicResults(params);
            locked = true;
        }
    });
}

function setDynamicResults(params) {
    var url = 'http://nrguploads.orcawebsites.com/dynamic-search/get-indexes.php?jsoncallback=?';
    var selected = new Array();
    
    // Generate URL and set waiting message
    $.each(params,function(i,item) {
        var menu = $('#ds-'+item[1]);
        selected[item[1]] = menu.children('option:selected').val();
        url = url+'&'+item[1]+'='+selected[item[1]];
        menu.html('<option value="">Please wait...</option>');
    });
    
    // Get JSON object
    $.getJSON(url,function(data) {
        $.each(data,function(i,item) {
            var menu = $('#ds-'+i);
            if(item.length == 0) { menu.attr('disabled',true); }
            else { menu.attr('disabled',false); }
            var htmlStr = '<option value="">All</option>';
            $.each(item,function(i2,item2) {
                if(item2 == selected[i]) { htmlStr = htmlStr+'<option value="'+item2+'" selected="selected">'+item2+'</option>'; }
                else { htmlStr = htmlStr+'<option value="'+item2+'">'+item2+'</option>'; }
            });
            menu.html(htmlStr);
        });
    });
}