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.74:8002/request=1';
autoSuggest.prototype.formObj			= false;
autoSuggest.prototype.formId			= 'formAsId';
autoSuggest.prototype.dict				= 'cities.pl';
autoSuggest.prototype.amount			= 100;
autoSuggest.prototype.sort				= false;
autoSuggest.activeTab					= false;
autoSuggest.resProc						= false;
autoSuggest.prepareReq					= false;
autoSuggest.prototype.filter			= false; 
autoSuggest.prototype.enableScr			= 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 ) {
		this.enableScr = false;
		if( this.itemsCount > 0 ) {
			return this.selectItem( keycode );
		}
	} else {
		this.enableScr = true;
	}
	return true;
}	
autoSuggest.prototype.checkTags			= function() {
	if( this.tagInputObj.value.length > 1 && this.lastkey != 13 ) {
		if( this.enableScr /* 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=';
			if( this.prepareReq ) {
				scrpath = scrpath + this.prepareReq( this.word );
			} else {
				scrpath = scrpath + this.word;
			}
			if( this.tagInputId == 'at' && this.filter.value != '' ) {
				scrpath = scrpath + '&filter=' + this.filter.value;
			} else if( this.tagInputId == 'inp2' ) {
				this.filter.value = "";
			}
			script.src = scrpath;
			this.head.appendChild(script);
			this.word = "";
//		} else {
		}
	} else if( this.itemsCount > 0 ) {		
		this.hideResults();
	}
	if( this.enableScr && this.tagInputId == 'inp2' ) {
		this.filter.value = "";
	}
}	
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++ ) {
//			data.results[i].word = data.results[i].word.substr( ( document.getElementById( 'inp2' ).value.length + 1 ) );
			if( this.resProc ) {
				data.results[i].word = this.resProc( data.results[i].word );
			}
			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';
		scrolDown();
		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;
		if( this.tagInputId == 'inp2' ) this.filter.value = this.results[this.selectedItem].property;
	}
}	
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() };
	this.filter = window.document.getElementById( 'filter_value' );
//	addEvent( window.document, 'click', function() { me.hideResults(); } );
}

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

function ctGetRes( results ) {
	ct.getResults( results );
}

function frGetRes( results ) {
	fr.getResults( results );
}

function ruGetRes( results ) {
	ru.getResults( results );
}

function esGetRes( results ) {
	es.getResults( results );
}

function deGetRes( results ) {
	de.getResults( results );
}

function nmGetRes( results ) {
	nm.getResults( results );
}

function prepReq( str ) {
	return document.getElementById( 'inp2' ).value + '|' + this.word;
}

window.onload = function() {
	ct = new autoSuggest();
	ct.dict = 'states.en';
	ct.calb = 'ctGetRes';
	ct.tagsContainerId = 'tags2';
	ct.tagInputId = 'inp2';
	ct.path = 'http://62.146.39.74:8002/request=1';
	ct.init();
	res = new autoSuggest();
	res.calb = 'getResults';
	res.tagsContainerId = 'tags';
	res.tagInputId = 'at';
	res.dict = 'cities.en';
	res.path = 'http://62.146.39.74:8002/request=1';
	//res.resProc = resProc;
	//res.prepareReq = prepReq;
	res.init();
	
	ru = new autoSuggest();
	ru.dict = 'cities.ru';
	ru.calb = 'ruGetRes';
	ru.tagsContainerId = 'tags_ru';
	ru.tagInputId = 'inp_ru';
	ru.path = 'http://62.146.39.74:8002/request=1';
	ru.init();
	
	fr = new autoSuggest();
	fr.dict = 'cities.fr';
	fr.calb = 'frGetRes';
	fr.tagsContainerId = 'tags_fr';
	fr.tagInputId = 'inp_fr';
	fr.path = 'http://62.146.39.74:8002/request=1';
	fr.init();
	
	es = new autoSuggest();
	es.dict = 'cities.es';
	es.calb = 'esGetRes';
	es.tagsContainerId = 'tags_es';
	es.tagInputId = 'inp_es';
	es.path = 'http://62.146.39.74:8002/request=1';
	es.init();
	
	de = new autoSuggest();
	de.dict = 'cities.de';
	de.calb = 'deGetRes';
	de.tagsContainerId = 'tags_de';
	de.tagInputId = 'inp_de';
	de.path = 'http://62.146.39.74:8002/request=1';
	de.init();
	
	nm = new autoSuggest();
	nm.dict = 'names.en';
	nm.calb = 'nmGetRes';
	nm.tagsContainerId = 'tags_nm';
	nm.tagInputId = 'inp_nm';
	nm.path = 'http://62.146.39.74:8002/request=1';
	nm.init();
	
	
	if( window.document.getElementById( 'sort_pop' ).checked ) {
		res.sort = 'rank';
	} else {
		res.sort  = false;
	}
	addEvent( window.document.getElementById( 'sort_pop' ), 'click', function() {
		if( window.document.getElementById( 'sort_pop' ).checked ) {
			res.sort = 'rank';
		} else {
			res.sort  = false;
		}
	});
	if( window.document.getElementById( 'sort_pop_es' ).checked ) {
		es.sort = 'rank';
	} else {
		es.sort  = false;
	}
	addEvent( window.document.getElementById( 'sort_pop_es' ), 'click', function() {
		if( window.document.getElementById( 'sort_pop_es' ).checked ) {
			es.sort = 'rank';
		} else {
			es.sort  = false;
		}
	});
	if( window.document.getElementById( 'sort_pop_nm' ).checked ) {
		nm.sort = 'rank';
	} else {
		nm.sort  = false;
	}
	addEvent( window.document.getElementById( 'sort_pop_nm' ), 'click', function() {
		if( window.document.getElementById( 'sort_pop_nm' ).checked ) {
			nm.sort = 'rank';
		} else {
			nm.sort  = false;
		}
	});
}


function resProc( str ) {
	return str.substr( ( document.getElementById( 'inp2' ).value.length + 1 ) );
}

function szkopy(results) {
	de.getResults( results );
}

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


function scrolDown() {
	if (document.body.scrollHeight) {
		window.scrollTo(0, document.body.scrollHeight);
	} else if (screen.height) {
		window.scrollTo(0, screen.height);
	}
}
