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


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


/*--- Aufloesungsanpassung ---*/

    function check_resolution() {
        if ($(window).height() > 450 && $(document).width() > $('#container').width()) {
            if ($(document).height() > $('body').height()) {
                $('#container').addClass('fixed');
                $('#gotoshortcuts .gotoshortcuts').css({display:'inline'}).click(function(){
                    if($.browser.safari) {
                        $('body').animate({scrollTop: 0}, 1000);
                    } else {
                        $('html').animate({scrollTop : 0}, 1000);
                    }
                    return false;
                });
            } else {
                $('#container').removeClass('fixed');
                $('#gotoshortcuts .gotoshortcuts').css({display:'none'});
            }
            if ($('body').height() - $('#subnav_container').height() - $('#nav_container').height() - $('#functions').height() > 180 && $(window).height() < $('#container').height()) {
                $('#functions').addClass('fixed');
                $('#subnav_container').addClass('fixed');
                $('#content').addClass('fixed');
            } else {
                $('#functions').removeClass('fixed');
                $('#subnav_container').removeClass('fixed');
                $('#content').removeClass('fixed');
            }
        } else {
            $('#functions').removeClass('fixed');
            $('#subnav_container').removeClass('fixed');
            $('#content').removeClass('fixed');
            $('#container').removeClass('fixed');
            $('#gotoshortcuts .gotoshortcuts').css({display:'inline'}).click(function(){
                $('html').animate({scrollTop: 0}, 1000);
                return false;
            });
        }

    }


/*-- clearfix ie -- */

    function clearfix(){
        // :after for IE
        if($.browser.msie && $.browser.version <= 8){
            $('.clearfix').each(function(){
                $(this).after(
                    $('<div>').css({
                        'clear': 'both',
                        'content': '" "',
                        'display': 'block',
                        'font-size': 0,
                        'height': 0,
                        'visibility': 'hidden'
                    })
                );
            });
        }   
    }


/*--- FIBUMobil ---*/

    function init_fibumobil() {

        if (!$('.startpage').length) {
            document.getElementById('sitetitle').innerHTML = document.getElementById('sitetitle').innerHTML.replace('FIBUMobil', 'FIBU<em>Mobil</em>');

            if (document.getElementById('subnav')) {
                var subnav_links = document.getElementById('subnav').getElementsByTagName('A');

                for (i=0; i<subnav_links.length; i++) {
                     if (subnav_links[i].innerHTML == 'FIBUMobil') {
                         subnav_links[i].innerHTML = 'FIBU<em>Mobil</em>';
                     }
                }
            }
        }
    }


/*--- DOM ---*/

    $(function(){

        // IE6nomore
        $.IE6nomore();

        // Search
        $('#search input[type=text]').defaultValue();
        $('#content').highlightSearchwords({ 'color': 'pink' });

        // Clearfix
        clearfix();

        // FIBUMobil
        init_fibumobil();

        // Aufloesungsanpassung
        if (!$('.startpage').length) {check_resolution();}

    });

    window.onresize = function() {
        if (!$('.startpage').length) {check_resolution();}
    }

    window.onload = function() {
        if (!$('.startpage').length) {check_resolution();}
    }

