$(function($) {

    var stage     = $('#stage .post');
    
    //listen on showMore link and toggle class if clicked 
    function showMore() {
        
        var showMore  = $('#stage a.moreInfo');
        
        showMore.bind('click', function(e) {
            e.preventDefault();
            $(this).closest('.post')
                    .toggleClass('wide narrow', 800);
            return false;
        });
    }
    
    showMore();
    
    
    // get image and overlay with blank image
    function imageProtection() {
        var allImages = $('img', stage);
        
        allImages.live('mousedown', function() {
            $(this).protectImage();
            return false;
        });
    }
    
    imageProtection();
    
    // Highlight active field
    $('input, textarea').live('mousedown keypress', function() {
          $(this).addClass('focus');          
    });
    
    
    var container   =   $('.thumb_container');
    
    if (container) {
        $('img.thumb').tipsy({
                            gravity: 's',
                            html: true
                            });
    };
    
    
    $('li#works').toggle(function() {
                  
              $(".subnav_child").stop().animate({
                                              height: "20px"
                                              }, 1000).animate({
                                              width: "350px"
                                                  }, 500);
                  
              }, function() {
                  $(".subnav_child").stop().animate({
                                                  height: "0px"
                                                  }, 1000);
      });
            
                
    //thumb navigation and ajax load    
    $('ul.thumbs li a, a[rel*="next"], a[rel*="prev"]').live('click', function(e) {
        e.preventDefault();
        
        $(this).parents('ul:first')
               .find('a')
               .removeClass('selected')
               .end()
               .end()
               .addClass('selected');
        
         $('#stage').html('<div id="loading">loading...<\/div>');
        
         var url  = $(this).attr('href');
         var post = ' .post';
        
         //load post via ajax into #stage
         $('#stage').load(url + post, {}, function(responseText, textStatus){
             if (textStatus !== 'success') {
                 alert(this);
                 return false;
             }
             $(this).find('img')
                    .hide()
                    .delay(100)
                    .fadeIn('slow');
         });
    });
    
    
    //add keyboard navigation 
    $(document.documentElement).keyup(function (event) {
         var direction = null;
        
         // handle cursor keys
         if (event.keyCode == 37) {
           direction = 'prev';
         } else if (event.keyCode == 39) {
           direction = 'next';
         }
        
         if (direction != null) {
           $('a.selected').parent()[direction]().find('a').click();
         }
    }   );
    
});