var $ = jQuery.noConflict();

$(window).ready(function() {

    var fadeDuration = 300;
    var delayDuration = 1000;

    var currentHref = window.location.href;
    var currentHrefMatches = currentHref.match(/\/(event|messe-ausstellung|incentive|kongress|csr)/);
    
    var $content = $('#content .section');
    var $contentHeight = $content.clone().appendTo('#content').addClass('clone').height();
    $('#content .clone').remove();
    
    if(currentHrefMatches != null) { // a main page gets loaded -> fade in and adjust height of #content with animation
        
        if($('#content').hasClass('fade')) {
            $('#content').hide().css('visibility', 'visible').fadeIn(fadeDuration);
            $content.animate({ height: $contentHeight }, fadeDuration);
        }
    
    } else if($('body').hasClass('node-type-referenzen') || $('body').hasClass('page-node-11')) { // page or subpage of  "Referenzen" gets loaded -> adjust the height of #content (no animation)
        if($('#block-views-referenzen-block').height() > $contentHeight)
            $content.css('height', $('#block-views-referenzen-block').height()-18 + 'px');
        else
            $content.css('height', $contentHeight + 'px');
    } else {
        $content.css('height', 'auto');
    }
    
    $('#block-menu-block-2 a, #front-menu a').click(function(e) {
        e.preventDefault();
        var href = $(this).attr('href');
        var matches = href.match(/\/(event|messe-ausstellung|incentive|kongress|csr)/);
            if((currentHrefMatches == null && matches[1] != null) || (matches[1] != null && currentHrefMatches[1] != matches[1])) {

                if($('body').hasClass('front')) {
                    var $el = $('.picture');
                } else {
                    var $el = $content;
                    $('#content').animate({ height: '362px' }, fadeDuration);
                }
                
                $('#main-wrapper').prepend('<img style="display: none" id="fade-image" src="/sites/all/themes/lucentive/pictures/' + matches[1] + '.jpg" alt="' + matches[1] + '" />');
                $('#fade-image').load(function() {
                    $(this).fadeIn(fadeDuration, function() {
                        $('.gallery').hide();
                        $('#block-views-referenzen-block, #block-views-mitgliedschaften-block, #block-views-partner-block').css('visibility', 'hidden');
                        $('#content').css('background-image', 'none').css('visibility', 'hidden');
                        $el.animate({ opacity: 0 }, fadeDuration, function() {
                            $('#front-menu .picture').css('background', '#e5e5e5');
                            $('#fade-image').delay(delayDuration).fadeOut(fadeDuration, function() {
                                window.location.href = href + '?f';
                            });
                        });
                    });

                });


            } else { // next page has same parent
                window.location.href = href + '?t';                
            }
    });
});

;
var $ = jQuery.noConflict();

$(function() {

    var currentHref = window.location.href;
    var currentHrefMatches = currentHref.match(/\/(event|messe-ausstellung|incentive|kongress|csr)/);
    
    var $gallery = $('.gallery');
    var $lastImage = $gallery.find('img').filter(':last');

    // randomize pictures
    $('.gallery ul').randomize('.gallery ul li');
    
    // prepend main image
    if($('.gallery ul').length == 0 && currentHrefMatches != null)
        $('#content > .section').append('<div class="gallery"><ul></ul></div>');
    
    if(currentHrefMatches != null) {
        var appendix = '<li><img src="/sites/all/themes/lucentive/pictures/medium/' + currentHrefMatches[1] + '.jpg" alt="' + currentHrefMatches[1] + '" /></li>';
        if($('#content').hasClass('t-fade'))
            $('.gallery ul').append(appendix);
        else
            $('.gallery ul').prepend(appendix);
    }
    
    // init vars
    var $images = $('.gallery img');
    $images.hide().css('visibility', 'visible');
    
    // show first fast if no fade
    if(!$('#content').hasClass('fade')) {
        $images.hide().css('visibility', 'visible').filter(':first').addClass('active').fadeIn(700);
    }
    
    // gallery fader
    if($images.length > 1) {
        $lastImage.load(function() {
            var timer = setInterval(function() {
                if($images.filter('.active').length === 0) {
                    $images.filter(':first').addClass('active').fadeIn(700);
                } else {
                    var $prev = $images.filter('.active').addClass('prev').removeClass('active');
                    if($images.filter(':last').hasClass('prev'))
                        var $next = $images.filter(':first');
                    else {
                        var $next = $prev.parent().next('li').find('img');
                    }
                    $next.addClass('active').fadeIn(700, function() {
                        $prev.hide().removeClass('prev');
                    });
                }
            }, 3000);
        });
    } else {
        $('.gallery img').filter(':first').load(function() {
            $(this).delay(3000).addClass('active').fadeIn(700);
        });
    }
    
});

(function($) {

    $.fn.randomize = function(childElem) {
        return this.each(function() {
            var $this = $(this);
            var elems = $this.children(childElem);
            
            elems.sort(function() { return (Math.round(Math.random())-0.5); });  
            
            $this.remove(childElem);  
            
            for(var i=0; i < elems.length; i++)
                $this.append(elems[i]);      

        });    
    }
})(jQuery);;

