/********************************/
/***** SUGGEST FIELD OBJECT *****/
/********************************/
// One object per suggest field will be created automatically by the suggest observer.
// Don't create instances manually!
var ajaxSuggestField = Class.create({
    id:null,                                            // (auto-set) id of the input field the suggest object is attached to
    containerId:null,                                   // (auto-set) id of the container element
    listId:null,                                        // (auto-set) id of the list element
    iframeId:null,                                      // (auto-set) id of the iframe (MSIE6 only)
    spanId:null,                                        // (auto-set) id of the span to calculate the size
    formName:null,                                      // (auto-set) form name
    formId:null,                                        // (auto-set) id of the form
    type:null,                                          // (auto-set) branchenbuch|telefonbuch
    autosearch:null,                                    // (auto-set) whether to start a search automatically
    isOpen:false,                                       // (auto-set) internal, whether the suggest list is visible
    text_color 		:'#000000',			// Textfarbe in der Eintragsliste
    bg_color		:'#FFFFFF',			// Hintergrundfarbe in der Eintragsliste
    border_style	:'1px dashed #FFFFFF',		// keine Umrandung in der Eintragsliste
    textrollover_color 	:'#000000',			// Hover: Textfarbe in der Eintragsliste
    bgrollover_color   	:'#FBDFC5',			// Hover: Hintergrundfarbe in der Eintragsliste
    borderrollover	:'1px dotted #000000',		// Hover: Umrandung
    width:0,                                            // (auto-set) internal
    height:0,                                           // (auto-set) internal
    currentValue:'',                                    // (auto-set) internal
    delayTimer:null,                                    // (auto-set) internal, timer for delaying search
    urlPath:'content/global/ajax/',                     // path prefix to the AJAX script
    url:'',                                             // (auto-set) internal, complete URL to the AJAX script
    position:0,                                         // (auto-set) internal, arrow key navigation
    position_alt:0,                                     // (auto-set) internal, arrow key navigation
    fieldWidth:0,                                       // (auto-set) width of the input field
    suggestType:null,                                   // (auto-set) city | what
    cityBrackets:false,                                 // (auto-set) Whether to use the AlOrt number and brackets (only BADE)
    isMSIE6:false,

    initialize:function(id, formName, formId, type, autosearch)
    {
        this.id=id;
        this.formName = formName;
        this.formId = formId;
        this.type = type;
        this.autosearch = autosearch;

        if($(id).hasClassName('suggestWhat')) {
            this.url = this.urlPath+'whatSuggest.php';
            this.suggestType = 'what';
        } else {
            this.url = this.urlPath+'citySuggest.php';
            this.suggestType = 'city';
            if(type == 'branchenbuch') {
                this.cityBrackets = true;
            }
        }

        // Create the required elements
        this.containerId  = 'suggest_container_'+this.id;
        this.listId       = 'suggest_list_'+this.id;
        this.spanId       = 'suggest_span_'+this.id;
        
        ajaxContainer     = new Element('div', { id:this.containerId });
        ajaxContainerList = new Element('ul',  { id:this.listId });
        
        $(document.body).appendChild(ajaxContainer);
        ajaxContainer.setStyle({'display':'none'}); 
        ajaxContainer.appendChild(ajaxContainerList);

        if (navigator.appName.indexOf("Explorer") != -1) {
                $(ajaxContainerList.id).setStyle({
                        zIndex:9999999,
                        display:'block'
                });
        } else {// Funktioniert im IE nicht, daher die Weiche
                $(ajaxContainerList.id).setStyle({
                        zIndex:9999999,
                        display:'block',
                        position:'absolute'
                });
        }

        // Detect IE < 7
        if(navigator.userAgent.indexOf("Opera")==-1){
                if (navigator.appVersion.indexOf("MSIE")!=-1){
                        var temp=navigator.appVersion.split("MSIE");
                        var version=parseFloat(temp[1]);
                        this.isMSIE = true;
                        this.ieMSIE6=(version < 7);
                }
        }
        
        dimensions = $(this.id).getDimensions();
        this.width = dimensions.width;
        this.height = dimensions.height;
        dimX = dimensions.width;
        dimY = dimensions.height;
        this.fieldWidth = dimensions.width;

        position = Position.cumulativeOffset($(this.id));
        posX = position[0];
        posY = position[1]+dimY-1;

        //Iframe nur im IE6 anzeigen
        if(this.ieMSIE6) {
            this.iframeId=this.containerId+"_iframe";
            ajax_iframe = new Element('iframe', { id:this.iframeId });
            $(this.containerId).appendChild(ajax_iframe);
            
            $(ajax_iframe.id).setStyle({
                    'backgroundColor':'#ffffff',
                    'position':'absolute',
                    'display':'block',
                    'zIndex':'10',
                    margin:0,
                    padding:0,
                    border:0
            });
        }
        
        ajaxContainerList.addClassName('suggestList');
        ajaxContainer.addClassName('suggestContainer');
        
        $(this.containerId).setStyle({
                left: posX+'px',
                top: posY+'px'
        });
        
        span = new Element('span', { id:this.spanId });
        $(document.body).appendChild(span);
        
        $(this.spanId).setStyle({
                position:'absolute',
                left:'-1000px'
        });
        $(this.spanId).addClassName('suggestList');  
        $(this.id).suggest = this;
    },

    open:function()
    {
        $(this.containerId).setStyle({
                width:this.width+'px',
                height:this.height+'px'
        });

        $(this.containerId).show();
        this.isOpen = true;
        this.currentValue='';
    },

    close:function()
    {
        this.currentValue = '';
        this.isOpen=false;
        $(this.containerId).hide();
        this.isOpen = false;
    },
    
    
    getValue:function()
    {
        return $F(this.id);
    },
    
    
    update:function(input) {
        //console.log('update('+input+')');
        if(this.delayTimer != null) {
                window.clearTimeout(this.delayTimer);
        }
        this.position = -1;
        var thisObj = this;
        new Ajax.Request(
            this.url,
            {
                method:'get',
                parameters: 'input='+(input)+'&field='+thisObj.id,
                onComplete:function(req){
                    //	window.onerror = alert;
                        if (req.responseText) {
                                // Content given, so paste into SPAN						
                                if($(thisObj.spanId) != null) {
                                        Element.update($(thisObj.spanId), req.responseText);
                                        // Get the SPAN dimensions and set the size of the contentDIV accordingly
                                        var ddsDims = $(thisObj.spanId).getDimensions();
                                        var ddsWidth = ddsDims.width;
                                        var ddsHeight = ddsDims.height;

                                        if(ddsWidth < this.fieldWidth) ddsWidth = this.fieldWidth;

                                        thisObj.width = ddsWidth;
                                        thisObj.height = ddsHeight;												
                                }
                                                                    
                                Element.update($(thisObj.listId), req.responseText);
                                Element.update($(thisObj.spanId), "");

                                $(thisObj.containerId).setStyle({
                                        //width:thisObj.width+'px',
                                        width:thisObj.width+'px',
                                        height:thisObj.height+'px'
                                });
                                
                                if(thisObj.iframeId) {
                                        Position.clone($(thisObj.containerId), $(thisObj.iframeId));						
                                        $(thisObj.iframeId).setStyle({
                                                //width:thisObj.width+'px',
                                                width:thisObj.width+'px',
                                                height:thisObj.height+'px'
                                        });
                                        //$(thisObj.ajax_iframe).show();
                                }

                                thisObj.open();
                        } else {
                                thisObj.close();
                        }				    
                },
                onError:function(e) { alert('AJAX-FEHLER');},
                evalScripts: true
            }
        );	
    },


    mouseOver:function(pos)
    {
        //console.log('SuggestField mouseOver ('+pos+')');
        this.position = pos;
        if(this.isCitySuggestEntry()) {
            var el = $('suggest_city_entry_'+pos+'_'+this.id);
        } else {
            var el = $('suggest_what_entry_'+pos+'_'+this.id);
        }
        this.highlightEntry(el);
    },


    mouseLeave:function(pos)
    {
        //console.log('SuggestField mouseLeave');
        this.position_alt=pos;
    },

    isCitySuggestEntry:function(el)
    {
        return (this.suggestType == 'city');
    },

    handleKeyEvent:function(el) {
        if(!this.isActive()) return;
        //console.log('SuggestField handleKeyEvent ['+this.id+']');
        var value = el.getValue();
        if(value != '') {
            //console.info('SEARCH '+value);
            if(this.delayTimer != null) {
                window.clearTimeout(this.delayTimer);
            }
            this.delayTimer = window.setTimeout(function(){ this.update(value); }.bindAsEventListener(this), 200);
        }
    },
    
    
    getFields:function(el, pos, pos_alt)
    {
        //console.log('SuggestField getFields');
        //console.log(el);
        if(pos == null) pos = this.position;
        if(pos_alt == null) pos_alt = this.position_alt;

        var field = null;
        if(el != null) {
            if(this.isCitySuggestEntry(el)) {
                var field_alt = $('suggest_city_entry_'+pos_alt+'_'+this.id);
                var field     = $('suggest_city_entry_'+pos+'_'+this.id);
            }
        }

        if(field == null) {
            var field_alt = $('suggest_what_entry_'+pos_alt+'_'+this.id);
            var field     = $('suggest_what_entry_'+pos+'_'+this.id);
        }
        
        return [field, field_alt];
    },
    
    
    
    highlightEntry:function(el)
    {
        //console.log("SuggestField highlightEntry");
        //console.log(el);
        var fieldInfo = this.getFields(el);
        var field = fieldInfo[0];
        var field_alt = fieldInfo[1];
       
        //console.log(fieldInfo);

        if (field_alt) {
            field_alt.setStyle({
                color:this.text_color,
                backgroundColor:this.bg_color,
                border:this.border_style
            });	
            $(this.id).value = field.childNodes[0].data;				
        } else {
            el.up('ul').down('li').setStyle({
                color:this.text_color,
                backgroundColor:this.bg_color,
                border:this.border_style
            });	
        }

        if(field) {
            field.setStyle({
                color:this.textrollover_color,
                backgroundColor:this.bgrollover_color,
                border:this.borderrollover
            });
        }
    },
    
    
    handleKeyNavigation:function(key, el) 
    {
        //console.log('SuggestField handleKeyNavigation ['+this.id+'] '+key);
        
        var fieldInfo = this.getFields(el);
        var field = fieldInfo[0];
        var field_alt = fieldInfo[1];
        
        var maxPos = 0;
        if(this.isCitySuggestEntry(el)) {
            maxPos = $F('suggest_city_entry_count_'+this.id)-1; // letzter Listeneintrag
        } else {
            maxPos = $F('suggest_what_entry_count_'+this.id)-1; // letzter Listeneintrag
        }

        if(key == 'arrowup' || key == 'arrowleft') {
            //console.log('SuggestField handleKeyNavigation GO UP');
            //console.info(this.position);
            if (this.position == -1 || this.position == 0) {
                this.position = maxPos; // letzter Listeneintrag
            } else {
                this.position--;
            }
        } else if(key == 'arrowdown' || key == 'arrowright') {
            //console.log('SuggestField handleKeyNavigation GO DOWN');
            if (this.position >= -1 ) {
                this.position++;
                if(this.position > maxPos) {
                    this.position = 0;
                }
            }
        }
        
        if (!field) {
            this.position = 0;
        }

        if (this.position_alt > -1) {
            this.highlightEntry(el);
        }
        
        if(field) {
            this.highlightEntry(el);
            this.position_alt = this.position;
        }
    },


    submitForm:function(e)
    {
        //console.log('SuggestField submitForm');
        if(e != null) Event.stop(e);
        //$(this.formId).submit();
        ajaxSuggestObserver.submitCompleteForm(this.formId);
    },

    
    handleCityClick:function(el) //pos, value, alort)
    {
        var pos   = el.getAttribute('sgPos');
        var value = el.getAttribute('sgVal');
        var alort = el.getAttribute('sgCityId');
        
        //console.log('SuggestField handleCityClick pos='+pos+', value='+value+', alort='+alort);
        if(this.cityBrackets) {
            value='['+value+']';
        }
        $('suggest').value = 1;
        this.hasChanged=false;
        $(this.id).value=value;
        $('searchbox_lid').value = alort;
        this.close();
        $(this.id).focus();
        if(this.autosearch) {
            //console.log('SuggestField autosearch');
            //this.submitForm(e);
            ajaxSuggestObserver.submitCompleteForm(this.formId);
        }
    },

    insertClose:function(e)
    {
        Event.stop(e);
        //console.log('SuggestField insertClose');
        if(this.suggestType == 'city') { // && this.cityBrackets) {
            var el = $('suggest_city_entry_'+this.position+'_'+this.id);
            if(el != null) {
                // alert('CALLING HANDLECITYCLICK !!!');
                this.handleCityClick(el);
                return;
            }
            
        } else {
            this.id.value = this.currentValue;
        }

        this.close();
        if(this.autosearch) {
            //console.log('SuggestField autosearch');
            //this.submitForm(e);
            ajaxSuggestObserver.submitCompleteForm(this.formId);
        } else {
            
        }
    },


    isActive:function()
    {
        if (this.getCookie('klickSuggest') == 1) {
            return true;
        } else {
            return false;
        }
    },

    getCookie:function(name){
        var i=0;
        var suche = name+"=";
        while (i<document.cookie.length){
            if (document.cookie.substring(i, i+suche.length)==suche){
                var ende = document.cookie.indexOf(";", i+suche.length);
                ende = (ende>-1) ? ende : document.cookie.length;
                var cook = document.cookie.substring(i+suche.length, ende);
                return unescape(cook);
            }
            i++;
        }
        return null;
    }
});



/****************************/
/***** SUGGEST OBSERVER *****/
/****************************/
// One global instance will be created for all suggests only
// Automatically creates instances of suggest field objects (see below)
var ajaxSuggestObserverClass = Class.create (
{
    suggestFields:[],
    forms:[],
    openSuggest:null,
    suggestObjects:[],
    isMSIE6:false,
    delayTimer:null,
    lastInsertCloseEvent:null,
    focusedElement:null,
    isMSIE:false,
    
    initialize:function() {
        //console.log('ajaxSuggestObserver starting...');

        // Detect IE < 7
        if(navigator.userAgent.indexOf("Opera")==-1){
                if (navigator.appVersion.indexOf("MSIE")!=-1){
                        var temp=navigator.appVersion.split("MSIE");
                        var version=parseFloat(temp[1]);
                        this.isMSIE = true;
                        this.ieMSIE6=(version < 7);
                }
        }

        // Find all fields where suggests should be attached to
        // <input id="..." class="suggestCity branchenbuch autosearch" ...>
        // autosearch only when no street field in the form!
        // <input id="..." class="suggestCity telefonbuch" ...>
        var fields=new Array();

        var fieldsCity = $$('form input.suggestCity');
        var fieldsWhat = $$('form input.suggestWhat');

        if(fieldsCity.length > 0) {
            fieldsCity.each(function(fld) {
                fields.push(fld);
            });
        }

        if(fieldsWhat.length > 0) {
            fieldsWhat.each(function(fld) {
                fields.push(fld);
            });
        }

        if(fields.length > 0) {
            fields.each(function(fld) {
                if(typeof fld.id == 'undefined' || fld.id != null || fld.id != '') {
                    // Generate a random id if none defined
                    fld.id = 'suggest_form_field_'+parseInt(Math.random()*10000);
                }
                
                var type = 'branchenbuch';
                if(fld.hasClassName('telefonbuch')) {
                    type='telefonbuch';
                }
                
                var frm = fld.up('form');
                var autosearch = false;
                if(fld.hasClassName('autosearch')) autosearch = true;

                if(typeof frm.id == 'undefined') {
                    // Generate a random id if none defined
                    frm.id = 'suggest_form_'+parseInt(Math.random()*10000);
                }
               
                var obj  = new ajaxSuggestField(fld.id, frm.getAttribute('name'), frm.id, type, autosearch);
                fld.suggest = obj;
                this.suggestObjects.push(obj);
                this.forms.push(frm.id);
                
            }.bind(this));
            this.forms = this.forms.uniq();
        }
        //console.log(this.suggestObjects);
        //console.log(this.forms);

        // Add event listeners
        if(this.forms.length > 0) {
            this.forms.each(function(frm) {
                Event.observe(frm, 'submit', this.handleSubmit.bindAsEventListener(this), true);
                Event.observe($(frm).select('button[type=submit]')[0], 'mouseup', function(e) { this.handleSubmit(e, true); }.bindAsEventListener(this), true);
                // Must be registered before document click to be able to stop the global event listener
                Event.observe(frm, 'click', this.handleFormClick.bindAsEventListener(this), true);
                Event.observe(frm, 'focus', this.handleFormFocus.bindAsEventListener(this), true);
                Event.observe(frm, 'keyup', this.handleKeyEvent.bindAsEventListener(this), true);
		//Event.observe(frm, 'keydown', this.handleKeyEvent.bindAsEventListener(this), true);
                Event.observe($(document.body), 'click', this.handleDocumentClick.bindAsEventListener(this), true);
            }.bind(this));
        }

        //console.log('ajaxSuggestObserver initialization finished');
    },
    
    
    cleanup:function() {
        //console.warn('CLEANING UP!');
        if(this.suggestObjects.length > 0) {
            this.suggestObjects.each(function(sg) {
                sg.close();
            });
        }
    },
    
    
    getKeyType:function(keycode)
    {
            var r='';
            switch(keycode)
            {
                    case Event.KEY_RETURN:  r='enter'; break;
                    case Event.KEY_ESC:     r='esc'; break;
                    case Event.KEY_UP:      r='arrowup';break;
                    case Event.KEY_DOWN:    r='arrowdown';break;
                    case 39:                r='arrowright';break;
                    case 37:                r='arrowleft';break;
                    case Event.KEY_TAB:     r='tab';break;
            }
            return r;
    },

    getTag:function(el)
    {
            var tag='';
            if(el != null) {
                    if(typeof el.tagName != 'undefined') {
                            tag=el.tagName.toLowerCase();
                    }
            }
            return tag;
    },

        
    isSuggestField:function(el)
    {
        return (el.hasClassName('suggestWhat') || el.hasClassName('suggestCity'));
    },
    


    /************************/
    /*** HANDLE KEY EVENT ***/
    /************************/
    handleKeyEvent:function(e)
    {
        //console.log('handleKeyEvent');
        var el = Event.element(e);
        //console.log(el);
        var key = this.getKeyType(e.keyCode);
        if(key == 'esc' || key == 'tab') {
            // ESC and TAB close all suggests
            //console.log('KEY ESC|TAB');
            this.cleanup();
        } else if(this.isSuggestField(el)) {
            //console.info('handKeyEvent: isSuggestField');
            if(this.getTag(el) == 'input') {
                //console.info('handKeyEvent: isSuggestField -> isInput');
                if(typeof el.type != 'undefined') {
                    if(el.type == 'text') {
                        //console.info('handKeyEvent: isSuggestField -> isInput -> is text');
                         if(el.suggest.isOpen) {

                            // Check if the fieldis empty!
                            if(el.suggest.getValue() == '') {
                                this.cleanup();
                            }

                           // console.info('handKeyEvent: isSuggestField -> isInput -> is text -> is open');
                            if(key == 'arrowup' || key == 'arrowdown' || key == 'arrowright' || key == 'arrowleft') {
                                el.suggest.handleKeyNavigation(key, el);
                            } else if(key == 'enter') {
                                // Just copy the value into the field and close it
                                //console.info('CAUGHT ENTER');
                                Event.stop(e);
                                this.lastInsertCloseEvent == 'enter';
                                el.suggest.insertClose(e);
                            } else {
                                el.suggest.handleKeyEvent(el);
                            }
                        } else {
                            //console.info('handKeyEvent: isSuggestField -> isInput -> is text -> is CLOSED');
                            // Keyevent in input field, i.e. typing in the field
                            if(key != 'enter') {
                                //console.log('Typing in suggest input field...');
                                // Delegate to to the suggest object to handle
                                el.suggest.handleKeyEvent(el);
                            } else {
                                if(this.lastInsertCloseEvent == 'enter') {
                                 //   this.lastInsertCloseEvent = '';
                                 //   el.suggest.submitForm();
                                }
                            }
                        }
                    }
                }
            }
        } else {
            var openedSuggest = null;
            //console.log('handleKeyEvent: not in suggest field');
            if(this.isMSIE) {
                this.suggestObjects.each(function(s) {
                    if(s.isOpen) {
                        openedSuggest = s;
                    }
                });
            }
            if(key == 'enter') {
                if(openedSuggest != null) {
                    //console.log('handleKeyEvent: MSIE && suggest open && not in suggest field && ENTER -> refocussing && insertClose');
                    $(openedSuggest.id).focus();
                    openedSuggest.insertClose(e);
                } else {
                    //console.log('handleKeyEvent: MSIE && suggest NOT open && not in suggest field && ENTER -> submitting');
                    this.submitCompleteForm(el.up('form'));
                }
            }
        }
    },

    
    /*********************/
    /*** HANDLE SUBMIT ***/
    /*********************/
    handleSubmit:function(e, fromSubmitButton)
    {
        //console.log('handleSubmit');
        
        var forceIESubmission = false;
        if(this.isMSIE) {
            if(fromSubmitButton && e.type=='mouseup' && Event.isLeftClick(e)) {
                forceIESubmission = true;
            }
        }
        
        var el = Event.element(e);
        //console.log(el);
        Event.stop(e);
        var openSuggests = 0;
        var openedSuggest = null;
        this.suggestObjects.each(function(s) {
            if(s.isOpen) {
                openSuggests++;
                openedSuggest = s;
            }
        });
        
        
        if(openSuggests == 0) {
            //console.info('No suggests open, submitting form...');
            if(typeof el.suggest != 'undefined') {
                var frm = $(el.suggest.formId);
                if(frm == null && typeof el.tagName != 'undefined') {
                    if(el.tagName.toLowerCase() == 'form') {
                        frm = el;
                    } else {
                        console.error('Whoops - no form for submission found!');
                    }
                }
                this.submitCompleteForm(frm);
            } else {
                // e.g. the submit button
                //el.up('form').submit();
                //console.info('No suggests open, submitting form... [B]');
                this.submitCompleteForm(el.up('form'));
            }
            //Event.stop(e);
        } else {
            //console.info(openSuggests+' suggests are open');
            if(this.focusedElement != null) {
                //console.info(openSuggests+' suggests are open: focused element is not null');
                if(!this.isSuggestField(this.focusedElement)) {
                    //console.info(openSuggests+' suggests are open: focused element is a suggest field');
                    // Element in focus is not a suggest field, so we can safely submit the form
                    if(el.up('form') != null) {
                       // console.info(openSuggests+' suggests are open: submitting form');
                        this.submitCompleteForm(el.up('form'));
                    }
                } else {
                   // console.info(openSuggests+' suggests are open: focused element is NOT a suggest field');
                }
            } else {
                // Special case: IE does NOT generate an event in the input field but on the submit button!!
                if(this.isMSIE) {
                    if(typeof el.suggest != 'undefined') {
                        el.suggest.insertClose(e);
                    } else {
                        console.error('MSIE redirected the event to the submit button, closing suggest and setting the focus back into the input field');
                        // City means: insert alort into hidden field and add brackets
                        if(openedSuggest.suggestType == 'city') {
                            var el = $('suggest_city_entry_'+openedSuggest.position+'_'+openedSuggest.id);
                            if(el != null) {
                                openedSuggest.handleCityClick(el);
                                return;
                            }
                        } else {
                            openedSuggest.id.value = openedSuggest.currentValue;
                        }
                    
                        openedSuggest.close();
                        $(openedSuggest.id).focus();
                        if(openedSuggest.autosearch) {
                            this.submitCompleteForm(openedSuggest.formId);
                        } else {
                            
                            if(forceIESubmission) {
                                // Forced IE submission
                                this.submitCompleteForm($(openedSuggest.formId));
                            }
                            Event.stop(e);
                        }


                    }
                }

            }
        }
    },


    submitCompleteForm:function(formEl)
    {
        var newURL = $(formEl).action+'?'+$(formEl).serialize();
        top.location.href=newURL;
    },


    /*************************/
    /*** HANDLE FORM CLICK ***/
    /*************************/
    handleFormClick:function(e)
    {
       // console.log('handleFormClick');
        var el = Event.element(e);

        // It does not make sense to show the suggest again if the user did not type at all
        var isNoKeyInput = false;
        if(typeof e.keyCode != undefined) {
            var isNoKeyInput = true;
        }

        // Check if clicked in suggest field
        if(this.isSuggestField(el) && !isNoKeyInput) {
            // Show suggest
            this.cleanup(); // Close all other suggests
            el.suggest.open();
        } else {
            
            if(this.MSIE) {
                if(typeof el.tagName != 'undefined' && el.tagName == 'button') {
                    if(el.getAttribute('type') && el.getAttribute('type') == 'submit') {
                        Event.stop(e);
                        this.submitCompleteForm(el.up('form'));
                    }
                }
            }
            
        }
    },


    /*************************/
    /*** HANDLE FORM FOCUS ***/
    /*************************/
    handleFormFocus:function(e)
    {
        //console.log('handleFormFocus');
        var el = Event.element(e);
        this.focusedElement=el;
    },


    /*****************************/
    /*** HANDLE DOCUMENT CLICK ***/
    /*****************************/
    handleDocumentClick:function(e)
    {
       // console.log('handleDocumentClick');
        var el = Event.element(e);
        if(!this.isSuggestField(el)) {
            if(this.isMSIE) {
                var eventForm = el.up('form');
                 if(eventForm == null) {
                        this.cleanup();
                 } else {
                    // Check if suggest form
                    if(eventForm.down('input.suggestCity') != null || eventForm.down('input.suggestWhat') != null) {
                        Event.stop(e);
                    }
                 }
            } else {
                this.cleanup();
            }
        }
    }

});



/**************************/
/***** INITIALIZATION *****/
/**************************/
var ajaxSuggestObserver;
Event.observe(window, 'load', function() {
    ajaxSuggestObserver = new ajaxSuggestObserverClass();
});


