jQuery(document).ready(function($){

  if ($('body').hasClass('使用中'))
    return;
  $('body').addClass('使用中');
  
  //
  // Projects Rotator
  //

  if($('#project_rotator') != null)
  {
    // Reorder
    $('#project_rotator').reorder();

    // CSS bits
    $('#project_rotator ul li').css('display','block');
    
    parent_element = $('#project_rotator').parent();
    
    // Add the navigation
    if ($('#project_rotator .rotator_nav').children().length == 0)
      parent_element.append('<div class="rotator_nav"><div class="rotator_arrows"><p class="rotator_next">Next</p><p class="rotator_prev">Previous</p></div></div><div class="clear"></div>');

    $('#project_rotator ul').cycle({
      timeout: 5000,
      speed: 500,
      fx: 'scrollHorz',
      next: '.rotator_next',
      prev: '.rotator_prev'
    });
  }
  
  //
  // Testimonial Rotator
  //

  if($('#testimonial_rotator') != null)
  {
    // Reorder
    $('#testimonial_rotator').reorder();

    // CSS bits
    $('#testimonial_rotator ul li').css('display','block');
    
    parent_element = $('#testimonial_rotator').parent();
    
    // Add the navigation
    if ($('#testimonial_rotator .rotator_nav').children().length == 0)
      parent_element.append('<div class="rotator_nav"><div class="rotator_arrows"><p class="rotator_next">Read another &raquo;</p><p class="rotator_prev">&laquo; Go back</p></div></div><div class="clear"></div>');

    $('#testimonial_rotator ul').cycle({
      timeout: 8000,
      speed: 500,
      fx: 'fade',
      next: '.rotator_next',
      prev: '.rotator_prev'
    });
  }

  var img = 'url(/wp-content/themes/thedextrousweb/images/google_custom_search_watermark.png) no-repeat center left';
  $('form.google input.search').css('background', img);
  $('form.google input.search').focus(function(){$(this).css('background', 'transparent')});
  $('form.google input.search').blur(function(){if($(this).val().length==0)$(this).css('background', img)});
 
  //
  // Interesting People cols
  //

  var ul = 'body.page-template-pagelinks-php #posts ul.links';
  if($(ul)) 
  {
    $(ul).addClass('main');
    $(ul + '.main').after('<ul class="links overflow"></ul>');
    $(ul).css('float','left');

    $(ul + '.main').each(function(){
      var thus = $(this);
      var nul = $(this).next();
      while (thus.children().length > nul.children().length+1) {
        var e = thus.children(':last-child');
        e.remove();
        nul.prepend(e);
      }
    });
  }

  // 
  // Fancybox images
  //

  $('body.page-template-pageproject-php .screenshots li a').fancybox({
      'transitionIn'  : 'elastic',
      'transitionOut' : 'elastic',
      'titlePosition' : 'inside'
    });

});
