﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />
var $FlagSubmit = true;

jQuery.AutoComplete = function(Nome, ArrayPalavras, InputText, DivAutoComplete, nMaxSize, nMinChar, CallBack) {
    // initialize member variables
    var $oText = InputText;
    var $Name = Nome;
    var $oDiv = DivAutoComplete;
    var $nMaxSize = nMaxSize;
    var $nMinChar = nMinChar;

    var $input = $($oText);
    var $div = $($oDiv);



    $div.Ativa = -1;

    this.AutoCompleteDB = function() {
        this.ArrayPalavras = new Array();
    }
    this.fnCallBack = function(strValue) {
        CallBack(strValue);
    }

    // preprocess the texts for fast access
    $db = new this.AutoCompleteDB();
    $db.ArrayPalavras = ArrayPalavras;

    this.MudaArray = function(NovoArray) {
        $db.ArrayPalavras = NovoArray;
    };
    var i, n = ArrayPalavras.length;

    // attach handlers to the text-box
    //oText.onkeydown = AutoComplete.prototype.onKeyUp;
    //$($oText).blur(AutoComplete.onTextBlur);


    this.preventDefault = function(e) {
        if (e == undefined)
        //e = window.e
            if (e.preventDefault) { //standart browsers
            e.preventDefault()
        } else { // internet explorer
            e.returnValue = false
        }
        return false;
    };

    this.getStrings = function(palavraBusca, ArraySaida) {

        //var lenFor = (this.nMaxSize > this.aStr.length) ? this.aStr.length : this.nMaxSize;
        for (var i = 0; i < $db.ArrayPalavras.length; i++) {
            if ($db.ArrayPalavras[i].toLowerCase().indexOf(palavraBusca.toLowerCase()) != -1) {
                ArraySaida.push($db.ArrayPalavras[i]);
            }
        }
    };

    this.ShowList = function() {
        var txt = $input.val();
        $FlagSubmit = false;
        $div.Ativa = -1;
        // get all the matching strings from the AutoCompleteDB
        var palavrasEncontradas = new Array();
        $objAuto.getStrings(txt, palavrasEncontradas);

        // count the number of strings that match the text-box value
        var nCount = palavrasEncontradas.length;
        if ((txt.length >= $nMinChar) && nCount <= 0) {
            this.fnCallBack(txt);
            $div[0].innerHTML = "";
            $div[0].style.visibility = "hidden";
            return;
        }
        // if a suitable number then show the popup-div
        if (($nMaxSize == -1) || ((nCount > 0) && (txt.length >= $nMinChar))) {
            // clear the popup-div.
            while ($div[0].hasChildNodes())
                $div[0].removeChild($div[0].firstChild);

            // add each string to the popup-div
            var i, n = palavrasEncontradas.length;
            var lenFor = ($nMaxSize > palavrasEncontradas.length) ? palavrasEncontradas.length : $nMaxSize;
            for (i = 0; i < lenFor; i++) {
                var oDiv = document.createElement('li');
                oDiv.id = $Name + "_li_" + i;
                oDiv.innerHTML = palavrasEncontradas[i];

                oDiv.onmousedown = function() {
                    $input.val(this.innerHTML);
                };
                oDiv.onmouseover = function() {
                    this.className = "AutoCompleteHighlight";
                };
                oDiv.onmouseout = function() {
                    this.className = "AutoCompleteBackground";
                };
                $div[0].appendChild(oDiv);
                //oDiv.onkeypress = AutoComplete.prototype.onKeyUp;
                //oDiv.AutoComplete = this;
            }
            $div[0].style.visibility = "visible";
        }
        else // hide the popup-div
        {
            $div[0].innerHTML = "";
            $div[0].style.visibility = "hidden";
        }
    };
    function onKeyUp(e) {
        var keynum = "";

        if (e == undefined || e == null) {
            if (window.event)
                e = window.event;
        }

        if (window.event) { // IE
            keynum = e.keyCode;
        } else {  // Netscape/Firefox/Opera
            keynum = e.charCode || e.keyCode || e.which;
        }

        switch (keynum) {
            case 38: // up
                //e.preventDefault();
                $div.Ativa = $objAuto.moveSelect(-1);
                return $objAuto.preventDefault(e);
                break;
            case 40: // down
                //e.preventDefault();
                $div.Ativa = $objAuto.moveSelect(1);
                return $objAuto.preventDefault(e);
                break;
            case 13: // return
                $input.val($objAuto.Select());
                $div[0].innerHTML = "";
                $div[0].style.visibility = "hidden";
                //$input.get(0).blur();
                //$FlagSubmit = true;
                return $objAuto.preventDefault(e);
                break;



        }

        return true;
    }; /*
    function onKeyPress()
    {
        var e = window.event;
        switch (e.keyCode)
        {
            case 13: // return
                $input.val($objAuto.Select());
                $div[0].innerHTML = "";
                $div[0].style.visibility = "hidden";
                //$input.get(0).blur();
                return $objAuto.preventDefault(e);
                break;

        }
        return true;
    };
*/
    this.Select = function() {

        var lis = $("li");
        if (!lis) return;
        var newLis = new Array();

        for (i = 0; i < lis.size(); i++) {
            if (lis[i].id.indexOf($Name + "_li_") != -1) {
                newLis.push(lis[i]);
            }
        }

        if ($div.Ativa == undefined)
            $div.Ativa = -1;

        if ($div.Ativa < 0) {
            $div.Ativa = 0;
        } else if ($div.Ativa >= newLis.length) {
            $div.Ativa = newLis.length - 1;
        }

        if (newLis.length > 0) {
            return $(newLis[$div.Ativa]).text();
        } else {
            var nameLink = '#' + jQuery("#hidlkbBuscar").val();
            jQuery(nameLink).trigger('focus');
            var onclickEvent = jQuery(nameLink)[0].href;
            eval(onclickEvent);
        }

        // Weird behaviour in IE
        // if (lis[active] && lis[active].scrollIntoView) {
        // 	lis[active].scrollIntoView(false);
        // }

    };



    this.moveSelect = function(step) {

        var lis = $("li");
        var newLis = new Array();
        if (!lis) return;
        lis.removeClass("AutoCompleteHighlight");
        for (i = 0; i < lis.size(); i++) {
            if (lis[i].id.indexOf($Name + "_li_") != -1) {
                newLis.push(lis[i]);
            }

        }

        if ($div.Ativa == undefined)
            $div.Ativa = -1;

        $div.Ativa += step;

        if ($div.Ativa < 0) {
            $div.Ativa = 0;
        } else if ($div.Ativa >= newLis.length) {
            $div.Ativa = newLis.length - 1;
        }

        $(newLis[$div.Ativa]).addClass("AutoCompleteHighlight");
        return $div.Ativa;


    };
    //$input[0].AutoComplete = this;
    //$input.keyup(this.AutoComplete);

    $input
        .blur(function() {
            $($oDiv)[0].style.visibility = "hidden";
        })
    /*.change(function(e)
    {
    if (onKeyUp(e))
    ShowList();
    })*/
        .keyup(function(e) {
            if (onKeyUp(e))
                $objAuto.ShowList();
        })
    //.keypress(function() { onKeyPress(); })
        ;

    var $objAuto = this;

};
/*
$.ready(
    
//$('').contents('form').submit(function() { alert('opa'); });
$("form").submit(function() { alert('opa'); });
//$("form")[0].action = "";
    
);
*/
/*AutoComplete.onchange = function()
{
this.ShowList();
}*/
/*
$div
.mousedown(function()
{
$input.val(this.innerHTML);
})
.mouseover(function()
{
this.className = "AutoCompleteHighlight";
})
.mouseout(function()
{
this.className = "AutoCompleteBackground";
});
*/

