var JiveSpotlightSearch = Class.create();
JiveSpotlightSearch.prototype = {

/*
* Initialize the JiveSpotlightSearch object.
* @spotlightSearchInput The id of the spotlight search input element - jive-query
* @spotlightSearchContainer The id of the spotlight search results container - jive-spotlight-search-container
* @spotlightSearchURL The url to perform the search - action 'spotlight-search'
*/
    initialize: function(spotlightSearchInput, spotlightSearchContainer, spotlightSearchURL)
    {
        this.spotlightSearchInput = spotlightSearchInput;
        this.spotlightSearchContainer = spotlightSearchContainer;
        this.spotlightSearchURL = spotlightSearchURL;

        this.spotlightSearchIndex = 0;
        $(this.spotlightSearchInput).observe("keyup", this.observeSpotlightSearchQuery.bind(this));
        $(this.spotlightSearchInput).observe("blur", this.onBlur.bind(this));
    },

    observeSpotlightSearchQuery: function(event) {
        switch(event.keyCode) {
            case Event.KEY_UP:
                this.selectIndex(this.spotlightSearchIndex-1);
                Event.stop(event);
                return;
            case Event.KEY_DOWN:
                this.selectIndex(this.spotlightSearchIndex+1);
                Event.stop(event);
                return;
            case Event.KEY_RETURN:
                this.clearSpotlightSearchEvent();
                if (this.spotlightSearchIndex > 0) {
                    this.loadSelectedIndex();
                } else {
                    $(this.spotlightSearchInput).form.submit();
                }
                Event.stop(event);
                return;
            case Event.KEY_ESC:
                this.clearSpotlightSearch();
                Event.stop(event);    
                return;
            case Event.KEY_LEFT:
            case Event.KEY_RIGHT:
            case Event.KEY_TAB:
            case Event.KEY_HOME:
            case Event.KEY_END:
            case Event.KEY_PAGEUP:
            case Event.KEY_PAGEDOWN:
                Event.stop(event);
                return;
        }

        this.clearSpotlightSearchEvent();
        this.spotlightSearchEvent = setTimeout(this.executeSpotlightSearch.bind(this), 400);
    },

    onBlur: function() {
        // needed to make click events working
        setTimeout(this.clearSpotlightSearch.bind(this), 250);
    },

    clearSpotlightSearch: function() {
        $(this.spotlightSearchContainer).update('');
    },

    executeSpotlightSearch: function() {
        var query = $F(this.spotlightSearchInput);
        if (query.length >= 3) {
            query = query + '*';
            new Ajax.Updater(this.spotlightSearchContainer, this.spotlightSearchURL, {
                method: 'get',
                asynchronous:true,
                evalScripts:true,
                parameters: {
                    'query': query
                }
            });
        } else {
            this.clearSpotlightSearch();
        }
        this.spotlightSearchIndex = 0;
    },

    viewAllResults: function() {
        var query = $F(this.spotlightSearchInput);
        if (query.length > 0) {
            query = query + '*';
            $(this.spotlightSearchInput).value = query;
            $(this.spotlightSearchInput).form.submit();
        }
    },

    selectIndex: function(index) {
        if (index > 0) {
            var elem = $('spotlight-index-' + index);
            if (elem) {
                $('jive-spotlight-search').select('li.hover').each(function(el) {
                    el.removeClassName('hover');
                });
                elem.addClassName('hover');
                elem.scrollIntoView(false);
                this.spotlightSearchIndex = index;
            }
        } else {
            $('jive-spotlight-search').select('li.hover').each(function(el) {
                el.removeClassName('hover');
            });
            $(this.spotlightSearchInput).scrollIntoView(false);
            this.spotlightSearchIndex = 0;
        }
    },

    loadSelectedIndex: function() {
        var elem = $('spotlight-index-' + this.spotlightSearchIndex);
        if (elem) {
            elem.select('a').each(function(anchor) {
                location.href = anchor.href;
            });
        }
    },

    clearSpotlightSearchEvent: function() {
        if (this.spotlightSearchEvent) {
            clearTimeout(this.spotlightSearchEvent);
        }
    }
}