/* -------------------------------------------------------------
    AUTHOR: Stefan Nafra
    UPDATED: 20.01.2011
    CONTENT: Allgemeine Funktionen die immer wieder benötigt werden.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* -------------------------------------------------------------
    01. EINSATZGEBIET: ? | POI
        BESCHREIBUNG: .
        01.1. Setzt die Dummyfelder beim Abschicken auf leer.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function clearForm() {
    $('div.poi-form input:text').each(function () {
        if ($(this).hasClass('searchinputcolor')) {
            $(this).val('');
        }
    });
}

/* -------------------------------------------------------------
        01.2. Setzt bei leeren Feldern die Dummy Werte wieder ein.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function initLabel() {
    $('div.poi-form input:text').each(function () {
        if ($(this).val() === '') {
            $(this).addClass('searchinputcolor').val($(this).attr('title'));
        }
    });
}

$(function () {
    initLabel();
});

/* -------------------------------------------------------------
        01.3. Löscht den Dummytext des Inputfeldes beim fokusieren.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('div.poi-form input:text').live('click', function () {
    if ($(this).hasClass('searchinputcolor')) {
        $(this).removeClass('searchinputcolor').val('').focus();
    }
});

/* -------------------------------------------------------------
        01.4. Erstellt aus dem Title den Dummytext sofern der Wert des Inputs leer ist.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {
    $('div.poi-form input:text').blur(function () {
        if ($(this).val() === '') {
            $(this).addClass('searchinputcolor').val($(this).attr('title'));
        }
    });
});

/* -------------------------------------------------------------
        01.5. Filtert die Liste nach den gewünschten Werten aus den Checkfeldern.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('input.poi-check').live('click', function () {
    $('.poi-result li').hide();
    $(this).closest('div').find('input.poi-check:checked').each(function () {
        $('.poi-result li.' + $(this).val() + ', .poi-header)').show();
    });
});

$('input.filter').live('click', function () {
    $('ul.sortable li').hide();
    $(this).parent().children('input.filter:checked').each(function () {
        $('ul.sortable li.' + $(this).val() + ')').show();
    });
});

/* -------------------------------------------------------------
        01.6. Setzt das ganze Formular zurück.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('a.form-delete').live('click', function () {
    $(this).closest('form').find('input:text, input:checkbox, select').each(function () {
        if ($(this).is('input:text')) {
            $(this).addClass('searchinputcolor').val($(this).attr('title'));
        } else if ($(this).is('input:checkbox')) {
            $(this).removeAttr('checked');
        } else if ($(this).hasClass('poi-sel')) {
            $(this).val('');
        }
    });
});

/* -------------------------------------------------------------
    02. EINSATZGEBIET: WEBSEITIG | GLOBAL
        BESCHREIBUNG: Ermöglich das gezielte Ansprechen von Get-Paramtern aus der URL.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {

    (function ($) {
        $.url = {};
        $.extend($.url, {
            _params: {},
            init: function () {
                var paramsRaw = '';
                try {
                    paramsRaw = (document.location.href.split('?', 2)[1] || '').split('#')[0].split('&') || [];
                    for (var i = 0; i < paramsRaw.length; i++) {
                        var single = paramsRaw[i].split('=');
                        if (single[0]) {
                            this._params[single[0]] = unescape(single[1]);
                        }
                    }
                }
                catch (e) {
                    alert(e);
                }
            },
            param: function (name) {
                return this._params[name] || '';
            },
            paramAll: function () {
                return this._params;
            }
        });
        $.url.init();
    })(jQuery);
});

/* -------------------------------------------------------------
    03. EINSATZGEBIET: WEBSEITIG - BLUMENBUERO | FLASHVIDEOS
        BESCHREIBUNG: Beim Betreten der Flashvideos über den Button auf der Startseite werden nur die 9 aktuellsten Videos angezeigt.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {
    if ($.url.param('id').match(/4813/) && $.url.param('id').match(/1092/)) {
        $('#content').children('div:gt(8)').hide();
    }
});

/* -------------------------------------------------------------
    04. EINSATZGEBIET: WEBSEITIG | LAGERHAUS - ANGEBOTE
        BESCHREIBUNG: Versieht Preise ohne Kommazahlen mit einem ,-- und Kommazahlen werden in einem em-tag gewrappt.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {

    $('div.preis').each(function () {

        var splittext = $(this).children('em').text().split(','), hasSpan = ($(this).children('span').length > 0) ? '<span>' + $(this).children('span').html() + '</span> ' : '';

        if (splittext[1]) {
            $(this).html(hasSpan + splittext[0] + ', <em>' + $.trim(splittext[1]) + '</em>');
        } else if (!isNaN(splittext[0]) && splittext[0] !== '') {
            $(this).html(hasSpan + splittext[0] + ',--');
        } else {
            $(this).html(hasSpan + splittext[0]);
        }
    });
});

/* -------------------------------------------------------------
    05. EINSATZGEBIET: DIGITALWORKROOM | LINK
        BESCHREIBUNG: Öffnet/Schließt die Linkbereiche und übernimmt die Werte.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('.toggle-link').live('click', function () {

    var $this = $(this), $area = $this.parents('div:eq(1)'), $area1 = $area.children('div:eq(0)'), $area2 = $area1.next(), path = $area1.children('div:eq(0)'), pathValue = $area2.children('div:eq(0)').find('input').val();

    if ($area2.is(':hidden')) {
        $this.text('Übernehmen');
    } else {
        $this.text('Bearbeiten');

        if (!$area.hasClass('toggle-download-area')) {
            path.html('<a href="parse.php?id=2500,' + pathValue + '" title="" target="_blank">' + pathValue + '</a>');
        } else {
            path.html('<a href="http://dw6.testit.at/mmedia/download' + pathValue.replace(/download=/, '') + '" title="" target="_blank">' + pathValue + '</a>');
        }

        $area1.children('div:gt(0)').each(function (index) {
            if ($area2.children('div:eq(' + (index + 1) + ')').children('input').length > 0) {
                $area1.children('div:eq(' + (index + 1) + ')').text($area2.children('div:eq(' + (index + 1) + ')').children('input').val());
            } else {
                $area1.children('div:eq(' + (index + 1) + ')').text($area2.children('div:eq(' + (index + 1) + ')').find('option:selected').text());
            //            $('#' + divID + ' .area-short .formElement' + i).html($('#' + divID + ' .area-full .formElement' + i + ' input:checked').parent().text());
            }
        });
    }
    $area1.toggle().next().toggle();
});

$('.toggle-model').live('click', function () {

    var $this = $(this), $area1 = $this.parents(':eq(1)').children(':eq(0)'), $area2 = $area1.next(), value;

    if ($area1.is(':hidden')) {
        $this.text('Bearbeiten').next().remove();

        $area2.children().each(function (index) {

            value = $(this).children().val();

            if (value !== undefined && value !== '') {
                $area1.children(':eq(' + (index) + ')').removeAttr('disabled').append('<option value="' + value + '">' + value + '</option>').children(':last').attr('selected', 'selected');
            }
        });

        $area1.children(':enabled:last').change();

        $.post('index.php', {
            mdoc_id: $('#formular input[name=mdoc_id]').val(),
            mlay_id: '20002',
            row: parseInt($this.closest('div[id*=sortable-]').attr('id').match(/\d+/), 10) - 1,
            model: $area1.children(':eq(0)').val(),
            type: $area1.children(':eq(1)').val(),
            version: $area1.children(':eq(2)').val()
        });
    } else {
        $this.text('Sichern').after(' <a href="javascript:void(0);" class="end-model">Abbrechen</a>');

        $area1.children(':not(disabled)').each(function (index) {
            if ($(this).children(':selected').text() !== '[leer]') {
                $area2.children(':eq(' + (index) + ')').text($area1.children(':eq(' + (index) + ')').children(':selected').text());
            } else {
                $area2.children(':eq(' + (index) + ')').html('<input class="input-model" type="text" name="" value="" />').nextAll().html('');
                return false;
            }
        });
    }
    $area1.toggle().next().toggle();
});

$('.end-model').live('click', function () {
    $(this).prev().text('Bearbeiten').parents(':eq(1)').children(':eq(0)').show().next().hide();
    $(this).remove();
});

$('#dw-model input.input-model').live('blur', function () {

    var $this = $(this), $next = $this.parent().next();

    if ($this.val() !== '' && $next.length > 0) {
        $next.html('<input class="input-model" type="text" name="" value="" />').find('input').focus();
    }
});

/* -------------------------------------------------------------
    06. EINSATZGEBIET: DIGITALWORKROOM | MEHRFACH
        BESCHREIBUNG: Stellt das Augenbild auf on/off.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('.toggle-eye').live('click', function () {
    if (!$(this).hasClass('forum')) {
        if ($(this).hasClass('yes')) {
            $(this).removeClass('yes').addClass('no').parent().next('input').val('no');
        } else {
            $(this).removeClass('no').addClass('yes').parent().next('input').val('yes');
        }
    } else {
        if ($(this).hasClass('open')) {
            $(this).removeClass('open').addClass('closed').parent().next('input').val('closed');
        } else {
            $(this).removeClass('closed').addClass('open').parent().next('input').val('open');
        }
    }
});

/* -------------------------------------------------------------
    07. EINSATZGEBIET: DIGITALWORKROOM | CONTENT
        BESCHREIBUNG: .
        07.1 Entfernt die Bildergröße des ausgwählten Bildes.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function deleteImageSize(divID) {
    $('#' + divID + ' .img-size').removeAttr('class').addClass('img-size');
    $('#' + divID + ' .img-size-bg').children('input').val('');
}

/* -------------------------------------------------------------
        07.2. Entfernt das ausgewählte Bild.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function deleteImage(divID) {
    deleteImageSize(divID);

    // Leert die zum Bild dazugehörigen InputFelder.
    $('#' + divID + ' input[name*=\'_P_\']').each(function () {
        $(this).val('');
    });

    $('#' + divID).find('td').html('<img src="/pages/img/digitalworkroom/light-plus.gif" alt="[light-plus.gif]" />').closest('table').prev().hide().next().next().val('').next().val('');
}

/* -------------------------------------------------------------
        07.3. Verändert die Anzeigegröße des Bildes.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('.change-imagesize').live('click', function () {
    var divID = $(this).closest('div[id^=\'sortable-\']').attr('id');
    deleteImageSize(divID);
    $(this).children().removeClass('img-size-active').addClass('img-size' + ($('#' + divID + ' .img-size-bg a').index(this) + 1) + '-active').closest('div').children('input').val($('#' + divID + ' .img-size-bg a').index(this) + 1);
});

/* -------------------------------------------------------------
        07.4. Entfernt das ausgewählte Bild.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('.delete-image').live('click', function () {
    deleteImage($(this).closest('div[id^=\'sortable-\']').attr('id'));
});

/* -------------------------------------------------------------
    08. EINSATZGEBIET: DIGITALWORKROOM | MEHRFACH
        BESCHREIBUNG: Vervollständigt Datumseingaben.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(document).click(function () {
    $('input.ac-date').each(function () {

        if ($(this).val() !== '') {
            var element = $(this), dateLength = element.val().split('.').length, currentDate = new Date(), dateSplit, checkDate;

            if (dateLength === 2) {
                element.val(element.val() + '.' + currentDate.getFullYear());
            } else if (dateLength === 1) {
                element.val(element.val() + '.' + (currentDate.getMonth() + 1) + '.' + currentDate.getFullYear());
            }

            dateSplit = element.val().split('.');
            dateSplit[0] = (dateSplit[0].length === 1) ? '0' + dateSplit[0] : dateSplit[0];
            dateSplit[1] = (dateSplit[1].length === 1) ? '0' + dateSplit[1] : dateSplit[1];

            element.val(dateSplit[0] + '.' + dateSplit[1] + '.' + dateSplit[2]);
            checkDate = new Date(dateSplit[2], dateSplit[1] - 1, dateSplit[0]);

            if (((checkDate.getMonth() + 1) !== parseInt(dateSplit[1], 10)) || (checkDate.getDate() !== parseInt(dateSplit[0], 10)) || (checkDate.getFullYear() !== parseInt(dateSplit[2], 10))) {
                element.val('');
            }
        }
    });
});

/* -------------------------------------------------------------
    09. EINSATZGEBIET: DIGITALWORKROOM | MEHRFACH
        BESCHREIBUNG: Schließt Overlays.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(document).click(function (e) {
    if ($(e.target).closest('.overlay-closeable').length === 0 && $(e.target).closest('.overlay-open').length === 0) {
        $('.overlay-closeable').hide();
    }
});

/* -------------------------------------------------------------
    10. EINSATZGEBIET: WEBSEITIG | LAGERHAUS - WERBEMITTEL
        BESCHREIBUNG: Macht die Adressen zuordenbar.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {

    var li = $('#zentrale').next().find('li');

    li.hide().parent().append('<li id="missing-center" style="background: none;">Bitte eine Zentrale auswählen</li>');

    $('#zentrale select').change(function () {
        li.hide();
        li.filter('.kdnrzen' + $(this).val()).show();
        if ($(this).val() !== '') {
            $('#missing-center').hide();
        } else {
            $('#missing-center').show();
        }
    });

    li.click(function (e) {
        $('body').css('cursor', 'move').append('<div id="move-li" style="position: absolute; background-color: #FFFFD8; border: 1px solid #F7F2B2; padding: 5px;">' + $(this).html() + '</div>');
        $('#move-li').css({
            top: (e.pageY + 15) + 'px',
            left: (e.pageX + 15) + 'px'
        }).children(':eq(0)').css('font-size', '13px');
        $(document).mousemove(function (e) {
            $('#move-li').css({
                top: (e.pageY + 15) + 'px',
                left: (e.pageX + 15) + 'px'
            });
        });
    });

    $('#cart-form div.rechnungsadresse, #cart-form div.lieferadresse').children('fieldset').click(function () {
        if ($('#move-li').length > 0) {
            var moveLi = $('#move-li'), ask = true;

            if ($(this).parent().hasClass('rechnungsadresse')) {
                ask = confirm('Wollen Sie die ausgewählte Adresse wirklich als Ihre Rechnungsadresse verwenden?');
            }
            if (ask === true) {
                $(this).children(':eq(1)').val(moveLi.children(':eq(0)').text()).nextAll(':eq(1)').val(moveLi.find('.st-strasse').text()).nextAll(':eq(1)').val(moveLi.find('.st-plz').text()).nextAll(':eq(1)').val(moveLi.find('.st-ort').text());
            }
            removeClick();
        }
    });

    function removeClick() {
        $('body').removeAttr('style').children('#move-li').remove();
    }

    $(document).dblclick(function () {
        removeClick();
    }).mousedown(function (e) {
        if (e.button === 2) {
            removeClick();
        }
    }).keydown(function (e) {
        if (e.keyCode === 27) {
            removeClick();
        }
    });
});

/* -------------------------------------------------------------
    11. EINSATZGEBIET: WEBSEITIG | MEHRFACH
        BESCHREIBUNG: Ermöglicht es via CSS Files Wrapper um HTML Konstrukte zu legen.
        USAGE:
            .klasse .wrap-klasse {}
            .klasse .innerwrap-klasse {}
            .klasse .nextwrap-klasse {}
            .klasse .prevwrap-klasse {}
            .klasse .nextelement-klasse {}
            .klasse .prevelement-klasse {}
            .klasse .innerelement-klasse {}
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {
    jbox.ready(function() {
        if (typeof wrapperPath !== 'undefined' && wrapperPath !== "") {
            $.get(wrapperPath, function (data) {
                var match = data.match(/\.[\w-]+\s*\.*[\w-]*\s+\.wrap-[\w-]+/g);
                if (match) {
                    $.each(match, function (index, value) {
                        $(value.split(' .wrap-')[0]).wrap('<div class="' + value.split(' .wrap-')[1] + '" />');
                    });
                }
                match = data.match(/\.[\w-]+\s*\.*[\w-]*\s+\.innerwrap-[\w-]+/g);
                if (match) {
                    $.each(match, function (index, value) {
                        $(value.split(' .innerwrap-')[0]).wrapInner('<div class="' + value.split(' .innerwrap-')[1] + '" />');
                    });
                }
                match = data.match(/\.[\w-]+\s*\.*[\w-]*\s+\.nextwrap-[\w-]+/g);
                if (match) {
                    $.each(match, function (index, value) {
                        $(value.split(' .nextwrap-')[0]).next().wrap('<div class="' + value.split(' .nextwrap-')[1] + '" />');
                    });
                }
                match = data.match(/\.[\w-]+\s*\.*[\w-]*\s+\.prevwrap-[\w-]+/g);
                if (match) {
                    $.each(match, function (index, value) {
                        $(value.split(' .prevwrap-')[0]).prev().wrap('<div class="' + value.split(' .prevwrap-')[1] + '" />');
                    });
                }
                match = data.match(/\.[\w-]+\s*\.*[\w-]*\s+\.nextelement-[\w-]+/g);
                if (match) {
                    $.each(match, function (index, value) {
                        $(value.split(' .nextelement-')[0]).after('<div class="' + value.split(' .nextelement-')[1] + '" />');
                    });
                }
                match = data.match(/\.[\w-]+\s*\.*[\w-]*\s+\.prevelement-[\w-]+/g);
                if (match) {
                    $.each(match, function (index, value) {
                        $(value.split(' .prevelement-')[0]).before('<div class="' + value.split(' .prevelement-')[1] + '" />');
                    });
                }
                match = data.match(/\.[\w-]+\s*\.*[\w-]*\s+\.innerelement-[\w-]+/g);
                if (match) {
                    $.each(match, function (index, value) {
                        $(value.split(' .innerelement-')[0]).prepend('<div class="' + value.split(' .innerelement-')[1] + '" />');
                    });
                }
            });
        }
    });
});

/* -------------------------------------------------------------
    12. EINSATZGEBIET: WEBSEITIG | 10OEAMTC
        BESCHREIBUNG: Erstellt aus normalen HTML Tabellen vergleichbare Tabellen.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function switchTableEntry($this) {

    var table = $($this).closest('table'), selectIndex = $('#' + table.closest('div[id]').attr('id') + ' select').index($this) + 1, i = 0, optionsLength = $($this).children().length;

    table.find('tr:gt(0)').each(function () {
        if ($(this).children(':eq(1)').length > 0) {
            $(this).children(':eq(' + selectIndex + ')').text(table.data()[i + $($this).attr('selectedIndex')]);
            i = i + optionsLength;
        }
    });
}

function makeTableComparable() {

    $('#content div.comparison table[class*=table-id]:visible:not(.used)').each(function () {

        var $this = $(this), selectEntries = $.map($this.find('tr:eq(0)').find('td:gt(0)'), function (value, index) {
            return $(value).text();
        }), selectEntriesValue = $.map($this.find('tr:gt(0)').find('td:gt(0)'), function (value, index) {
            return $(value).text();
        }), options;

        $this.data(selectEntriesValue);

        $.each(selectEntries, function (index, value) {
            options += '<option value="' + value + '">' + value + '</option>';
        });

        $this.addClass('used').find('tr').find('td:gt(2)').hide();
        $this.find('tr:eq(0)').children(':eq(1)').html('<select onchange="switchTableEntry(this)">' + options + '</select>').next().html('<select onchange="switchTableEntry(this)">' + options + '</select>');
        $this.find('select:eq(1)').children(':eq(1)').attr('selected', 'selected');
    });
}

/* -------------------------------------------------------------
    13. EINSATZGEBIET: WEBSEITIG | MEHRFACH.
        BESCHREIBUNG: .
        13.1 Entfernt den Pretext der Inputs beim Abschicken.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function clearPretext() {
    $('input:text').each(function () {
        if ($(this).hasClass('pretextstyle')) {
            $(this).val('');
        }
    });
}

/* -------------------------------------------------------------
        13.2. Setzt die Pretexte in die Inputs ein.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {
    $('input.pretext').each(function () {
        $(this).addClass('pretextstyle').val($(this).attr('title'));
        if ($(this).is(':password')) {
            $(this).replaceWith('<input id="' + $(this).attr('id') + '" class="' + $(this).attr('class') + '" type="text" title="' + $(this).attr('title') + '" maxlength="' + $(this).attr('maxlength') + '" value="' + $(this).val() + '" name="' + $(this).attr('name') + '"');
        }
    });
});

/* -------------------------------------------------------------
        13.3. Löscht den Dummytext des Inputfeldes beim fokusieren und setzt ihn beim Verlassen, sofern der Value leer ist, wie ein.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('input.pretext').live('click', function () {
    if ($(this).hasClass('pretextstyle')) {
        $(this).removeClass('pretextstyle').val('').focus();
        if ($(this).attr('id').match(/password/)) {
            $(this).replaceWith('<input id="' + $(this).attr('id') + '" class="' + $(this).attr('class') + '" type="password" title="' + $(this).attr('title') + '" maxlength="' + $(this).attr('maxlength') + '" value="' + $(this).val() + '" name="' + $(this).attr('name') + '"');
        }
        $('#' + $(this).attr('id')).focus();
    }

    $('input.pretext').blur(function () {
        if ($(this).val() === '') {
            $(this).addClass('pretextstyle').val($(this).attr('title'));
            if ($(this).is(':password')) {
                $(this).replaceWith('<input id="' + $(this).attr('id') + '" class="' + $(this).attr('class') + '" type="text" title="' + $(this).attr('title') + '" maxlength="' + $(this).attr('maxlength') + '" value="' + $(this).val() + '" name="' + $(this).attr('name') + '"');
            }
        }
    });
});

/* -------------------------------------------------------------
    14. EINSATZGEBIET: Noch keines.
        BESCHREIBUNG: Noch keine vorhanden.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

//$(function () {
//    $('.tabnavigation').children('li').click(function () {
//        $('#tabset' + $(this).parent().attr('id').match(/\d+/)).children('li').hide().end().children('li:eq(' + $('#' + $(this).parent().attr('id') + ' li').index(this) + ')').show();
//    });
//});

/* -------------------------------------------------------------
    15. EINSATZGEBIET: WEBSEITIG | k.A.
        BESCHREIBUNG: Gibt den Bereichen die Anzahl der dazugehörigen Forumseinträgen zurück.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

//$(function () {
//    $.getJSON('comments.php', function (data) {
//
//    var comment;
//
//        $.each(data, function (index, value) {
//
//            comment = (parseInt(value, 10) === 1) ? ' Kommentar' : ' Kommentare';
//            $('#forumeintrag-' + index).text(value + comment);
//        });
//    });
//});

/* -------------------------------------------------------------
    16. EINSATZGEBIET: DIGITALWORKROOM | BOXEN
        BESCHREIBUNG: .
        16.1 .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function toggleLinkType(thisSelect) {

    var val = $(thisSelect).val();

    if (val === 'channel' || val === 'more' || val === 'dynamic') {
        $(thisSelect).next().show();
    } else {
        $(thisSelect).next().hide().next().hide();
    }
}

/* -------------------------------------------------------------
        16.2. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function changeLinkPath(thisSelect) {

    var thisSelectParent = $(thisSelect).parent(), value = $(thisSelect).val();

    if (thisSelectParent.hide().prevAll('select').val() !== 'dynamic') {
        thisSelectParent.prevAll('input').val(value);
    } else {
        thisSelectParent.prevAll('input').val('id_key=' + value.slice(value.lastIndexOf(',') + 1) + '&count=3');
    }
}

/* -------------------------------------------------------------
        16.3. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function changeLinkPath2(thisSelect) {
    $(thisSelect).parent().hide().prevAll('input').val($(thisSelect).val());
}

/* -------------------------------------------------------------
        16.4. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('.toggle-linktypeselection a').live('click', function () {
    $(this).parent().next().toggle();
});

/* -------------------------------------------------------------
    17. EINSATZGEBIET: GLOBAL | GLOBAL.
        BESCHREIBUNG: .
        17.1 .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function toggleSectionByName(thisSelect, target) {
    $('input[name*=' + target + ']').val($(thisSelect).find('option:selected').text());
}

// legacy
function toggleSection(thisSelect, target) {
    $('input[name*=' + target + ']').val($(thisSelect).find('option:selected').text());
}

/* -------------------------------------------------------------
        17.2. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function toggleSectionByValue(thisSelect, target) {
    $('input[name*=' + target + ']').val($(thisSelect).val());
}

/* -------------------------------------------------------------
        17.3. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function toggleClassByAttribute(thisSelect, target, caseJSON, off) {

    var newClass = (typeof caseJSON[$(thisSelect).val()] !== 'undefined') ? caseJSON[$(thisSelect).val()] : off, classArray = $('#' + target).attr('class').split(' ');

    classArray.pop();
    $('#' + target).attr('class', classArray.join(' ')).addClass(newClass);
}

/* -------------------------------------------------------------
        17.4. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function toggleClassByValue(thisSelect, target) {

    var classArray = $('#' + target).attr('class').split(' ');

    classArray.pop();
    $('#' + target).attr('class', classArray.join(' ')).addClass($(thisSelect).val().toLowerCase());
}

/* -------------------------------------------------------------
        17.5. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

var arrayStatus = [];

function saveCookie(target, state) {
    if (state === 1) {
        arrayStatus.push('save::' + target);
    } else {
        arrayStatus = $.grep(arrayStatus, function (value) {
            return (value !== 'save::' + target);
        });
    }
    document.cookie = arrayStatus;
}

/* -------------------------------------------------------------
        17.6. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function loadClass() {
    if (document.cookie) {
        $.each(document.cookie.split(';'), function (index, value) {
            if (value.match(/save::/)) {
                $.each(value.split(','), function (index, val) {
                    $('#' + val.split('::')[1]).removeClass('show').addClass('hide');
                    $('.' + $.trim(val).split('::')[1]).show();
                    arrayStatus.push($.trim(val));
                });
                return false;
            }
        });
    }
}

/* -------------------------------------------------------------
        17.7. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function exchangeClass(target, class1, class2) {

    if ($('#' + target).hasClass(class1)) {
        $('#' + target).removeClass(class1).addClass(class2);
        saveCookie(target, 1);
    } else {
        $('#' + target).removeClass(class2).addClass(class1);
        saveCookie(target, 2);
    }
}

function exchangeClass2(target, addClass, removeClass) {
    var classArray = $.grep($('#' + target).attr('class').split(' '), function (value) {
        return !value.match(removeClass);
    });
    $('#' + target).removeAttr('class').attr('class', classArray.join(' ')).addClass(addClass);
}

/* -------------------------------------------------------------
        17.8. .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function toggleVisibility(target) {
    $('#' + target).toggle();
}

/* -------------------------------------------------------------
    18. EINSATZGEBIET: WEBSEITIG | k.A.
        BESCHREIBUNG: Zeigt ein LoaderBild an bis die Jbox den Content geladen hat.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('#searchbutton').live('click', function () {
    $(this).parent().after('<img class="loader" style="margin: 40px;" src="/pages/img/portal/loading.gif" title="loading" alt="loading" />');
    jbox['dv-content'].ready(function () {
        $('img.loader').remove();
    });
    jbox['ldb-content'].ready(function () {
        $('img.loader').remove();
    });
});

/* -------------------------------------------------------------
    19. EINSATZGEBIET: WEBSEITIG | 10OEAMTC - LÄNDERDATENBANK
        BESCHREIBUNG: .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('div.erw-kontinent li').live('click', function () {

    var classArray = $('div.erw-laender').attr('class').split(' ');

    classArray.pop();
    $(this).parent().find('li.selected').removeClass('selected');
    $('div.erw-laender').attr('class', classArray.toString().replace(/,/g, ' ')).addClass($(this).addClass('selected').text().toLowerCase()).find('li.selected').removeClass('selected');
});

$('div.erw-kategorie li, div.erw-laender li').live('click', function () {
    if ($(this).closest('ul').children('li.selected').length < parseInt($(this).parent().attr('class').match(/\d+/), 10) || $(this).hasClass('selected')) {
        $(this).toggleClass('selected');
    } else {
        alert('Sie haben schon ' + $(this).parent().attr('class').match(/\d+/) + ' Einträge ausgewählt.');
    }
});

function writeInputHidden2() {
    $('div.ldb-selection input').val('');
    $('div.erw-kategorie li.selected, div.erw-laender ul:visible li.selected, div.erw-kontinent li.selected').each(function () {
        $(this).closest('div').children('input').val($(this).closest('div').children('input').val() + $(this).attr('id').split('-')[1].substr(1) + ',');
    });
}

//$(function () {
//
//    function makeOptions($this, idKey) {
//
//        var stdQry = 'ID_KEY, STATUS_PUB, DATE_ONLINE, DATE_OFFLINE, DOC_TYPE', stdOpr = 'eq, eq, le, gt, eq', stdVal = idKey + ', on, checkdate, checkdate, content', stdView, attributes = $this.closest('form').attr('class').split(' '), options  = $.map(attributes, function (value) {
//            return (value.match(/option-/) ? '[' + value.replace(/option-/, '') + ']' : null);
//        }), fields = $.map(attributes, function (value) {
//            return (value.match(/element-/) ? value.replace(/element-/, '') : null);
//        }), blackList = ['welt'], sendData, oldValue = $this.val();
//
//        $this.parent().children().each(function (index) {
//            if ($this.get(0) !== this) {
//                stdQry += ',' + fields[index];
//                stdOpr += ',eq';
//                stdVal += ',' + $(this).val();
//            } else {
//                stdView = fields[index];
//                return false;
//            }
//        });
//
//        sendData = { 'mlay_id' : 20001, 'xmlval__MY_QUERY[0]' : stdQry, 'xmlval__MY_OPERATOR[0]' : stdOpr, 'xmlval__MY_VALUE[0]' : stdVal, 'xmlval__MY_VIEW[0]' : stdView };
//
//        $.post('index.php', sendData, function (data) {
//
//            $this.empty();
//            if (typeof data.options !== 'undefined' && data.options.length > 0) {
//                $.each(data.options, function (index, value) {
//                    if (!blackList.inArray(value)) {
//                        if (value !== '') {
//                            $this.append('<option value="' + value + '">' + value + '</option>');
//                        } else {
//                            $this.append('<option value="[leer]">' + options[parseInt($this.attr('id').split('-')[1], 10)] + '</option>');
//                        }
//                    }
//                });
//            }
//
//            if ($this.find('option[value=' + oldValue + ']').length > 0) {
//                $this.find('option[value=' + oldValue + ']').attr('selected', 'selected').change();
//            } else {
//                $this.nextAll().attr('disabled', 'disabled').empty();
//            }
//        }, 'json');
//    }
//
//    $('form[id^=ajaxkeyword]').each(function () {
//
//        var $form = $(this), idKey = $form.find('input[name=xmlval_ID_KEY[0]]').val();
//
//        makeOptions($form.find('select.ajaxselect:eq(0)'), idKey);
//
//        $form.find('select.ajaxselect').change(function () {
//
//            var $this = $(this), nextSelect = $this.next();
//
//            if (nextSelect.length > 0 && $this.val() !== '[leer]') {
//                makeOptions(nextSelect.removeAttr('disabled'), idKey);
//            } else {
//                $this.nextAll().attr('disabled', 'disabled').empty();
//            }
//        });
//
//        $form.find('a[id^=ajaxkeywordbutton]').click(function () {
//            $form.submit();
//        });
//
//        $form.find('a[id^=ajaxkeyworddelete]').click(function () {
//            $form.find('select.ajaxselect:eq(0) option:eq(0)').attr('selected', 'selected').change();
//        });
//    });
//});

/* -------------------------------------------------------------
    20. EINSATZGEBIET: WEBSEITIG | 10OEAMTC - 12500
        BESCHREIBUNG: Toggle DIV + Speichern.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('#head div.erweitert a').live('click', function () {

    if ($(this).parent().next().is(':hidden')) {
        saveCookie('ft-infothek', 1);
    } else {
        saveCookie('ft-infothek', 2);
    }
    $(this).parent().next().toggle();
});

$('div.close a').live('click', function () {
    $(this).closest('div.ft-at').hide();
});

/* -------------------------------------------------------------
    21. EINSATZGEBIET: - | 10OEAMTC
        BESCHREIBUNG: folgt noch.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {

    $('div.someid ul li').live('click', function () {
        if ($(this).parent().next('ul').length === 1) {
            var $this = $(this), targetUL = $this.parent().next();
            if (!targetUL.next().val().match($this.attr('id'))) {
                targetUL.children('li.emptylist').remove();
                if ($this.data('init') !== true) {
                    targetUL.prepend('<li class="' + $this.attr('id') + '">' + $this.addClass('clicked').html().split('<div')[0] + '</li>');
                } else {
                    targetUL.append('<li class="' + $this.attr('id') + '">' + $this.addClass('clicked').html().split('<div')[0] + '</li>');
                    $this.data('init', false);
                }
                targetUL.next().val(targetUL.next().val() + $this.attr('id') + ';');
            }
        }
    });

    $('div.someid ul li:not(.emptylist)').live('dblclick', function () {
        if ($(this).parent().next('ul').length === 0) {
            var $this = $(this), inputHidden = $this.parent().next();
            inputHidden.val(inputHidden.val().replace($this.attr('class') + ';', ''));
            $('#' + $this.attr('class')).removeClass('clicked');
            if ($this.parent().children().length > 1) {
                $this.remove();
            } else {
                $this.text('Leer').removeAttr('class').addClass('emptylist');
            }
        }
    });

    $('.delete-all').live('dblclick', function () {
        $(this).parent().next().find('ul:eq(1)').html('<li class="emptylist">Leer</li>').next().val('').prev().prev().find('.clicked').removeClass('clicked');
    });

    jbox.ready(function () {
        if ($('#mainedit').length > 0) {
            $('div.someid li.clicked').each(function () {
                var $this = $(this), targetUL = $this.parent().next();
                if (!targetUL.next().val().match($this.attr('id'))) {
                    targetUL.children('li.emptylist').remove();
                    targetUL.append('<li class="' + $this.attr('id') + '">' + $this.addClass('clicked').html().split('<div')[0] + '</li>');
                    targetUL.next().val(targetUL.next().val() + $this.attr('id') + ';');
                }
            });
            jbox['mainedit'].ready(function () {
                $('div.someid li.clicked').each(function () {
                    var $this = $(this), targetUL = $this.parent().next();
                    if (!targetUL.next().val().match($this.attr('id'))) {
                        targetUL.children('li.emptylist').remove();
                        targetUL.append('<li class="' + $this.attr('id') + '">' + $this.addClass('clicked').html().split('<div')[0] + '</li>');
                        targetUL.next().val(targetUL.next().val() + $this.attr('id') + ';');
                    }
                });
            }, true);
        }
    });
});

/* -------------------------------------------------------------
    22. EINSATZGEBIET: DIGITALWORKROOM | 10OEAMTC - BACKOFFICE
        BESCHREIBUNG: Markierung aller Checkboxen.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {

    $('#dw-mdoc input.radio:eq(0)').live('click', function () {
        if ($(this).is(':checked')) {
            $('#dw-mdoc input.radio:gt(0)').attr('checked', 'checked');
        } else {
            $('#dw-mdoc input.radio:gt(0)').removeAttr('checked');
        }
    });
});

/* -------------------------------------------------------------
    23. EINSATZGEBIET: WEBSITE | 10OEAMTC
        BESCHREIBUNG: Abfangen der Entertaste und submit ausführen.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {
    $('#fulltext').keydown(function (event) {
        if (event.keyCode == '13') {
            $(this).next().children().click();
        }
    });
});


/* -------------------------------------------------------------
    24. EINSATZGEBIET: WEBSITE | 10OEAMTC
        BESCHREIBUNG: Zurückbutton
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$('#link-back').live('click', function () {
    
    $('#simplesearch').append('<input type="hidden" value="'+($.url.param("backlink") !== '' ? $.url.param("backlink") : '0' )+'" name="start">').submit();
});

$(function () {

    if ($.url.param('backlink') !== '') {
        $('#content div.content-functions').append('<a id="link-back" href="javascript:void(0);" title="Zurück zum Suchergebnis">Zurück zum Suchergebnis</a>');
    }

    jbox.ready(function () {
        if ('knot' in jbox) {
//            jbox['knot'].ready(function () {
//                if ($('#content').hasClass('volltext')) {
//                    if ($.url.param('backlink') !== '') {
//                        $('#search_start').val($.url.param('backlink'));
//                        window.setTimeout("$('#search_form_submit').click();", 1);
//                    }
//                }
//            });
            jbox['knot'].ready(function () {
                if ($('#content').hasClass('volltext')) {
                    $('#knot h3 a').each(function () {
						var tmp = ($('#knot a.active:eq(0)').text() - 1);
						if(tmp < 0 )
							tmp = 0;
                        $(this).attr('href', $(this).attr('href') + '&backlink=' + tmp);
                    });
                }
            }, true);
        }
    });
});

/* -------------------------------------------------------------
    END +/- DOKUMENTIERT UND STABIL
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

//$(function () {
//    jbox.ready(function () {
//        if ($('object[id*=_api]').length > 0) {
//            $('object[id*=_api]').append('<param name="wmode" value="opaque" />');
//            $('object[id*=_api]').closest('ul').css('position', 'relative');
//            $('#moviedisplay').css('position', 'relative');
//        }
//    });
//});

/* -------------------------------------------------------------
    04. EINSATZGEBIET: DIGITALWORKROOM | ANGEBOTE
        BESCHREIBUNG: Folgt noch. ;-)
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function toggleArticleType(thisSelect) {
    $(thisSelect).closest('.container').removeAttr('class').attr('class', 'container clear ' + $(thisSelect).val()).parent().nextAll(':eq(1)').removeAttr('class').attr('class', 'article ' + $(thisSelect).val()).find('div[id^=\'sortable-\']').each(function () {
        if ($(this).attr('id').match(/\-area\d+/).toString() !== '-area1') {
            $(this).remove();
        }
    });
}

$(function () {
    if ($('#dw-keyword-a').length > 0) {
        $('div[id^=dw-keyword-a]:not(#dw-keyword-a)').hide();
    }

    $('#dw-keyword-a input:radio').live('click', function () {
        $('div[id^=dw-keyword-a]:not(#dw-keyword-a)').hide();
        $('#dw-keyword-aa' + $(this).val()).show().find('h2').html('Unterkategorie - ' + $(this).next().html());
    });
});

/* -------------------------------------------------------------
    06. EINSATZGEBIET: GLOBAL | GLOBAL
        BESCHREIBUNG: .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function toggleClass(thisSelect, target, caseJSON, off) {

    var selectedVal = $('.ansprechpartner-search span:visible select option:selected').val(), isStandart = (typeof caseJSON[$(thisSelect).val()] !== 'undefined') ? ' ' + caseJSON[$(thisSelect).val()] : ' ' + off;
    $('#' + target).removeAttr('class').attr('class', 'container clear' + isStandart);

    if ($('.ansprechpartner-search span:visible select option[value=' + selectedVal + ']').length > 0) {
        $('.ansprechpartner-search span:visible select option[value=' + selectedVal + ']').attr('selected', 'selected');
    } else {
        $('.ansprechpartner-search span:visible select option:eq(0)').attr('selected', 'selected');
    }
}

//.................
$(function () {

    var postData = ($('#post-data').length > 0) ? $('#post-data').html().split(' ') : '', location = (postData[0] === 'Kärnten') ? postData[1] : postData[2], i;

    if (postData[0] === '') {
        i = postData[3];
    } else if (postData[0] === 'Kärnten') {
        i = postData[4];
    } else {
        i = postData[5];
    }

    $('.st-bld option[value=' + postData[0] + ']').attr('selected', 'selected').change().parent().nextAll('span:visible').find('option[value=' + location + ']').attr('selected', 'selected');
    $('.ansprechpartner-search span:visible option[value=' + i + ']').attr('selected', 'selected');

    $('div.bereich-more').hide();

    $('div.weitere a').click(function (e) {
        if ($(this).children('span').html() !== 'schließen') {
            $(this).children('span').html('schließen').parents(':eq(2)').next().show();
        } else {
            $(this).children('span').html('jetzt anzeigen').parents(':eq(2)').next().hide();
        }
    });

    $('.standort-search input').focus(function () {
        $('.st-bld option:eq(0)').attr('selected', 'selected').parent().nextAll('span:visible').find('option:eq(0)').attr('selected', 'selected');
    });

    $('.standort-search select').change(function () {
        if ($(this).hasClass('st-umkr')) {
            $('.st-bld option:eq(0)').attr('selected', 'selected').parent().nextAll('span:visible').find('option:eq(0)').attr('selected', 'selected');
        } else {
            $('.st-umkr option:eq(0)').attr('selected', 'selected').parent().prev().val('');
        }
    });
});

/* -------------------------------------------------------------
    .
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function checkSelect() {
    if ($('select[name=\'xmlval_KEYWORD_OPTIONS[0]\']').length > 0 && $('select[name=\'xmlval_KEYWORD_OPTIONS[0]\'] option:selected').val() === '') {
        $('select[name=\'xmlval_KEYWORD_OPTIONS[0]\']').css('background-color', 'red');
        return false;
    } else {
        return true;
    }
}

function changeStatus(thisSelect) {
    if ($(thisSelect).val() === 'off') {
        var currentDate = new Date();
        $('#dw-informationen input[name=\'xmlval_DATE_OFFLINE[0]\']').val(currentDate.getDate() + '.' + (currentDate.getMonth() + 1) + '.' + currentDate.getFullYear());
        $('#dw-funktionen input[name*=\'[F_VON]\']').each(function () {
            if ($(this).val() !== '') {
                $(this).nextAll('input').val(currentDate.getDate() + '.' + (currentDate.getMonth() + 1) + '.' + currentDate.getFullYear());
            }
        });
    }
}

$('#EHRENGAST').live('change', function () {
    $('#GEBURTSDATUM').toggleClass('required').removeClass('error-required');
    if ($(this).val() === 'ja') {
        $('#GEBURTSDATUM').prev().text($('#GEBURTSDATUM').prev().text().replace(/ \*/, ''));
    } else {
        $('#GEBURTSDATUM').prev().text($('#GEBURTSDATUM').prev().text() + ' *');
    }
});

//..........
function changeFunction(thisSelect) {

    if ($(thisSelect).parent().nextAll(':eq(1)').val() === '') {
        var currentDate = new Date();
        $(thisSelect).parent().nextAll(':eq(1)').val(currentDate.getDate() + '.' + currentDate.getMonth() + '.' + currentDate.getFullYear());
    }
}

//..........
$('#dw-laz input:checkbox').live('click', function () {
    changeFunction($(this));
});

//..........
function autocomplete(id) {
    if ($('#' + id).val() !== '') {
        $('#plz-result').find('li').hide();
        $('#plz-result').show().find('li[class*=-' + $('#' + id).val().toLowerCase() + ']').show();
    }
}

$('#plz-result li').live('click', function () {
    $('#POSTLEITZAHL').val($(this).children(':eq(0)').html());
    $('#ORT').val($(this).children(':eq(1)').html());
    $('#plz-result').hide().find('li').hide();
});

//..........
function saveStatus() {
    var active = $('#dw-link').attr('class').split(' ').pop();
    active = active.substr(0, 1).toUpperCase() + active.substr(1);
    jbox['mainedit'].ready(function () {
        $('#dw-link h2:contains(' + active + ')').click();
    });
}
