function autoSuggest() {}
autoSuggest.prototype.tagInputId		= 'as';
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:80/request=1';
autoSuggest.prototype.dict				= 'yp3_cities.pl';
autoSuggest.prototype.amount			= 100;
autoSuggest.prototype.sort				= 'rank';
autoSuggest.activeTab					= false;
autoSuggest.prototype.disable			= false;
autoSuggest.prototype.redirectInputName	= false;
autoSuggest.prototype.formAction		= 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 ) {
	if(this.disable){
		$('.sugg').hide();		
		return false;
	}
	keycode = false;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	this.lastkey = keycode;
	
	if(keycode !=13){
		this.deleteRid();
		$('#'+this.formId).attr('action',this.formAction);
	}
	
	if( keycode == 38 || keycode == 40 || keycode == 27 || keycode == 13 || keycode == 33 || keycode == 34 || keycode == 35 || keycode == 36 || keycode == 9 ) {
		if( this.itemsCount > 0 ) {//alert(keycode);
			return this.selectItem( keycode );
		}
	}
	return true;
}	
autoSuggest.prototype.checkTags			= function() {
		
	if(this.disable){
		$('.sugg').hide();		
		return false;
	}
	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.deleteRid		= function() {
	$('#'+this.formId+' input[name='+this.redirectInputName+']').remove();	
	$('#'+this.formId+' input[name=fid]').remove();
}
	
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 false;
			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;
	}
	try{
		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;
		}
	}catch(e){ var a=e; }
	return false;
}	
autoSuggest.prototype.getResults		= function(data) {
	
	if(this.disable){
		$('.sugg').hide();		
		return false;
	}	
	if( data.results_number > 0 ) {	
		autoSuggest.activeTab = this.tagsContainerObj.id;		
		var me = this;
		this.selectedItem = -1;
		this.itemsCount = data.results_number;
		var maxLen = 0;
		var property_arr = false;
		var txt = '<ul>';
		for( i = 0; i < this.itemsCount; i++ ) {
				
				if(data.results[i].word.length > maxLen) maxLen = data.results[i].word.length + data.results[i].property.length;	
			
				if(typeof(data.results[i].property.split('::'))!='undefined')
					data.results[i].property_arr = data.results[i].property.split('::');
					
				else
					data.results[i].property_arr = false;
					
				switch(me.type){
			
					case 'where':	
					
						data.results[i].property = data.results[i].property_arr[2];
						if(typeof(data.results[i].property_arr[3])!='undefined' && data.results[i].property_arr[3]!='')
							data.results[i].property += ', '+data.results[i].property_arr[3];	
									
						break;
	
					case 'what':
					
						if(typeof(data.results[i].property_arr[1])!='undefined'){
							data.results[i].property = data.results[i].property_arr[1];
						}
							
						break;
					case 'cpv':
					data.results[i].property = '';
					case 'cpv_sup':
					data.results[i].property = '';
					case 'cpv_search':
					data.results[i].property = '';
									
					break;
					
				}

				txt += "<li id='TagId_"+i+"' title='"+data.results[i].property+"'>" + data.results[i].word;
				
				if(data.results[i].property)
				txt += ' <i>('+data.results[i].property+')</i>'
					
				txt +='</li>'
		}			
		txt += '</ul>';
		this.items = this.tagsContainerObj.getElementsByTagName('li');
		if( this.disable ){
			this.tagsContainerObj.style.display = 'none';			
			return false;
		}else{
			this.tagsContainerObj.style.display = 'block';
			this.tagsContainerObj.innerHTML = txt;
		}	
		if(maxLen > 25) {
			if(this.tagsContainerObj.style.width != '300px') 
				$('#' + this.tagsContainerId)
					.css({'width': '300px'});
		} else {
			if(this.tagsContainerObj.style.width != '300px')
				$('#' + this.tagsContainerId)
					.css({'width': '300px'});
		}
		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]);
			this.items[i].onclick = function() {
			
				me.moveResult();				
			};
		}		
	} else {
		this.hideResults();
	}	
}
autoSuggest.prototype.moveResult		= function() {
	
	
	if( this.selectedItem != -1 ) {	
		try{	
			this.tagInputObj.value = this.results[this.selectedItem].word;

			if(this.type=='what' && typeof(this.results[this.selectedItem].property_arr[0])!='undefined' && this.results[this.selectedItem].property_arr[0]!=''){
				
				if(this.results[this.selectedItem].property_arr[0]==17763)
					this.results[this.selectedItem].property_arr[0]=3001056;
				
				$('#'+this.formId).attr('action','/profile/show');
				$('#'+this.formId).append('<input type="hidden" name="fid" value="'+this.results[this.selectedItem].property_arr[0]+'"/>');
			}

			if(this.redirectInputName){
				$('#'+this.formId).append('<input type="hidden" name="'+this.redirectInputName+'" value="'+this.results[this.selectedItem].property_arr[0]+'"/>');
				if(this.type=='where')
					$('#last_region_id').attr('value',this.results[this.selectedItem].property_arr[0]);
			}	
			if(this.type=='cpv')
				{
					getAddSubcategs_id(this.results[this.selectedItem].property_arr[1], lang);
					setAddCategoryId(this.results[this.selectedItem].property_arr[0],this.results[this.selectedItem].word);
				}
			if(this.type=='cpv_sup')
				{
					getSubscriptionSubcategs_id(this.results[this.selectedItem].property_arr[1], lang);
					setSubscriptionCategoryId(this.results[this.selectedItem].property_arr[0],this.results[this.selectedItem].word);
				}
			if(this.type=='cpv_search')
				{
					getBranchSubcategs_id(this.results[this.selectedItem].property_arr[1], lang);
					setBranchCategoryId(this.results[this.selectedItem].property_arr[0],this.results[this.selectedItem].word);
				}
				
				
		}catch(e){var a=e;}	
	}
}
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 );
	try{
		if( tmp >= 0 ) {
			this.items[tmp].className = '';
		}
	}catch(e){ var a=e;}
	
	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() };
	this.formAction = $('#'+me.formId).attr('action');
//	addEvent( window.document, 'click', function() { me.hideResults(); } );
}

autoSuggest.prototype.showPos = function() {
	alert(topPos + ' ' + leftPos + ' ' + heightPos);
}

function getPosition(obj){
    var topValue= 0,leftValue= 0;
    while(obj){
		leftValue+= obj.offsetLeft;
		topValue+= obj.offsetTop;
		obj= obj.offsetParent;
    }
    finalvalue = leftValue + ',' + topValue;
    return finalvalue;
}

function print_r( e ) {
    var str = '';
    for( var i in e ) {
        str += i + ': ' + e[i] + '\n';
    }
    alert( str );
};

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
