function OnLoad() {
    new RawSearchControl();
}

function getVar(name) {
    get_string = document.location.search;
    return_value = '';
    do {
        name_index = get_string.indexOf(name + '=');
        if(name_index != -1) {
            get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
            end_of_value = get_string.indexOf('&');
            if(end_of_value != -1)
                value = get_string.substr(0, end_of_value);
            else
                value = get_string;
            if(return_value == '' || value == '')
                return_value += value;
            else
                return_value += ', ' + value;
        }
    } while(name_index != -1)
    space = return_value.indexOf('+');
    while(space != -1) {
        return_value = return_value.substr(0, space) + ' ' +
        return_value.substr(space + 1, return_value.length);

        space = return_value.indexOf('+');
    }
    return(return_value);
}

function RawSearchControl() {
    this.searcherform = document.getElementById("googleSearchForm");
    this.results = document.getElementById("results");
    this.cursor = document.getElementById("cursor");
    this.searchform = document.getElementById("searchform");

    var searcher = new GwebSearch();
    searcher.setSiteRestriction("018442707248580444177:d26z34l2tra");
    searcher.setNoHtmlGeneration();
    searcher.setResultSetSize(GSearch.LARGE_RESULTSET);
    searcher.setSearchCompleteCallback(this, RawSearchControl.prototype.searchComplete, [searcher]);
    searcher.setLinkTarget(GSearch.LINK_TARGET_SELF);

    var keyword = getVar('keyword');
    if (keyword) {
        searcher.execute(keyword);

        if (document.getElementById("googleSearchForm") != null) {
            this.searcherform.keyword.value = keyword;
        }

    } else {
        // When there are no results show this div
        var noRes = createDiv('Geen enkele pagina voldeed aan uw zoekopdracht.');
        this.results.appendChild(noRes);
    }
}

RawSearchControl.prototype.searchComplete = function(searcher) {
    this.clearResults();

    if (searcher.results && searcher.results.length > 0) {
        for (var i=0; i<searcher.results.length; i++) {
            var result = searcher.results[i];
            searcher.createResultHtml(result);
            if (result.html) {
                div = result.html.cloneNode(true);
            } else {
                div = createDiv("** failure to create html **");
            }
            this.results.appendChild(div);
        }

        if (searcher.cursor) {
            var cursorNode = createDiv(null, "gsc-cursor");
            for (var i=0; i<searcher.cursor.pages.length; i++) {
                var className = "gsc-cursor-page";
                if (i == searcher.cursor.currentPageIndex) {
                    className = className + " gsc-cursor-current-page";
                }
                var pageNode = createDiv("["+searcher.cursor.pages[i].label+"]", className);
                pageNode.onclick = methodClosure(this, this.gotoPage, [searcher, i]);
                cursorNode.appendChild(pageNode);
            }
            this.cursor.appendChild(cursorNode);
            var more = createLink(searcher.cursor.moreResultsUrl, GSearch.strings["more-results"] + "&nbsp;&raquo;", GSearch.LINK_TARGET_BLANK, "gsc-trailing-more-results");
            this.cursor.appendChild(more);
        }
    } else {
        var noRes = createDiv('Geen enkele pagina voldeed aan uw zoekopdracht.');
        this.results.appendChild(noRes);
    }
}

RawSearchControl.prototype.gotoPage = function(searcher, page) {
    searcher.gotoPage(page);
}

RawSearchControl.prototype.clearResults = function() {
    removeChildren(this.results);
    removeChildren(this.cursor);
}


function removeChildren(parent) {
    while (parent.firstChild) {
        parent.removeChild(parent.firstChild);
    }
}

function createDiv(opt_text, opt_className) {
    var el = document.createElement("div");
    if (opt_text) {
        el.innerHTML = opt_text;
    }
    if (opt_className) { el.className = opt_className; }
    return el;
}

function methodClosure(object, method, opt_argArray) {
    return function() {
        return method.apply(object, opt_argArray);
    }
}

function createLink(href, opt_text, opt_target, opt_className, opt_divwrap) {
    var el = document.createElement("a");
    el.href = href;
    if (opt_text) {
        el.innerHTML = opt_text;
    }
    if (opt_className) {
        el.className = opt_className;
    }
    if (opt_target) {
        el.target = opt_target;
    }
    if (opt_divwrap) {
        var div = this.createDiv(null, opt_className);
        div.appendChild(el);
        el = div;
    }
    return el;
}