var jquery_loader         = 'http://www.phoenix.ac.jp/common/images/blog/loading.gif';
var json_search_data      = 'http://www.phoenix.ac.jp/report/js/search_data.txt';
var json_search_keyword   = '';
var json_search_is_loaded = false;
var json_search_entries;
var cloce_btn    = '<p class="btn"><a href="javascript:void(0)" onclick="clear_block(\'#search-content\')"><img src="http://www.phoenix.ac.jp/common/images/blog/close.gif" alt="×閉じる" title="×閉じる" width="32" height="32" /></a></p>';
var result_cloce = '<p class="hide"><a href="javascript:void(0)" onclick="clear_block(\'#search-content\')">×閉じる</a></p>';
$(document).ready(function() {
    $('#search-content').css({'display':'none','margin-bottom':'16px','height':'230px','border':'1px solid #C9C3B2','overflow':'auto','padding':'4px 4px 4px 16px'});
});
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 = cloce_btn + '<p class="ErrMsg">一致する記事が見つかりませんでした。</p>';
    }
    else
    {
        result_html = json_search_result_html(result_data, keywords);
    }
    $('#search-content').css('background-image', 'none').html(result_html);
}
function json_search_result_html(result_data, keywords)
{
    var html = cloce_btn + '<dl><dt><strong>【検索結果】</strong>&nbsp;<span>（' + result_data.length + '件の記事が見つかりました ）</span></dt><dd><ul>';
    for (var i= 0;i < result_data.length;i++)
    {
        html += '<li><a href="' + result_data[i].link + '">' + result_data[i].title + '</a><br />' + result_data[i].body.substring(0,100) + '…</li>';
    }
    html += '</ul></dd></dl>' + result_cloce;
    return html;
}
function json_search()
{
    json_search_keyword = $('input#search_box').val();
    $('#search-content').html(result_cloce).css('background', 'transparent url(' + jquery_loader + ') no-repeat center center').slideDown("slow");
    if (json_search_is_loaded)
    {
        json_search_archive();
    }
    else
    {
        $.ajax({url: json_search_data, type: 'GET', dataType: 'xml', timeout: 1500,
            error: function() {
                $('#search-content').css('background-image', 'none').html(cloce_btn + '<p class="ErrMsg">検索エラーです</p>');
            },
            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)
{
    $(blockid).slideUp("slow").html('');
}

