﻿//http://code.google.com/intl/pt-BR/apis/ajaxsearch/documentation/reference.html#_class_GSearch
function GoogleSearch(pSiteSearch) 
{
    document.write('<script type="text/javascript" language="javascript">google.load("search", "1", { "nocss": true }, { "language": "pt-BR" })</script>');
    this.urlSearch = new String(pSiteSearch);
    this.elementForm = new Object();
    this.elementResult = new Object();
    this.elementBranding = new Object();
    this.wordSearch = new String();
    this.txtSearchLabel = new String("Resultados da busca");
    this.txtNoSearchResult = new String("Nenhum Resultado foi encontrado para sua busca.");

    this.Initialize = function(pElementForm, pElementResult, pElementBranding, pWordSearch) {
        this.elementForm = document.getElementById(pElementForm);
        this.elementResult = document.getElementById(pElementResult);
        this.elementBranding = document.getElementById(pElementBranding);
        this.wordSearch = pWordSearch;
        google.setOnLoadCallback(GoogleSearch.SearchControl);
    }

    this.SearchControl = function() {
        var searchControl = new google.search.SearchControl();
        var drawOptions = new google.search.DrawOptions();
        var searchOptions = new google.search.SearcherOptions();
        var siteSearch = new google.search.WebSearch();

        // Cria o input do formulario
        drawOptions.setSearchFormRoot(GoogleSearch.elementResult);

        //Opcoes do resultado de busca
        searchOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

        // Define site que sera exibido o resultado
        siteSearch.setUserDefinedLabel(GoogleSearch.txtSearchLabel);
        siteSearch.setSiteRestriction("" + GoogleSearch.urlSearch + "");
        searchControl.addSearcher(siteSearch, searchOptions);

        // Cria o resultado de busca
        google.search.Search.getBranding(GoogleSearch.elementBranding, google.search.Search.HORIZONTAL_BRANDING);
        searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
        searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
        searchControl.setNoResultsString(GoogleSearch.txtNoSearchResult);
        searchControl.draw(GoogleSearch.elementResult, drawOptions);

        searchControl.execute(GoogleSearch.wordSearch);
        searchControl.setSearchCompleteCallback(searchControl, GoogleSearch.OnSearchComplete);
    }

    this.OnSearchComplete = function(sc, searcher) {
        if (searcher.cursor != undefined && searcher.cursor.estimatedResultCount > 1)
            $(".gsc-control .gsc-stats").html("Foram encontrados <b>" + searcher.cursor.estimatedResultCount + "</b> resultados para a busca realizada.");
        else if (searcher.cursor != undefined)
            $(".gsc-control .gsc-stats").html("Foi encontrado <b>" + searcher.cursor.estimatedResultCount + "</b> resultado para a busca realizada.");
        else
            $(".gsc-control .gsc-stats").hide();
    }

    this.GoToSearch = function(pValue) {
        if (pValue != "")
            location.href = "/Busca/" + pValue.replace(/ /g, "+");
    }

    this.SearchKeyDown = function(e, obj) {
        var keyCode = (e.which) ? e.which : e.keyCode;
        if (keyCode == 13)
            GoogleSearch.GoToSearch(obj.value);
    }
}

