function SugerenciasControl(oTextbox, oProvider) {

    this.cur = -1;
    this.lastString = '';
    this.layer = null;
    this.provider = oProvider;
    this.textbox = oTextbox;
    this.init();

}

SugerenciasControl.prototype.sugerencias = function (aSugerencias, bTypeAhead) {

    if (aSugerencias.length > 0) {
        this.lastString = this.textbox.value;
        if (bTypeAhead) {
           this.typeAhead(aSugerencias[0]);
        }
        this.showSugerencias(aSugerencias);
    } else {
        this.hideSugerencias();
        if (this.textbox.value != '')
			this.textbox.value = this.lastString;
    }
}

SugerenciasControl.prototype.createDropDown = function () {

    var oThis = this;

    this.layer = document.createElement("div");
    this.layer.className = "sugerencias";
    this.layer.style.visibility = "hidden";
    //para que no se vea la capa
    this.layer.style.display = "none";
    this.layer.style.width = this.textbox.offsetWidth;

    //when the user clicks on a suggestion, get the text (innerHTML)
    //and place it into a textbox
    this.layer.onmousedown =
    this.layer.onmouseup =
    this.layer.onmouseover = function (oEvent) {
        oEvent = oEvent || window.event;
        oTarget = oEvent.target || oEvent.srcElement;

        if (oEvent.type == "mousedown") {
            oThis.textbox.value = oTarget.firstChild.nodeValue;
            oThis.hideSugerencias();
        } else if (oEvent.type == "mouseover") {
            oThis.highlightSugerencia(oTarget);
        } else {
            oThis.textbox.focus();
        }
    };
    document.body.appendChild(this.layer);
}

SugerenciasControl.prototype.getLeft = function () {

    var oNode = this.textbox;
    var iLeft = 0;

    while(oNode.tagName != "BODY") {
        iLeft += oNode.offsetLeft;
        oNode = oNode.offsetParent;
    }

    return iLeft;
}

SugerenciasControl.prototype.getTop = function () {

    var oNode = this.textbox;
    var iTop = 0;

    while(oNode.tagName != "BODY") {
        iTop += oNode.offsetTop;
        oNode = oNode.offsetParent;
    }

    return iTop;
}

SugerenciasControl.prototype.handleKeyDown = function (oEvent) {

    switch(oEvent.keyCode) {
        case 38: //up arrow
            this.previousSugerencia();
            break;
        case 40: //down arrow
            this.nextSugerencia();
            break;
        case 13: //enter
            this.hideSugerencias();
            break;
    }

}

SugerenciasControl.prototype.handleKeyUp = function (oEvent) {

    var iKeyCode = oEvent.keyCode;

    //for backspace (8) and delete (46), shows suggestions without typeahead
    if (iKeyCode == 8 || iKeyCode == 46) {
        this.provider.pedirSugerencias(this, false);

    //make sure not to interfere with non-character keys
    } else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignore
    } else {
        //request suggestions from the suggestion provider with typeahead
        this.provider.pedirSugerencias(this, true);
    }
}

SugerenciasControl.prototype.hideSugerencias = function () {
    this.layer.style.visibility = "hidden";
}

SugerenciasControl.prototype.highlightSugerencia = function (oSugerenciaNode) {

    for (var i=0; i < this.layer.childNodes.length; i++) {
        var oNode = this.layer.childNodes[i];
        if (oNode == oSugerenciaNode) {
            oNode.className = "current"
        } else if (oNode.className == "current") {
            oNode.className = "";
        }
    }
}

SugerenciasControl.prototype.init = function () {

    //save a reference to this object
    var oThis = this;

    //assign the onkeyup event handler
    this.textbox.onkeyup = function (oEvent) {

        //check for the proper location of the event object
        if (!oEvent) {
            oEvent = window.event;
        }

        //call the handleKeyUp() method with the event object
        oThis.handleKeyUp(oEvent);
    };

    //assign onkeydown event handler
    this.textbox.onkeydown = function (oEvent) {

        //check for the proper location of the event object
        if (!oEvent) {
            oEvent = window.event;
        }

        //call the handleKeyDown() method with the event object
        oThis.handleKeyDown(oEvent);
    };

    //assign onblur event handler (hides suggestions)
    this.textbox.onblur = function () {
        oThis.hideSugerencias();
    };

    //create the suggestions dropdown
    this.createDropDown();
}

SugerenciasControl.prototype.nextSugerencia = function () {
    var cSugerenciaNodes = this.layer.childNodes;

    if (cSugerenciaNodes.length > 0 && this.cur < cSugerenciaNodes.length-1) {
        var oNode = cSugerenciaNodes[++this.cur];
        this.highlightSugerencia(oNode);
        this.textbox.value = oNode.firstChild.nodeValue;
    }
}

SugerenciasControl.prototype.previousSugerencia = function () {
    var cSugerenciaNodes = this.layer.childNodes;

    if (cSugerenciaNodes.length > 0 && this.cur > 0) {
        var oNode = cSugerenciaNodes[--this.cur];
        this.highlightSugerencia(oNode);
        this.textbox.value = oNode.firstChild.nodeValue;
    }
}

SugerenciasControl.prototype.selectRange = function (iStart, iLength) {

    //use text ranges for Internet Explorer
    if (this.textbox.createTextRange) {
        var oRange = this.textbox.createTextRange();
        oRange.moveStart("character", iStart);
        oRange.moveEnd("character", iLength - this.textbox.value.length);
        oRange.select();

    //use setSelectionRange() for Mozilla
    } else if (this.textbox.setSelectionRange) {
        this.textbox.setSelectionRange(iStart, iLength);
    }

    //set focus back to the textbox
    this.textbox.focus();
}

SugerenciasControl.prototype.showSugerencias = function (aSugerencias) {

    var oDiv = null;
    this.layer.innerHTML = "";  //clear contents of the layer

    for (var i=0; i < aSugerencias.length; i++) {
        oDiv = document.createElement("div");
        oDiv.appendChild(document.createTextNode(aSugerencias[i]));
        this.layer.appendChild(oDiv);
    }

    this.layer.style.left = "199px";
//    this.layer.style.left = this.getLeft() + "px";
    this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
    this.layer.style.visibility = "visible";
}

SugerenciasControl.prototype.typeAhead = function (sSugerencia) {

    //check for support of typeahead functionality
    if (this.textbox.createTextRange || this.textbox.setSelectionRange){
        var iLen = this.textbox.value.length;
        this.textbox.value = sSugerencia;
        this.selectRange(iLen, sSugerencia.length);
    }
}

TipoViaSugerencias.prototype.pedirSugerencias = function (oAutoSuggestControl, bTypeAhead) {
    var aSugerencias = [];
    var sTextboxValue = oAutoSuggestControl.textbox.value;

    if (sTextboxValue.length > 0){

        for (var i=0; i < this.tiposVia.length; i++) {
            if (this.tiposVia[i].indexOf(sTextboxValue.toUpperCase()) == 0) {
                aSugerencias.push(this.tiposVia[i]);
            }
        }
    }
    //proporcionar sugerencias al control
    oAutoSuggestControl.sugerencias(aSugerencias, bTypeAhead);
}

