$(document).ready(function() {
    // Linkiconclasses
	/*
    $("a[href$='.pdf']").addClass("pdf");
    $("a[href$='.doc']").addClass("doc");
    $("a[href$='.xls']").addClass("xls");
    */
    $("a[href^='mailto:']").addClass("email");
	$("a[href^='tel:']").addClass("phonenumber");


    // AXA on the web overlay
    if ($("li#axaontheweb a").length > 0) {
        var axaontheweb_url = $("li#axaontheweb a").attr('href');
        axaontheweb_url += (axaontheweb_url.split('?')[1] ? '&' : '?') + 'type=22220';
        $("li#axaontheweb a").attr('href', axaontheweb_url);
        $("li#axaontheweb a").fancybox({
            'type':'inline',
            'centerOnScroll':'true',
            'showNavArrows':'false'
        });
    }

    // Languageselector
    if ($(".tx-languages").length > 0) {
        var $tx_languages = $(".tx-languages");
        var $tx_languages_ul = $(".tx-languages > ul").hide();
        var $tx_languages_label = $(".tx-languages > label");
        var activelang = $tx_languages_ul.find('li a.act').html();

        $tx_languages_label.html(activelang);

        $tx_languages.click(function () {
            if ($tx_languages_ul.css('display') == 'none') {
                $($tx_languages_ul).fadeIn('fast');
            } else {
                $($tx_languages_ul).fadeOut('fast');
            }
        });
    }

    // Teaser
    $('ul.tx_cbsitemaps_gallery').sameHeights();

    // Slider
    if ($('.tx_cbteaser div.item').length > 1) {
        var totalitems = $('.tx_cbteaser div.item').length;
        var slidenavigation = $('.slidenavigation');

        $('.previous').hide();
        slidenavigation.hide();

        $('#slider').hover(
          function () {
            slidenavigation.fadeIn('fast');
          },
          function () {
            slidenavigation.fadeOut('fast');
          }
        );

        // Initialize the slider
        var slider = new Lectric();
        slider.init('#slider', {animateDuration: 'slow', next: '.next', previous: '.previous'});

        var viewport = $('#slider .items');

        slider.subscribe('transition_end', function(c) {
            toggleArrows(c, {hide: true});
            showInfos(slider);
        });

        slider.subscribe('nextButton', function(c) {
            hideInfos();
            toggleArrows(c, {hide: true});
            showInfos(slider);
        });

        slider.subscribe('previousButton', function(c) {
            hideInfos();
            toggleArrows(c, {hide: true});
            showInfos(slider);
        });

        var count = 0;
        var timer = setInterval(function(c) {
            count = (count < slider.itemCount() - 1) ? count + 1 : 0;
            slider.to(count);
            hideInfos();
            toggleArrows(slider, {hide: true});
            showInfos(slider);
        }, 5000);

        // Clear the interval if we touch the slider for the first time
        // or tap one the next or previous buttons
        var clearIt = function(s, e) {
            clearInterval(timer);
            slider.unsubscribe(e.type, e.handler);
        };
        slider.subscribe('start', clearIt);
        slider.subscribe('previousButton', clearIt);
        slider.subscribe('nextButton', clearIt);

        // Keyboard shortcuts for left and right arrows
        $(document).keydown(function(e) {
            if (e.keyCode === 39) {
                e.preventDefault();
                var previous = slider.page();
                slider.to(previous + 1);
            } else if (e.keyCode === 37) {
                e.preventDefault();
                var previous = slider.page();
                slider.to(previous - 1);
            }
        });

        function showInfos(slider) {
            var page_num = slider.page(slider.current_x);
            var current = viewport.find('.item').eq(page_num);
            var overlay_left = current.find('.teaserInfoLeft');
            var overlay_right = current.find('.teaserInfoRight');
            overlay_left.hide();
            overlay_right.hide();

            if (!overlay_left.is(':visible')) {
              overlay_left.delay(500).fadeIn();
            }

            if (!overlay_right.is(':visible')) {
              overlay_right.delay(500).fadeIn();
            }
        }

        function hideInfos(slider) {
            $('.teaserInfoLeft, .teaserInfoRight').fadeOut('fast');
        }

        function toggleArrows(slider, opts) {
            var opts = jQuery.extend({hide: false}, opts);
        
            var page_num = slider.page(slider.current_x);
            if (page_num === totalitems - 1) {
              if (opts.hide) {
                $('a.next').css({display: 'none'});
              } else {
                $('a.next').css('opacity', 0.25);
              }
            } else {
              if (opts.hide) {
                $('a.next').css({display: 'block'});
              } else {
                $('a.next').css('opacity', 1);
              }
            }
            if (page_num === 0) {
              if (opts.hide) {
                $('a.previous').css({display: 'none'});
              } else {
                $('a.previous').css('opacity', 0.25);
              }
            } else {
              if (opts.hide) {
                $('a.previous').css({display: 'block'});
              } else {
                $('a.previous').css('opacity', 1);
              }
            }
      }



    }
});

$.fn.sameHeights = function() {
    $(this).each(function() {
        var tallest = 0;

        $(this).children().each(function(i) {
            if (tallest < $(this).height()) {
                tallest = $(this).height();
            }
        });
        $(this).children().css({'height': tallest});
    });
    return this;
};



