
jQuery.fn.extend({
    scrollV : function(speed, margin, easing) {
        var speed = speed || 1000;
        var margin = margin || 0;
        return this.each(function() {
            var targetOffset = $(this).offset().top;
            $('html, body').animate({scrollTop: targetOffset}, speed, easing);
        });
    },
    
    SearchTopic : function(callback,ref) {
        var ref = ref || document.referrer;
        
        var Search = {
            engines: [
                [/^http:\/\/(www\.)?google\./i, /q=([^&]+)/i],                            // Google
                [/^http:\/\/(www\.)?search\.yahoo\./i, /p=([^&]+)/i],                     // Yahoo
                [/^http:\/\/(www\.)?search\.msn\./i, /q=([^&]+)/i],                       // MSN
                [/^http:\/\/(www\.)?search\.live\./i, /query=([^&]+)/i],                  // MSN Live
                [/^http:\/\/(www\.)?search\.aol\./i, /userQuery=([^&]+)/i],               // AOL
                [/^http:\/\/(www\.)?ask\.com/i, /q=([^&]+)/i],                            // Ask.com
                [/^http:\/\/(www\.)?altavista\./i, /q=([^&]+)/i],                         // AltaVista
                [/^http:\/\/(www\.)?feedster\./i, /q=([^&]+)/i],                          // Feedster
                [/^http:\/\/(www\.)?search\.lycos\./i, /q=([^&]+)/i],                     // Lycos
                [/^http:\/\/(www\.)?alltheweb\./i, /q=([^&]+)/i],                         // AllTheWeb
                [/^http:\/\/(www\.)?technorati\.com/i, /([^\?\/]+)(?:\?.*)$/i]            // Technorati
                ],
            
            decodeURL: function(URL,reg) {
                URL = decodeURIComponent(URL);
                var query = null;
                try {
                    $.each(reg,function(i,n){
                        if(n[0].test(URL)) {
                            var match = URL.match(n[1]);
                            if(match) {
                                query = match[1].toLowerCase();
                                return false;
                            }
                        }
                    });
                    
                    if (query) {
                        query = query.replace(/(\'|")/, '\$1');
                        query = query.split(/[\s,\+\.]+/);
                    }
                } catch (e) {}
                
                return query;
            }
        };
        
        var topics = Search.decodeURL(ref,Search.engines);
        for (i=0; i<this.length; i+=1) {
            callback(this[i],topics);
        }
        
        return this;
    }
});

var contains = function(src,keys) {
    var check = false;
    if (keys) {
        for (j=0; j<keys.length; j +=1) {
            if (con.indexOf(keys[j]) != -1) {
                check = true;
            }
        }
    }
    return check;
};

$(function() {
    
    $('#ref').prepend('<div id="change" style="display:none"></div>');
    setInterval("$('#change').animate({ opacity: 'toggle'},3000)", 10000);

    
    $('#ref li:eq(18)').css("padding-left","80px")
    
    if ($.browser.safari) { //fix für Safari 2.0.4
        $("#sub dd").prepend("<span> </span>");
    }
    
    $('#sub')
    .addClass("shrink")
    .find('dt')
    .hover(function(){
        $(this).addClass("over");
    },     function(){
        $(this).removeClass("over");
    })
    .click(function() {
        $(this).toggleClass("open")
        .next().animate( { height: 'toggle', opacity: 'toggle' }, 'slow');
    })
    .next().hide()
    .end()
    .find('dd').SearchTopic(function(elm,t){
        var con = elm.innerHTML.toLowerCase().replace(/<\/?[^>]+>/gi, '');
        if (contains(con,t)) {
            $(elm).show().prev().addClass("open");
        }
    }).end();
    
    $('#p-kontakt').find("#sub").find("dt:first").addClass("open").next().show();
    
    $('#teaser').find("a:not(.extern)").click(function(){
        if (window.location.pathname.indexOf(this.pathname) != -1) {
            $('#sub dt').removeClass("open")
            .next().stop().animate({ height: 'hide', opacity: 'hide' }, 'slow');
            $('#' + this.search.slice(1)).addClass("open")
            .next().stop().animate({ height: 'show', opacity: 'show' }, 'slow');
            return false;
        }
    })
    .each(function(){
        this.href = this.href.replace("#","?");
    });
    
    if (window.location.search) {
        var art = window.location.search.slice(1);
        $('#' + art).addClass("open").next().show();
        /*if ($('#main :first').attr("id") != art) {
            $('#' + art).scrollV(1500);
        }*/
    }
});
