function autoSuggest() {}
autoSuggest.prototype.tagInputId		= "at1";
autoSuggest.prototype.tagInputObj 		= false;
autoSuggest.prototype.tagsContainerId 	= "tags";
autoSuggest.prototype.tagsContainerObj 	= false;	
autoSuggest.prototype.head 				= window.document.getElementsByTagName('head')[0];
autoSuggest.prototype.items 			= false;
autoSuggest.prototype.itemsCount 		= 0;
autoSuggest.prototype.selectedItem 		= -1;
autoSuggest.prototype.word 				= "",
autoSuggest.prototype.position 			= false;
autoSuggest.prototype.results 			= false;
autoSuggest.prototype.lastKey 			= false;
autoSuggest.prototype.liHeight 			= '15';
autoSuggest.prototype.lastkey			= false;
autoSuggest.prototype.calb				= false;
autoSuggest.prototype.path				= 'http://62.146.39.73/request=1';
autoSuggest.prototype.formObj			= false;
autoSuggest.prototype.formId			= 'formAsId';
autoSuggest.prototype.dict				= 'yp3_cities.pl';
autoSuggest.prototype.amount			= 100;
autoSuggest.prototype.sort				= "rank";
autoSuggest.activeTab					= false;

autoSuggest.prototype.hideResults		= function() {
	if( autoSuggest.activeTab ) {
		ob = window.document.getElementById( autoSuggest.activeTab );
		ob.innerHTML = "";
		ob.style.display = 'none';		
		this.itemsCount = 0;
		this.results = false;
	}
}	
autoSuggest.prototype.checkScrol		= function( me, e ) {
	keycode = false;	
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	this.lastkey = keycode;
	if( keycode == 38 || keycode == 40 || keycode == 27 || keycode == 13 || keycode == 33 || keycode == 34 || keycode == 35 || keycode == 36 || keycode == 9 ) {
		if( this.itemsCount > 0 ) {
			return this.selectItem( keycode );
		}
	}
	return true;
}	
autoSuggest.prototype.checkTags			= function() {
	if( this.tagInputObj.value.length > 1 && this.lastkey != 13 ) {
		if(this.tagInputObj.value != this.word ) {
			this.word = this.tagInputObj.value;
			var script= document.createElement('script');
			var scrpath = '';
			script.type= 'text/javascript';
			scrpath = this.path;
			scrpath = scrpath + '&subset=' + this.dict;
			scrpath = scrpath + '&amount=' + this.amount;
			if( this.calb ) {
				scrpath = scrpath + '&var=' + this.calb; 	
			}
			if( this.sort ) {
				scrpath = scrpath + '&sort=' + this.sort;
			}
			scrpath = scrpath + '&word=' + this.word;
			script.src = scrpath;
			this.head.appendChild(script);
		}
	} else if( this.itemsCount > 0 ) {		
		this.hideResults();
	}
}	
autoSuggest.prototype.selectItem		= function(code) {
	var tmp = this.selectedItem;
	switch(code) {
		case 38://up
				if( tmp >= 0 ) {
					this.selectedItem = ( tmp > 0 ) ? this.selectedItem - 1 : this.itemsCount - 1;
				} else {
					this.selectedItem = this.itemsCount - 1;
				}
				if( this.selectedItem >= 5 ) {
					this.tagsContainerObj.scrollTop = ( 5 + this.selectedItem ) * this.liHeight;
				}
			break;
		case 40://down
				this.selectedItem = ( tmp <  ( this.itemsCount - 1 ) && tmp >= 0 ) ? this.selectedItem + 1 : 0 ;
			break;
		case 27://esc
				this.hideResults();				
				return fals;
			break;
		case 13://enter
				this.moveResult();
				return false;
			break;
		case 34://pgdn
				if( ( this.selectedItem + 10 ) < ( this.itemsCount - 1 ) ) {
					this.selectedItem += 10;
				} else {
					this.selectedItem = this.itemsCount - 1;
				}
			break;
		case 33://pgup
				if( this.selectedItem > 10 ) {
					this.selectedItem -= 10; 
				} else {
					this.selectedItem  = 0;
				}
			break;
		case 36://home
				this.selectedItem = 0;					
			break;
		case 35://end
				this.selectedItem = this.itemsCount - 1;
			break;
		case 9://tab
			this.hideResults();
			return true;
			break;
	}
	if( tmp >= 0 ) {
		this.items[tmp].className = "";			
	}
	if( this.selectedItem >= 0 ) this.items[this.selectedItem].className = "selected";
	if( this.selectedItem >= 5 ) {
		this.tagsContainerObj.scrollTop = ( this.selectedItem - 5 ) * this.liHeight;
	} else {
		this.tagsContainerObj.scrollTop = 0;
	}
	return false;
}	
autoSuggest.prototype.getResults		= function(data) {	
	var txt = "";
	if( data.results_number > 0 ) {
		autoSuggest.activeTab = this.tagsContainerObj.id;
		var me = this;
		this.selectedItem = -1;
		this.itemsCount = data.results_number;
		txt = "<ul>"
		for( i = 0; i < this.itemsCount; i++ ) {
			txt += "<li id='TagId_"+i+"' title='"+data.results[i].property+"'>" + data.results[i].word + '</li>';
		}
		txt += "</ul>";
		this.tagsContainerObj.innerHTML = txt;			
		this.items = this.tagsContainerObj.getElementsByTagName('li');
		this.tagsContainerObj.style.display = 'block';
		this.results = data.results;
		this.tagsContainerObj.scrollTop = 0;
		for( i = 0; i < this.itemsCount; i++ ) {
			this.items[i].onmouseover = function( ev ) { me.mouseOver( this, ev ); };
			this.items[i].onclick = function() { me.moveResult(); };
		}		
	} else {
		this.hideResults();			
	}
}
autoSuggest.prototype.moveResult		= function() {
	if( this.selectedItem != -1 ) {
		this.tagInputObj.value = this.results[this.selectedItem].word;
	}
}	
autoSuggest.prototype.mouseOver			= function( me, e ) {
	if (window.event) ev = window.event.srcElement;
	else if (e) ev = e.target;
	var tmp = this.selectedItem;
	this.selectedItem = this.getId( me.id );
	if( tmp >= 0 ) {
		this.items[tmp].className = "";			
	}
	this.items[this.selectedItem].className = "selected";
}
autoSuggest.prototype.getId				= function( str ) {
	return str.substr( 6 );
}
autoSuggest.prototype.init				= function() {
	var me = this;
	this.tagInputObj = window.document.getElementById(this.tagInputId);
	this.tagsContainerObj = window.document.getElementById( this.tagsContainerId );
	this.tagInputObj.onkeyup = function( ev ){ me.checkTags( this, ev ); };
	this.tagInputObj.onkeydown = function( ev ) { return me.checkScrol(this, ev ); };
	topPos = this.tagInputObj.offsetTop;
	leftPos = this.tagInputObj.offsetLeft;
	heightPos = this.tagInputObj.offsetHeight;
	this.position = getPosition(this.tagInputObj).split(",");
	this.tagsContainerObj.style.left = this.position[0]+"px";
	this.tagsContainerObj.style.top = ( parseInt( this.position[1] ) + heightPos ) + "px";
	window.document.onclick = function() { me.hideResults() };
//	addEvent( window.document, 'click', function() { me.hideResults(); } );
}

function getResults( results ) {
	res.getResults( results );
}

window.onload = function() {
	res = new autoSuggest();
	res.calb = 'getResults';
	res.tagsContainerId = 'tags';
	res.tagInputId = 'tagssearch';
//	res.dict = 'yp3_cities.pl';
	res.init();
}

function getPosition(obj){
    var topValue= 0,leftValue= 0;
    while(obj){
		leftValue+= obj.offsetLeft;
		topValue+= obj.offsetTop;
		obj= obj.offsetParent;
    }
    finalvalue = leftValue + "," + topValue;
    return finalvalue;
}


