﻿var cityAC, searchAC;

SPDAutocompleter = Class.create(Ajax.Autocompleter, {
	fixIEOverlapping: function() {
		Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)});
//		this.iefix.style.zIndex = 1;
//		this.update.style.zIndex = 2;
		Element.show(this.iefix);
	},
	selectEntry: function() {
		this.active = false;
		if(this.index!=0)
			this.updateElement(this.getCurrentEntry());
	},
	markPrevious: function() {
		if(this.index > 0)
			if(this.index != 1) {
				if(this.getEntry(this.index-1).className == "title")
					this.index-=2;                
				else            
					this.index--;
			}
			else
				this.index = this.entryCount-1;
		else this.index = this.entryCount-1;
		this.getEntry(this.index).scrollIntoView(false);
	},
	
	markNext: function() {
		if(this.index < this.entryCount-1){
			if(this.getEntry(this.index+1).className == "title")
				this.index+=2;
			else            
				this.index++;
		}else {
			this.index = 1;
		}        
		this.getEntry(this.index).scrollIntoView(false);
	},
	addObservers: function(element) {
		if(element.id != "title"){
			Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));
			Event.observe(element, "click", this.onClick.bindAsEventListener(this));
		}        
	},
	//niko - with titles
	markPrevious: function() {
		while(this.titles[--this.index]) {}

		if(this.index < 0)	{
				this.index = this.entryCount-1;
			
			while(this.titles[this.index])
				this.index--;
		}

		this.getEntry(this.index).scrollIntoView(false);
	},
	//niko - with titles
	markNext: function() {
		while(this.titles[++this.index]) {}

		if(this.index > this.entryCount-1)	{
				this.index = 0;
			while(this.titles[this.index])
				this.index++;
		}
		   
		this.getEntry(this.index).scrollIntoView(false);
	},
	//niko - with titles
	getEntry: function(index) {
		return this.items[index];
	},
	//niko - with titles
	updateChoices: function(choices) {
		if(!this.changed && this.hasFocus) {
			this.update.innerHTML = choices;
			Element.cleanWhitespace(this.update);
			Element.cleanWhitespace(this.update.down());

			//niko
			this.titles = [];
			//IE caching - the only IE way to make things faster
			this.items = $A(this.update.firstChild.childNodes);
			var titles = this.update.select(this.options.titles);
			var firstIndex = true;
			//!niko

			if(this.update.firstChild && this.update.down().childNodes) {
				this.entryCount = this.update.down().childNodes.length;
				
				for (var i = 0; i < this.entryCount; i++) {
					var entry = this.getEntry(i);

					var isTitle = titles.include(entry);
					if(!isTitle)	{
						entry.autocompleteIndex = i;
						this.addObservers(entry);

						if(firstIndex)	{
							this.index = i;
							firstIndex = false;
						}
					}

					this.titles.push(isTitle);
					}
				}
				else {
					this.entryCount = 0;
				}

				this.stopIndicator();

				//original
				//this.index = 0;
				//!original

				if(this.entryCount==1 && this.options.autoSelect) {
				this.selectEntry();
				this.hide();
			}
			else {
				this.render();
			}
		}
	},
	//niko - with titles
	addObservers: function(element) {
		Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));
		Event.observe(element, "click", this.onClick.bindAsEventListener(this));
	},
	//vadim
	switchAC: function(on) {
		if(on)
		{
			Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this));
			Event.observe(this.element, 'keydown', this.onKeyPress.bindAsEventListener(this));
		}
		else
			this.element.stopObserving('blur').stopObserving('keydown');
	}
});

function SPDonShow(element, update)
{
	if(SearchForm.SearchListControl) 
			SearchForm.SearchListControl.close();
	
	if(!update.style.position || update.style.position=='absolute') {
	  update.style.position = 'absolute';
	  Position.clone(element, update, {
		setHeight: false,
		offsetTop: element.offsetHeight
	  });
	}
	Effect.Appear(update,{duration:0.15});
}
      
function enableAC(on)
{
	if(on)
	{
		setCookieValue('ac', 1);		
		
		SearchForm.citykeyAC = "";
		SearchForm.searchTypeAC = SearchForm.searchTypes.none;
			
		if(!cityAC && !searchAC)
		{
			cityAC=new SPDAutocompleter('city','ac4update_city',"Autocompleter.ashx?sid="+sid, { fullSearch: true, partialSearch: true, callback: updateCityParams, afterUpdateElement: setCityKey, titles: ".title", onShow: SPDonShow});
			searchAC=new SPDAutocompleter('search','ac4update_address',"Autocompleter.ashx?sid="+sid, { fullSearch: true, partialSearch: true, callback: updateAddressParams, afterUpdateElement: setCityKey, titles: ".title", onShow: SPDonShow});
		}
		else
		{
			cityAC.switchAC(true);
			searchAC.switchAC(true);		
		}	
		
		SearchForm.acOn = true;	
	}
	else
	{
		setCookieValue('ac', 0);
		SearchForm.acOn = false;
		SearchForm.citykeyAC = "";
		SearchForm.searchTypeAC = SearchForm.searchTypes.none;
		
		if(cityAC && searchAC)
		{				
			cityAC.switchAC(false);
			searchAC.switchAC(false);
		}
	}
}

function updateCityParams(inputField, qString)
{
	SearchForm.citykeyAC = "";
	
    /* This should become ... */
    //$("searchtype_poi").checked = false;
    //$("searchtype_adr").checked = false;
    /* ... this */
    SearchForm.searchTypeAC = SearchForm.searchTypes.none;
    
    qString = qString.replace(/%3C/g,"").replace(/%3E/g,"");
    return qString + "&country=" + $("CountriesDDL").value + "&searchtype=city";
}

function updateAddressParams(inputField, qString)
{
    var city_ac = null;

    /* This should become ... */
    //$("searchtype_poi").checked = false;
    //$("searchtype_adr").checked = false;
    /* ... this */
    SearchForm.searchTypeAC = SearchForm.searchTypes.none;
    
    if($("city").value == "")
        SearchForm.citykeyAC = "";
        
    qString = qString.replace(/%3C/g,"").replace(/%3E/g,"");
    city_ac = $("city").value.replace(/>/g,"").replace(/</g,"");
        
    return qString + "&city=" + city_ac + "&country=" + $("CountriesDDL").value +  "&citykey=" + SearchForm.citykeyAC + "&searchtype=address";
}

function setCityKey(text, li)
{
    if(li.attributes['city'])
        $('city').value=li.attributes['city'].value;
    
    SearchForm.citykeyAC = li.id;
 
    if(li.attributes['actype']) {
        if(li.attributes['actype'].value == 'poi') {
     	    /* This should become ... */
     	    //$("searchtype_poi").checked = true;
	        //$("searchtype_adr").checked = false;
	        /* ... this */
			SearchForm.searchTypeAC = SearchForm.searchTypes.poi;
        }
        else {
 	        /* This should become ... */
 	        //$("searchtype_poi").checked = false;
	        //$("searchtype_adr").checked = true;
	        /* ... this */
			SearchForm.searchTypeAC = SearchForm.searchTypes.adr;
        }
    }
    
    return;
}

function clearField(obj)
{
    if(obj.value == '(z.B. PLZ und/oder Ort, Ortsteil)' && obj.id == 'city')
    {    
		obj.value = "";
        SearchForm.citykeyAC = "";
    }
    else
    {
		if(obj.value == '(z.B. Straße und Hausnummer, POIs,...)' && obj.id == 'search')
		{
			obj.value = "";
			SearchForm.searchTypeAC = SearchForm.searchTypes.none;
		}
    }     
	return;
}

function resetSearchFields(condition){
    var search = $("search");
    var city = $("city");
    
    if(condition) {
        search.value = "";
        city.value = "";
    }

    $('city').focus();
    return false;
}

function initAC()
{
	if(document.cookie.indexOf("ac=1")>=0)
	{
		enableAC(true);
		$("acOn").checked=true;
	}
    else
    {
		enableAC(false);
		$("acOff").checked=true;
	}
}

