var jquery_loader         = 'http://www.musu-b.com/images/loading-ajax-s.gif'; 
var json_search_data      = 'http://www.musu-b.com/oshiete/search_data.txt'; 
var json_search_keyword   = ''; 
var json_search_is_loaded = false; 
var json_search_entries; 
  
function json_search_archive() 
{ 
    var result_data = new Array(); 
  
    json_search_keyword = json_search_keyword.replace(/([\/\\\.\*\+\?\|\(\)\[\]\{\}\$\^])/g, "\\$1"); 
    json_search_keyword = json_search_keyword.replace(/( +|　+)/, ' '); 
    json_search_keyword = json_search_keyword.replace(/( |　)$/, ''); 
  
    var keywords = json_search_keyword.split(/ |　/); 
    var key      = new Array(); 
  
    for (var i = 0;i < keywords.length;i++) 
    { 
        key[key.length] = new RegExp(keywords[i], "i"); 
    } 
  
    for (var i = 0;i < json_search_entries.length - 1;i++) 
    { 
        var is_match = true; 
  
        for (var j = 0;j < key.length;j++) 
        { 
            var res_body  = key[j].exec( json_search_entries[i].body ); 
            var res_title = key[j].exec( json_search_entries[i].title ); 
  
            if (!res_body && !res_title) 
            { 
                is_match = false; 
            } 
        } 
  
        if (is_match) 
        { 
            result_data[result_data.length] = json_search_entries[i]; 
        } 
    } 
  
    var result_html = ''; 
  
    if (result_data.length == 0) 
    { 
        result_html = '<div id="search-result-ajax"><p class="tcenter">このキーワードでは見つかりませんでした</p></div>'; 
    } 
    else 
    { 
        result_html = json_search_result_html(result_data, keywords); 
    } 
  
//var ultag = '<ul>';
  
    j$('#search-content').css('height', 'auto') 
                        .css('background-image', 'none') 
                        .html(result_html);
//.ready(function() {
//j$("li:nth-child(20n)").after(ultag);
//});

} 


function json_search_result_html(result_data, keywords) 
{ 
    var html = '<script type="text/javascript" src="http://www.musu-b.com/js/jquery.pager.js"></script><script type="text/javascript">j$(document).ready(function () {j$(\'#module-content\').pager(\'ul\');});</script><div id="search-result-ajax"><p id="search-result-ajax-count" class="tleft"><span class="search-keyword">【' + keywords + '】</span>の検索結果…<span class="count">' + result_data.length + '</span>件の質問が見つかりました</p><div id="module-content"><ul>'; 
 


                 
    for (var i= 0;i < result_data.length;i++) 
    {                 

               //for (var c= 20 ;c < i;c = c + 20){
               //    html += '<ul>';
               //    } 
                 
                   
                //10の倍数
                if (i % 10 == 0) {
                    if (i == 0) {;
                    }else{
                    html += '</ul><ul>';
                    }
                }
                
        html += '<li><a href="' + result_data[i].link + '#search_word=' + json_search_keyword + '" target="_blank">' + result_data[i].title + '</a></li>'; 
               

    } 
    

    
    html += '</ul></div><div class="clear"></div><p class="search_hide"><a class="imgbutton" href="javascript:void(0)" style="font-weight:bold;" onclick="clear_block(\'#search-content\')"><span>検索結果を閉じる</span></a></p></div>'; 
  
    return html; 
} 

function json_search() 
{ 
    json_search_keyword = j$('input#search_box').val(); 
  
    j$('#search-content').html('') 
                        .css('height', '100px') 
                        .css('background', 'transparent url(' + jquery_loader + ') no-repeat center center'); 
  
    if (json_search_is_loaded) 
    { 
        json_search_archive(); 
    } 
    else 
    { 
        j$.ajax({url: json_search_data, type: 'GET', dataType: 'xml', timeout: 1500, 
            error: function() { 
                j$('#search-content').css('height', 'auto') 
                                    .css('background-image', 'none') 
                                    .html('Error loading XML document'); 
            }, 
            complete: function(xml) { 
                var data_string = xml.responseText; 
  
                data_string         = data_string.replace(new RegExp('<.*?>', "i"), ''); 
                json_search_entries = eval(data_string); 
  
                json_search_archive(); 
  
                json_search_is_loaded = true; 
            } 
        }); 
    } 
} 
  
function clear_block(blockid) 
{ 
    j$(blockid).html(''); 
} 

