//var activeHelpBox = null;
function jsonGetHelp(id, win) {
    $.ajax({type:"GET",url:'/lang/json.php?key='+id,data:null,dataType:"json", success:showHelpText});
}

function showHelpText(result) {
    $('#helpdialog').removeClass('helploading');
    $('#helpdialog').dialog('option', 'title', '');
    $('#helpdialog').html(result.html);

    if (typeof(result.class_name) != "undefined") {
        $('#helpdialog').addClass(result.class_name);
    }
    if (typeof(result.title) != "undefined") {
        $('#helpdialog').dialog('option', 'title', result.title);
    }
    if (typeof(result.width) != "undefined") {
        $('#helpdialog').dialog('option', 'width', result.width * 1);
    }
    if (typeof(result.height) != "undefined") {
        $('#helpdialog').dialog('option', 'height', result.height * 1);
    } else {
        $('#helpdialog').css('height', 'auto');
    }
}

function openHelpText(hid) {
    $('#helpdialog').dialog({autoOpen: false});
    $('#helpdialog').dialog('option', 'height', 150);
    $('#helpdialog').dialog('option', 'width', 300);
    $('#helpdialog').css('width', 'auto'); /* BEOBACHTEN HELPICONS NACH WIDTH HEIGHT AUS CSV*/
    $('#helpdialog').dialog('option', 'dialogClass', 'tribax-help-dialog');
    jsonGetHelp(hid);
    $('#helpdialog').removeClass();
    $('#helpdialog').addClass('helploading');
    $('#helpdialog').dialog('option', 'title', '');
    $('#helpdialog').dialog('open');
    $('.ui-dialog.ui-widget.tribax-help-dialog').show();
    $('#helpdialog').addClass('h'+hid); // for the "?"-toggle 
}

$(document).ready(function() {
    if(document.getElementById('helpdialog') == null) {
        $('#wrapper_outer').before(document.createElement('div').setAttribute('id', 'helpdialog'))
    }
    $(".helptext").click(function(){
        var param, hclass, hid;
        $('#helpdialog').empty();
        param = $(this).attr('class').split(' ');
        for (i=0; i < param.length; i++) {
            if (param[i] != 'helptext') hid = param[i].replace (/[^\d]/, '');
        }
        var existingWindow = $('#helpdialog.h'+hid).get(0); // toggle win on "?"-click
        if (existingWindow) {
            $(existingWindow).removeClass();
            if ($('.ui-dialog.ui-widget.tribax-help-dialog').css('display') == 'none') {
                // we need this, when the user uses the builtin close button of jquery ui.
                openHelpText(hid);
                return true;
            }
            else {
                $('.ui-dialog.ui-widget.tribax-help-dialog').hide();
            }
        }
        else if (hid) {
            openHelpText(hid);
        }
    });
});
