/*-- innerfade --*/

    (function($) {
    
        $.fn.innerfade = function(options) {
            return this.each(function() {   
                $.innerfade(this, options);
            });
        };
    
        $.innerfade = function(container, options) {
            var settings = {
                'animationtype':    'fade',
                'speed':            'normal',
                'type':             'sequence',
                'timeout':          2000,
                'containerheight':  'auto',
                'runningclass':     'innerfade',
                'children':         null
            };
            if (options)
                $.extend(settings, options);
            if (settings.children === null)
                var elements = $(container).children();
            else
                var elements = $(container).children(settings.children);
            if (elements.length > 1) {
                $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
                for (var i = 0; i < elements.length; i++) {
                    $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
                };
                if (settings.type == "sequence") {
                    setTimeout(function() {
                        $.innerfade.next(elements, settings, 1, 0);
                    }, settings.timeout);
                    $(elements[0]).show();
                } else if (settings.type == "random") {
                        var last = Math.floor ( Math.random () * ( elements.length ) );
                    setTimeout(function() {
                        do {
                                                    current = Math.floor ( Math.random ( ) * ( elements.length ) );
                                            } while (last == current );             
                                            $.innerfade.next(elements, settings, current, last);
                    }, settings.timeout);
                    $(elements[last]).show();
                            } else if ( settings.type == 'random_start' ) {
                                    settings.type = 'sequence';
                                    var current = Math.floor ( Math.random () * ( elements.length ) );
                                    setTimeout(function(){
                                        $.innerfade.next(elements, settings, (current + 1) %  elements.length, current);
                                    }, settings.timeout);
                                    $(elements[current]).show();
                            }   else {
                                alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
                            }
                    }
        };
    
        $.innerfade.next = function(elements, settings, current, last) {
            if (settings.animationtype == 'slide') {
                $(elements[last]).slideUp(settings.speed);
                $(elements[current]).slideDown(settings.speed);
            } else if (settings.animationtype == 'fade') {
                $(elements[last]).fadeOut(settings.speed);
                $(elements[current]).fadeIn(settings.speed, function() {
                    removeFilter($(this)[0]);
                });
            } else
                alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
            if (settings.type == "sequence") {
                if ((current + 1) < elements.length) {
                    current = current + 1;
                    last = current - 1;
                } else {
                    current = 0;
                    last = elements.length - 1;
                }
            } else if (settings.type == "random") {
                last = current;
                while (current == last)
                    current = Math.floor(Math.random() * elements.length);
            } else
                alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
            setTimeout((function() {
                $.innerfade.next(elements, settings, current, last);
            }), settings.timeout);
        };
    })(jQuery);
    
    // **** remove Opacity-Filter in ie ****
    function removeFilter(element) {
        if(element.style.removeAttribute){
            element.style.removeAttribute('filter');
        }
    }


/*  sticky Submenu
------------------------------------------------------------------------------------------------ */

function drawNavigation() {
    if ($(".shortnews_wrapper").css('display') == 'block') {
        var nav_top = '415';
    } else {
        var nav_top = '180';
    }
    $('#submenu').css({
        position: 'absolute',
        top: nav_top + 'px'
    });

    $(window).scroll(function() {
        if (($(window).height() > 200)) {
            if($(window).scrollTop() > nav_top) {
               $('#submenu').delay(10).stop().animate({top:$(window).scrollTop()},350);
            } else {
                $('#submenu').delay(10).stop().animate({top:nav_top+'px'},200);
            }
        }
    });
}


/*  min-height Anpassung
------------------------------------------------------------------------------------------------ */
       
function show_news() {
/*
    var windowHeight = $(window).height();
    var contentHeight = $("#content").innerHeight();
    if(contentHeight >= windowHeight && windowHeight > 400 || $('.startpage').length) {
        $(".shortnews_wrapper").css({display: 'block'});
    }
*/
    if ($("#submenu").length) {
        var contentHeight = $("#box_right").innerHeight();
        $(".shortnews_wrapper").css({display: 'block'});
        var leftboxHeight = 270 /* Logo 190 und contactinfo 80 */ + $("#submenu").innerHeight() + $('.shortnews_wrapper').innerHeight() + 100 /* 'spazi' */;
        if(contentHeight >= leftboxHeight || $('.startpage').length) {
            $(".shortnews_wrapper").css({display: 'block'});
        } else {
            $(".shortnews_wrapper").css({display: 'none'});
        }
    } else {
        $(".shortnews_wrapper").css({display: 'block', border: '0'});
    }
}


/* IE6nomore
------------------------------------------------------------------------------------------------ */

$.IE6nomore = function() {

    if(!$('#ie6nomore').length)
        return;

    $('#ie6nomore').css({
        'left'      : 0,
        'top'       : 0,
        'bottom'    : 0,
        'right'     : 0,
        'position'  : 'absolute',
        'z-index'   : 9999
    });

};

/* Swap image
------------------------------------------------------------------------------------------------ */

function swapImage(element, newimage) {

    var oldsrc      = element.src;
    element.src     = newimage;

    if (!element.onmouseout) {
        element.onmouseout = function() {
            swapImage(this, oldsrc);
        }
    }

}

/* Spam protection
------------------------------------------------------------------------------------------------ */

function getAdr(prefix, postfix, text) {
    document.write('<a href="mailto:' + prefix + '@' + postfix + '">' + (text ? text.replace(/&quot;/g, '"').replace(/%EMAIL%/, prefix + '@' + postfix) : prefix + '@' + postfix) + '</a>');
}


/* Create onchange select plugin
------------------------------------------------------------------------------------------------ */

(function($){
    $.fn.createOnchangeSelect = function(options){
        var settings = $.extend({
            'label': false,
            'group': false,
            'id': 'selection'
        }, options);

        return this.each(function(){
            var el = this;
            if(!$('li a', el).length)
                return;
            if(settings.group){
                // Create form and select
                $(el).append(
                    form = $('<form>').attr('action', '').append(
                        select = $('<select>').attr({
                            'id': settings.id,
                            'name': settings.id
                        }).addClass($(this).attr('class'))
                    )
                );
                // Create optgroup
                $(settings.group, el).each(function(){
                    $(select).append(
                        optgroup = $('<optgroup>').attr('label', $(this).text())
                    );
                    // Fill select with options
                    $.fn.createOnchangeSelect.fillSelect($(this).next('ul'), optgroup);
                });
            }
            else{
                $('ul, ol', el).each(function(){
                    // Create form and select
                    $(this).after(
                        form = $('<form>').attr('action', '').append(
                            select = $('<select>').attr({
                                'id': settings.id,
                                'name': settings.id
                            }).addClass($(this).attr('class'))
                        )
                    );
                    // Fill select with options
                    $.fn.createOnchangeSelect.fillSelect(el, select);
                });
            }
            // Onchange
            $(select).change(function(){
                location.href=this.value;
            });
            // Create label
            if(settings.label){
                $(form).prepend(
                    label = $('<label>').attr('for', settings.id).text($(settings.label).text())
                );
            }
            // Remove non javascript html code
            $('ul, ol, '+settings.group, el).remove();
            $(settings.label).remove();
        });
    }

    // Fill select with options
    $.fn.createOnchangeSelect.fillSelect = function(el, select){
        $('li a', el).each(function(){
            $(select).append(
                option = $('<option>').val(this.href).text($(this).text())
            );
            if($(this).hasClass('active'))
                $(option).attr('selected', 'selected');
        });
    }
})(jQuery);



/* DOM
------------------------------------------------------------------------------------------------ */

$(document).ready(function() {

    // IE6nomore

    $.IE6nomore();

  
    // Forms

    $('#contact').forms({
        'confirmpage'   : 'iframe'
    });

    $('#event').forms({
        'confirmpage'   : 'iframe'
    });

    $('#appointmentmaker').forms({
        'confirmpage'   : 'iframe',
        'multipage'   : true
    });

    $('#recommendation').forms({
        'confirmpage'   : 'iframe',
        'multipage'   : false
    });

    $('#inforequest').forms({
        'confirmpage'   : 'iframe'
    });

    $('#newsletter_subscribe').forms({
        'confirmpage'   : 'iframe',
        'multipage'     : false
    });

    $('textarea').autogrow();

    // Datepicker
    $('input.date').datepicker({
        'hideInput' : true
    });
    
    
    // News object
    $('#news_selection').createOnchangeSelect({
        'label' : '#news_selection h3'
    });

    // Infolists
    $('.selection').createOnchangeSelect({
        'group': '.group'
    });

    // Onlinetools object
    $('.onlinetool .popup').popup();

    // Menu
    $('#nav').menu({
        'removeTitle'   : false
    });

    // shortnews fadeeffekt
    $(document).ready(function(){
        if($('#shortnews')) {
        $('#shortnews').innerfade({
            speed: 'slow',
            timeout: 10000,
            type: 'sequence'
        });
        }
    });

    // min height Anpassung
    show_news();


    // sticky Navigation
    drawNavigation();

});

