$(document).ready(setupBlockSelect);
$(document).ready(setupBlockSet);

function setupBlockSelect() {
   if (!Globals.editMode) return;

   /////////

   var blocks = $('.column_item');
   var selectedClass = 'column_item_selected';

   blocks.click(function () {
      currentBlock = $(this);
      //select block
      blocks.removeClass(selectedClass);
      $('.bm_selectedblock').hide();

      currentBlock.addClass(selectedClass);

      //set form values
      $('.bm_block_id').val(currentBlock.attr('blockid'));

      $('.bm_selectedblock', currentBlock.parent()).css({
         top: currentBlock.position().top,
         left: currentBlock.position().left,
         width: currentBlock.width(),
         height: currentBlock.height(),
         display:'block'
      });
   });
}

function setupBlockSet() {
   //loop blocks that are sets
   $('.colset').each(function () {
      blockSet = $('.column_item', this);

      //only do anything if there are multiple blocks, obviously.
      if (blockSet.length > 1) {
         // loop blocks within each set, store height as property,
         // because it's needed later and cannot be read reliably by a browser when the element is not displayed
         blockSet.each(function () {
            this.calcHeight = this.offsetHeight;
         });

         this.style.height = blockSet.get(0).offsetHeight + 'px';
         this.style.width = this.offsetWidth + 'px';
         this.style.overflow = 'hidden';

         BlockSetScroller.setup($(this));
      }
   });
}

var BlockSetScroller = new Map();

///settings
BlockSetScroller.displayTime = 5500;
BlockSetScroller.pauseTime = 10; //due to fading, the pause time is basically redundant.
BlockSetScroller.fadeTime = 500;
BlockSetScroller.setParent;
BlockSetScroller.set;

///functionality

BlockSetScroller.setup = function (jBlockSet) {
   BlockSetScroller.setParent = jBlockSet;
   BlockSetScroller.set = jBlockSet.children();

   BlockSetScroller.set.each(function () {
      $(this).hide();
   });
   BlockSetScroller.set.eq(0).show();

   //enter slideshow loop.
   setTimeout(BlockSetScroller.pause, BlockSetScroller.displayTime);
}

BlockSetScroller.index = 0;
BlockSetScroller.getNextIndex = function () {
   BlockSetScroller.index = ++BlockSetScroller.index % BlockSetScroller.set.length;

   return BlockSetScroller.index;
}

BlockSetScroller.display = function () {
   BlockSetScroller.set.eq( BlockSetScroller.index ).fadeIn(BlockSetScroller.fadeTime, function () {
      setTimeout(BlockSetScroller.pause, BlockSetScroller.displayTime);
   });
}
BlockSetScroller.pause = function () {
   var _index;

   //jQuery can sometimes get a little crazy with nested anonymous functions.
   //what happens:
   // fade out current block
   //    on fade-end: progress slideshow, set parent height(animated) using pre-calculated cached block height
   //       on animation-end: set pause timeout before next display
   BlockSetScroller.set.eq( BlockSetScroller.index ).fadeOut(BlockSetScroller.fadeTime, function () {
      _index = BlockSetScroller.getNextIndex();

      //before display, set blockset height to next item height, so that there are no layout holes due to varying block heights
      BlockSetScroller.setParent.animate(
         { height: BlockSetScroller.set.get(_index).calcHeight },
         BlockSetScroller.fadeTime,
         'swing',
         function () {
            setTimeout(BlockSetScroller.display, BlockSetScroller.pauseTime);
         }
      )
   });

}