/*
 * smartbar.js - Smartbar(r) engine
 * (c)2002 A3.boot.development s.r.o.
 * All rights reserved
 *
 * Programming is like any other "artwork",
 * THIS piece of data is an ART which has it's artworker.
 * Thus it is proteced by copyright law and international treaties !
 * You can not use any part of it, you can not modify it and then use it in any concept !
 *
 * If you do so, you can be prosecuted by maximal extend
 * under the law.
 */

// array of words (shortcuts)
var sbGwrds = new Array( "love" );
// helpfull variables
var sbGlastFound, sbGoff;
var sbGout, sbGinp;
var sbColor = "#808080";

// sbInit, set up defaults 
// oIn should be <input // for compatibility purposes
// oOut should be <div
// style and posisiton has to be set by caller
function sbInit( oIn, oOut ) {
		
try{
	// initialize
	sbGoff = 0;
	sbGout = oOut;
	sbGinp = oIn;

	// events
	sbGinp.onkeyup = sbOnKup;
//	sbGinp.onfocus = sbOnFocus;
	sbGout.onclick = sbOnClick;
	
}catch( e ) {
}
}

// onKeyUp event for <input
// sbOnAction function has to be implemented by caller
// the parameter is index to words array passed to engine
// with sbSetW();
function sbOnKup( oEvent ) {
	
	var strIn;
	var strOut;
	var i, len;
	
	strIn = sbGinp.value;
	strOut = sbGout.innerHTML;
	
	if( document.all )	// compatibility
		oEvent = event;
	
	if( oEvent.keyCode == 13 ) {
		if( strOut != "" )
			sbOnAction( sbGlastFound );
		else
			sbOnAction( -1 );
			
		return;
	}

	sbGout.innerHTML = "";
	sbGlastFound = 0;
	
	if( strIn == "" )
		return;
	
	for( i = sbGoff; i < sbGwrds.length; i++ ) {
		len = strIn.length;
		if( sbGwrds[i].substring( 0, len ).toLowerCase() == strIn.toLowerCase() ) {
			sbGout.innerHTML = strIn + "<font color='" + sbColor + "'>" + sbGwrds[i].substring( len, 100 ) +"</font>";
			sbGlastFound = i;
			break;
		}
	}
	
	// do not bubble events through hierarchy
	oEvent.cancelBubble = true;	
}

// set offset to start comparing of word array
// usable for fast language switching for example.
// ( you can put one language to first n items in array and another
// to next n items, then when offset is set, first n items are neglected.
function sbSetOffset( i ) {
	sbGoff = i;
}

function sbSetColor( c ) {
	sbColor = c;
}

// fix clicks on div, ( should be clicks on input, from users point of view )
function sbOnClick() {
	sbGinp.value = "";
	sbGout.innerHTML = "";
	sbGinp.focus();
}

// set words array, this could be changed anytime
function sbSetW( arW ) {
	sbGwrds = arW;
}

// set focus to smartbar
function sbFocus() {
//	sbGinp.value = "";
//	sbGout.innerHTML = "";
	sbGinp.focus();
}

// set output <div to some text.
// used for lower complexity
function sbSetOut( t ) {
	sbGinp.value = "";
	sbGout.innerHTML = t;
	sbGout.focus();
}

// get a text from <div
function sbGetOut() {
	return sbGout.innerHTML;
}
