(function($){
  $(document).ready(function(){
  
    $(document).bind({
      'scroll':function(ev){
        if($(this).scrollTop() > 171) {
          $('#nav').css({'position':'fixed'});
          $('#navtitle').show();
        } else {
          $('#nav').css({'position':'relative'});
          $('#navtitle').hide();
        }
      }
    });
  
    $('.hoverchild').each(function(){
      var $this = $(this);
      $this.parent().bind({
        'mouseenter':function(){
          $this.show();
        }
        ,'mouseleave':function(){
          $this.hide();
        }
      });
    });
  
    $('.slideshow').each(function(){
      var $this = $(this);
      var speed = $this.attr('speed') ? $this.attr('speed') : 2000;
//      $this.css({'textAlign':'right'});
      var $slides = $this.find('img, .slide');

      $slides.css({'opacity':0}).hide();
      
      var $showing = [];

      var transition = function(slide) {
        if($showing.length == 0) {
          $showing = $slides.first().show().animate({'opacity':1}, 2000, function(){
            setTimeout(transition, speed);
          });
        } else {
          $showing.animate({'opacity':0}, 2000, function(){
            $showing = $showing.hide().next();
            if($showing.length == 0) {
              $showing = $slides.first();
            }
            $showing.show().animate({'opacity':1}, 2000, function(){
              setTimeout(transition, speed);
            });
          });
        }
      };
      transition();
    });
    
    /*
    $('#nav').each(function(){
      $(this)
        .find('>div')
          .css({'display':'inline-block','position':'relative'})
          .each(function(){
            var $menu = $(document.createElement('div')).hide();
            $(this).find('a').not(':first').appendTo($menu);
            $(this).append($menu);
            var height = $menu.height();
            $menu.css({'height':0}).show();
            $(this).bind({
              'mouseenter':function(){
                $menu.animate({'height':height});
              }
              ,'mouseleave':function(){
                $menu.animate({'height':0});
              }
            });
          })
      ;
    });
    //*/

    jQuery('ul.sf-menu').superfish();
    
    function shuffleWord($word) {
      if($word.css('opacity') == 1) {
        $word.animate({'opacity':0},2000,function(){
          setTimeout(function(){shuffleWord($word);}, 4000);
        });
      } else {
        $word.animate({'opacity':1},2000,function(){
          setTimeout(function(){shuffleWord($word);}, 6000);
        });
      }
    }
    $('.timed-words').each(function(){
      $(this).find('img, div').each(function(i){
        var $word = $(this);
        $word
          .css({'opacity':0})
        ;
        setTimeout(function(){shuffleWord($word);}, 2000*i);
      });
      /*
      setTimeout(function(){$word.animate({'opacity':1},2000);}, 2000*i);
      */
    });
  
  });
})(jQuery);
