﻿/**
* Elsevier Commercial Sales Magazines
* Public Site
* Search
*
* @requires    csm.js
* @namespace   csm.search
* @version     1
*
*/

csm_search = {};

csm_search._searchFocused = false;

csm_search.defaultValue = 'Enter your search term';

csm_search.txtSearch = null;
csm_search.btnSearch = null;

csm_search.init = function() {
    this.txtSearch = $(arguments[0]);
    this.btnSearch = $(arguments[1]);

    this.txtSearch.onfocus = function() {
        csm_search._searchFocused = true;

        var _value = csm_search.txtSearch.value;
        if (_value == csm_search.defaultValue) {
            csm_search.txtSearch.value = '';
            csm_search.txtSearch.style.color = 'black';
            csm_search.txtSearch.style.background = '#eee';
        }
    };

    this.txtSearch.onkeydown = function() {
        csm_search._searchFocused = true;
    };

    this.txtSearch.onblur = function() {

        setTimeout(function() {
            csm_search._searchFocused = false;
        }, 250);

        var _value = csm_search.txtSearch.value;
        if (_value == '') {
            csm_search.txtSearch.value = csm_search.defaultValue;
            csm_search.txtSearch.style.color = '#6C6C6C';
            csm_search.txtSearch.style.background = '#BEBEBE';
        }
    };

    document.body.onkeyup = this.onKeyUp;
};

csm_search.onKeyUp = function() {
    var evtobj = (window.event ? event : arguments[0]);
    var uniCode = (evtobj.charCode ? evtobj.charCode : evtobj.keyCode);

    if (csm_search._searchFocused) {
        if (uniCode == 13) {
            if (csm_search.txtSearch.value != '' && csm_search.txtSearch.value != csm_search.defaultValue)
                window.location = '/search/default.aspx?query=' + csm_search.txtSearch.value;
        }
    }
};

csm_search.btnPress = function() {
    if (csm_search.txtSearch.value != '' && csm_search.txtSearch.value != csm_search.defaultValue)
        window.location = '/search/default.aspx?query=' + csm_search.txtSearch.value;
};

csm_search.submitForm = function() {
    return (!csm_search._searchFocused ? true : false);
};
