$(function () {
    $('nav.top-level nav a').hide();

    $('nav.top-level > a').hoverIntent(function() {
        $('nav.top-level > a').removeClass('active');

        var button = $(this);
        button.addClass('active');
        button.css('border-right', 'none').css('margin-right', '1px');
        button.prevAll('a:first').css('border-right', 'none').css('margin-right', '1px');

        var nav = button.next('nav').css({ left: button.position().left+'px' });

        var sublinks = nav.children('a');
        $('nav nav a').stop(true).hide();

        function show_one_of(links) {
            links.eq(0).fadeIn();
            var links_m1 = links.slice(1);

            setTimeout(function() { 
                if(links_m1.length) {
                  show_one_of(links_m1);
                }
            }, 50);
        }
        show_one_of(sublinks);

    }, function(e) {
        var button = $(this);

        var nav = button.next('nav');
        var to_element = e.toElement;
        if(to_element.tagName == 'SPAN') {
            to_element = $(to_element).parent()[0];
        }

        if ($(e.toElement).is('nav.top-level') || $(e.toElement).is('nav.top-level > a')) {
            button.removeClass('active');
            button.next('nav').children('a').stop(true).hide();
            button.css('border-right', '1px solid rgb(200, 200, 200)').css('margin-right', 0);
            button.prevAll('a:first').css('border-right', '1px solid rgb(200, 200, 200)').css('margin-right', 0);
        }
    });

    $('nav.top-level nav').mouseleave(function() {
      var button = $(this).prev('a');
      button.removeClass('active');
      button.next('nav').children('a').stop(true).fadeOut();
      button.css('border-right', '1px solid rgb(200, 200, 200)').css('margin-right', 0);
      button.prevAll('a:first').css('border-right', '1px solid rgb(200, 200, 200)').css('margin-right', 0);
      $(this).children('a').stop(true).hide();
    });

    $('#side-links').hide();

    var bottom_hover_in = function() {
        var height = $('.bottom-shelf').find('.content').height() + 85;
        $('.bottom-shelf').animate({ 'height': height });
        $('#side-links').show();
    };
    var bottom_hover_out = function() {
        $('.bottom-shelf').animate({ 'height': '60px' });
        $('#side-links').hide();
    };

    if(!$.browser.msie || parseInt($.browser.version, 10) > 8) {
        $('.bottom-shelf').hoverIntent(bottom_hover_in, bottom_hover_out);
    }
    else { // thanks IE
        $('.bottom-shelf').css({'cursor': 'pointer'});
        $('.bottom-shelf a').click(function(ev) {
          ev.stopPropagation();
        });

        $('.bottom-shelf').click(function(ev) {
            if($(this).height() == 60) {
              bottom_hover_in();
            }
            else {
              bottom_hover_out();
            }
        });
    }

    var images = $('.middle-image').not('.gallery').children('img');
    if(images.length > 1) {
        images.not(':first').css('opacity', 0);
        var image = images[0];

        setInterval(function() {
            var next_image = $(image).next('img');
            if(next_image.length == 0) {
                next_image = images[0];
            }
            $(image).animate({ opacity: 0 }, 1000);
            $(next_image).animate({ opacity: 1 }, 1000);

            image = next_image;
        }, 5000);
    }

    var gallery = $('.middle-image.gallery');
    gallery.attr('data-img_visible', 0);

    var gallery_imgs = gallery.children('img');
    gallery_imgs.hide().eq(0).show();

    $('.middle-image.gallery .prev').click(function(ev) {
      ev.preventDefault();

      var index = $('.middle-image.gallery').attr('data-img_visible');
      var prev_index = (index == 0 ? gallery_imgs.length - 1 : parseInt(index) - 1)

      gallery_imgs.hide().eq(prev_index).fadeIn();
      gallery.attr('data-img_visible', prev_index);
    });

    $('.middle-image.gallery .next').click(function(ev) {
      ev.preventDefault();

      var index = $('.middle-image.gallery').attr('data-img_visible');
      console.log(index);
      var next_index = (index == gallery_imgs.length - 1 ? 0 : parseInt(index) + 1)

      gallery_imgs.hide().eq(next_index).fadeIn();
      gallery.attr('data-img_visible', next_index);
    });

    $('.middle-image.gallery .fullscreen').click(function(ev) {
      ev.preventDefault();

      $('.middle-image.gallery').css({
        'position': 'fixed',
        'top': 0,
        'left': 0,
        'height': '100%',
        'width': '100%',
        'background': 'url(/static/img/bg-dark-tile.jpg)',
        'z-index': 3
      }).children('img').css({
        'top': 0,
        'left': 0
      });


      $('.middle-image.gallery .fullscreen').hide();
      $('.middle-image.gallery .restore-size').show();
    });

    $('.middle-image.gallery .restore-size').click(function(ev) {
      ev.preventDefault();

      $('.middle-image.gallery').css({
        'position': 'absolute',
        'top': 0,
        'left': 0,
        'background': 'none',
        'height': '100%',
        'width': '100%',
        'z-index': 3
      }).children('img').css({
        'top': '',
        'bottom': 0,
        'margin-bottom': 0
      });

      $('.middle-image.gallery .fullscreen').show();
      $('.middle-image.gallery .restore-size').hide();
    });
});

