﻿function SearchContext(keywords, div, button) {
    this.keywords = keywords;
    this.div = div;
    this.loadingDiv = null;
    this.button = button;
    this.intervalObj;
    this.searchItems = new Array();

    this.AddItem = function(item) {
        this.searchItems.push(item);
        item.context = this;
    }
}

function SearchItem(url, title) {
    this.url = url;
    this.title = title;
    this.context;
}

//function SearchContext.prototype.AddItem(item) {
//    this.searchItems.push(item);
//    item.context = this;
//}

function Search(context) {
    //context.button.disabled = false;
    //context.waitDiv.innerHTML = '<img src="/Content/Images/loading.gif"></img>';
    context.div.style.display = 'inline';
    for (var i = 0; i < context.searchItems.length; i++) {
        var item = context.searchItems[i];
        SearchPerPage(item);
    }
    context.waitDiv.innerHTML = '';
    context.button.disabled = false;
}

function SearchWithTimeout(context, timeout) {
    setTimeout(function() { SearchMain(context, timeout) }, 100);
}

function SearchMain(context, timeout) {
    context.div.innerHTML = '';
    if (context.keywords != undefined && context.keywords != null && context.keywords.replace(/(^\s*)|(\s*$)/g, '') != '') {
        //context.button.disabled = true;
        context.waitDiv.innerHTML = '<img src="/Content/Images/loading.gif"></img>';
        setTimeout(function() { Search(context) }, timeout);
    }
}

var itemIndex = 0;
function SearchInterval(context) {
    if (itemIndex < context.searchItems.length) {
        var item = context.searchItems[itemIndex];
        SearchPerPage(item);
        itemIndex++;
    }
    else {
        itemIndex = 0;
        clearInterval(context.intervalObj);
        context.waitDiv.innerHTML = '';
        //context.button.disabled = false;
    }
}

function SearchWithInterval(context, interval) {
    if (context.keywords != undefined && context.keywords != null && context.keywords.replace(/(^\s*)|(\s*$)/g, '') != '') {
        context.button.disabled = true;
        context.intervalObj = setInterval(function() { SearchInterval(context) }, interval);
    }
}

function SearchPerPage(item) {
    var xmlHttp;
    var resultHTML = "";
    var resultText = "";
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            resultHTML = xmlHttp.responseText.split('<!-- Content -->')[1];
            resultText = xmlHttp.responseText.split('<!-- Content -->')[1].replace(/<script\b[^>]*?>[\s\S]*?<\/script>/gi, '').replace(/<[^>]+?>/g, '').replace(/(\s{2,})/g, " "); ;
            if (resultText.indexOf(item.context.keywords) > 0) {
                if (resultText.length > 100) {
                    if (resultText.indexOf(item.context.keywords) <= 50) {
                        resultText = resultText.substr(0, 100);
                    }
                    else {
                        if (resultText.indexOf(item.context.keywords) + 50 < resultText.length) {
                            resultText = resultText.substring(resultText.indexOf(item.context.keywords) - 50, resultText.indexOf(item.context.keywords) + 50);
                        }
                        else {
                            resultText = resultText.substring(resultText.indexOf(item.context.keywords) - 50, resultText.length);
                        }
                    }
                }
                resultText = resultText.replace(new RegExp(item.context.keywords, "g"), '<strong>' + item.context.keywords + '</strong>');
                item.context.div.innerHTML += '<div class="searchresult"><a href="' + item.url + '">' + item.title + '</a></div><div class="searchdetails">' + resultText + '</div>';
            }
        }
    }
    //var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
    xmlHttp.open("POST", item.url, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send("");

//    if (xmlHttp.status != 200) {
//        alert('网络故障(xmlHttp.status=' + xmlHttp.status + ')，请稍后再试！');
//    }
}