﻿/**
 * Konstruktor
 */
var OptionenLayer = new function() {
    this.timerId = null;
    var isResetting = false;

    this.GetIsResetting = function() { return isResetting; }
    this.SetIsRestting = function(value) { isResetting = value; };
}

/******************************************************************************
***                              Hauptfunktionen                            ***
******************************************************************************/

/**
 * Zeigt die Detailssuche an bzw. blendet sie aus.
 * @param boolean closeQueryBuilder Gibt an ob der QueryBuilder (Profisuche) geschlossen werden soll.
 */
OptionenLayer.toggleOptionenlayer = function(closeQueryBuilder, anchorId) {
    var optionenlayer = $get('optionenlayer');

    if (optionenlayer == null || optionenlayer.childNodes.length == 0) {
        XmlLoader.loadXml('1', null, "collectFromSession=true&details=on", null, null, true, OptionenLayer.onLoadedXml, anchorId);
    } else {
        ToggleElement('optionenlayer');

        if (optionenlayer != null) {
            if (optionenlayer.style.display == 'none')
                optionenlayer.style.display = 'block';

            if (optionenlayer.style.visibility == 'visible')
                AddHiddenField('details', 'on');
            else
                AddHiddenField('details', '');
        }
    }
    if (closeQueryBuilder) {
        QueryBuilder.collapseAllSpecialSearches();
        QueryBuilder.deselectAllSpecialSearches();
        QueryBuilder.close();
    }
}


/**
 * Setzt die Suche komplett zurück
 */
OptionenLayer.clearSearch = function() {
    var wordsInput = $get('words');
    if (wordsInput != null) {
        wordsInput.value = '';
    }

    this.reset();
}

OptionenLayer.activatechkdoktyp = function() {
    var chkDokType = $get('chkdoktyp');
    var checkBoxes = new Array(
                        'chkkomm',
                        'chkrspr',
                        'chkaufs',
                        'chkges',
                        'chkform',
                        'chkMeld',
                        'chkVerw',
                        'chklexika'
                    );
    if (chkDokType == null)
        return;

    if (chkDokType.checked == true) {
        for (var checkBoxIndex = 0; checkBoxIndex < checkBoxes.length; checkBoxIndex++) {
            var checkBox = $get(checkBoxes[checkBoxIndex]);
            if (checkBox != null)
                checkBox.checked = false;
        }
        document.getElementById("txtopus").value = '';
    }
}

OptionenLayer.deactivatechkdoktyp = function() {
    document.getElementById("chkdoktyp").checked = false;
}

OptionenLayer.setTextBoxWidth = function() {
    var setForRow = function(row) {
        var input = row.find(':text');

        if (input == null){
            return;
        }

        var otherWidth = 0;
        row.children()
       .not(input)
       .each(function(index) { otherWidth += $(this).width(); });

        var inputPadding = parseFloat(input.css('padding-left')) + parseFloat(input.css('padding-right'));
        input.width(row.width() - otherWidth - inputPadding - 1);
    }

    $('.line-container').each(function(index) {
        setForRow($(this));
    });
}

/******************************************************************************
***                          Funktionen der Buttons                         ***
******************************************************************************/

/**
 * Der Hilfe Button
 */
OptionenLayer.showHelp = function() {
    ShowHelp('SearchFormControl', 'optionenlayercontrolhelp');
}

/**
* Der Reset Button
*/
OptionenLayer.reset = function() {
    this.SetIsRestting(true);
    var optionenlayer = $get('optionenlayer');
    var inputlist = optionenlayer.getElementsByTagName("input");

    for (i = 0; i < inputlist.length; i++) {
        if (inputlist[i].type == 'text')
            inputlist[i].value = '';
        else if (inputlist[i].type == 'checkbox')
            inputlist[i].checked = false;
    }

    if ($get('chkdoktyp') != null)
        $get('chkdoktyp').checked = true;

    OptionenLayer.activatechkdoktyp();
    OptionenLayer.refresh(0);
}

/**
 * Der Schließen Button
 */
OptionenLayer.close = function() {
    entferneAktuelleSuchOption();
    var optionenlayer = $get('optionenlayer');

    if (optionenlayer == null || optionenlayer.style.visibility != 'visible') {
        return;
    }
    this.toggleOptionenlayer(false);
}

/******************************************************************************
***                              Hilfsmethoden                              ***
******************************************************************************/

/**
 * Zeigt das Control erstmalig an
 */
OptionenLayer.init = function(anchorId) {
    this.toggleOptionenlayer(true, anchorId);
    markiereAktuelleSuchOption(0);
}

/**
 * Aktualisiert die Liste
 * @param integer time Wird eine Zeitspanne angegeben, so wird das Laden der Liste verzögert
 */
OptionenLayer.refresh = function(time) {
    if (this.timerId != null) {
        clearTimeout(this.timerId);
        this.timerId = null;
    }

    if (typeof time != "undefined") {
        this.timerId = XmlLoader.loadXmlWithTimer('1', null, null, null, null, true, OptionenLayer.onLoadedXml, time);
    } else {
        XmlLoader.loadXml('1', null, null, null, null, true, OptionenLayer.onLoadedXml, null);
    }
}

/**
 * Wird nach dem fertig ausgeführten Ajax-Request ausgeführt
 */
OptionenLayer.onLoadedXml = function(executor, eventArgs) {
    XmlLoader.onLoadedXml(executor, eventArgs);
    if (OptionenLayer.GetIsResetting() == true) {
        SetFocus('words');
        OptionenLayer.SetIsRestting(false);
    }
    // Textboxen auf enabled setzen
    var optionenlayer = $get('optionenlayer');
    var inputlist = optionenlayer.getElementsByTagName("input");
    for (i = 0; i < inputlist.length; i++) {
        if (inputlist[i].type == 'text' && inputlist[i].value != '') {
            inputlist[i].disabled = false;
        }
    }

    var words = $get('words');
    words.disabled = false;

    if (words.attributes['autocomplete'] != null) {
        $("#txtrgebiet").autocomplete('SuggestHandler.aspx?typ=rechtsgebiet', { selectFirst: false, delay: 250 });
        $("#txtopus").autocomplete('SuggestHandler.aspx?typ=publikation', { selectFirst: false, delay: 250 });
        $("#txtmodule").autocomplete('SuggestHandler.aspx?typ=modul', { selectFirst: false, delay: 250 });
        $("#txtnorm").autocomplete('SuggestHandler.aspx?typ=norm', { selectFirst: false, delay: 250 });
        $("#txtgericht").autocomplete('SuggestHandler.aspx?typ=gericht', { selectFirst: false, delay: 250 });
        $("#txtbehoerde").autocomplete('SuggestHandler.aspx?typ=behoerde', { selectFirst: false, delay: 250 });
        $("#txtZeitsFundst").autocomplete('SuggestHandler.aspx?typ=zeitschrift', { selectFirst: false, delay: 250 });
        $("#txtfundst").autocomplete('SuggestHandler.aspx?typ=fundstelle', { selectFirst: false, delay: 250 });
        $("#txtaz").autocomplete('SuggestHandler.aspx?typ=az', { selectFirst: false, delay: 250 });
        $("#txtAuthor").autocomplete('SuggestHandler.aspx?typ=autor', { selectFirst: false, delay: 250 });
        $("#txtnormgeber").autocomplete('SuggestHandler.aspx?typ=normgeber', { selectFirst: false, delay: 250 });
    }

    OptionenLayer.setTextBoxWidth();
}
