﻿/// <reference path="../Plugins/jquery-1.3.2-vsdoc.js" />
Type.registerNamespace("Controls");


/// <summary>
/// V1.4 searches while you writes
/// This plugin can also be used to call custom controller which i.e. only lists the custmers ordernumbers
/// Depends:
/// jquery-1.4.js
///                       ui.core.js
/// ExtendJquery.js
/// MicrosoftAjax.js
/// </summary>
Controls.AsYouTypeSearch = function() {
}
Controls.AsYouTypeSearch.prototype =
{
    _index: 0,
    _count: 0,
    _init: function() {
        $(this.element).keyup($.createEventDelegate(this, this._onKeyup));
        $(this.element).keydown($.createEventDelegate(this, this._onKeydown));
        this.options.LastSearchText = $(this.element).val();
        $(this.element).focus($.createEventDelegate(this, this._onFocus));
        if (this.options.searchResult == null) {
            this.options.searchResult = this.element.attr("id") + "AsYouTypeSearchResult";
            this.element.after('<div id="' + this.options.searchResult + '" class="searchResult"></div>')
            this.options.searchResult = "#" + this.options.searchResult;
        }
        $(this.options.closeButton, this.options.searchResult).live("click", $.createEventDelegate(this, this._onClose));
        if (this.options.closeOnBlur) {
            $(this.element).blur($.createEventDelegate(this, this._onBlur));
            //$(this.options.searchResult).mouseout($.createEventDelegate(this, this._onShow));
        }
        this.options.isAjaxStarted = false;
    },
    _onBlur: function(element, event) {
        window.setTimeout(Function.createDelegate(this, this._onClose), 1000);
    },
    _onFocus: function(element, event) {
        this._onShow();
        this._onKeyup();
    },
    _up: function() {
        if (this._index > 0) {
            this._index--;
            $(this.options.searchResult).find(this.options.resultItem).removeClass(this.options.highlightClass);
            $($(this.options.searchResult).find(this.options.resultItem)[this._index]).addClass(this.options.highlightClass);
        }
    },
    _down: function() {
        if (this._index < this._count - 1) {
            this._index++;
            $(this.options.searchResult).find(this.options.resultItem).removeClass(this.options.highlightClass);
            $($(this.options.searchResult).find(this.options.resultItem)[this._index]).addClass(this.options.highlightClass);
        }
    },
    _select: function() {
        $("#searchForm").attr('action', '');
        var aTag = $($(this.options.searchResult).find(this.options.resultItem)[this._index]).find('a');
        // For javascript click event
        var retval = aTag.triggerHandler('click');
        var url = aTag.attr('href');

        // Goto href unless propagations was stopped
        if (retval != false && url != "undefined" && url != "" && url != "#") {
            $.gotoUrl(url);
        }
    },
    _onKeydown: function(element, event) {
        if (event) {
            if (event.which == 27) {
                this._onClose();
            }
            else if (event.which == 38) {
                this._up();
                return;
            }
            else if (event.which == 40) {
                this._down();
                return;
            }
            else if (event.which == 13) {
                if (this._index >= 0) {
                    this._select(this._index)
                    event.stopPropagation();
                    event.preventDefault();
                }
            }
        }
    },
    _onKeyup: function(element, event) {
        var text = $(this.element).val();
        if (text != this.options.LastSearchText && text.length > this.options.startSearchAfterCharNo && text != this.options.ignoreText) {
            $.log("onKeyUp input text:" + text + " ,LastSearchText" + this.options.LastSearchText);
            this.Search();
        }
        else {
            $.log("no need for search! input text:" + text + " ,LastSearchText" + this.options.LastSearchText);
        }
        if (text.length <= this.options.startSearchAfterCharNo) {
            this.options.LastSearchText = "";
            this._onClose();
        }
    },
    _onClose: function(element, event) {
        //$(this.options.searchResult).html("");
        $(this.options.searchResult).css({ display: "none" });
        this._index = -1;
        this._count = 0;
    },
    _onShow: function() {
        if (this.options.doPositioning) {
            //$(this.options.searchResult).css({ position: "absolute", top: $(this.element).position().top + $(this.element).outerHeight() + (this.element).offset().top + "px", left: $(this.element).position().left });
            $(this.options.searchResult).css({ position: "absolute", top: ($(this.element).outerHeight() + $(this.element).offset().top) + "px", left: $(this.element).offset().left });
        }
        if ($(this.options.searchResult).html() != "")
            $(this.options.searchResult).css({ display: "inline" });
    },
    _onLoaded: function(resultData, status) {
        $.log("_onLoaded");
        this.options.isAjaxStarted = false;
        if (status == "success") {
            $(this.options.searchResult).html(resultData);
        } else {
            $(this.options.searchResult).html("");
        }
        this._onShow();
        this._index = -1;
        this._count = $(this.options.searchResult).find(this.options.resultItem).size();

        if (MasterLoad) {
            $.log("runs MasterLoad!");
            MasterLoad(true, $(this.options.searchResult));
        }
        if (this.options.onLoaded != null) {
            this.options.onLoaded(resultData, status);
        }
        if (this.options.LastSearchText != $(this.element).val()) {
            this._onKeyup();
        }
    },
    Search: function() {
        $.log("try search!:" + this.options.isAjaxStarted);
        if (!this.options.isAjaxStarted) { //avoid multiple ajax requests
            this.options.isAjaxStarted = true;
            this.options.LastSearchText = $(this.element).val();
            $.log("search:" + this.options.LastSearchText);

            if (this.options.useGet) {
                $.get(this.options.url, { searchText: this.options.LastSearchText }, Function.createDelegate(this, this._onLoaded), "html");
            }
            else {
                $.post(this.options.url, { searchText: this.options.LastSearchText }, Function.createDelegate(this, this._onLoaded), "html");
            }
        }
    },


    defaults: {
        searchResult: null,
        closeButton: ".CloseButton",
        onLoaded: null,
        startSearchAfterCharNo: 2,
        url: "/AsYouTypeSearch/Search",
        maxOneRquestPer: 0, /*TODO*/
        closeOnBlur: true, /*needs that the search result is fully clickable */
        doPositioning: true,
        useGet: true,
        ignoreText: null,
        highlightClass: "over",
        resultItem: "li"
    }
}

Controls.AsYouTypeSearch.registerClass('Controls.AsYouTypeSearch', null, Sys.IDisposable);
$.registerAsWidget(Controls.AsYouTypeSearch);



