﻿search = {
    _defaultValue: 'Search Elsevier Mental Health',
    _searchFocused: null,

    init: function() {
        document.body.onkeyup = this.onKeyUp;
        $('site-search').value = this._defaultValue;
    },

    focus: function() {
        var obj = $('site-search');
        if (obj.value == search._defaultValue) {
            obj.value = '';
            obj.style.color = 'black';
            search._searchFocused = true;
        }
        else {
            if (obj.value == '') {
                obj.value = search._defaultValue;
                obj.style.color = '#999999';
                search._searchFocused = false;
            }
        }
    },

    doSearch: function() {
        var searchQuery = $('site-search').value;
        if (searchQuery == search._defaultValue)
            searchQuery = '';
            
        window.location = '/search/default.aspx?query=' + searchQuery;
    },

    onKeyUp: function() {
        var evtobj = (window.event ? event : arguments[0]);
        var uniCode = (evtobj.charCode ? evtobj.charCode : evtobj.keyCode);

        if (search._searchFocused) {
            if (uniCode == 13) {
                search.doSearch();
            }
        }
    }
};