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

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

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

/*--- border-radius plugin ---*/

    (function($){
        $.fn.borderRadius = function(radius){
            return this.each(function(e){
                $(this).css({
                    'border-radius': radius,
                    '-moz-border-radius': radius,
                    '-webkit-border-radius': radius
                });
            });
        };
    })(jQuery);


/*-- display elements --*/

    (function($){
        $.fn.showElement = function(options){
            var SE_Settings = {
                handler: 'toggle',
                destination: '',
                animate: false
            };
            var SE_Settings = $.extend(SE_Settings, options);

            // hide href anchor destination
            if($(this).length){                
                if($(this).is('a')) {
                    var destination = ($(SE_Settings.destination).length) ? $(SE_Settings.destination) : $('#'+$(this).attr('href').split('#').pop());
                }
                else {
                    $(this).wrapInner('<a href="#" />');
                    var destination = $(SE_Settings.destination);
                }
                destination.hide();

                // show destination
                if(SE_Settings.handler == 'toggle') {

                    // toogle destination
                    $(this).toggle(
                        function(){ (SE_Settings.animate) ? destination.animate(SE_Settings.animate, { duration: 'slow' }) : destination.show(); },
                        function(){ (SE_Settings.animate) ? destination.animate(SE_Settings.animate, { duration: 'slow' }) : destination.hide(); }
                    );
                }
                else if(SE_Settings.handler == 'mouseover') {

                    // show destination on mouseover
                    $(this).bind({
                        click: function(){ return false; },
                        mouseover: function(){ (SE_Settings.animate) ? destination.animate(SE_Settings.animate, { duration: 'slow' }) : destination.show(); },
                        mouseout: function(){ (SE_Settings.animate) ? destination.animate(SE_Settings.animate, { duration: 'slow' }) : destination.hide(); }
                    });
                }
            }
        };
    })(jQuery);


/*-- Default value -- */

    (function($){
        $.fn.defaultValue = function(){
            $(this).focus(function(){
                if(!$(this).data('defaultValue')) $(this).data('defaultValue', $(this).attr('value'));
                if($(this).data('defaultValue') == $(this).attr('value')) $(this).attr('value', '');
            }).blur(function(){
                if(!$(this).attr('value').length) $(this).attr('value', $(this).data('defaultValue'));
            });
        };
    })(jQuery);


/*-- Word highlighting --*/

    (function($){
        $.fn.highlight = function(text, options){
            var defaults = {
                color: 'red',
                background: 'yellow'
            };
            var options = $.extend(defaults, options);
    
            function innerHighlight(node, text){
                var skip = 0;
                if (node.nodeType == 3){
                    var pos = node.data.toUpperCase().indexOf(text);
                    if (pos >= 0){
                        var spannode = document.createElement('span');
                        spannode.className = 'highlight';
                        spannode.style.color = options.color;
                        spannode.style.background = options.background;
                        var middlebit = node.splitText(pos);
                        var endbit = middlebit.splitText(text.length);
                        var middleclone = middlebit.cloneNode(true);
                        spannode.appendChild(middleclone);
                        middlebit.parentNode.replaceChild(spannode, middlebit);
                        skip = 1;
                    }
                }
                else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)){
                    for (var i = 0; i < node.childNodes.length; ++i){
                        i += innerHighlight(node.childNodes[i], text);
                    }
                }
            return skip;
            }
    
            return this.each(function(){
                innerHighlight(this, text.toUpperCase());
            });
        };
    })(jQuery);

/*-- Searchword highlightning --*/

    (function($){
        $.fn.highlightSearchwords = function(options){
            var url = document.location.href;
            var ref = document.referrer;
            if(ref.indexOf('%') > -1){
                ref = unescape(ref);
            }
            if(ref.indexOf('words=') > -1 && url.indexOf('words=') == -1){
                var words = ref.split('words=');
                words = words[1].split('&');
                words = words[0].split('+');
                for(w=0; w<words.length; w++){
                    $(this).highlight(words[w], options);
                }
            }
        };
    })(jQuery);


/*--- position fixed ---*/

    var init_top = new Array();
    function positionFixed() {
        if ($('html').innerHeight() > 570 && $(window).scrollTop() > 200 && $('html').innerWidth() > 1005) {
            $('#nav_container, #sujet_container').css({'position':'fixed', 'top':'10px'});
            $('#author').css({'position':'fixed', 'top':'306px', 'border':0});
        } else {
            $('#nav_container, #sujet_container').css({'position':'absolute', 'top':init_top['nav_container']});
            $('#author').css({'position':'absolute', 'top':init_top['author']});
        }
    }


/*--- graustufenbilder ---*/

    function grayscaleImageIE(imgObj) {
        imgObj.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayScale=1)';
    }

    function grayscaleImage(imgObj) {
        var canvas = document.createElement('canvas');
        var canvasContext = canvas.getContext('2d');
        var imgW = imgObj.width;
        var imgH = imgObj.height;
        canvas.width = imgW;
        canvas.height = imgH;
        canvasContext.drawImage(imgObj, 0, 0);
        var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);
        for (var y = 0; y < imgPixels.height; y++) {
            for (var x = 0; x < imgPixels.width; x++) {
                var i = (y * 4) * imgPixels.width + x * 4;
                var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
                imgPixels.data[i] = avg;
                imgPixels.data[i + 1] = avg;
                imgPixels.data[i + 2] = avg;
            }
        }
        canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
        if (chromefix != 1) {
            window.setTimeout("chrome_fix()", 50);
            chromefix = 1;
        }
        return canvas.toDataURL();
    }

    /*--- crazy chrome-fix ---*/
    function chrome_fix() {
        $('#news_page .news_image').hover();
    }


/*-- DOM -- */

    var imgSrc;
    var chromefix;

    $(function(){

        // Forms
        if(!$('.startpage').length) {
            $('form.contact').forms({multipage:true, summarypage:true});
            $('form.appointmentmaker').forms({multipage:true, summarypage:true});
            $('form.inforequest').forms({multipage:true, summarypage:true});
            $('form.application').forms();
            $('form.newsletter').forms({multipage:false});
            $('form.recommendation').forms({multipage:false});
        }

        // Iframe popup
        if (!$('.startpage').length) {
            $('.popup').popup();
        }

        // Show elements
        $('.show_disclaimer').showElement({ animate:{'opacity':'toggle', 'height':'toggle'}});

        // Navigation
        if ($('#nav_e40241').length) {
            var nav_html = $('#nav_e40241').html().split(' und').join('<br />und');
            document.getElementById('nav_e40241').innerHTML = nav_html;
        } else if ($('#nav_e107170').length) {
            var nav_html = $('#nav_e107170').html().split(' und').join('<br />und');
            document.getElementById('nav_e107170').innerHTML = nav_html;
        }

        // position-fixed
        if ($('.startpage').length < 1) {
            init_top['nav_container'] = $('#nav_container').position().top;
            init_top['author'] = $('#author').position().top;

            $(window).scroll(function() {
               positionFixed();
            });
        }

        // Search
        $('.func_search a').click(function() {
            if ($('#search').css('display') == 'none') {
                $('#search').css({
                    display: 'block',
                    opacity: 0
                });
                $('#search').animate({
                    opacity: 1
                }, 100);
            } else {
                $('#search').animate({
                    opacity: 0
                }, 100, function() {
                    $(this).css('display', 'none');
                });
            }
            return false;
        });
        $('#search input[type=text]').defaultValue();
        $('#content').highlightSearchwords({ color: 'pink' });

        // infolist-overview
        if ($('a', $('.infolist_overview')).length && $('p', $('.infolist_overview')).length) {
            $('.infolist_overview:not(.single_onlinetool) p').css({display:'none'});
            $('.infolist_overview:not(.single_onlinetool) a').addClass('info_ov_headline');
            $('<a class="more_info" href="#" title="Info"><img src="'+more_info_button+'" title="info" alt="info" /></a>').insertAfter('.infolist_overview:not(.single_onlinetool) p');
            $('.infolist_overview .more_info').click(function() {
                if ($('p', $(this).parent()).css('display') == 'none') {
                    $('p', $(this).parent()).show('slow');
                } else {
                    $('p', $(this).parent()).hide('slow');
                }
                return false;
            });
            $('.infolist_overview .open_list').click(function() {
                window.location = $('.info_ov_headline', $(this).parent()).attr('href');
            });
        }

        // toplink
        if ($('html').innerHeight() > $('body').innerHeight()) {
            $('#gotoshortcuts').hide();
        } else {
            $('#gotoshortcuts').show();
        }

        // news
        if ($('#news_selection').length) {
            var news_selection = $('#news_selection');
            news_selection.css({
                height: '33px',
                overflow: 'hidden'
            });
            news_selection.mouseover(function() {
                if (news_selection.innerHeight() == 33) {
                    news_selection.css({height: 'auto'});
                    var new_height = news_selection.innerHeight();
                    news_selection.css({height: '33px'});
                    news_selection.animate({
                        height: new_height
                    }, 150, function() {
                        news_selection.css({height: 'auto', overflow: 'visible'});
                    });
                }
            });
            news_selection.mouseleave(function() {
                if (news_selection.innerHeight() != 33) {
                    news_selection.css({overflow: 'hidden'});
                    news_selection.animate({
                        height: '33px'
                    }, 150);
                }
            });
        }

        if ($('#news_page').length) {
            var imgObj = document.getElementById('news_page').getElementsByTagName('IMG')[0];
            if (imgObj) {
                imgSrc = imgObj.src;
                if($.browser.msie){
                    grayscaleImageIE(imgObj);
                } else {
                    imgObj.src = grayscaleImage(imgObj);
                }
                $('#news_page .news_image').hover(function() {
                    if($.browser.msie){
                        imgObj.style.filter = '';
                    } else {
                        this.src = imgSrc;
                    }
                }, function() {
                    if($.browser.msie){
                        grayscaleImageIE(imgObj);
                    } else {
                        imgObj.src = grayscaleImage(imgObj);
                    }
                });
            }
        }
    });

    window.onresize = function() {
        if ($('html').innerHeight() > $('body').innerHeight()) {
            $('#gotoshortcuts').hide();
        } else {
            $('#gotoshortcuts').show();
        }
        positionFixed();
    }
        
