﻿// JScript File
var Search = {
    dictionary: null,
    searchTerm: null,
    inputBox: null,
    cssInitialState: 'initialState',
    feedbackBox: null,
    container: null,
    init: function() {
        //find the input box
        $$('input').each( function(el) {
            if (el.type && el.id && el.type == 'text' && el.id.test('search','i')) {
                this.inputBox = el;
                if (!el.hasClass(this.cssInitialState)) { el.addClass(this.cssInitialState); }
                //setup event handlers
                el.addEvent('keyup',this.onkeyup.bind(this));
                el.addEvent('focus',this.onfocus.bind(this));
                //el.onkeyup = this.onkeyup.pass(el,this);
                //el.onfocus = this.onfocus.pass({ctrl:el, defaultValue:el.value},this);
            }
        }, this);
        if (this.inputBox) {
            this.dictionary = new Array;
            $$('a').each( function(el) {
                if (el.rel && el.rel == 'Search') {
                    this.dictionary[this.dictionary.length] = {
                        ctrl: el,
                        name: el.innerHTML
                    };
                    if (!this.container) { this.container = el.getParent(); }                                        
                }            
            }, this);
            
            //console.info('inputbox = ' + this.inputBox);
            //console.info('found ' + $$('a').length + ' links');
            //console.info("loaded " + this.dictionary.length + " items!");
            
            //find feedbackBox
            this.feedbackBox = $('SearchFeedback');
            if (!this.feedbackBox) {
                //console.warn('missing SearchFeedback');
            }
        }
    },
    onkeyup: function (evt) {
        evt = new Event(evt);
        ctrl = evt.target;
        if (!this.container) { return; }
        if (ctrl && ctrl.value && this.dictionary) { 
            this.searchTerm = ctrl.value.escapeRegExp(); 
            var found = 0;                      
            this.dictionary.each ( function (el) {
                if (el.name.test('^' + this.searchTerm,'i')) {
                    el.ctrl.setStyle('display','');
                    found++;
                } else {
                    el.ctrl.setStyle('display','none');
                }
            }, this);
            if (found == 0) {
                this.container.setStyle('display','none');
            } else {
                this.container.setStyle('display','');
            }
            if (this.feedbackBox && found == 1) {
                this.feedbackBox.setHTML(found + ' plant');
            } else if (this.feedbackBox) {    
                this.feedbackBox.setHTML(found + ' plants');
            }
        } else {
            //console.error('Error in onkeyup');
        }
    },
    onfocus: function (evt) {
        evt = new Event(evt);
        ctrl = evt.target;
        if (ctrl && ctrl.value) {
            if (ctrl.hasClass(this.cssInitialState)) {
                ctrl.removeClass(this.cssInitialState);
                ctrl.value = '';
            }
        } else {
            //console.error('Error in onfocus');
        }
    }
};

window.addEvent("domready", Search.init.bind(Search));
