/********************************************************************/
/* START: AutoCompleteLayer                                         */

var AutoCompleteLayer = Class.create();
AutoCompleteLayer.prototype = Object.extend(new Layer, {
	initialize: function(input) {
		if(Info.browser.isIEpre6) return;
		this.input     = input;
		this.form      = this.input.up('form');
		this.content   = null;
		this.lastValue = this.input.value;
		this.node = document.createElement('div');
		this.node.className = 'autocomplete-wrapper';
		this.timeout = false;
		this.formSubmitted = false;
		
		input.insert({after: $(this.node)})
		input.setAttribute('autocomplete', 'off');
		this.node.observe('keydown', function(e) {
			this.onkeydown(e);
		}.bindAsEventListener(this));
		input.observe('keydown', function(e) {
			this.onkeydown(e);
		}.bindAsEventListener(this));
		input.observe('keyup', function(e) {
			this.onkeyup(e);
		}.bindAsEventListener(this));
		
		if (Info.browser.isIEpre7) {
			this.iframeLining = new IframeLining(this.node);
		}
		
		new Form.Element.Observer(
			this.input.id,
			0.25,
			function() {
				this.timeout = setTimeout(function() { if(this.input.value && !this.formSubmitted) this.load(); }.bind(this),500);
			}.bind(this)
		);
	},
	onkeyup: function(e) {		
		if (this.input.value != this.lastValue) { // input value changed
			this.lastValue = this.input.value;
			if (this.input.value) {
				//this.load();
			} else {
				this.close();
			}
		}
	},
	onkeydown: function(e) {
		if(Info.browser.isOpera) return; // no key handling, since preventing default actions seems to be impossible in Opera
		if (this.timeout) clearTimeout(this.timeout); //clear Timeout
		
		if(this.isOpen) {
			var activeListElement = this.node.select('ul li.active');
			activeListElement = activeListElement.length ? activeListElement[0]: null;
			var newListElement = null;

			switch (e.keyCode) {
				case Event.KEY_UP:
					if (activeListElement && activeListElement.previous()) {
						newListElement = activeListElement.previous();
					}
					Event.stop(e);
					break;
				
				case Event.KEY_DOWN:
					if (activeListElement && activeListElement.next()) {
						newListElement = activeListElement.next();
					} else if (!activeListElement) {
						newListElement = this.node.select('ul li').first();
					}
					Event.stop(e);
					break;
					
				case Event.KEY_HOME:
					newListElement = this.node.select('ul li').first();
					Event.stop(e);
					break;

				case Event.KEY_END:
					newListElement = this.node.select('ul li').last();
					Event.stop(e);
					break;
					
				case Event.KEY_ESC:
					this.input.focus();
					break;
			}
			
			if (newListElement) {
				$(newListElement).down('a').focus();
				this.setInput(newListElement.down('a').innerHTML);
			}
		} else {
			if (e.keyCode == Event.KEY_DOWN) {
				if (this.input.value) {
					//this.load();
				}
			}
		}
	},
	getSuggestions: function() {
		this.load();
	},
	display: function() {
		var that = this;
		if(!this.content) {
			this.close();
		} else {
			this.node.update(this.content);
			var ul = this.node.down('ul');
			if (ul) {
				var links = $A(ul.getElementsByTagName('a'));
				// add events to links to submit form with selected value
				links.each(function(a) {
					a = $(a);

					a.observe('focus', function(e) {
						$(this.parentNode).addClassName('active');
					}.bindAsEventListener(a));

					a.observe('blur', function(e) {
						$(this.parentNode).removeClassName('active');
					}.bindAsEventListener(a));

					a.observe('mouseover', function(e) {
						a.focus();
					}.bindAsEventListener(a));

					a.observe('mousemove', function(e) {
						a.focus();
					}.bindAsEventListener(a));

					a.observe('click', function(e) {
						that.close();
						that.setInput(a.innerHTML);
						that.formSubmitted = true;
						that.form.submit();
					}.bindAsEventListener(a));
				});
			}
			if (this.iframeLining) {
				this.iframeLining.refresh();
			}

			this.open();
		}
	},
	setInput: function(value) {
		this.input.value = value;
		this.lastValue = value;
	},
	show: function() {
		this.node.setStyle({'display':'block'});
	},
	hide: function() {
		this.node.setStyle({'display':'none'});
	},
	load: function() {
		alert('Implementation Error: AutoComplete.load is missing');
	}
});

AutoCompleteLayer.prototype.load = function() {
	value	= encodeURIComponent(this.input.value);

	var url = window.location.href;
	var suggestPath = 'apps/search/suggest.php?query='+value+'&cs=CORP_SIEMENS'; 
	// dev
    if( url.match(/edit.siemens.dev.publicis.de/))
		url_ajax	= window.location.protocol + '//edit.siemens.dev.publicis.de/cc/corp/root/' + suggestPath;
    // stage
	if( url.match(/stage.siemens.com/))
		url_ajax	= window.location.protocol + '//stage.siemens.com/cc/corp/nwa/' + suggestPath;
    // live
    if( url.match(/www.siemens.com/))
		url_ajax	= window.location.protocol + '//www.siemens.com/corp/' + suggestPath;

	new Ajax.Request(url_ajax,{asynchronous: false,
	onSuccess: function(xhr)
		{
			if(xhr.responseText.length > 0)
			{
				this.content = '<ul>';
				var suggestion = xhr.responseText.split("||");
				suggestion.each(function(item)
				{
					this.content += '<li><a href=\"javascript:void(0);\">'+item+'</a></li>';
				}.bind(this));
				this.content += '</ul>';
				var that = this;
				window.setTimeout(function() { that.display(); }, 400);
			}
		}.bind(this)
	});
}

/********************************************************************/
/* START: Search and filter */

function init_search() {
	if(Info.browser.isIEpre6) return;
	new AutoCompleteLayer($('site-search-input'));
	// correct minor IE6 layout issues
	if (Info.browser.isIEpre7) {
		var suggestion = $$('div.search-suggestion').first();
		if (suggestion && suggestion.next().hasClassName('recommendations')) {
			suggestion.next().style.marginTop = '-3px';
		}
	}
}

var ActiveSearchFilter = Class.create();
ActiveSearchFilter.prototype = {
	initialize: function(name) {
		this.name = name;
		this.content = null;
	},
	display: function() {
		$('active-filter-' + this.name).insert({after: this.content}).hide();
		
		var node = $('filter-' + this.name);
		if (node) {
			GuiSelect.build($A([node]), GuiSelect.searchTransformer, true);
		}
	},
	get: function() {
		if (this.content) {
			this.display();
		} else {
			this.load(); // will set content and display the filter
		}
	},
	load: function() {
		// overwrite this to implement a custom method
		alert("Implementation Error: ActiveSearchFilter.load is missing");
	}
}

/* END: Search and filter */
/********************************************************************/

document.observe('dom:loaded', function() {
	init_search();
});
