/* Author: mishareyzlin.com */

// Da closure
(function(){
  // This stuff only happens on the homepage
  
  if ( $('body').attr('id') === 'index' ) {
    /* ===== THE CAROUSEL ===== */
    var carouselInit = function ( carousel ) {
      $('.film-gallery .next').bind('click', function ( e ) {
        e.preventDefault();
        carousel.next();
      });

      $('.film-gallery .prev').bind('click', function ( e ) {
        e.preventDefault();
        carousel.prev();
      });
    };

    $(".film-gallery").jcarousel({
      initCallback: carouselInit,
      buttonNextHTML: null,
      buttonPrevHTML: null,
      wrap: "circular",
      scroll: 2
    }).find('.next').trigger('click');
    
    /* ===== THE LIGHTBOX ==== */
    Shadowbox.init({
      language:   "ru",
      skipSetup: true
    });

    Shadowbox.setup('.film-gallery a');
  }
  
  // This is shared functionality 
  
  /* === ALIGN COLUMN HEIGHTS WITH CONTENT === */
  /* So, the problem is that content has background by itself, 
     but the sidebars, they are only wrappers for "blocks" that have
     backgrounds and there might be any amount of them.
     We need the last block in the sidebar to match the height
     of the content (in case content has the biggest height).
  */
  
  // find the highest column after window has loaded 
  // (the font affects the height of columns) 
  $(window).load( function() {
    var cols = $('.mainmain').children(),
        highest, theHeight = 0;

    cols.each( function() {
      var col = $(this),
          height = col.outerHeight();

      if ( height > theHeight ) {
        theHeight = height;
        highest = col;
      }
    });

    cols.each( function() {
      var col = $(this);

      if ( col.hasClass('main') ) {
        col.height(
          '+=' + ( theHeight - col.outerHeight() + ( $.browser.version === "8.0" ? 4 : 0 ) )
        );
      }
      else {
        col.children().last().height(
          // "+ 10" compensates the last box's 10px margin 
          '+=' + ( theHeight - col.outerHeight() + 10 )
        );
      }

    });
  });
  
  // Sticky footer 
  var content = $('.main'),
      footer = $('.footer'),
      win = $(window),
      footerClass = 'absolute-footer';

  var positionFooter = function() {
    if ( content.height() < win.height() - 400 ) {
      footer.addClass( footerClass );
    }
    else {
      footer.removeClass( footerClass );
    }
  };
  
  win.bind('resize', positionFooter ).trigger('resize');

})();
