function initialize(){

         new suche();

}


var suche = Class.create();

suche.prototype = {


         initialize : function(){

              var optionIndex = 0;
              this.options = Object.extend({
                 id       :         "suche",
                 className:         "suche",
                 version:           "default",
                 value:             "suchen",
                 bgcolor    :       "rgb(241, 242, 239)",
                 bgcolor_hover:     "rgb(115, 139, 144)",
                 textcolor:         "rgb(0, 0,0)",
                 textcolor_hover:    "rgb(255, 255,255)"

               }, arguments[optionIndex] || {});

               this._createSuche(this.options.id);

               this.aktiv = "false";

               this.objekt = this;
               this.input  = $(this.options.className + "_inp");
               this.abbrechen = $(this.options.className + "_abbrechen");

               Event.observe(this.input, "blur",  this._initBlur.bind(this));
               Event.observe(this.input, "click", this._initClick.bind(this));
               Event.observe(this.input, "keyup", this._initKeyup.bind(this));
               Event.observe(this.abbrechen, "click", this._initBlur.bind(this));

         },


         _initClick : function(event) {

                 $(Event.element(event)).clear();

         },

         _initBlur : function(event) {

                 bgcolor = this.options.bgcolor;

                 if ( this.aktiv == "false" ) {

                 $(Event.element(event)).value = this.options.value;
                 $(this.options.className + "_results").style.visibility = 'hidden';
                 $(this.options.className + "_laden").style.visibility = 'hidden';
                 $(this.options.className + "_abbrechen").style.visibility = 'visible';


                 } else{

                    e = $(this.aktiv).down(1);
                    self.location.href=e;

                 }



         },

         _initKeyup : function(event) {

                var x = Form.Element.serialize($(this.input)) ;

                var url = 'suche.php';
                var className = this.options.className;
                var laden     = this.options.className + "_laden";
                var abbrechen = this.options.className + "_abbrechen";
                var objekt    = this.objekt;

                new Ajax.Request(url,  {

                                        method: 'post'

                                       ,parameters: x

                                       ,onCreate: function () {

                                                         $(abbrechen).style.visibility = "hidden";
                                                         $(laden).style.visibility = "visible";
                                                  }

                                       ,onSuccess: function(transport) {

                                                         $(className + "_results").innerHTML="";


                                                          var win            = document.createElement("div");
                                                              win.setAttribute('id', 'suche_utitel');

                                                              win.innerHTML ="<div>Suchergebnisse</div>";

                                                              Element.insert(className +"_results",win);

                                                         var xml_document = transport.responseXML;
                                                         var eintraege =  xml_document.getElementsByTagName( 'eintrag');
                                                         for( var a = 0; a < eintraege.length;  a++ ) {


                                                            titel  = eintraege[a].getElementsByTagName("titel")[0].firstChild.nodeValue;
                                                            text   = eintraege[a].getElementsByTagName("text")[0].firstChild.nodeValue;
                                                            link   = eintraege[a].getElementsByTagName("link")[0].firstChild.nodeValue;


                                                            var win            = document.createElement("div");
                                                                win.className  = className + "_result";
                                                                win.setAttribute('id', className+ "_"+a);

                                                                win.innerHTML ="\
                                                                               <div id=\"titel\">\
                                                                                 <a href='"+link+"'>"+titel+"</a>\
                                                                               </div>\
                                                                               <div id=\"text\" class=\"suche_text\">"+text+"</div>\
                                                                               ";

                                                             Element.insert(className +"_results",win);



                                                             id_a = $(className + "_" + a);
                                                             Event.observe(id_a, "mouseover",  objekt._initResultMouseOver.bind(objekt));
                                                             Event.observe(id_a, "mouseout",   objekt._initResultMouseOut.bind(objekt));


                                                         }

                                                          var win            = document.createElement("div");
                                                              win.setAttribute('id', 'suche_utitel_unten');

                                                              win.innerHTML ="<div></div>";

                                                              Element.insert(className +"_results",win);


                                                         $(className + "_results").style.visibility = "visible";
                                                         $(abbrechen).style.visibility              = "visible";
                                                         $(laden).style.visibility                  = "hidden";




                                                   }


                                       ,onFailure:  function(transport){

                                                    } // End onFailure


                                       }
                    );


         },


         _initResultMouseOver : function(event) {

            RegEx = /^suche_+([0-9])/;
               e  = Event.element(event);
               e  = e.up(0);
               id = e.identify();

            if ( !id.match(RegEx)){

                    e = e.up(0);
               id = e.identify();
            }

            this.aktiv = id;

            e.setStyle({  backgroundColor: this.options.bgcolor_hover  });

            e.setStyle({  color: this.options.textcolor_hover  });

            e = e.down(1);
            e.setStyle({  color: this.options.textcolor_hover  });



         },

         _initResultMouseOut : function(event) {

              RegEx = /^suche_+([0-9])/;

                 e  = Event.element(event);
                 e  = e.up(0);
            id = e.identify();

            if ( !id.match(RegEx)) {

                   e = e.up(0);
              id = e.identify();

            }

            this.aktiv = "false";
            e.setStyle({ backgroundColor: this.options.bgcolor  });
            e.setStyle({  color: this.options.textcolor  });

            e = e.down(1);
            e.setStyle({  color: this.options.textcolor  });


         },



         _createSuche : function(id) {

                          var className      = this.options.className;
                          var value          = this.options.value;
                          var win            = document.createElement("div");

                              win.setAttribute('id', id+ "_layer");
                              win.className = className;

                              win.innerHTML = "\
                                   <div id='"+ className +"_anfang'></div>\
                                   <div id='"+ className +"_suche'>\
                                    <input id='"+ className +"_inp' type=\"search\" value='"+value+"' autocomplete=\"off\" name=\"suche\" accesskey=\"s\" />\
                                   </div>\
                                   <div id='"+ className +"_ende'>\
                                     <div id='"+ className +"_laden' style=\"visibility:hidden\"></div>\
                                     <div id='"+ className +"_abbrechen' style=\"visibility:visible\"></div>\
                                   </div>\
                                   <div id='"+ className +"_results' style=\"visibility:hidden\">\
                                   </div>\
                                   ";
                              Element.insert(id,win);

         }



}
