﻿var ObitSearch = new Class({
    initialize: function() {
        this.q = $('q');
        this.qb = $('qb');
        this.hint = 'SEARCH FOR OBITUARIES';
        
        if (this.q && this.qb) {
            this.q.value = this.hint;
            this.addEvents();
        }
        
        if (window.location.search.length > 0) {
            this.q.value = window.location.search.substring(1).parseQueryString().q;
        }
    },
    
    addEvents: function() {
        this.q.addEvents({
            'keydown': function(event) {
                    if (event.key == 'enter')
                        this.doSearch();
                }.bind(this),
            'focus': function() {
                    if (this.q.value == this.hint)
                        this.q.value = '';
                }.bind(this),
            'blur': function() {
                    if (this.q.value.length == 0)
                        this.q.value = this.hint;
                }.bind(this)
        });
        this.qb.addEvent('click', function() {
            this.doSearch();
        }.bind(this));
    },
    
    doSearch: function() {
        if (this.q.value.length == 0 || this.q.value == this.hint) {
            alert('Please enter a name to search for.');
            return;
        }
        window.location.href = 'search.aspx?q=' + escape(this.q.value);
    }
});

window.addEvent('domready', function() {
    new ObitSearch();
});