﻿var autoBoxHeight = 180;
var autoBoxWidth = 260;
var overAutoBox = 0;

function showAutoBox(sender) {
    var autoBoxHeight = $('autoBox').getFirst('ul').getSize().y;
    var parentPos = sender.getCoordinates(document.body);
    var parentDim = sender.getSize();
    if ($('autoBox')) {
        $('autoBox').setStyles({ 'top': parentPos.top + parentDim.y, 'left': parentPos.left });
        $('autoBox').set('morph', { duration: 300 });
        $('autoBox').morph({ 'opacity': 1, 'height': autoBoxHeight, 'width': autoBoxWidth });
    }
}

function createAutoBox(sender) {
    // Controllo che esista il box
    if (!$('autoBox')) {
        var autoBox = new Element('div', {
            'id': 'autoBox',
            'class': 'autoBox',
            'events': {
                'mouseenter': function() {
                    overAutoBox = 1;
                },
                'mouseleave': function() {
                    overAutoBox = 0;
                }
            }
        });
        /*if ($(sender))
            autoBox.inject(sender, 'after');
        else
            autoBox.inject(document.body);*/
        autoBox.inject(document.body);
        autoBox.setPosition($(sender).getPosition(document.body));
        
        autoBox.setStyles({ 'opacity': 0, 'display': 'block', 'width': 0, 'height': 0 });
    }
    $('autoBox').empty();
}

function hideAutoBoxClick() {
    overAutoBox = 0;
    if ($('autoBox')) {
        $('autoBox').set('morph', { duration: 300 });
        $('autoBox').morph({ 'opacity': 0, 'height': 0, 'width': 0 });
    }
}
function hideAutoBox() {
    if ($('autoBox')) {
        if (overAutoBox == 1)
            return;
        $('autoBox').set('morph', { duration: 300 });
        $('autoBox').morph({ 'opacity': 0, 'height': 0, 'width': 0 });
    }
}

function onDigit(sender) {

    if ($(sender).get('value').length <= 0) {
        hideAutoBox();
        return;
    }
    else {
        var text = sender.get('value');
        //text = myReplace(text, "'", "");
        var qryStr = 'text=' + text + '&cache=' + Math.floor(Math.random() * 32767);
        var myRequest = new Request({
            onRequest: function() {
                //showLoader(); 
            },
            onCancel: function() {
                //hideLoader();
            },
            onSuccess: function(responseText, responseXML) {
                if (responseText.length > 0) {
                    if (responseText.indexOf('##') >= 0) {
                        var aStr = responseText.split("##");
                        if (aStr.length <= 0) {
                            hideAutoBox();
                            //hideLoader();
                            return;
                        }

                        createAutoBox(sender);

                        var ul = new Element('ul');

                        var c = 0;
                        while (c < aStr.length) {
                            if (aStr[c].length > 0) {
                                var li = new Element('li', {
                                    'html': aStr[c],
                                    'events': {
                                        'click': function() {
                                            sender.set('value', this.get('html'));
                                            sender.focus();
                                            hideAutoBoxClick();
                                        }
                                    }
                                });
                                li.inject(ul);
                            }
                            c++;
                        }
                        ul.inject($('autoBox'));

                        showAutoBox(sender);
                    }
                    else {
                        hideAutoBox();
                        //hideLoader();
                        return;
                    }

                    //hideLoader();
                }
            },
            onFailure: function(instance) {
                //hideLoader();
                //alert("Errore durante l'operazione!");
            },
            method: 'post',
            url: '/Scripts/search_tags.aspx'
        }
        ).send(qryStr);
    }
}

window.addEvent('domready', function() {
    if ($('search'))
        $('search').set('autocomplete', 'off');
});