var timer;
var current=-1;
var suggestions=0;
var aSuggestions = [];
var top=0;
/**
 * An autosuggest textbox control.
 * @class
 * @scope public
 */
function showbox() { 
	document.getElementById('list').style.display="block";
    clearTimeout(timer);
	timer=setTimeout('closebox()',5000);
}


function closebox()
{ document.getElementById('list').style.display="none";
        /*document.getElementById('kcover').style.display="none";*/
        current=-1;
        top=0;
}
function setSuggestion(sSuggestion /*:String*/) {
	document.getElementById('txt1').value = sSuggestion; 
	document.getElementById('list').style.display="none";
	document.getElementById('txt1').focus();
	document.search_box.submit();
	current=-1;
	top=0;
	}

function AutoSuggestControl(oTextbox /*:HTMLInputElement*/, 
                            oProvider /*:SuggestionProvider*/) {
       
    /**
     * Suggestion provider for the autosuggest feature.
     * @scope private.
     */
    this.provider /*:SuggestionProvider*/ = oProvider;
    
    /**
     * The textbox to capture.
     * @scope private
     */
    this.textbox /*:HTMLInputElement*/ = oTextbox;
    
    //initialize the control
    this.init();
    
}

/**
 * Autosuggests one or more suggestions for what the user has typed.
 * If no suggestions are passed in, then no autosuggest occurs.
 * @scope private
 * @param aSuggestions An array of suggestion strings.
 */
AutoSuggestControl.prototype.autosuggest = function (tSuggestions /*:Array*/) {
    
    //make sure there's at least one suggestion
    if (tSuggestions.length > 0) {
        this.typeAhead(tSuggestions);
    }
};


/**
 * Handles keyup events.
 * @scope private
 * @param oEvent The event object for the keyup event.
 */
AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {

    var iKeyCode = oEvent.keyCode;
   
	if(iKeyCode==38 )
    { 
	  if(current>top)
	  { var i=top;
        current--;
	    if((current)<suggestions-1)
		{ document.getElementById('suggestion'+(current+1)).style.background='#1A1A1A';
		}
	    document.getElementById('suggestion'+current).style.background='#484848';
	    showbox();
	  }else
	  { if(current>0)
	    { top--;
	      current--;
	      write_suggestions();
		  
		  document.getElementById('suggestion'+(current+1)).style.background='#1A1A1A';
	      document.getElementById('suggestion'+current).style.background='#484848';
	    }
	  	
	  }
      
	}
    if(iKeyCode==40)
    { if(current<(suggestions)-1)
	  { var i=top;
        current++;
	    if((current-1)>-1)
		document.getElementById('suggestion'+(current-1)).style.background='#1A1A1A';
	    document.getElementById('suggestion'+current).style.background='#484848';
	    showbox();
	  }else
	  { if(current<aSuggestions.length-1)
	    { top++;
	      current++;
	      write_suggestions();
		  
		  document.getElementById('suggestion'+(current-1)).style.background='#1A1A1A';
	      document.getElementById('suggestion'+current).style.background='#484848';
	    }
	  }
	  
    }
    //make sure not to interfere with non-character keys
    if(iKeyCode==13)
    { if(current>-1)
	  { setSuggestion(aSuggestions[current])
	  } else
	  { document.search_box.submit();
	  }
	} 
	
	if ((iKeyCode < 32 && iKeyCode!=8) || (iKeyCode >= 33 && iKeyCode <= 45) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignore
    } else {
        //request suggestions from the suggestion provider
        current=-1;
        top=0;
		this.provider.requestSuggestions(this);
	
	}
};

/**
 * Initializes the textbox with event handlers for
 * auto suggest functionality.
 * @scope private
 */
AutoSuggestControl.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);
      return false;
	};
    
};

/**
 * Selects a range of text in the textbox.
 * @scope public
 * @param iStart The start index (base 0) of the selection.
 * @param iLength The number of characters to select.
 */
AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) {

    //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();      
}; 

/**
 * Inserts a suggestion into the textbox, highlighting the 
 * suggested part of the text.
 * @scope private
 * @param sSuggestion The suggestion for the textbox.
 */
AutoSuggestControl.prototype.typeAhead = function (sSuggestion /*:String*/) {

    //check for support of typeahead functionality
    //if (this.textbox.createTextRange || this.textbox.setSelectionRange){
    //    var iLen = this.textbox.value.length; 
    //    //this.textbox.value = sSuggestion; 
    //    this.selectRange(iLen, sSuggestion.length);
    //}
    aSuggestions=sSuggestion;
	write_suggestions();

};




function write_suggestions()
{  var t=8+top;
    if(aSuggestions.length<t)
    { t=aSuggestions.length;
    }   
    suggestions=t;
		
		document.getElementById('list').style.display="block";
		/*
        document.getElementById('iframe').height=((t-top)*14)+2;
        document.getElementById('kcover').style.display="block";
        */
        clearTimeout(timer);
		timer=setTimeout('closebox()',5000);
     var i=top;
	 var write="<table cellpadding='0' cellspacing='0' width='100%'>";
	 var word="";
	 while(i<t)
	 { if(aSuggestions[i].length>30)
	   { word=(aSuggestions[i].substring(0,30))+'..';
	   }else
	   { word=aSuggestions[i];
	   }
	   write=write+"<tr><td id='suggestion"+i+"' style='padding-left:4px'><span class='norm'> <a href='javascript:void(0)' onmouseover=\"showbox()\" onClick=\"setSuggestion('"+aSuggestions[i]+"')\">"+word+"</a></span></td></tr>";   
       i++;
	 }		

	 write=write+"</table>";
		document.getElementById('words').innerHTML=write;
	
	
}