/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 * 
 * This file extends and partially overrides original Varien js class for
 * autocompletion in order to:
 * - fix autocompletion bahaviour for Chrome browser(this part was developed by
 *   Eduard Melnitskiy <eduard.melnitskiy@symmetrics.de>);
 * - add "Group by categroy" feature.
 *
 * @category  Symmetrics
 * @package   Symmetrics_Autocomplete
 * @author    symmetrics gmbh <info@symmetrics.de>
 * @author    Anton Shatenfeld <anton.shatenfeld@symmetrics.de> 
 * @author    Eduard Melnitskiy <eduard.melnitskiy@symmetrics.de>
 * @copyright 2009-2011 symmetrics gmbh
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @link      http://www.symmetrics.de/
 */

Varien.searchSuggestForm = Class.create();
Varien.searchSuggestForm.prototype = {
    initialize: function(form, field, emptyText) {
        this.form   = $(form);
        this.field  = $(field);
        this.emptyText = emptyText;

        Event.observe(this.form,  'submit', this.submit.bind(this));
        Event.observe(this.field, 'focus', this.focus.bind(this));
        Event.observe(this.field, 'blur', this.blur.bind(this));
        this.blur();
    },

    submit: function(event) {
        if (this.field.value == this.emptyText || this.field.value == ''){
            Event.stop(event);
            return false;
        }
        return true;
    },

    focus: function(event) {
        if(this.field.value==this.emptyText) {
            this.field.value='';
        }

    },
    
    blur: function(event) {
        if(this.field.value==''){
            this.field.value=this.emptyText;
        }
    },

    initAutocomplete: function(url, destinationElement) {
        /**
         * Extends Ajax.Autocompleter class
         */
        Ajax.AutocompleterUnimall = Class.create(Ajax.Autocompleter, {  
            onClick:function(event) {
                var element = Event.findElement(event, 'LI');
                if (typeof(element) != 'undefined') {
                    this.index = element.autocompleteIndex;
                    this.selectEntry();
                }
                this.hide();
            },
            
            addObservers: function(element) {
                Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));
                Event.observe(element, "click", this.onClick.bindAsEventListener(this));
                Event.observe(document, "click", this.onClick.bindAsEventListener(this));
            },
            onHover: function(event) {
                var element = Event.findElement(event, 'LI');
                if (typeof(element) != 'undefined') {
                    if(this.index != element.autocompleteIndex)
                    {
                        this.index = element.autocompleteIndex;
                        this.render();
                    }
                }
                Event.stop(event);
            },

            onClick: function(event) {
                var element = Event.findElement(event, 'LI');
                if (typeof(element) != 'undefined') {
                    var url = $(element).readAttribute('src');
                    if (url) {
                        window.location.href = url;
                    } else {
                        this.index = element.autocompleteIndex;
                        this.selectEntry();
                    }
                    this.hide();
                }
            },
            
            onBlur: function(event) {
                // Line below contains in original code and should be removed, 
                // because does not work correctly in browser like safary.
                
                //setTimeout(this.hide.bind(this), 250);
                this.hasFocus = false;
                this.active = false;
            }
        });

        return new Ajax.AutocompleterUnimall(
            this.field,
            destinationElement,
            url,
            {
                paramName: this.field.name,
                method: 'get',
                minChars: 3,
                updateElement: this._selectAutocompleteItem.bind(this),
                onShow : function(element, update) {
                    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});
                }
            }
        );
    },

    _selectAutocompleteItem: function(element){
        this.form.submit();
    }
};
