var jquery_loader = '/rent/image/contents/ajax-loader.gif';
var json_search_data = '/rent/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 = '<p id="searchNone">「<span>' + json_search_keyword + '</span>」の含まれているコンテンツは見つかりませんでした。<br />検索ワードを変更してお試しください。スペースで区切るとAND検索も可能です。</p>';
    } else {
        result_html = json_search_result_html(result_data, keywords);
    }
    $('div#searchContent').css('height', 'auto')
                        .css('background', 'transparent')
                        .html(result_html);
}
function json_search_result_html(result_data, keywords){
    var html = '<p id="searchNotice">「<span>' + json_search_keyword + '</span>」の検索結果（' + result_data.length + '件ヒット）</p><div id="moduleContent"><ul id="searchList">';
    for (var i= 0;i < result_data.length;i++){
        html += '<li><a href="' + result_data[i].link + '#search_word=' + json_search_keyword + '">' + result_data[i].title + '</a></li>';
    }
    html += '</ul></div><p id="searchHide"><a class="imgbutton" href="#header" onclick="clear_block(\'#searchContent\')"><span>&raquo; CLOSE</span></a></p>';
    return html;
}
function json_search(){
    json_search_keyword = $('input#searchBox').val();
    $('div#searchContent').html('')
                        .css('height', '100px')
                        .css('background', 'transparent url(' + jquery_loader + ') no-repeat center center');
    if (json_search_is_loaded){
        json_search_archive();
    } else {
        $.ajax({url: json_search_data, type: 'GET', dataType: 'xml', timeout: 91500,
            error: function() {
                $('div#searchContent').css('height', 'auto')
                                    .css('background', 'transparent')
                                    .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){
    $(blockid).html('');
}
$(function(){
    var c = {
        placeholder: $("input#searchBox").attr("placeholder"),
        input: $("input#searchBox"),
        focusColor: "focus",
        defaultColor: "default",
        error: "検索ワードをご入力ください"
    }
    // Firefox placeholder対応
    $(c.input).val(c.placeholder).addClass(c.defaultColor);
    $(c.input).focus(function(){
        if($(this).val() == c.error | $(this).val() == c.placeholder){
            $(this).val("").addClass(c.focusColor).removeClass(c.defaultColor);
        }
    })
    .blur(function(){
        if($(this).val() == "" | $(this).val() == c.error){
            $(this).val(c.placeholder).addClass(c.defaultColor).removeClass(c.focusColor);
        }
    });
    $("form#search").submit(function(){
        if(c.input.val() == "" | c.input.val() == c.placeholder){
            c.input.val(c.error).select();
            return false;
        } else if(c.input.val() == c.error){
            c.input.val("");
            return false;
        }
    });
});