﻿/// <reference name="MicrosoftAjax.js" />

Type.registerNamespace('CodeCounsel.Homepage.Search');

CodeCounsel.Homepage.Search.SearchBox = function(element) {
    CodeCounsel.Homepage.Search.SearchBox.initializeBase(this, [element]);
    this.searchBox = element;
    this.button = this.searchBox.childNodes.item(0).childNodes.item(0);
    this.inputField = this.searchBox.childNodes.item(1).childNodes.item(0);
    this._searchUri = null;
}

CodeCounsel.Homepage.Search.SearchBox.prototype =
{
    searchUri: null,
    searchBox: null,
    inputField: null,
    button: null,
    hasFocus: false,
    query: null,

    get_searchUri: function() {
        return this._searchUri;
    },
    set_searchUri: function(value) {
        this._searchUri = value;
    },

    initialize: function() {
        this.searchBox.control = this;
        this.inputField.control = this;
        this.button.control = this;
        $addHandler(this.button, 'mouseover', this.buttonMouseOver);
        $addHandler(this.button, 'click', this.buttonClick);
        $addHandler(this.inputField, 'keydown', this.fieldKeyDown);
        $addHandler(this.inputField, 'mouseout', this.fieldMouseOut);
        $addHandler(this.inputField, 'focus', this.fieldFocus);
        $addHandler(this.inputField, 'blur', this.fieldBlur);
        $addHandler(this.searchBox, 'mouseout', this.searchBoxMouseOut);
        CodeCounsel.Homepage.Search.SearchBox.callBaseMethod(this, 'initialize');
    },

    dispose: function() {
        $removeHandler(this.button, 'mouseover', this.buttonMouseOver);
        $removeHandler(this.button, 'click', this.buttonClick);
        $removeHandler(this.inputField, 'keydown', this.fieldKeyDown);
        $removeHandler(this.inputField, 'mouseout', this.fieldMouseOut);
        $removeHandler(this.inputField, 'focus', this.fieldFocus);
        $removeHandler(this.inputField, 'blur', this.fieldBlur);
        $removeHandler(this.searchBox, 'mouseout', this.searchBoxMouseOut);
        CodeCounsel.Homepage.Search.SearchBox.callBaseMethod(this, 'dispose');
    },

    showSearchBox: function() {
        this.inputField.style.display = "inline";
        if (this.query != null && this.inputField.value == '') {
            this.inputField.value = this.query;
        }
    },

    hideSearchBox: function() {
        if (this.hasFocus == false) {
            this.query = this.inputField.value;
            this.inputField.value = '';
            this.inputField.style.display = "none";
        }
    },

    searchBoxMouseOut: function(e) {
        if (e.target == this.control.searchBox) {
            this.control.hideSearchBox();
        } 
    },

    fieldMouseOut: function(e) {
        this.control.hideSearchBox();
    },

    fieldFocus: function(e) {
        this.control.hasFocus = true;
    },

    fieldBlur: function(e) {
        this.control.hasFocus = false;
        this.control.hideSearchBox();
    },

    buttonMouseOver: function(e) {
        this.control.showSearchBox();
    },

    fieldKeyDown: function(e) {
        if (e.keyCode === 13) {
            this.control.doSearch();
            return false;
        }
        return true;
    },

    buttonClick: function(e) {
        this.control.doSearch();
        return false;
    },

    doSearch: function() {
        var value = this.getTrimmedQuery();
        if (value !== '') {
            window.location = this._searchUri + '?k=' + value;
        } else {
            this.inputField.focus();
        }
    },

    getTrimmedQuery: function() {
        var value = this.inputField.value;
        value = value.replace(/^\s+/, '');
        value = value.replace(/\s+$/, '');
        return value;
    }
}

if (typeof (Sys) !== 'undefined') {
    CodeCounsel.Homepage.Search.SearchBox.registerClass('CodeCounsel.Homepage.Search.SearchBox', Sys.UI.Control);
}
if (typeof (Sys) !== 'undefined') {
    Sys.Application.notifyScriptLoaded();
}