
//var inputs = $$('.control_input');



function DoComboPopup(item) {
    new Form.Element.Observer(
        item,
        0.5,
        function(el, value) {
            hideComboPopup();
            DoCreateComboPopup(item);
        }
    );
}

var firstset = false;
var value_set = 0;
var ComboPopup = new Element('div',{'class':'DivComboPopup'});

var DoCreateComboPopup = function(item) {
    if(value_set == -1) {
        value_set = 0;
        return;
    }

    // Display the loadingbar
    //var offset_arr = $(item).cumulativeOffset();
    //$('L3008250').update('x:'+offset_arr[0]+' y:'+offset_arr[1]+' h:'+$(item).getHeight());
    //var soffset_arr = $(item).cumulativeScrollOffset();

    ComboPopup.clonePosition($(item), {
       'offsetTop': $(item).getHeight()+1,
       'setWidth': '100%',
       'setHeight':false
    });
    ComboPopup.style.zIndex = 1000;
    ComboPopup.style.position = 'absolute';

    var LoadingImage = '<center><img src=\'sys/img/copy_wait.gif\'></center>';

    if(!firstset) {
       document.body.appendChild(ComboPopup);
       firstset = true;
       DoCreateComboPopup(item);
       return;
    }

    ComboPopup.show();
    ComboPopup.update(LoadingImage);

    new Ajax.Request('GetPropertyValues.php', {
        parameters: {'property_id': $(item).id,
                      'value': $(item).value,
                      'sprache': $(item).lang
                    },
        onException: function () {
            var FailImage = '<center><img src=\'sys/img/exclamation.png\'></center>';
            ComboPopup.update(FailImage);
            setTimeout(function() {
                ComboPopup.hide();
            }, 1000, null, null)
        },
        onFailure: function () {
            var FailImage = '<center><img src=\'sys/img/exclamation.png\'></center>';
            ComboPopup.update(FailImage);
            setTimeout(function() {
                ComboPopup.hide();
            }, 1000, null, null)
        },
        onSuccess: function(r) {
            dataArray = r.responseText.evalJSON();
            if (dataArray.length > 0) {
                var selectbox = '<div style=\'width: 100%; height: 100%\'><table style=\"background-image:url(sys/img/grau_trans2.png); width: 100%;\">';
                for(var i = 0; i < dataArray.length; i++) {
                    selectbox+= '<tr><td onMouseOver=\"javascript:{this.style.backgroundColor=\'#B0B0B0\';}\" onMouseOut=\"javascript:{this.style.backgroundColor=\'\';}\" onclick=\"SetComboValue(\''+$(item).id+'\',\''+dataArray[i][2]+'\');\" style=\"width:100%; cursor: pointer; font-size: 9pt;\">'+dataArray[i][1]+'</td></tr>';
                }
                selectbox+= '</table></div>';
                ComboPopup.update(selectbox);
            } else { // nothing were found
                ComboPopup.hide();
            }
        }
    });
}

var hideComboPopup = function() {
    ComboPopup.hide();
}

var showComboPopup = function() {
    ComboPopup.show();
}

var SetComboValue = function(id, value) {
    $(''+id+'').value = value;
    $(''+id+'').onkeyup();
    hideComboPopup();
    value_set = -1;
}
