/*
Original by: Adam Crownoble (adam@bryan.edu)
Date: April 3 2006
License: LGPL (http://www.gnu.org/copyleft/lesser.html)
*/
var LiveSearch = Class.create();
LiveSearch.prototype = {
 initialize: function(searchBox,results,search_link,starterText) {
		this.form 					= searchBox.form;
		this.searchBox 			= searchBox;
		this.results 				= results;
		this.search_link 			= search_link;
		this.stopSubmit 			= true;
		this.searchString 		= this.searchBox.value;
		this.starterText 			= starterText
		this.minLength 			= 2;
		this.timeoutID 			= 0;
		this.delay 	  				= 5000;
		this.isIE		  			= false;

		if (navigator.userAgent.indexOf('Safari') > 0) {
		} 	else if (navigator.product == 'Gecko') {
		} 	else { this.isIE = true; }

  		Event.observe(this.form, 	  'submit'  , this.submit.bind(this));
//		Event.observe(this.searchBox, 'keyup'   , this.update.bind(this));

	  	Event.observe(this.searchBox, 'keyup'   , this.search.bind(this));
//  		Event.observe(this.searchBox, 'keypress'   , this.search.bind(this));

  		Event.observe(this.searchBox, 'focus'   , this.handleStarterText.bind(this));
//  		Event.observe(this.searchBox, 'blur'    , this.handleStarterText.bind(this));
//  	Event.observe(this.searchBox, 'keypress', this.Key_Press.bind(this));

  		this.searchBox.setAttribute("autocomplete","off");
  		this.form.reset();

  		if(this.starterText){ // Suchfeld mit Hilfetext füllen
  			if(! this.searchBox.value.len){
  				this.searchBox.value = this.starterText;
  			}
  		}
	},

 	handleStarterText: function(event) {
		switch(event.type) {
			case 'focus':
			if(this.searchBox.value == this.starterText) {
			 	Field.clear(this.searchBox);
			}
			break;

			case 'blur':
		 	if(this.searchBox.value == '') {
		  		this.searchBox.value = this.starterText;
		 	}
			break;
		}
 	},

 	update: function() {
//  	if(   this.searchString != this.searchBox.value
//  	   && this.searchBox.value != this.starterText) {
   	this.searchString = this.searchBox.value;
   	clearTimeout(this.timeoutID);
   	if(this.searchString.length >= this.minLength) {
    		this.results.innerHTML = this.searchingMessage;
    		this.timeoutID = setTimeout(this.search.bind(this), this.delay);
   	} else {
    		this.results.innerHTML = '';
   	}
//  	}
 	},

	search: function(objEvent) {
		var key = objEvent.which || objEvent.keyCode;
		if (key == 40 || key == 38 || key == 27)
		{
			this.Key_Press(objEvent);
		} else {
		  	this.searchString = this.searchBox.value;
	   	if(this.searchString.length > this.minLength) {
				new Ajax.Updater(
				 	this.results,
				 	this.search_link + escape(this.searchBox.value),
				   {
				   	asynchronous:true,
				   	method: 'get'
					}
				);
				this.display_on();
			}else{
				this.display_off();
			}
		}
 	},

 	display_off: function() {
 		var highlight = $('LSHighlight');
		if (highlight) { highlight.removeAttribute('id'); }
 		if(this.timeoutID){
 			clearTimeout(this.timeoutID);
 			this.timeoutID = 0;
 		}
 		this.results.hide();
 	},

 	display_on: function() {
 		if(this.timeoutID){
 			clearTimeout(this.timeoutID);
 			this.timeoutID = 0;
 		}
		this.results.show();
		this.timeoutID = setTimeout(this.display_off.bind(this), this.delay);
 	},


	Key_Press: function (objEvent) {
	var key = objEvent.which || objEvent.keyCode;
 		if (key == 40 ){ //KEY DOWN
			var highlight = $('LSHighlight');
			if (!highlight) {
				highlight = this.results.firstChild.firstChild;
			} else {
				highlight.removeAttribute('id');
				highlight = highlight.nextSibling;
			}
			if (highlight) {
					highlight.focus();
				highlight.setAttribute('id','LSHighlight');
			}
//			window.status = this.results.style.display +' - '+ highlight;
			if (!this.isIE) { objEvent.preventDefault(); }
			this.display_on();
		}
		//KEY UP
		else if (key == 38 ) {
			var highlight = $('LSHighlight');
			if (!highlight) {
				highlight = this.results.firstChild.lastChild;
			}
			else {
				highlight.removeAttribute('id');
				highlight = highlight.previousSibling;
			}
			if (highlight) {
				highlight.focus();
				highlight.setAttribute('id','LSHighlight');
			}
			if (!this.isIE) { objEvent.preventDefault(); }
			this.display_on();
		}
		//ESC
		else if (key == 27) {
			var highlight = $('LSHighlight');
			if (highlight) {
				highlight.removeAttribute('id');
			}
			this.display_off();
		}
	},
	submit: function(event) {
	   var highlight = $('LSHighlight');
		if (highlight && highlight.firstChild){
			if(this.stopSubmit) { Event.stop(event); }
			var url = highlight.firstChild.getAttribute("href");
		  	window.location = url;
			return false;
		}else{
			if(this.searchBox.value == this.starterText) {
			 	this.searchBox.value=' ';
			}
		  	return true;
		}
	}
};

var LiveSearch_B = Class.create( LiveSearch,
{
initialize: function(searchBox,results,search_link,starterText,pos_element,pos_offset_x,pos_offset_y) {
		this.form 					= searchBox.form;
		this.searchBox 			= searchBox;
		this.results 				= results;
		this.search_link 			= search_link;
		this.stopSubmit 			= true;
		this.searchString 		= this.searchBox.value;
		this.starterText 			= starterText

	   this.pos_element  	= pos_element ||'';
	   this.pos_offset_x 	= pos_offset_x;
	   this.pos_offset_y 	= pos_offset_y;

		this.minLength 			= 2;
		this.timeoutID 			= 0;
		this.delay 	  				= 5000;
		this.isIE		  			= false;

		if (navigator.userAgent.indexOf('Safari') > 0) {
		} 	else if (navigator.product == 'Gecko') {
		} 	else { this.isIE = true; }

  		Event.observe(this.form, 	  'submit'  , this.submit.bind(this));
//		Event.observe(this.searchBox, 'keyup'   , this.update.bind(this));

	  	Event.observe(this.searchBox, 'keyup'   , this.search.bind(this));
//  		Event.observe(this.searchBox, 'keypress'   , this.search.bind(this));

  		Event.observe(this.searchBox, 'focus'   , this.handleStarterText.bind(this));
//  		Event.observe(this.searchBox, 'blur'    , this.handleStarterText.bind(this));
//  	Event.observe(this.searchBox, 'keypress', this.Key_Press.bind(this));

  		this.searchBox.setAttribute("autocomplete","off");
  		this.form.reset();

  		if(this.starterText){ // Suchfeld mit Hilfetext füllen
  			if(! this.searchBox.value.len){
  				this.searchBox.value = this.starterText;
  			}
  		}
	},

  	setxy: function(){
  		if(this.pos_element){
	   	this.results.setStyle({
	      	left: (
	      				this.pos_element.positionedOffset().left + this.pos_offset_x
	      			)+'px',
	          top: (
	          			this.pos_element.positionedOffset().top  + this.pos_offset_y
	          		)+'px'
	      });
   	}
// 		console.warn ($('img_wkorb').offsetLeft +' - '+$('img_wkorb').offsetTop+' - '+$('img_wkorb').getWidth()+' - '+$('img_wkorb').getHeight());
  	},

 	display_on: function() {
 		if(this.timeoutID){
 			clearTimeout(this.timeoutID);
 			this.timeoutID = 0;
 		}
 		this.setxy();
		this.results.show();
		this.timeoutID = setTimeout(this.display_off.bind(this), this.delay);
 	}
});