cms.core.suggest = function(moduleId, inputId, ajaxMethod, dataId, formId, indicatorId, highlight)
{
    this.moduleId = moduleId;
    this.inputId = inputId;
    this.formId = formId;
    this.inputElement = document.getElementById(this.inputId);
    this.formElement = document.getElementById(this.formId);
    this.ajaxMethod = ajaxMethod;
    this.dataId = dataId;
    this.indicatorId = indicatorId;
    this.highlight = highlight;

    // catch enter onsubmit
    jQuery('#' + formId).attr('onSubmit', 'return false');

    if (this.dataId)
    {
        this.dataElement = document.getElementById(this.dataId);
    }
    else
    {
        this.dataElement = null;
    }
    
    if (this.indicatorId)
    {
    	this.indicatorElement = document.getElementById(this.indicatorId);
    }
    else
    {
    	this.indicatorElement = null;
    }
    
    this.suggestElement = null;
    this.selectedIndex = -1;
    this.jsonData;
    this.ajaxRequest;
    
    this.initialize = function()
    {    	
    	// Haal beide velden leeg
        //this.inputElement.value = '';
        if (this.dataElement)
        {
            this.dataElement.value = '';
        }

        // Initialiseer het object met de juiste events
        cms.core.event.attach(this.inputElement, 'focus', this.update, this);
        cms.core.event.attach(this.inputElement, 'keyup', this.goto, this);
        cms.core.event.attach(this.inputElement, 'keyup', this.update, this);
        cms.core.event.attach(this.inputElement, 'blur', this.hide, this);
        this.inputElement.autocomplete = 'off';
        
        // Maak de suggest box aan
        this.create();
    }
    
    this.create = function()
    {
        this.suggestElement = document.createElement('DIV');
        this.suggestElement.id = inputId + '-suggest';
        this.suggestElement.className = 'autoSuggest';
        this.suggestElement.style.position = 'absolute';
        
        if (this.indicatorElement) // plaats na indicator als aanwezig
        {
        	this.indicatorElement.parentNode.parentNode.appendChild(this.suggestElement);
        }
        else
        {
        	this.inputElement.parentNode.appendChild(this.suggestElement);
        }
    };

    this.update = function(e)
    {    	
    	if (window.event)
        {
            key = e.keyCode;
        }
        else if(e.which)
        {
            key = e.which;
        }
    	
    	if (this.inputElement.value == '')
        {
            // Verwijder indien nodig ook de gevonden waarde
            if (this.dataElement)
            {
                this.dataElement.value = '';
            }        
        
            this.hide();
            return;
        }
        else
        {

        	
        	var json = 'json={"moduleId":' + this.moduleId + ',"method":"' + this.ajaxMethod + '","args":""}';
        	var post = json + '&' + jQuery(this.formElement).serialize();
        	
        	if (this.indicatorElement)
        	{
        		this.indicatorElement.style.visibility = 'visible';
        	}
        	
        	this.ajaxRequest = new cms.core.httpRequest();
            this.ajaxRequest.setCallback(this.process, this, false);
            this.ajaxRequest.sendRequest('/ajax.php', post, true);
        }
    };

    this.process = function(response, request)
    {
        var link = jQuery(this.formElement).attr('action');
    	
    	// Handel alleen de laatste request af
        if (request != this.ajaxRequest)
        {
            return false;
        }
        
        if (this.indicatorElement)
    	{
    		this.indicatorElement.style.visibility = 'hidden';
    	}
        
        if (response == '')
        {
        	// Verwijder indien nodig ook de gevonden waarde
            if (this.dataElement)
            {
                this.dataElement.value = '';
            }
                    
            this.suggestElement.style.display = 'none';
        }
        else
        {
            this.jsonData = eval('('+response+')');
            
            this.suggestElement.innerHTML = '';
            
            if (this.jsonData.terms.length > 0)
            {
                for (i = 0; i < this.jsonData.terms.length; i++) 
                {
                    a = document.createElement("A");
                    a.rel = i;
                    a.href = link + '&' + jQuery(this.inputElement).attr('name') + '=' + this.jsonData.terms[i]['label'];
                    cms.core.event.attach(a, 'click', this.select, this, false, true);
                    //cms.core.event.attach(a, 'mouseover', this.clearSelectedIndex, this);
                    
                    if (this.jsonData.terms[i]['code'])
                    {
                        a.innerHTML = '<span>' + this.jsonData.terms[i]['code'] + '</span>' + this.jsonData.terms[i]['label'];
                    }
                    else
                    {
                        a.innerHTML = this.jsonData.terms[i]['label'];
                    }
                    
                    if (this.highlight)
                    {
                        var re = new RegExp(this.jsonData.query, 'ig');
                        a.innerHTML = a.innerHTML.replace(re, '<em>' + this.jsonData.query + '</em>');
                    }                    
                    
                    if (i == this.selectedIndex)
                    {
                        a.style.backgroundColor = '#eeeeee';
                        a.style.color = '#FF6600';
                        if (this.dataElement)
                        {
                            this.dataElement.value = this.jsonData.terms[i]['id'];
                        }
                    }
                    this.suggestElement.appendChild(a);
                }
                
                var offset = jQuery(this.inputElement).position();
                this.suggestElement.style.left = offset.left + 'px';
                this.suggestElement.style.top = offset.top + (this.inputElement.offsetHeight - 1) + 'px';
                this.suggestElement.style.display = 'block';
            }
            else
            {
                // Geen gevonden items
                this.suggestElement.style.display = 'none';
            }
        }
    };

    this.select = function(event)
    {
        var element = event.srcElement || event.target;
        this.inputElement.value = this.jsonData.terms[element.rel].label;
        if (this.dataElement)
        {
            this.dataElement.value = this.jsonData.terms[element.rel].id;
        }
        this.suggestElement.style.display = 'none';
        
        // Verzend de zoekopdracht
        this.formElement.submit();
    };

    this.hide = function(event)
    {
        if (event)
        {
            var mouseX = event.clientX;
            var mouseY = event.clientY;
            
            // Controleer of de mouse click binnen de box van het suggest window valt
            // In dat geval wordt de hide() niet uitgevoerd
            if (mouseX >= (this.suggestElement.offsetLeft + this.suggestElement.offsetWidth - 20)
                && mouseX <= (this.suggestElement.offsetLeft + this.suggestElement.offsetWidth)
                && mouseY >= this.suggestElement.offsetTop
                && mouseY <= (this.suggestElement.offsetTop + this.suggestElement.offsetHeight)) 
            {
                this.inputElement.focus();
                return false;
            }
        }
    
        setTimeout('document.getElementById("' + this.suggestElement.id + '").style.display = "none"', 200);
    };

    this.goto = function(e)
    {    	
    	if (window.event)
        {
            key = e.keyCode;
        }
        else if(e.which)
        {
            key = e.which;
        }
        
        var children = this.suggestElement.childNodes;
        var max = children.length - 1;
        
        switch (key)
        {
            case 3:
            case 13: //enter
                if (this.selectedIndex >= 0)
                {
                    n = children[this.selectedIndex].innerHTML.replace(/<\/?[^>]+(>|$)/g, "");

                    this.inputElement.value = n;
                }
                this.formElement.submit();
                break;
                
            case 38: //up arrow
                this.selectedIndex--;
                if (this.selectedIndex == -1)
                {
                    this.selectedIndex = max;
                }
                break;

            case 40: //down arrow
                this.selectedIndex++;
                if (this.selectedIndex > max)
                {
                    this.selectedIndex = 0; 
                }

                break;
        }
            
        if (key != 3 && key != 13)
        {
            return key;
        }
    };
    
    this.initialize();
};
