﻿//Google Search API Stuff
//<![CDATA[
function OnLoad() {
    new RawSearchControl();
    var arVersion = navigator.appVersion.split("MSIE")
    var version = parseFloat(arVersion[1])

    if ((version >= 5.5) && (document.body.filters)) 
    {
       for(var i=0; i<document.images.length; i++)
       {
          var img = document.images[i]
          var imgName = img.src.toUpperCase()
          if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
          {
             var imgID = (img.id) ? "id='" + img.id + "' " : ""
             var imgClass = (img.className) ? "class='" + img.className + "' " : ""
             var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
             var imgStyle = "display:inline-block;" + img.style.cssText 
             if (img.align == "left") imgStyle = "float:left;" + imgStyle
             if (img.align == "right") imgStyle = "float:right;" + imgStyle
             if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
             var strNewHTML = "<span " + imgID + imgClass + imgTitle
             + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
             + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
             + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
             img.outerHTML = strNewHTML
             i = i-1
          }
       }
    }   
}

/**
* The RawSearchControl demonstrates how to use Searcher Objects
* outside of the standard GSearchControl. This includes calling
* searcher .execute() methods, reacting to search completions,
* and if you had previously disabled html generation, how to generate
* an html representation of the result.
*/
function RawSearchControl() {
    // latch on to key portions of the document
    this.searcherform = document.getElementById("searcher");
    this.results = document.getElementById("mainContent");
    //this.cursor = document.getElementById("cursor");
    this.searchform = document.getElementById("searchform");

    // create map of searchers as well as note the active searcher
    //this.activeSearcher = "web";
    this.searchers = new Array();

    // create and wire up an instance of GwebSearch and one of
    // GlocalSearch. Note, we disable html generation. We are doing
    // this so that we can demonstrate how to manually create it if
    // needed. Note that we register to handle search completion notifications
    // when searches complete, they are called in the context of this instance
    // of RawSearchControl and they are passed the searcher that just completed

    // wire up a raw GwebSearch searcher
    var searcher = new GwebSearch();
    searcher.setNoHtmlGeneration();
    searcher.setSearchCompleteCallback(this, RawSearchControl.prototype.searchComplete, [searcher]);
    searcher.setUserDefinedLabel("Columbia Mountour Visitors Bereau");
    searcher.setUserDefinedClassSuffix("siteSearch");
    searcher.setSiteRestriction("iTourColumbiaMontour.com");
    this.searchers["web"] = searcher;

    // do the same for local
    //searcher = new GlocalSearch();
    //searcher.setNoHtmlGeneration();
    //searcher.setCenterPoint("98074");
    //searcher.setSearchCompleteCallback(this, RawSearchControl.prototype.searchComplete, [searcher]);
    //this.searchers["local"] = searcher;

    // now, create a search form and wire up a submit and clear handler
    this.searchForm = new GSearchForm(true, this.searchform);
    this.searchForm.setOnSubmitCallback(this, RawSearchControl.prototype.onSubmit);
    this.searchForm.setOnClearCallback(this, RawSearchControl.prototype.onClear);
}

/**
* onSubmit - called when the search form is "submitted" meaning that
* someone pressed the search button or hit enter. The form is passed
* as an argument
*/
RawSearchControl.prototype.onSubmit = function(form) {
    //this.computeActiveSearcher();
    if (form.input.value) {
        // if there is an expression in the form, call the active searcher's
        // .execute method
        this.searchers["web"].execute(form.input.value);
    }

    // always indicate that we handled the submit event
    return false;
}

/**
* onClear - called when someone clicks on the clear button (the little x)
*/
RawSearchControl.prototype.onClear = function(form) {
    this.clearResults();
}

/**
* searchComplete - called when a search completed. Note the searcher
* that is completing is passes as an arg because thats what we arranged
* when we called setSearchCompleteCallback
*/
RawSearchControl.prototype.searchComplete = function(searcher) {
    // always clear old from the page
    this.clearResults();
    // if the searcher has results then process them
    if (searcher.results && searcher.results.length > 0) {
        // print the result titles
        
        var div = createDiv("", "header");
        this.results.appendChild(div);
        for (var i=0; i<searcher.results.length; i++) {
            var result = searcher.results[i];
            if (i == 0) {
                var sResult = "<h1>Search Results</h1><a href=\"" + result.url.replace(/%2520/g, ' ') + "\">" + result.title + "</a><br />" + result.content + "<br /><span style=\"color:green\">" + result.visibleUrl + "</span><br />&nbsp;"; 
            }
            else {
                var sResult = "<a href=\"" + result.url + "\">" + result.title + "</a><br />" + result.content + "<br /><span style=\"color:green\">" + result.visibleUrl + "</span><br />&nbsp;";
            }
            
            div = createDiv(sResult);
            this.results.appendChild(div);
        }
    }else{
        var sResult = "<br /><br/><br/>Your search return 0 results.";
        div = createDiv(sResult);
        this.results.appendChild(div);
    }
    
    // Paging Control
    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.results.appendChild(cursorNode);
    }
}

RawSearchControl.prototype.gotoPage = function(searcher, page) {
    searcher.gotoPage(page);
}

/**
* clearResults - clear out any old search results
*/
RawSearchControl.prototype.clearResults = function() {
    removeChildren(this.results);
    removeChildren(this.results);
}

/**
* Static DOM Helper Functions
*/
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;
}

// register to be called at OnLoad when the page loads
GSearch.setOnLoadCallback(OnLoad);


//]]>