/* -------------------------------------------------------------
    AUTHOR: Stefan Nafra
    UPDATED: 22.09.2010
    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 () {
    var arrayFilter = [];

    $('ul.sortable li').hide();

    $(this).parent().children('input.filter:checked').each(function () {
        arrayFilter.push($(this).val());
    });

    if (arrayFilter.join('.') !== '') {
        $('ul.sortable li.' + arrayFilter.join('.')).show();
    } else {
        $('.d-setnr1:eq(0)').click();
    }
});

/* -------------------------------------------------------------
        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/)) {
        $('div#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 divID = $(this).closest('div[id^=\'sortable-\']').attr('id'), formElementLength, i;

    if ($('#' + divID + ' .area-full').is(':hidden')) {
        $('#' + divID + ' .toggle-link').html('&Uuml;bernehmen');
    } else {
        $('#' + divID + ' .toggle-link').html('Bearbeiten');

        if (!$(this).parents(':eq(1)').hasClass('toggle-download-area')) {
            $('#' + divID + ' .area-short .formElement1').html('<a href="parse.php?id=2500,' + $('#' + divID + ' .area-full .formElement1 input').val() + '" title="" target="_blank">' + $('#' + divID + ' .area-full .formElement1 input').val() + '</a>');
        } else {
            $('#' + divID + ' .area-short .formElement1').html('<a href="http://dw6.testit.at/mmedia/download' + $('#' + divID + ' .area-full .formElement1 input').val().replace(/download=/, '') + '" title="" target="_blank">' + $('#' + divID + ' .area-full .formElement1 input').val() + '</a>');
        }

        formElementLength = $('#' + divID + ' .area-short div[class*=\'formElement\']').length;
        for (i = 2; i <= formElementLength; i += 1) {
            if ($('#' + divID + ' .area-full .formElement' + i + ' input:text').length > 0) {
                $('#' + divID + ' .area-short .formElement' + i).html($('#' + divID + ' .area-full .formElement' + i + ' input').val());
            } else {
                $('#' + divID + ' .area-short .formElement' + i).html($('#' + divID + ' .area-full .formElement' + i + ' select option:selected').html());
                $('#' + divID + ' .area-short .formElement' + i).html($('#' + divID + ' .area-full .formElement' + i + ' input:checked').parent().text());
            }
        }
    }
    $('#' + divID + ' .area-short').toggle();
    $('#' + divID + ' .area-full').toggle();
});

/* -------------------------------------------------------------
    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').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 h3 .wrap-klasse {}
            .klasse h3 .innerwrap-klasse {}
            .klasse h3 .nextwrap-klasse {}
            .klasse h3 .prevwrap-klasse {}
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$(function () {

    if (typeof wrapperPath !== 'undefined') {
        $.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] + '" />');
                });
            }
        });
    }
});

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

var selectEntriesValue;

function switchTableEntry($this) {

    var table = $($this).closest('table'), i = 1, x = 0, optionsLength = $($this).children().length;

    for (i; i <= optionsLength; i += 1) {
        table.find('tr:eq(' + i + ')').children(':eq(' + ($('#content table.table-id select').index($this) + 1) + ')').text(selectEntriesValue[x + $($this).attr('selectedIndex')]);
        x = x + optionsLength;
    }
}

function makeTableComparable() {
    if (!$('#content table.table-id').hasClass('used')) {

        var table = $('#content table.table-id'), selectEntries = $.map(table.find('tr:eq(0)').find('td:gt(0)'), function (value, index) {
            return $(value).text();
        }), options = '';

        selectEntriesValue = $.map(table.find('tr:gt(0)').find('td:gt(0)'), function (value, index) {
            return $(value).text();
        });

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

        table.addClass('used').find('tr').find('td:gt(2)').hide();
        table.find('tr:eq(0)').children(':eq(1)').html('<select onchange="switchTableEntry(this)">' + options + '</select>').next().html('<select onchange="switchTableEntry(this)">' + options + '</select>');
        table.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();
    });
});

/* -------------------------------------------------------------
    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) + ',');
    });
}

/* -------------------------------------------------------------
    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();
});

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

/* -------------------------------------------------------------
    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('');
        }
    });
});

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

/* Statistik 2001 Script */

$(function () {

    // Ajaxcalls miteinbeziehen in der Statistik.
    $('.track').live('click', function () {
        if (typeof i !== 'undefined') {
            $.get(i.src + '&c=' + Math.floor(Math.random() * 101));
        }
        if (typeof OEWA !== 'undefined' && typeof r !== 'undefined' && typeof t !== 'undefined') {
            $('body').append('<img width="0" height="0" src="' + OEWA + '?r=' + r + '&d=' + t + '&c=' + Math.floor(Math.random() * 101) + '" />');
        }
        if (typeof stat_ajaxcall !== 'undefined') {
            $('body').append('<img width="0" height="0" src="' + stat_ajaxcall + '" />');
	 }
    });

    // Beim Seitenladen ein Statistikaufruf machen.
    if (typeof stat_url !== 'undefined') {
        $('body').append('<img width="0" height="0" src="' + stat_url + '" />');
    }

    // Bannerzählen.
    var typ = {};

    $('a[class*=stat-]').each(function () {
        var advertisement = $.map($(this).attr('class').split(' '), function (value) {
            return value.match(/stat-\D+/);
        });
        advertisement = advertisement.toString().replace(/stat-/, '');
        if (advertisement !== '') {
            typ[advertisement] = (typ[advertisement] === undefined) ? 1 : typ[advertisement] + 1;
        }
    });

    // Banneraufrufe an die Statistik weiterleiten.
    if (typeof banner_info !== 'undefined') {
        $('body').append('<img width="0" height="0" src="' + banner_info + '&params=' + $.param(typ).replace(/=/g, ':').replace(/&/g, ',') + '&type=1" />');
    }

    // Bannerclick miteinbeziehen in der Statistik.
    $('a[class*=stat-]').live('click', function () {
        if (typeof banner_info !== 'undefined') {
            var banner = $.map($(this).attr('class').split(' '), function (value) {
                return value.match(/stat-\D+/);
            });
            $('body').append('<img width="0" height="0" src="' + banner_info + '&banner=' + banner.toString().replace(/stat-/, '') + '&type=2" />');
        }
    });

    // Downloads miteinbeziehen in der Statistik.
    $('a[class*=download-]').live('click', function () {
        if (typeof download_info !== 'undefined') {
            var download = $.map($(this).attr('class').split(' '), function (value) {
                return value.match(/download-\D+/);
            });
            $('body').append('<img width="0" height="0" src="' + download_info + '&download=' + download.toString().replace(/stat-/, '') + '&type=3" />');
        }
    });
});

/* End Statistik 2011 Script */

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() + ' *');
    }
});

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

//..........
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());
    }
}

//..........
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();
    });
}
