$.noConflict();
jQuery(document).ready(function($) {

    // --------------------------------------
    // MainNavi Hover
    // --------------------------------------
    $('#mainnavi ul.level_1 li.submenu').hover(
        function() {
            if ($(this).children().size() > 1) {
                $('#mainnavi li.company a').css('background-position','0 -86px');
                $('#mainnavi li.contact a').css('background-position','-176px -86px');
                $('#mainnavi li.company span').css('background-position','0px -129px');
                $('#mainnavi li.contact span').css('background-position','-278px -86px');
                if ($('#mainnavi li.company a').hasClass('trail')) {
                    $('#mainnavi li.company a').css('background-position','-113px -86px');
                }
                if ($(this).hasClass('company')) {
                    $('#mainnavi li.company a').css('background-position','0 -129px');
                }
                if ($(this).hasClass('ships')) {
                    $('#mainnavi li.ships a').css('background-position','-113px -129px');
                }
                if ($(this).hasClass('team')) {
                    $('#mainnavi li.team a').css('background-position','-198px -129px');
                }
                if ($(this).hasClass('contact')) {
                    $('#mainnavi li.contact a').css('background-position','-278px -129px');
                }
                $(this).find('ul.level_2').stop().height('auto').slideDown(250,function() {
                    // Das ganze muss auch noch mal nach dem slideDown, weil es sonst passieren kann,
                    // dass die background-position vom mouseLeave gesetzt wird.
                    $('#mainnavi li.company a').css('background-position','0 -86px');
                    $('#mainnavi li.contact a').css('background-position','-176px -86px');
                    $('#mainnavi li.company span').css('background-position','0px -129px');
                    $('#mainnavi li.contact span').css('background-position','-278px -86px');
                    if ($('#mainnavi li.company a').hasClass('trail')) {
                        $('#mainnavi li.company a').css('background-position','-113px -86px');
                    }
                    if ($(this).parent().hasClass('company')) {
                        $('#mainnavi li.company a').css('background-position','0 -129px');
                    }
                    if ($(this).parent().hasClass('ships')) {
                        $('#mainnavi li.ships a').css('background-position','-113px -129px');
                    }
                    if ($(this).parent().hasClass('team')) {
                        $('#mainnavi li.team a').css('background-position','-198px -129px');
                    }
                    if ($(this).hasClass('contact')) {
                        $('#mainnavi li.contact a').css('background-position','-278px -129px');
                    }
                });
            } else {
                //alert($(this).attr('class'));
                if ($(this).hasClass('contact')) {
                    // Timeout, weil erst das SlideUp zu Ende laufen muss (sonst ist er wieder bei mouseout
                    // und hat die falsche background-position)
                    if ($('ul.level_2').is(':animated')) {
                        setTimeout(function() {
                            $('#mainnavi li.contact a').css('background-position','-278px -43px');
                        }, 270);
                    } else {
                        $('#mainnavi li.contact a').css('background-position','-278px -43px');
                    }
                } else if ($(this).hasClass('team')) {
                    if ($('ul.level_2').is(':animated')) {
                        setTimeout(function() {
                            $('#mainnavi li.team a').css('background-position','-198px -129px');
                        }, 270);
                    } else {
                        $('#mainnavi li.team a').css('background-position','-198px -129px');
                    }
                } else if ($(this).hasClass('ships')) {
                    if ($('ul.level_2').is(':animated')) {
                        setTimeout(function() {
                            $('#mainnavi li.ships a').css('background-position','-113px -43px');
                        }, 270);
                    } else {
                        $('#mainnavi li.ships a').css('background-position','-113px -43px');
                    }
                }
            }
        },
        function() {
            if ($(this).children().size() > 1) {
                $(this).find('ul.level_2').stop().slideUp(250,function() {
                    hover_mouseout();
                });
            } else {
                hover_mouseout();
            }
        }
    );

    function hover_mouseout() {
        $('#mainnavi li.company a').css('background-position','0 0');
        $('#mainnavi li.ships a').css('background-position','-113px 0');
        $('#mainnavi li.team a').css('background-position','-198px 0');
        $('#mainnavi li.contact a').css('background-position','-278px 0');
        $('#mainnavi li.company span, #mainnavi li.ships a.trail').css('background-position','0 -43px');
        $('#mainnavi li.ships span, #mainnavi li.company a.trail').css('background-position','-113px -43px');
        $('#mainnavi li.team span, #mainnavi li.team a.trail').css('background-position','-198px -43px');
        $('#mainnavi li.contact span, #mainnavi li.contact a.trail').css('background-position','-278px -43px');
    }


    // --------------------------------------
    // Katalog Accordion
    // --------------------------------------
    $('.ships .accordion_toggler .content').toggle();

    $('.ships .accordion_toggler .head').click(function() {
        $(this).toggleClass('active');
        $(this).next().slideToggle(300);
        //$(this).next().fadeOut();
        return false;
    }).next().hide();


    // --------------------------------------
    // Accordion Historie
    // --------------------------------------
    if ($('body').hasClass('historie')) {

        // Alle zuklappen bis auf die ersten drei
        $('#historie .accordion_toggler .content').toggle();
        $('#historie .accordion_toggler .content:lt(3)').toggle();
        $('#historie .accordion_toggler .head:lt(3)').toggleClass('active');

        // Höchste Höhe setzen
        var hoehe = 0;
        for (i=0; i<3; i++) {
            if ($('#historie .accordion_toggler .content')[i].offsetHeight > hoehe) {
                hoehe = $('#historie .accordion_toggler .content')[i].offsetHeight;
            }
        }
        $('.history_list .accordion_toggler:nth-child(1) .content').height(hoehe);
        $('.history_list .accordion_toggler:nth-child(2) .content').height(hoehe);
        $('.history_list .accordion_toggler:nth-child(3) .content').height(hoehe);

        $('#historie .accordion_toggler .head').click(function() {
            // Alle in der gleichen Zeile in toggleElements merken
            var topPos = $(this).position().top;
            toggleElements = new Array();
            if ($(this).parent().next().length > 0) {
                toggleElement($(this).parent().next(), topPos);
            }
            if ($(this).parent().next().next().length > 0) {
                toggleElement($(this).parent().next().next(), topPos);
            }
            if ($(this).parent().prev().length > 0) {
                toggleElement($(this).parent().prev(), topPos);
            }
            if ($(this).parent().prev().prev().length > 0) {
                toggleElement($(this).parent().prev().prev(), topPos);
            }

            // Höchste Höhe ermitteln und für alle setzen
            if ($(this).next().css('display') == 'none') {
                $(this).next().css('display','block');
                hoehe = $(this).next().height();
                $(this).next().css('display','none');
                for (i=0; i<toggleElements.length; i++) {
                    toggleElements[i].children('.content').css('display','block');
                    if (toggleElements[i].children('.content').height() > hoehe) {
                        hoehe = toggleElements[i].children('.content').height();
                    }
                    toggleElements[i].children('.content').css('display','none');
                }
                $(this).next().height(hoehe);
                for (i=0; i<toggleElements.length; i++) {
                    toggleElements[i].children('.content').height(hoehe);
                }
            }

            // Klasse setzen und auf-/zuklappen
            $(this).toggleClass('active');
            $.each(toggleElements, function() {
                this.children('.content').toggle(300);
                this.children('.head').toggleClass('active');
            });
            $(this).next().toggle(300);
            
            return false;
        });
    }

    function toggleElement(element, topPos) {
        if (element.position().top == topPos) {
            toggleElements.push(element);
        }
    }


    // --------------------------------------
    // BG-Bilder
    // --------------------------------------
    //	Supersized - Fullscreen Background jQuery Plugin
    //	Version 3.1.3 Core
    //	www.buildinternet.com/project/supersized
    //	By Sam Dunn / One Mighty Roar (www.onemightyroar.com)
    //	Released under MIT License / GPL License
    // --------------------------------------
    // Einen Pool Zufallsbilder pro Seite festlegen
    // --------------------------------------
    if ($('body').hasClass('home')) {
        pool = new Array('modellschiff-gr.jpg','gaschem-hamburg-gr.jpg','gaschem-monrovia-gr.jpg','gaschem-nordsee-gr.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else if ($('body').hasClass('links')) {
        pool = new Array('team.jpg','bureau-1.jpg','bureau-2.jpg','bureau-3.jpg','bureau-4.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    }else if ($('body').hasClass('imprint')) {
        pool = new Array('team.jpg','bureau-1.jpg','bureau-2.jpg','bureau-3.jpg','bureau-4.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else if ($('body').hasClass('ships')) {
        pool = new Array('gaschem-monrovia.jpg','gaschem-bergen.jpg','gaschem-pacific.jpg','gaschem-weser.jpg','regenbogen.jpg','schiff.jpg','schiffe.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else if ($('body').hasClass('fully-refrigerated')) {
        pool = new Array('gaschem-monrovia.jpg','gaschem-bergen.jpg','gaschem-pacific.jpg','gaschem-weser.jpg','regenbogen.jpg','schiff.jpg','schiffe.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else if ($('body').hasClass('semi-refrigerated')) {
        pool = new Array('gaschem-monrovia.jpg','gaschem-bergen.jpg','gaschem-pacific.jpg','gaschem-weser.jpg','regenbogen.jpg','schiff.jpg','schiffe.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else if ($('body').hasClass('pressurized')) {
        pool = new Array('gaschem-monrovia.jpg','gaschem-bergen.jpg','gaschem-pacific.jpg','gaschem-weser.jpg','regenbogen.jpg','schiff.jpg','schiffe.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else if ($('body').hasClass('newbuildings')) {
        pool = new Array('gaschem-monrovia.jpg','gaschem-bergen.jpg','gaschem-pacific.jpg','gaschem-weser.jpg','regenbogen.jpg','schiff.jpg','schiffe.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else if ($('body').hasClass('company')) {
        pool = new Array('team.jpg','bureau-1.jpg','bureau-2.jpg','bureau-3.jpg','bureau-4.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else if ($('body').hasClass('news')) {
        pool = new Array('team.jpg','bureau-1.jpg','bureau-2.jpg','bureau-3.jpg','bureau-4.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else if ($('body').hasClass('team')) {
        pool = new Array('team.jpg','bureau-1.jpg','bureau-2.jpg','bureau-3.jpg','bureau-4.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else if ($('body').hasClass('contact')) {
        pool = new Array('team.jpg','bureau-1.jpg','bureau-2.jpg','bureau-3.jpg','bureau-4.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    } else {
        pool = new Array('gaschem-nordsee.jpg','gaschem-hamburg.jpg','gaschem-monrovia.jpg','gaschem-bergen.jpg','gaschem-pacific.jpg','gaschem-weser.jpg','regenbogen.jpg','schiff.jpg','bureau-1.jpg','bureau-2.jpg','bureau-3.jpg','bureau-4.jpg');
        i = Math.floor(Math.random() * pool.length);
        bild = pool[i];
    }
    $.supersized({
        slides:  [{image: 'tl_files/layout/backgrounds-full/'+bild}]
    });
    $('#header .inside').css('background-image','url(tl_files/layout/backgrounds/'+bild+')');

    // --------------------------------------
    // Fuß ganz unten positionieren, falls
    // die Seite zu kurz ist für das
    // Browserfenster
    // --------------------------------------
    positionFooter();
    $(window)
        .scroll(positionFooter)
        .resize(positionFooter)
    function positionFooter(){
        if($('html').height() < $(window).height()){
            $('#footer').css({position: 'absolute',top:($(window).scrollTop()+$(window).height()-$('#footer').height())+'px'})
            $('#container').css({minHeight: '407px'});
        }
    }


    // --------------------------------------
    // Katalog Slider
    // --------------------------------------
    // Wenn nur ein Bild, dann die Vor-/Zurück-Buttons ausblenden
//    if ($('#slider .scrollContainer').children('.panel').length == 1) {
//        $('#slider .scrollButtons').hide();
//    }

    if ($('#slider').length > 0) {
        var $panels = $('#slider .scrollContainer > div');
        var $container = $('#slider .scrollContainer');

        // if false, we'll float all the panels left and fix the width
        // of the container
        var horizontal = true;

        // float the panels left if we're going horizontal
        if (horizontal) {
          $panels.css({
            'float' : 'left',
            'position' : 'relative' // IE fix to ensure overflow is hidden
          });

          // calculate a new width for the container (so it holds all panels)
          $container.css('width', $panels[0].offsetWidth * $panels.length);
        }

        // collect the scroll object, at the same time apply the hidden overflow
        // to remove the default scrollbars that will appear
        var $scroll = $('#slider .scroll').css('overflow', 'hidden');

        // apply our left + right buttons
        // Nur, wenn mehr als ein Bild vorhanden
        if ($('#slider .scrollContainer').children('.panel').length > 1) {
        $scroll
          .before('<img class="scrollButtons left" src="tl_files/layout/slider-button-left.png" />')
          .after('<img class="scrollButtons right" src="tl_files/layout/slider-button-right.png" />');
        }

        // handle nav selection
        function selectNav() {
          $(this)
            .parents('ul:first')
              .find('a')
                .removeClass('selected')
              .end()
            .end()
            .addClass('selected');
        }

        $('#slider .navigation').find('a').click(selectNav);

        // go find the navigation link that has this target and select the nav
        function trigger(data) {
          var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
          selectNav.call(el);
        }

        if (window.location.hash) {
          trigger({id : window.location.hash.substr(1)});
        } else {
          $('ul.navigation a:first').click();
        }

        // offset is used to move to *exactly* the right place, since I'm using
        // padding on my example, I need to subtract the amount of padding to
        // the offset.  Try removing this to get a good idea of the effect
        var offset = parseInt((horizontal ?
          $container.css('paddingTop') :
          $container.css('paddingLeft'))
          || 0) * -1;


        var scrollOptions = {
          target: $scroll, // the element that has the overflow

          // can be a selector which will be relative to the target
          items: $panels,

          navigation: '.navigation a',

          // selectors are NOT relative to document, i.e. make sure they're unique
          prev: 'img.left',
          next: 'img.right',

          // allow the scroll effect to run both directions
          axis: 'xy',

          onAfter: trigger, // our final callback

          offset: offset,

          // duration of the sliding effect
          duration: 500,

          // easing - can be used with the easing plugin:
          // http://gsgd.co.uk/sandbox/jquery/easing/
          easing: 'swing'
        };

        // apply serialScroll to the slider - we chose this plugin because it
        // supports// the indexed next and previous scroll along with hooking
        // in to our navigation.
        $('#slider').serialScroll(scrollOptions);

        // now apply localScroll to hook any other arbitrary links to trigger
        // the effect
        $.localScroll(scrollOptions);

        // finally, if the URL has a hash, move the slider in to position,
        // setting the duration to 1 because I don't want it to scroll in the
        // very first page load.  We don't always need this, but it ensures
        // the positioning is absolutely spot on when the pages loads.
        scrollOptions.duration = 1;
        $.localScroll.hash(scrollOptions);
    }
});
