jQuery(document).ready( function($) {
  // rotating images (for the home page, but will work elsewhere)
  var curtain_rotation_timeout = null;
  var rotate_images_with_curtain = function( $element, $curtain ) {
    var delay = 5000;
    var transition = 500;
    
    $next = $element.next();
    if ( $next.length < 1 ) {
      $next = $element.siblings(':first-child');
      //delay = delay*2;
    }
    $curtain.fadeIn(transition, function() {
      $element.hide();
      $next.show();
      $curtain.fadeOut(transition, function() {
        curtain_rotation_timeout = setTimeout( function(){
          rotate_images_with_curtain($next, $curtain);
        }, delay );
      });
    });
  }
  var rotation_timeout = null;
  var rotate_images = function( $element, delay ) {
    var transition = 1000;
    
    $next = $element.next();
    if ( $next.length < 1 ) {
      $next = $element.siblings(':first-child');
    }
    
    $element.fadeOut(transition);
    $next.fadeIn(transition, function() {
      rotation_timeout = setTimeout( function(){
        rotate_images($next, delay);
      }, delay );
    });
  }
  
  if ( $('#content .portfolio-staging').length < 1 ) { // don't use curtain for portfolio
    if ( $('#content ul.rotating-images li').length > 1 ) {
      $('#content .posts').append('<div class="post-curtain"></div>');
      $('#content ul.rotating-images li:first-child').show();
      curtain_rotation_timeout = setTimeout( function(){
        rotate_images_with_curtain($('#content ul.rotating-images li:first-child'), $('#content .posts .post-curtain'));
      }, 5000 );
    }
  }
  if ( $('#sidebar ul.rotating-images li').length > 1 ) {
    $('#sidebar').append('<div class="sidebar-curtain"></div>');
    $('#sidebar ul.rotating-images li:first-child').show();
    setTimeout( function(){
      rotate_images_with_curtain($('#sidebar ul.rotating-images li:first-child'), $('#sidebar .sidebar-curtain'));
    }, 5000 );
  }
  
  
  
  
  // add non-semantic divs for drop shadows and crop marks
  $('#sidebar').prepend('<div class="drop-shadows"><div class="drop-shadow-side"></div><div class="drop-shadow-bottom"></div></div><div class="crop-marks"><div class="top-crop"></div><div class="bottom-crop"></div></div>');
  $('#content').prepend('<div class="drop-shadows"><div class="drop-shadow-right"></div><div class="drop-shadow-bottom"></div><div class="drop-shadow-corner"></div><div class="drop-shadow-left"></div></div><div class="crop-marks"><div class="top-crop"></div><div class="top-crop-middle"></div><div class="bottom-crop"></div><div class="bottom-crop-middle"></div></div>');
  
  // controls for portfolio
  $('.portfolio-item a.thumbnail').append('<span class="blind"></span>');
  $('.portfolio-item .blind').fadeTo(0, 0.6).css({backgroundColor: '#FFFFFF'});
  $('.portfolio-item').hover(
    function() { $(this).find('.blind').stop(true).fadeTo(200, 0); },
    function() { $(this).find('.blind').stop(true).fadeTo(200, 0.6); }
  );
  
  $('.portfolio-item a.thumbnail').click( function() {
    $('.portfolio-staging').empty();
    $('.portfolio-item').removeClass('active');
    $(this).parents('.portfolio-item').addClass('active')
    .children().clone().appendTo('.portfolio-staging');
    return false;
  });
  $('.portfolio-item a.thumbnail:eq(0)').click();
  
  // controls for marketing (similar to portfolio
  $('.marketing-item h3.marketing-title a').click( function() {
    clearTimeout(rotation_timeout);
    $('.portfolio-staging').empty();
    $(this).parents('.marketing-item').find('.marketing-content')
    .children().clone().appendTo('.portfolio-staging');
    if ( $('#content .portfolio-staging ul.rotating-images li').length > 1 ) {
      $('#content .portfolio-staging ul.rotating-images').height($('#content .portfolio-staging ul.rotating-images li:first-child').height());
      $('#content .portfolio-staging ul.rotating-images li').hide();
      $('#content .portfolio-staging ul.rotating-images li:first-child').show();
      rotation_timeout = setTimeout( function(){
        rotate_images($('#content .portfolio-staging ul.rotating-images li:first-child'), 4000);
      }, 4000 );
    }
    return false;
  });
  $('.marketing-item h3.marketing-title a:eq(0)').click();
  
  // make two-column lists
  $('ul.two-column, ol.two-column').each( function() {
    $this = $(this);
    $clone = $this.clone();
    $(this).wrap('<div class="two-column"></div>');
    length = $this.children().length;
    $clone.children(':lt('+Math.ceil(length/2)+')').remove();
    $this.children(':gt('+(Math.ceil(length/2)-1)+')').remove();
    $this.after($clone);
  });
});