﻿// research-finder.js
google.load("search", "1");

document.documentElement.className = 'js';

function OnLoad() {

    /*PageMethods.LoadSearch(
        function(result, userContext, methodName) {
            var options = new GsearcherOptions();
            options.setExpandMode(GSearchControl.EXPAND_MODE_CLOSED);

            for (var i in result) {
                var site = result[i];
                var siteSearch = new google.search.WebSearch();
                siteSearch.setUserDefinedLabel(site.DisplayName);
                siteSearch.setUserDefinedClassSuffix('l-' + site.GeographicLocationId + '-i-' + site.InstitutionTypeId);
                siteSearch.setSiteRestriction(site.Url);
                if (site.FileType != null) {
                    siteSearch.setQueryAddition("filetype:" + site.FileType);
                }
                searchControl.addSearcher(siteSearch, options);
            }

            searchControl.draw(document.getElementById("searchControl"));
            var drawOptions = new google.search.DrawOptions();
            drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
            searchControl.draw(document.getElementById("searchControl"), drawOptions);
            // make sure our label for the Google AJAX Search control
            // can be clicked to select the input field
            $('input[@name="search"]').attr('id', 'search');

            // add the file type to the search box when the appropriate
            // radio button is clicked
            var $gscInput = $('#searchControl input.gsc-input');

            $('#advancedSearch #fileTypes li > input:radio').click(function() {
                var re = /\s*filetype:[a-z]+/;
                // remove any existing filetype filter first
                $gscInput.val($gscInput.val().replace(re, ''));

                var $val = $(this).val();
                if ($val.length > 0) {
                    // append the new filetype filter
                    $gscInput.val($gscInput.val() + ' filetype:' + $val);
                }
            });

            // set the initial form focus
            $gscInput.focus();

            // allow users to save results
            searchControl.setOnKeepCallback(this, storeResults, "save result");

            searchControl.setSearchCompleteCallback(this, onSearchComplete);
            var test = $('<div>Test</div>');
            $(test).click(function() {
                PageMethods.SearchATR($('input.gsc-input').val(),
                    function(result, userContext, methodName) {
                        for (var i in result) {
                            $('div.gsc-resultsRoot:last').after(result[i].DisplayName);
                        }
                    },
                    function(error, userContext, methodName) {
                        alert('error');
                    }
                );
            });
            $('span.gs-spacer:last').after(test);
        },
        function(error, userContext, methodName) {
            $('#content').append('<p>Sorry, but there has been a problem while configuring the site.</p>');
        }
    );*/

    $('input[id*="btnResearchFilter"]').click(function() {
        // Create a search control
        var searchControl = new google.search.SearchControl();
        // tell the searcher to draw itself in tabbed mode
        searchControl.setResultSetSize(GSearch.SMALL_RESULTSET);
        var i = 0;
        var args = new Array();

        $('input[name="institutions[]"]:checked').each(function() {
            args[i++] = $(this).val();
        });
        PageMethods.LoadResearchCentres(args,
            function(result, userContext, methodName) {
                var options = new GsearcherOptions();
                options.setExpandMode(GSearchControl.EXPAND_MODE_CLOSED);

                for (var i in result) {
                    var site = result[i];
                    var siteSearch = new google.search.WebSearch();
                    siteSearch.setUserDefinedLabel(site.DisplayName);
                    siteSearch.setUserDefinedClassSuffix('l-' + site.GeographicLocationId + '-i-' + site.InstitutionTypeId);
                    siteSearch.setSiteRestriction(site.Url);
                    searchControl.addSearcher(siteSearch, options);
                }

                searchControl.draw(document.getElementById("searchControl"));
                // make sure our label for the Google AJAX Search control
                // can be clicked to select the input field
                $('input[@name="search"]').attr('id', 'search');

                // add the file type to the search box when the appropriate
                // radio button is clicked
                var $gscInput = $('#searchControl input.gsc-input');

                // set the initial form focus
                $gscInput.focus();

                searchControl.execute($('input[id*="txtResearchFilter"]').val());
            },
            function(error, userContext, methodName) {
                $('#content').append('<p>Sorry, but there has been a problem while configuring the site.</p>');
            }
        );
        return false;
    });
    
    /*PageMethods.LoadResearchCentres( args,
        function(result, userContext, methodName) {
            var options = new GsearcherOptions();
            options.setExpandMode(GSearchControl.EXPAND_MODE_CLOSED);

            for (var i in result) {
                var site = result[i];
                var siteSearch = new google.search.WebSearch();
                siteSearch.setUserDefinedLabel(site.DisplayName);
                siteSearch.setUserDefinedClassSuffix('l-' + site.GeographicLocationId + '-i-' + site.InstitutionTypeId);
                siteSearch.setSiteRestriction(site.Url);
                searchControl.addSearcher(siteSearch, options);
            }

            searchControl.draw(document.getElementById("searchControl"));
            // make sure our label for the Google AJAX Search control
            // can be clicked to select the input field
            $('input[@name="search"]').attr('id', 'search');

            // add the file type to the search box when the appropriate
            // radio button is clicked
            var $gscInput = $('#searchControl input.gsc-input');

            $('#advancedSearch #fileTypes li > input:radio').click(function() {
                var re = /\s*filetype:[a-z]+/;
                // remove any existing filetype filter first
                $gscInput.val($gscInput.val().replace(re, ''));

                var $val = $(this).val();
                if ($val.length > 0) {
                    // append the new filetype filter
                    $gscInput.val($gscInput.val() + ' filetype:' + $val);
                }
            });

            // set the initial form focus
            $gscInput.focus();

            // allow users to save results
            //searchControl.setOnKeepCallback(this, storeResults, "save result");

            //searchControl.setSearchCompleteCallback(this, onSearchComplete);
        },
        function(error, userContext, methodName) {
            $('#content').append('<p>Sorry, but there has been a problem while configuring the site.</p>');
        }
    );*/

    // clear the saved results area
    $('#clearResults').click(function() {
        if (confirm('Are you sure you want to clear your saved search results?')) {
            $('#savedResults > div').remove();
            $('#savedResults p').fadeIn();
        }
        return false;
    });

    $('div.advancedSearch ul > li > input:checked').click(function() {
        var $resultBlock = $('.gsc-control div[class*=' + $(this).attr('id') + ']');
        if ($(this).attr('checked')) {
            $resultBlock.css('backgroundColor', '#ffff99');
            $resultBlock.fadeIn(500);
            $resultBlock.animate({
                backgroundColor: '#ffffff'
            }, 1500);
            return true;
        }

        // hide the Google AJAX Search results that have the
        // class suffix that we defined above
        $resultBlock.fadeOut(500);
    });

    function storeResults(result) {
        // clear the help text if it is there
        $('#savedResults p').hide();
        var node = result.html.cloneNode(true);
        node.style.display = 'none';
        var scratchpad = $('#savedResults').get(0);
        scratchpad.appendChild(node);
        $(node).fadeIn(750);
    }

    function onSearchComplete(sc, searcher) {
        if (searcher.results && searcher.results.length > 0) {
            // show the clipboard
            $('#savedResults').show();
        }
    }

}
google.setOnLoadCallback(OnLoad);