var Netpioneer = {
    start: function() {
        new Fx.SmoothScroll();
        this.initTopNavigation();
        this.disableDummyLinks();
        this.targetHandler();
        this.topLinkHandler();
        this.printLinkHandler();
    },

    initTopNavigation: function() {
        if ($('topnav-active')) {
            var currActive = $('topnav-active').setStyle('background-position', '0 -45px');
        }
        var active = new Fx.Tween(currActive, {
            duration: 200,
            link: 'cancel'
        });
        $$('div#topnav li a').each(function(el) {
            if (el.get('id') != 'topnav-active') {
                var tween = new Fx.Tween(el, {
                    duration: 200,
                    link: 'cancel'
                });
                el.setStyle('background-position', '0 0').addEvents({
                    'mouseenter': function(e) {
                        if (currActive) active.start('background-position', '0 0');
                        tween.start('background-position', '0 -45px');
                    },
                    'mouseleave': function(e) {
                        if (currActive) active.start('background-position', '0 -45px');
                        tween.start('background-position', '0 0');
                    }
                });
            }
            el.removeClass('topnav-link');
        });
    },

    disableDummyLinks: function() {
        $$('#columns a[href$=#]').addEvent('click',
        function(event) {
            event.stop();
        });
    },

    targetHandler: function() {
        $$('a.new-window').each(function(link) {
            link.addEvent('click',
            function() {
                window.open(this);
                return false;
            });
        });
    },

    topLinkHandler: function() {
        if (window.getScroll().y < 99) {
            $('toplink').setStyle('opacity', 0.00001);
        }
        if ($('toplink') != null) {
            $('toplink').setStyle('display', 'inline').addEvent('click',
            function(event) {
                event.stop();
                var myFx = new Fx.Scroll($(document.body), {
                    duration: 400,
                    wait: false
                }).toTop();
            });
        }
    },

    printLinkHandler: function() {
        if ($('printlink') != null) {
            $('printlink').setStyle('display', 'inline').addEvent('click',
            function(event) {
                event.stop();
                window.print();
            });
        }
    },
    
    imgHover: function() {
        $$('#referenzen img').each(function(img) {
          var src = img.getProperty('src');
          var extension = src.substring(src.lastIndexOf('.'),src.length)
          img.addEvent('mouseenter', function() { img.setProperty('src',src.replace(extension,'_mo' + extension)); });
          img.addEvent('mouseleave', function() { img.setProperty('src',src); });
        });
    }
}

window.addEvent('domready', function() {
    Netpioneer.start();
    // Coverflow scrolling.
    if (Browser.Engine.gecko && (flashContainer = $('flash'))) {
        flashContainer.addEvent('mousewheel', function(event) {
            flashContainer.sendToActionScript(event.wheel);
            event.stop();
        });
    }
    if ($('referenzen')) {
      Netpioneer.imgHover();
    }
});

window.addEvent('scroll', function() {
    $('toplink').fade((window.getScroll().y > 100) ? 'in': 'out');
});
